Example usage for javax.naming InitialContext lookup

List of usage examples for javax.naming InitialContext lookup

Introduction

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

Prototype

public Object lookup(Name name) throws NamingException 

Source Link

Usage

From source file:edu.harvard.i2b2.crc.util.QueryProcessorUtil.java

public CronEjbLocal getCronLocal() throws I2B2Exception {
    InitialContext ctx;
    try {/* ww w.  ja v  a2s .  c om*/
        ctx = new InitialContext();
        return (CronEjbLocal) ctx.lookup("QP1/CronEjb/local");
    } catch (NamingException e) {
        throw new I2B2Exception("Bean lookup error Cron ", e);
    }
}

From source file:edu.harvard.i2b2.crc.util.QueryProcessorUtil.java

public StartAnalysisLocal getStartAnalysisLocal() throws I2B2Exception {
    InitialContext ctx;
    try {//from  ww  w . j  av  a2 s  .  c om
        ctx = new InitialContext();
        return (StartAnalysisLocal) ctx.lookup("QP1/StartAnalysis/local");
    } catch (NamingException e) {
        throw new I2B2Exception("Bean lookup error Analysis ", e);
    }
}

From source file:edu.harvard.i2b2.crc.util.QueryProcessorUtil.java

public AnalysisPluginInfoLocal getAnalysisPluginInfoLocal() throws I2B2Exception {
    InitialContext ctx;
    try {/*from w w  w  .j a v a2s  . c  om*/
        ctx = new InitialContext();
        return (AnalysisPluginInfoLocal) ctx.lookup("QP1/AnalysisPluginInfo/local");
    } catch (NamingException e) {
        throw new I2B2Exception("Bean lookup error Anaylysis Plugin", e);
    }
}

From source file:org.josso.jb32.agent.JBossCatalinaNativeRealm.java

/** This creates a java:comp/env/security context that contains a
 securityMgr binding pointing to an AuthenticationManager implementation
 and a realmMapping binding pointing to a RealmMapping implementation.
 *//*  ww w  .j av a2s  . c om*/
protected Context prepareENC() throws NamingException {

    if (logger.isDebugEnabled())
        logger.debug("JBossCatalinaRealm.prepareENC, Start");

    ClassLoader loader = Thread.currentThread().getContextClassLoader();

    InitialContext iniCtx = new InitialContext();

    boolean securityContextExists = false;
    boolean isJaasSecurityManager = false;
    try {
        Context envCtx = (Context) iniCtx.lookup("java:comp/env");
        Context securityCtx = (Context) envCtx.lookup("security");
        securityContextExists = true;

        AuthenticationManager securityMgr = (AuthenticationManager) securityCtx.lookup("securityMgr");

        // If the Security Manager set in the web application ENC is not
        // a JaasSecurityManager, unbind the Security context and rebind it
        // with the JaasSecurityManager associated with the JOSSO Security Domain.
        // Note: the jboss-web.xml file of the partner application MUST not have an
        // entry referring to a security domain.
        if (!(securityMgr instanceof JaasSecurityManager)) {
            Util.unbind(envCtx, "security");
        } else
            isJaasSecurityManager = true;
    } catch (NamingException e) {
        // No Security Context found
    }

    // If we do not have a SecurityContext create it
    Context envCtx = null;
    if (!securityContextExists) {
        Thread currentThread = Thread.currentThread();
        if (logger.isDebugEnabled())
            logger.debug("Creating ENC using ClassLoader: " + loader);
        ClassLoader parent = loader.getParent();
        while (parent != null) {

            if (logger.isDebugEnabled())
                logger.debug(".." + parent);

            parent = parent.getParent();
        }

        envCtx = (Context) iniCtx.lookup("java:comp");
        envCtx = envCtx.createSubcontext("env");
    } else
        envCtx = (Context) iniCtx.lookup("java:comp/env");

    // If the Security Manager binded is not a JaasSecurityManager, rebind using
    // the Security Manager associated with the JOSSO Security Domain.
    if (!isJaasSecurityManager) {
        // Prepare the Security JNDI subcontext
        if (logger.isDebugEnabled())
            logger.debug("Linking security/securityMgr to JNDI name: " + JOSSO_SECURITY_DOMAIN);

        Util.bind(envCtx, "security/securityMgr", new LinkRef(JOSSO_SECURITY_DOMAIN));
        Util.bind(envCtx, "security/realmMapping", new LinkRef(JOSSO_SECURITY_DOMAIN));
        Util.bind(envCtx, "security/security-domain", new LinkRef(JOSSO_SECURITY_DOMAIN));
        Util.bind(envCtx, "security/subject", new LinkRef(JOSSO_SECURITY_DOMAIN + "/subject"));
    }

    if (logger.isDebugEnabled())
        logger.debug("JBossCatalinaRealm.prepareENC, End");

    return (Context) iniCtx.lookup("java:comp/env/security");
}

