List of usage examples for javax.naming Context lookup
public Object lookup(String name) throws NamingException;
From source file:org.eclipse.ecr.testlib.runner.RuntimeFeature.java
public static void rebind(Context ctx, String key, Object value) throws NamingException { Name name = ctx.getNameParser("").parse(key); int depth = name.size() - 1; for (int i = 0; i < depth; i++) { String segment = name.get(i); try {//from ww w . j av a2 s . c om ctx = (Context) ctx.lookup(segment); } catch (NameNotFoundException e) { ctx = ctx.createSubcontext(segment); } } ctx.rebind(name.get(depth), value); }
From source file:org.jamwiki.db.DatabaseConnection.java
/** * Static method that will configure a DataSource based on the Environment setup. *///from w ww . j a v a 2s .c o m private synchronized static void configDataSource() throws SQLException { if (dataSource != null) { closeConnectionPool(); // DataSource has already been created so remove it } String url = Environment.getValue(Environment.PROP_DB_URL); DataSource targetDataSource = null; if (url.startsWith("jdbc:")) { try { // Use an internal "LocalDataSource" configured from the Environment targetDataSource = new LocalDataSource(); } catch (ClassNotFoundException e) { logger.error("Failure while configuring local data source", e); throw new SQLException("Failure while configuring local data source: " + e.toString()); } } else { try { // Use a container DataSource obtained via JNDI lookup // TODO: Should try prefix java:comp/env/ if not already part of the JNDI name? Context ctx = new InitialContext(); targetDataSource = (DataSource) ctx.lookup(url); } catch (NamingException e) { logger.error("Failure while configuring JNDI data source with URL: " + url, e); throw new SQLException( "Unable to configure JNDI data source with URL " + url + ": " + e.toString()); } } dataSource = new LazyConnectionDataSourceProxy(targetDataSource); transactionManager = new DataSourceTransactionManager(targetDataSource); }
From source file:org.compass.core.jndi.NamingHelper.java
/** * Bind val to name in ctx, and make sure that all intermediate contexts * exist./*w w w. j ava 2 s .c o m*/ * * @param ctx * the root context * @param name * the name as a string * @param val * the object to be bound * @throws javax.naming.NamingException */ public static void bind(Context ctx, String name, Object val) throws NamingException { try { ctx.rebind(name, val); } catch (Exception e) { Name n = ctx.getNameParser("").parse(name); while (n.size() > 1) { String ctxName = n.get(0); Context subctx = null; try { subctx = (Context) ctx.lookup(ctxName); } catch (NameNotFoundException nfe) { // don't do nothing } if (subctx != null) { ctx = subctx; } else { ctx = ctx.createSubcontext(ctxName); } n = n.getSuffix(1); } ctx.rebind(n, val); } }
From source file:com.wso2telco.dbutils.DbUtils.java
/** * Initialize datasources.//from w w w. jav a 2 s .c o m * * @throws SQLException * the SQL exception * @throws AxataDBUtilException * the axata db util exception */ public static void initializeDatasources() throws SQLException, AxataDBUtilException { if (axiataDatasource != null) { return; } try { log.info("Before DB Initialize"); Context ctx = new InitialContext(); axiataDatasource = (DataSource) ctx.lookup(AXIATA_DATA_SOURCE); } catch (NamingException e) { handleException("Error while looking up the data source: " + AXIATA_DATA_SOURCE, e); } }
From source file:org.jamwiki.db.DatabaseConnection.java
/** * Return a connection to the database with the specified parameters. * The caller <b>must</b> close this connection when finished! * * @param driver A String indicating the full path for the database driver class. * @param url The JDBC driver URL.//from w w w. ja v a 2s.c om * @param user The database user. * @param password The database user password. * @throws SQLException Thrown if any failure occurs while getting the test connection. */ protected static Connection getTestConnection(String driver, String url, String user, String password) throws SQLException { if (url.startsWith("jdbc:")) { if (!StringUtils.isBlank(driver)) { try { // ensure that the Driver class has been loaded ResourceUtil.forName(driver); } catch (ClassNotFoundException e) { throw new SQLException("Unable to instantiate class with name: " + driver); } } return DriverManager.getConnection(url, user, password); } else { DataSource testDataSource = null; try { Context ctx = new InitialContext(); // TODO: Try appending "java:comp/env/" to the JNDI Name if it is missing? testDataSource = (DataSource) ctx.lookup(url); } catch (NamingException e) { logger.error("Failure while configuring JNDI data source with URL: " + url, e); throw new SQLException( "Unable to configure JNDI data source with URL " + url + ": " + e.toString()); } return testDataSource.getConnection(); } }
From source file:com.ironiacorp.persistence.datasource.HibernateConfigurationUtil.java
/** * Retrieve the data sources registered at JNDI. * /* w w w . j a va 2 s. c o m*/ * @return A collection of the data sources found. */ public static Collection<String> getAvailableDataSources() { Context initCtx = null; Context envCtx = null; try { initCtx = new InitialContext(); envCtx = (Context) initCtx.lookup("java:comp/env"); } catch (NamingException e) { return new ArrayList<String>(0); } return getAvailableDataSources(envCtx); }
From source file:com.wso2telco.core.dbutils.DbUtils.java
/** * Gets the db connection./*from ww w .j a va 2 s . com*/ * * @return the db connection * @throws SQLException * the SQL exception */ public static synchronized Connection getDbConnection(DataSourceNames dataSourceName) throws Exception { try { if (!dbDataSourceMap.containsKey(dataSourceName)) { Context ctx = new InitialContext(); dbDataSourceMap.put(dataSourceName, (DataSource) ctx.lookup(dataSourceName.jndiName())); } DataSource dbDatasource = dbDataSourceMap.get(dataSourceName); if (dbDatasource != null) { log.info(dataSourceName.toString() + " DB Initialize successfully."); return dbDatasource.getConnection(); } else { log.info(dataSourceName.toString() + " DB NOT Initialize successfully."); return null; } } catch (Exception e) { log.info("Error while looking up the data source: " + dataSourceName.toString(), e); throw e; } }
From source file:de.micromata.genome.util.runtime.LocalSettingsEnv.java
private static LocalSettingsEnv createJndiLocalSettingsEnv() { Hashtable<String, Object> env = new Hashtable<String, Object>(); Context initialContext; try {//from www . j ava 2 s.co m try { initialContext = new InitialContext(); initialContext.lookup("java:"); } catch (NameNotFoundException | NoInitialContextException ex) { log.info("No initialContext. Create own context"); JndiMockupNamingContextBuilder contextBuilder = new JndiMockupNamingContextBuilder(); InitialContextFactory initialContextFactory = contextBuilder.createInitialContextFactory(env); initialContext = initialContextFactory.getInitialContext(env); contextBuilder.activate(); } LocalSettingsEnv localSettingsEnv = localSettingsEnvSupplier.apply(initialContext); log.info("Jndi LocalSettingsEnv intialized: " + JndiDumper.getJndiDump()); return localSettingsEnv; } catch (NamingException ex) { throw new RuntimeException(ex); } }
From source file:org.grouter.common.hibernate.HibernateUtilContextAware.java
/** * Create session factories and configurations and store in hibernateConfigMap. On * completion we enter INITIALISED state. *///from ww w.ja va2 s . c om private static void createSessionFactoriesFromConfigMap() { // read in all config and create session factories Iterator iter = hibernateConfigMap.keySet().iterator(); while (iter.hasNext()) { SessionFactory sessionFactory; String key = (String) iter.next(); HibernateConfigItem hibernateConfigItem = hibernateConfigMap.get(key); String file = hibernateConfigItem.getConfigFile(); Configuration configuration; if (file == null) { log.info("Loading properties config and not from file "); configuration = hibernateConfigItem.getConfiguration(); } else { log.info("Loading properties from : " + file); configuration = new Configuration(); configuration = configuration.configure(file); } try { String sessionFactoryName = configuration.getProperty(Environment.SESSION_FACTORY_NAME); if (sessionFactoryName != null) { log.debug("Looking up SessionFactory in JNDI with name : " + sessionFactoryName); try { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, configuration.getProperty(Environment.JNDI_CLASS)); env.put(Context.URL_PKG_PREFIXES, configuration.getProperty(Environment.JNDI_PREFIX)); env.put(Context.PROVIDER_URL, configuration.getProperty(Environment.JNDI_URL)); Context context = new InitialContext(env); JNDIUtils.printJNDI(context, log); sessionFactory = (SessionFactory) context.lookup(sessionFactoryName); if (sessionFactory == null) { throw new IllegalStateException( "SessionFactory from JNDI lookup returned a null implemenation using file : " + file); } } catch (NamingException ex) { log.error("Failed looking up sessinfactory : " + sessionFactoryName, ex); throw new RuntimeException(ex); } } else { sessionFactory = configuration.buildSessionFactory(); if (sessionFactory == null) { throw new IllegalStateException( "SessionFactory could not be createed from the configuration using file : " + file); } } hibernateConfigItem.setConfiguration(configuration); hibernateConfigItem.setSessionFactory(sessionFactory); // We need to have a default sessionfactory / configuration if (hibernateConfigItem.isDeafult()) { hibernateConfigItemDefault = hibernateConfigItem; } // setInterceptor(configuration, null); // hibernateConfigMap.put(key) } catch (Throwable ex) { log.error("Failed initializing from configuration.", ex); throw new ExceptionInInitializerError(ex); } } currentState = STATE.INITIALISED; log.info("Entered state : " + currentState); }
From source file:Util.java
/** * Create a subcontext including any intermediate contexts. * // ww w .ja v a 2 s .c o m * @param ctx * the parent JNDI Context under which value will be bound * @param name * the name relative to ctx of the subcontext. * @return The new or existing JNDI subcontext * @throws NamingException * on any JNDI failure */ public static Context createSubcontext(Context ctx, Name name) throws NamingException { Context subctx = ctx; for (int pos = 0; pos < name.size(); pos++) { String ctxName = name.get(pos); try { subctx = (Context) ctx.lookup(ctxName); } catch (NameNotFoundException e) { subctx = ctx.createSubcontext(ctxName); } // The current subctx will be the ctx for the next name component ctx = subctx; } return subctx; }