Example usage for javax.naming InitialContext InitialContext

List of usage examples for javax.naming InitialContext InitialContext

Introduction

In this page you can find the example usage for javax.naming InitialContext InitialContext.

Prototype

public InitialContext() throws NamingException 

Source Link

Document

Constructs an initial context.

Usage

From source file:com.autentia.common.util.JndiUtils.java

private static InitialContext getInitialContext() {
    if (ctx == null) {
        try {/*from   w w  w. j  a va  2s. co  m*/
            ctx = new InitialContext();
        } catch (NamingException e) {
            log.fatal("It is not possible to create a new InitialContext.", e);
            throw new RuntimeException(e);
        }
    }
    return ctx;
}

From source file:JNDIUtil.java

/**
 * Get the JNDI Context./*from   w  w w . j av a 2 s  .  c  o  m*/
 * <p/>
 * Don't forget to close it when done!
 *
 * @param jndiProperties JNDI properties.
 * @return The context.
 * @throws javax.naming.NamingException Error getting context.
 */
public static Context getNamingContext(final Properties jndiProperties) throws NamingException {
    Context context;
    try {
        context = jndiProperties.isEmpty() ? new InitialContext() : new InitialContext(jndiProperties);
    } catch (NamingException e) {
        System.out.println(
                "NamingException while try to create initialContext. jndiProperties are " + jndiProperties + e);
        throw ((NamingException) new NamingException("Failed to load InitialContext: " + jndiProperties)
                .initCause(e));
    }
    if (context == null) {
        throw new NamingException("Failed to create JNDI context.  Check that '" + Context.PROVIDER_URL + "', '"
                + Context.INITIAL_CONTEXT_FACTORY + "', '" + Context.URL_PKG_PREFIXES
                + "' are correctly configured in the supplied JNDI properties.");
    }

    return context;
}

From source file:EmailJndiServlet.java

public void init() throws ServletException {

    Context env = null;// w  w w  .ja v a 2 s  . c  o m

    try {

        env = (Context) new InitialContext();

        mailSession = (Session) env.lookup("MyEmail");

        if (mailSession == null)
            throw new ServletException("MyEmail is an unknown JNDI object");

        //close the InitialContext
        env.close();

    } catch (NamingException ne) {

        try {
            env.close();
        } catch (NamingException nex) {
        }

        throw new ServletException(ne);

    }

}

From source file:ar.com.zauber.commons.spring.configurers.JndiInitialContextHelper.java

/** dado paths jndi retorna archivos de propiedades */
public static Resource[] getJndiLocations(final String[] filePathJndiNames) {

    final ResourceLoader resourceLoader = new DefaultResourceLoader();

    try {//from  w  w w .ja  v  a 2 s .c o  m
        final InitialContext initCtx = new InitialContext();
        final Resource[] locations = new Resource[filePathJndiNames.length];
        boolean found = false;

        try {
            final Context envCtx = (Context) initCtx.lookup("java:comp/env");
            for (int i = 0; i < filePathJndiNames.length; i++) {
                locations[i] = resourceLoader.getResource((String) envCtx.lookup(filePathJndiNames[i]));
            }
            found = true;
        } catch (final NamingException e) {
            LOGGER.warn("Error JNDI looking up 'java:comp/env':" + e.getExplanation());
            // void
        }

        if (!found) {
            // Para Jetty 7 Server
            try {
                for (int i = 0; i < filePathJndiNames.length; i++) {
                    locations[i] = resourceLoader.getResource((String) initCtx.lookup(filePathJndiNames[i]));
                }
            } catch (final NamingException e) {
                LOGGER.warn("Hubo un error en el lookup de JNDI. Se usaran " + "properties del classpath: "
                        + e.getExplanation());
                return null;
            }
        }

        return locations;
    } catch (final NamingException e) {
        LOGGER.warn("Hubo un error en el lookup de JNDI. Se usaran " + "properties del classpath: "
                + e.getExplanation());
        return null;
    }
}

From source file:EnvEntry.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();

    try {/* ww w.  j  a va  2 s.  c o  m*/
        Context initCtx = new InitialContext();
        NamingEnumeration e = initCtx.listBindings("java:comp/env");

        while (e.hasMore()) {
            Binding binding = (Binding) e.next();
            out.println("Name: " + binding.getName());
            out.println("Type: " + binding.getClassName());
            out.println("Value: " + binding.getObject());
            out.println();
        }
    } catch (NamingException e) {
        e.printStackTrace(out);
    }
}

From source file:BeanServlet.java

public void init() throws ServletException {

    Context env = null;/*from ww w .  ja  v  a2s  . c o  m*/

    try {
        // Compile error since there is no StockPriceBean.class
        // change the name according to your requirements
        env = (Context) new InitialContext().lookup("java:comp/env");
        spbean = (StockPriceBean) env.lookup("bean/pricebean");

        //close the InitialContext
        env.close();

        if (spbean == null)
            throw new ServletException("bean/pricebean is an unknown JNDI object");

    } catch (NamingException ne) {

        try {
            env.close();
        } catch (NamingException nex) {
        }

        throw new ServletException(ne);

    }

}

From source file:DbServletTrans.java

public void init() throws ServletException {

    Context env = null;//from www  .j  a va  2s  .  co m

    try {

        env = (Context) new InitialContext().lookup("java:comp/env");
        pool = (DataSource) env.lookup("jdbc/oracle-8i-athletes");
        if (pool == null)
            throw new ServletException("'oracle-8i-athletes' is an unknown DataSource");

    } catch (NamingException ne) {

        throw new ServletException(ne);

    }

}

From source file:be.fedict.hsm.model.security.AdministratorSecurityBean.java

public static AdministratorSecurityBean getInstance() {
    try {//from  w  w w . j  a  v  a2 s . c  om
        InitialContext initialContext = new InitialContext();
        return (AdministratorSecurityBean) initialContext.lookup(JNDI_NAME);
    } catch (NamingException e) {
        throw new RuntimeException("JNDI error: " + e.getMessage(), e);
    }
}

From source file:dsd.dao.DAOProvider.java

/**
 * // w  w  w . j av a 2s  .com
 * @return
 */
public static DataSource getDataSource() {
    try {
        if (dataSource == null) {
            Context ctx = new InitialContext();
            dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/RTBMconnection");
        }
    } catch (NamingException e) {
        e.printStackTrace();
    }
    return dataSource;
}

From source file:com.wso2telco.ids.datapublisher.util.DBUtil.java

private static void initializeDatasources() throws AuthenticatorException {
    if (mConnectDatasource != null) {
        return;// ww w.  java2 s. c  om
    }

    String dataSourceName = null;
    try {
        Context ctx = new InitialContext();
        dataSourceName = DataHolder.getInstance().getMobileConnectConfig().getDataSourceName();
        mConnectDatasource = (DataSource) ctx.lookup(dataSourceName);
    } catch (NamingException e) {
        handleException("Error while looking up the data source: " + dataSourceName, e);
    }
}