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:com.idega.slide.webdavservlet.DomainConfig.java

private static Context getEnvContext() throws NamingException {
    InitialContext initialContext = new InitialContext();
    return (Context) initialContext.lookup("java:comp/env");
}

From source file:org.wso2.carbon.appfactory.core.util.AppFactoryDBUtil.java

public static void initializeDatasource() throws AppFactoryException {
    AppFactoryConfiguration configuration = ServiceHolder.getAppFactoryConfiguration();
    String datasourceName = configuration.getFirstProperty(AppFactoryConstants.DATASOURCE_NAME);
    if (datasourceName != null) {
        InitialContext context;
        try {// w  ww .j ava2 s . com
            context = new InitialContext();
        } catch (NamingException e) {
            String msg = "Could get JNDI initial context.Unable to get datasource for appfactory";
            log.error(msg, e);
            throw new AppFactoryException(msg, e);
        }
        try {
            AppFactoryDBUtil.dataSource = (DataSource) context.lookup(datasourceName);
        } catch (NamingException e) {
            String msg = "Could not found data source " + datasourceName + ".Please make sure the "
                    + "datasource is configured in appfactory.xml";
            log.error(msg, e);
            throw new AppFactoryException(msg, e);
        }
    } else {
        //This is only needed for unit test .
        DBConfiguration dbConfiguration;
        dbConfiguration = getDBConfig(configuration);
        String dbUrl = dbConfiguration.getDbUrl();
        String driver = dbConfiguration.getDriverName();
        String username = dbConfiguration.getUserName();
        String password = dbConfiguration.getPassword();
        if (dbUrl == null || driver == null || username == null || password == null) {
            log.warn("Required DB configuration parameters unspecified. So App Factory "
                    + "will not work as expected.");
        }

        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(driver);
        basicDataSource.setUrl(dbUrl);
        basicDataSource.setUsername(username);
        basicDataSource.setPassword(password);
        dataSource = basicDataSource;
    }
    setupDatabase();
}

From source file:org.firstopen.singularity.util.JMSUtil.java

public static Queue createQueue(String queueName) throws InfrastructureException {

    Queue queue = null;//from  w w w . j a  va 2s  .com
    InitialContext jndiContext = null;
    try {
        queue = queueExists(queueName);
    } catch (Exception e) {
        /*
         * try to create the queue
         */
        try {
            jndiContext = JNDIUtil.getInitialContext();

            ManagementHome mejbHome = (ManagementHome) jndiContext.lookup("ejb/mgmt/MEJB");
            Management mejb = mejbHome.create();
            ObjectName objectName = new ObjectName("jboss.mq:service=DestinationManager");
            mejb.invoke(objectName, "createQueue", new Object[] { queueName, "queue/" + queueName },
                    new String[] { String.class.getName(), String.class.getName() });

            queue = queueExists(queueName);
        } catch (Exception e1) {
            log.error("unable to create queue/" + queueName, e1);
            throw new InfrastructureException();
        }
    }
    return queue;
}

From source file:org.pentaho.platform.engine.services.connection.datasource.dbcp.PooledDatasourceHelper.java

public static DataSource getJndiDataSource(final String dsName) throws DBDatasourceServiceException {

    try {//www.java2  s .  com
        InitialContext ctx = new InitialContext();
        Object lkup = null;
        DataSource rtn = null;
        NamingException firstNe = null;
        // First, try what they ask for...
        try {
            lkup = ctx.lookup(dsName);
            if (lkup != null) {
                rtn = (DataSource) lkup;
                return rtn;
            }
        } catch (NamingException ignored) {
            firstNe = ignored;
        }
        try {
            // Needed this for Jboss
            lkup = ctx.lookup("java:" + dsName); //$NON-NLS-1$
            if (lkup != null) {
                rtn = (DataSource) lkup;
                return rtn;
            }
        } catch (NamingException ignored) {
            // ignored
        }
        try {
            // Tomcat
            lkup = ctx.lookup("java:comp/env/jdbc/" + dsName); //$NON-NLS-1$
            if (lkup != null) {
                rtn = (DataSource) lkup;
                return rtn;
            }
        } catch (NamingException ignored) {
            // ignored
        }
        try {
            // Others?
            lkup = ctx.lookup("jdbc/" + dsName); //$NON-NLS-1$
            if (lkup != null) {
                rtn = (DataSource) lkup;
                return rtn;
            }
        } catch (NamingException ignored) {
            // ignored
        }
        if (firstNe != null) {
            throw new DBDatasourceServiceException(firstNe);
        }
        throw new DBDatasourceServiceException(dsName);
    } catch (NamingException ne) {
        throw new DBDatasourceServiceException(ne);
    }
}

From source file:org.nimbustools.ctxbroker.service.ContextBrokerServiceImpl.java