From source file:org.josso.jb32.agent.JBossCatalinaRealm.java

/** This creates a java:comp/env/security context that contains a
 securityMgr binding pointing to an AuthenticationManager implementation
 and a realmMapping binding pointing to a RealmMapping implementation.
 *//*  w w w .j a  v a  2s .co m*/
protected Context prepareENC() throws NamingException {

    ClassLoader loader = Thread.currentThread().getContextClassLoader();

    InitialContext iniCtx = new InitialContext();

    boolean securityContextExists = false;
    boolean isJaasSecurityManager = false;
    try {
        Context envCtx = (Context) iniCtx.lookup("java:comp/env");
        Context securityCtx = (Context) envCtx.lookup("security");
        securityContextExists = true;

        AuthenticationManager securityMgr = (AuthenticationManager) securityCtx.lookup("securityMgr");

        // If the Security Manager set in the web application ENC is not
        // a JaasSecurityManager, unbind the Security context and rebind it
        // with the JaasSecurityManager associated with the JOSSO Security Domain.
        // Note: the jboss-web.xml file of the partner application MUST not have an
        // entry referring to a security domain.
        if (!(securityMgr instanceof JaasSecurityManager)) {
            Util.unbind(envCtx, "security");
        } else
            isJaasSecurityManager = true;
    } catch (NamingException e) {
        // No Security Context found
    }

    // If we do not have a SecurityContext create it
    Context envCtx = null;
    if (!securityContextExists) {
        Thread currentThread = Thread.currentThread();
        logger.debug("Creating ENC using ClassLoader: " + loader);
        ClassLoader parent = loader.getParent();
        while (parent != null) {
            logger.debug(".." + parent);
            parent = parent.getParent();
        }

        envCtx = (Context) iniCtx.lookup("java:comp");
        envCtx = envCtx.createSubcontext("env");
    } else
        envCtx = (Context) iniCtx.lookup("java:comp/env");

    // If the Security Manager binded is not a JaasSecurityManager, rebind using
    // the Security Manager associated with the JOSSO Security Domain.
    if (!isJaasSecurityManager) {
        // Prepare the Security JNDI subcontext
        logger.debug("Linking security/securityMgr to JNDI name: " + JOSSO_SECURITY_DOMAIN);
        Util.bind(envCtx, "security/securityMgr", new LinkRef(JOSSO_SECURITY_DOMAIN));
        Util.bind(envCtx, "security/realmMapping", new LinkRef(JOSSO_SECURITY_DOMAIN));
        Util.bind(envCtx, "security/security-domain", new LinkRef(JOSSO_SECURITY_DOMAIN));
        Util.bind(envCtx, "security/subject", new LinkRef(JOSSO_SECURITY_DOMAIN + "/subject"));
    }

    logger.debug("JBossCatalinaRealm.prepareENC, End");

    return (Context) iniCtx.lookup("java:comp/env/security");
}

From source file:com.francetelecom.clara.cloud.commons.JndiAwarePropertyPlaceholderConfigurer.java

/**
 * Resolve the given placeholder using the given properties. Default
 * implementation simply checks for an environment entry for a corresponding
 * property key.//from  w  w w  . j a va 2 s .co  m
 * <p>
 * Subclasses can override this for customized placeholder-to-key mappings
 * or custom resolution strategies, possibly just using the given lookup as
 * a fallback.
 * 
 * @param placeholder
 *            the placeholder to resolve
 * @return the resolved value, of <code>null</code> if none
 */
