List of usage examples for javax.naming NameClassPair getName
public String getName()
From source file:Main.java
public static void main(String[] argv) throws Exception { String url = "iiop://localhost/"; Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory"); env.put(Context.PROVIDER_URL, url); Context ctx = new InitialContext(env); NamingEnumeration e = ctx.list("child"); while (e.hasMore()) { NameClassPair entry = (NameClassPair) e.next(); System.out.println(entry.getName()); }/*from ww w . j a v a 2 s . c o m*/ }
From source file:be.fedict.eid.dss.sp.StartupServletContextListener.java
public static void bindComponent(String jndiName, Object component) throws NamingException { LOG.debug("bind component: " + jndiName); InitialContext initialContext = new InitialContext(); String[] names = jndiName.split("/"); Context context = initialContext; for (int idx = 0; idx < names.length - 1; idx++) { String name = names[idx]; LOG.debug("name: " + name); NamingEnumeration<NameClassPair> listContent = context.list(""); boolean subContextPresent = false; while (listContent.hasMore()) { NameClassPair nameClassPair = listContent.next(); if (!name.equals(nameClassPair.getName())) { continue; }//from w ww . j av a 2s.com subContextPresent = true; } if (!subContextPresent) { context = context.createSubcontext(name); } else { context = (Context) context.lookup(name); } } String name = names[names.length - 1]; context.rebind(name, component); }
From source file:com.funambol.server.db.DataSourceContextHelperTest.java
/** * Usefull method for troubleshooting/*from w w w . j a va2 s. c o m*/ * @throws java.lang.Exception */ private static void printContext(String contextName) throws Exception { System.out.println("---------- Listing '" + contextName + "' ------------"); InitialContext initialContext = new InitialContext(); NamingEnumeration<NameClassPair> nameEnum = initialContext.list(contextName); if (nameEnum != null) { while (nameEnum.hasMore()) { NameClassPair name = nameEnum.next(); String nameInSpace = name.getName(); String className = name.getClassName(); System.out.println("NameInSpace: " + nameInSpace + ", class: " + className); } } System.out.println("--------------------------------------------"); }
From source file:info.bunji.jdbc.logger.JdbcLoggerFactory.java
/** ******************************************** * get loggerName from jdbc url.//from w w w . j av a 2 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:com.flexive.shared.EJBLookup.java
/** * Discover which lookup strategy works for the given class * * @param appName EJB application name * @param environment properties passed to the initial context * @param type the class//from w w w . ja v a 2s.c o m * @return appName (may have changed) */ private static <T> String discoverStrategy(String appName, final Hashtable<String, String> environment, Class<T> type) { InitialContext ctx = null; for (STRATEGY strat : STRATEGY.values()) { if (strat == STRATEGY.UNKNOWN) continue; used_strategy = strat; try { final Hashtable<String, String> env = environment != null ? new Hashtable<String, String>(environment) : new Hashtable<String, String>(); prepareEnvironment(strat, env); ctx = new InitialContext(env); ctx.lookup(buildName(appName, type)); if (used_strategy == STRATEGY.EJB31_MODULE) { // we need to resolve all interfaces required by non-web components (stream server, scheduler), // since they run outside the module context resolveKnownInterfaces(); } return appName; } catch (Exception e) { if (LOG.isDebugEnabled()) { LOG.debug("Strategy " + strat + " failed: " + e.getMessage(), e); } //ignore and try next } } //houston, we have a problem - try locale and remote with appname again iterating through all "root" ctx bindings //this can happen if the ear is not named flexive.ear try { if (ctx == null) ctx = new InitialContext(environment); NamingEnumeration<NameClassPair> ncpe = ctx.list(""); while (ncpe.hasMore()) { NameClassPair ncp = ncpe.next(); if (ncp.getClassName().endsWith("NamingContext")) { appName = ncp.getName(); try { used_strategy = STRATEGY.APP_SIMPLENAME_LOCAL; ctx.lookup(buildName(ncp.getName(), type)); APPNAME = ncp.getName(); LOG.info("Using application name [" + appName + "] for lookups!"); return APPNAME; } catch (Exception e) { //ignore and try remote } try { used_strategy = STRATEGY.APP_SIMPLENAME_REMOTE; ctx.lookup(buildName(ncp.getName(), type)); APPNAME = ncp.getName(); LOG.info("Using application name [" + appName + "] for lookups!"); return APPNAME; } catch (Exception e) { //ignore and try remote } } } } catch (Exception e) { LOG.warn(e); } used_strategy = null; return appName; }
From source file:com.egt.core.util.Utils.java
public static void traceContext(Context c, String name) { NameClassPair ncp; NamingEnumeration<NameClassPair> ncpenum; try {//from w w w. j ava 2 s . c om ncpenum = c.list(name); while (ncpenum.hasMore()) { ncp = ncpenum.next(); if (ncp.getClassName().endsWith("javaURLContext")) { traceContext(c, ncp.getName()); } else { Bitacora.logTrace(ncp.getName() + " = " + ncp.getClassName()); } } } catch (NamingException ex) { Bitacora.logFatal(ThrowableUtils.getString(ex)); } }
From source file:net.chrisrichardson.foodToGo.ejb3.facadeWithSpringDI.PlaceOrderFacadeUsingIntegratedDependencyInjectImpl.java
private void listContext(InitialContext initialContext, String root) throws NamingException { Enumeration<NameClassPair> enumeration = initialContext.list(root); while (enumeration.hasMoreElements()) { NameClassPair nc = enumeration.nextElement(); logger.debug("Name=" + root + "/" + nc.getName()); if (nc.getClassName().endsWith("NamingContext")) listContext(initialContext, root + "/" + nc.getName()); }//from www .j a va2 s . c om }
From source file:com.netspective.axiom.connection.JndiConnectionProvider.java
public Set getAvailableDataSources() { Set result = new HashSet(); try {//from ww w . j a v a 2 s . co m String envPath = getRootContextName(); Context env = getRootContext(); if (env != null) { for (NamingEnumeration e = env.list(""); e.hasMore();) { NameClassPair entry = (NameClassPair) e.nextElement(); result.add(envPath != null ? (envPath + "/" + entry.getName()) : entry.getName()); } } } catch (NamingException e) { log.debug(JndiConnectionProvider.class.getName() + ".getAvailableDataSources()", e); } return result; }
From source file:com.netspective.axiom.connection.JndiConnectionProvider.java
public ConnectionProviderEntries getDataSourceEntries(ValueContext vc) { ConnectionProviderEntries entries = new BasicConnectionProviderEntries(); try {/* w w w . j a va 2 s. c o m*/ String envPath = getRootContextName(); Context env = getRootContext(); if (env != null) { for (NamingEnumeration e = env.list(""); e.hasMore();) { NameClassPair entry = (NameClassPair) e.nextElement(); String dataSourceId = envPath != null ? (envPath + "/" + entry.getName()) : entry.getName(); try { DataSource source = (DataSource) env.lookup(entry.getName()); entries.add(getDataSourceEntry(dataSourceId, source)); } catch (NamingException ex) { log.debug(JndiConnectionProvider.class.getName() + ".getDataSourceEntries()", ex); } catch (SQLException ex) { log.debug(JndiConnectionProvider.class.getName() + ".getDataSourceEntries()", ex); } } } } catch (NamingException e) { log.error("Errorw in getDataSourceEntries()", e); throw new NestableRuntimeException(e); } return entries; }
From source file:catalina.startup.ContextConfig.java
/** * Accumulate and return a Set of resource paths to be analyzed for * tag library descriptors. Each element of the returned set will be * the context-relative path to either a tag library descriptor file, * or to a JAR file that may contain tag library descriptors in its * <code>META-INF</code> subdirectory. * * @exception IOException if an input/output error occurs while * accumulating the list of resource paths */// www . j a v a 2 s.c o m private Set tldScanResourcePaths() throws IOException { if (debug >= 1) { log(" Accumulating TLD resource paths"); } Set resourcePaths = new HashSet(); // Accumulate resource paths explicitly listed in the web application // deployment descriptor if (debug >= 2) { log(" Scanning <taglib> elements in web.xml"); } String taglibs[] = context.findTaglibs(); for (int i = 0; i < taglibs.length; i++) { String resourcePath = context.findTaglib(taglibs[i]); // FIXME - Servlet 2.3 DTD implies that the location MUST be // a context-relative path starting with '/'? if (!resourcePath.startsWith("/")) { resourcePath = "/WEB-INF/web.xml/../" + resourcePath; } if (debug >= 3) { log(" Adding path '" + resourcePath + "' for URI '" + taglibs[i] + "'"); } resourcePaths.add(resourcePath); } // Scan TLDs in the /WEB-INF subdirectory of the web application if (debug >= 2) { log(" Scanning TLDs in /WEB-INF subdirectory"); } DirContext resources = context.getResources(); try { NamingEnumeration items = resources.list("/WEB-INF"); while (items.hasMoreElements()) { NameClassPair item = (NameClassPair) items.nextElement(); String resourcePath = "/WEB-INF/" + item.getName(); // FIXME - JSP 1.2 is not explicit about whether we should // scan subdirectories of /WEB-INF for TLDs also if (!resourcePath.endsWith(".tld")) { continue; } if (debug >= 3) { log(" Adding path '" + resourcePath + "'"); } resourcePaths.add(resourcePath); } } catch (NamingException e) { ; // Silent catch: it's valid that no /WEB-INF directory exists } // Scan JARs in the /WEB-INF/lib subdirectory of the web application if (debug >= 2) { log(" Scanning JARs in /WEB-INF/lib subdirectory"); } try { NamingEnumeration items = resources.list("/WEB-INF/lib"); while (items.hasMoreElements()) { NameClassPair item = (NameClassPair) items.nextElement(); String resourcePath = "/WEB-INF/lib/" + item.getName(); if (!resourcePath.endsWith(".jar")) { continue; } if (debug >= 3) { log(" Adding path '" + resourcePath + "'"); } resourcePaths.add(resourcePath); } } catch (NamingException e) { ; // Silent catch: it's valid that no /WEB-INF/lib directory exists } // Return the completed set return (resourcePaths); }