protected static RestHttp discoverRestHttp() throws Exception {

    InitialContext ctx = null;
    try {/* www  .  ja v a  2s.  co m*/
        ctx = new InitialContext();

        final RestHttp rest = (RestHttp) ctx.lookup(REST_HTTP);

        if (rest == null) {
            throw new Exception("null from JNDI for RestHttp (?)");
        }

        return rest;

    } finally {
        if (ctx != null) {
            ctx.close();
        }
    }
}

From source file:info.bunji.jdbc.logger.JdbcLoggerFactory.java

/**
 ********************************************
 * get loggerName from jdbc url.//w  ww  . j av a2 s . c  o m
 *
 * @param url connection url
 * @return loggerName(jdbc url or DataSourceName)
 ********************************************
 */
private static String getLoggerName(String url) {
    if (!dsNameMap.containsKey(url)) {
        dsNameMap.put(url, url);

        // datasource????
        InitialContext ctx = null;
        try {
            // JNDI??????
            ctx = new InitialContext();
            NamingEnumeration<NameClassPair> ne = ctx.list("java:comp/env/jdbc");
            while (ne.hasMoreElements()) {
                NameClassPair nc = ne.nextElement();
                DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/" + nc.getName());
                // ?DataSource????getUrl()
                Method method = ds.getClass().getMethod("getUrl");
                String dsUrl = (String) method.invoke(ds);
                if (dsUrl.startsWith(DriverEx.DRIVER_URL_PREFIX)) {
                    dsUrl = dsUrl.replace(DriverEx.DRIVER_URL_PREFIX, "jdbc:");
                    if (dsUrl.equals(url)) {
                        dsNameMap.put(url, nc.getName());
                        break;
                    }
                }
            }
        } catch (Throwable t) {
            printThrowable(t);
        } finally {
            try {
                if (ctx != null)
                    ctx.close();
            } catch (NamingException e) {
            }
        }
    }
    return dsNameMap.get(url);
}

From source file:org.nimbustools.ctxbroker.service.ContextBrokerServiceImpl.java

protected static ContextBrokerHome discoverHome() throws Exception {

    InitialContext ctx = null;
    try {/* w  w  w  .j av a  2  s . com*/
        ctx = new InitialContext();

        final ContextBrokerHome home = (ContextBrokerHome) ctx.lookup(CONTEXTUALIZATION_HOME);

        if (home == null) {
            throw new Exception("null from JNDI for ContextBrokerHome (?)");
        }

        return home;

    } finally {
        if (ctx != null) {
            ctx.close();
        }
    }
}

From source file:de.mpg.escidoc.services.test.search.TestBase.java

/**
 * Helper method to retrieve a EJB service instance. The name to be passed to the method is normally
 * 'ServiceXY.SERVICE_NAME'./*ww  w .java2 s  . c o  m*/
 * 
 * @return instance of the EJB service
 * @throws NamingException
 */
protected static Object getService(String serviceName) throws NamingException {

    InitialContext context = new InitialContext();
    Object serviceInstance = context.lookup(serviceName);
    assertNotNull(serviceInstance);
    return serviceInstance;
}

From source file:org.n52.wps.server.database.PostgresDatabase.java

private static boolean createConnection() {
    Properties props = new Properties();
    DataSource dataSource;//  www .jav a2 s. c o m
    String jndiName = getDatabaseProperties("jndiName");
    String username = getDatabaseProperties("username");
    String password = getDatabaseProperties("password");

    if (jndiName != null) {
        InitialContext context;
        try {
            context = new InitialContext();
            dataSource = (DataSource) context.lookup("java:comp/env/jdbc/" + jndiName);
            PostgresDatabase.conn = dataSource.getConnection();
            PostgresDatabase.conn.setAutoCommit(false);
            LOGGER.info("Connected to WPS database.");
        } catch (NamingException e) {
            LOGGER.error("Could not connect to or create the database.", e);
            return false;
        } catch (SQLException e) {
            LOGGER.error("Could not connect to or create the database.", e);
            return false;
        }
    } else {
        props.setProperty("create", "true");
        props.setProperty("user", username);
        props.setProperty("password", password);
        PostgresDatabase.conn = null;
        try {
            PostgresDatabase.conn = DriverManager.getConnection(PostgresDatabase.connectionURL, props);
            PostgresDatabase.conn.setAutoCommit(false);
            LOGGER.info("Connected to WPS database.");
        } catch (SQLException e) {
            LOGGER.error("Could not connect to or create the database.", e);
            return false;
        }
    }
    return true;
}

From source file:org.eclipse.ecr.runtime.jtajca.NuxeoContainer.java

@SuppressWarnings("unchecked")
protected static <T> T resolveBinding(String name) throws NamingException {
    InitialContext ctx = new InitialContext();
    try {/*from   ww w  .  j  av a 2s . c o m*/
        return (T) ctx.lookup("java:comp/" + name);
    } catch (NamingException compe) {
        try {
            return (T) ctx.lookup("java:comp/env/" + name);
        } catch (NamingException enve) {
            return (T) ctx.lookup(name);
        }
    }
}