protected String resolveJndiProperty(String placeholder) {
    InitialContext initialContext = null;
    try {
        initialContext = new InitialContext();
        try {

            String prefix;
            if (resourceRef) {
                prefix = "java:comp/env/";
            } else {
                prefix = "";
            }
            return (String) initialContext.lookup(prefix + placeholder);
        } catch (NameNotFoundException e) {
            return null;
        } catch (NamingException e) {
            return null;
        }
    } catch (NamingException e) {
        throw new RuntimeException(e);
    } finally {
        if (initialContext != null) {
            try {
                initialContext.close();
            } catch (NamingException e) {
            }
        }
    }
}

From source file:org.eclipse.dirigible.repository.datasource.DataSourceFacade.java

private DataSource getFromContext(String jndiName, boolean wrap) {

    String defaultDataSourceType = System.getProperty(DEFAULT_DATASOURCE_TYPE);
    if (!DEFAULT_DATASOURCE_TYPE_JNDI.equalsIgnoreCase(defaultDataSourceType)) {
        logger.warn(//from ww  w . j a  v a2s . c  o  m
                "Getting from Context not possible - no configured default DataSource as initial parameter");
        return null;
    }

    logger.debug("Try to get datasource from the InitialContext");

    try {
        InitialContext context = (InitialContext) System.getProperties().get(ICommonConstants.INITIAL_CONTEXT);
        if ((context == null) || (jndiName == null)) {
            return null;
        }
        DataSource datasource = (DataSource) context.lookup(jndiName);
        if (datasource == null) {
            logger.error("Could not find DataSource in Initial Context by name: " + jndiName);
        } else {
            Connection con = null;
            try {
                con = datasource.getConnection();
            } catch (Exception e) {
                logger.error(
                        "Datasource retrieved from InitialContext, but it is broken - not bound to a real database",
                        e);
                return null;
            } finally {
                if (con != null) {
                    con.close();
                }
            }

            if (wrap) {
                WrappedDataSource wrappedDataSource = new WrappedDataSource(datasource);
                logger.debug("Datasource retrieved from InitialContext and wrapped");
                return wrappedDataSource;
            }
            logger.debug("Datasource retrieved from InitialContext and returned unwrapped");
            return datasource;
        }
    } catch (Throwable e) {
        logger.error("Could not find DataSource", e);
    }

    return null;
}

From source file:org.collectionspace.authentication.realm.db.CSpaceDbRealm.java

private Connection getConnection() throws LoginException, SQLException {
    InitialContext ctx = null;
    Connection conn = null;/*from w  w  w .  j a va  2 s.  c om*/
    try {
        ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup(getDataSourceName());
        if (ds == null) {
            throw new IllegalArgumentException("datasource not found: " + getDataSourceName());
        }
        conn = ds.getConnection();
        return conn;
    } catch (NamingException ex) {
        LoginException le = new LoginException("Error looking up DataSource from: " + getDataSourceName());
        le.initCause(ex);
        throw le;
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
            }
        }
    }

}

From source file:org.codesearch.commons.database.DBAccessImpl.java

@Inject
public DBAccessImpl() {
    Connection testConnection = null;
    try {/* w w w.  j  a  v  a2  s  .  c om*/
        InitialContext ic = new InitialContext();
        Context evtContext = (Context) ic.lookup("java:comp/env/");
        dataSource = (DataSource) evtContext.lookup("jdbc/codesearch");
        if (dataSource != null) {
            testConnection = dataSource.getConnection();
            if (testConnection.isValid(3)) {
                LOG.info("Successfully connected to database");
            }
        } else {
            LOG.error("Database is not configured, code analysis will not be available");
        }
    } catch (SQLException ex) {
        LOG.error("Error accessing database, code analysis will not be available:\n", ex);
    } catch (NamingException ex) {
        LOG.error("Error accessing database, code analysis will not be available:\n", ex);
    } finally {
        if (testConnection != null) {
            try {
                testConnection.close();
            } catch (Exception ex) {
                LOG.warn("Could not close test database connection:\n", ex);
            }
        }
    }
}