private static Map<String, DataSource> getJndiDataSourcesAt(String jndiPrefix) throws NamingException { final InitialContext initialContext = new InitialContext(); final Map<String, DataSource> dataSources = new LinkedHashMap<String, DataSource>(2); try { for (final NameClassPair nameClassPair : Collections.list(initialContext.list(jndiPrefix))) { // note: il ne suffit pas de tester // (DataSource.class.isAssignableFrom(Class.forName(nameClassPair.getClassName()))) // car nameClassPair.getClassName() vaut "javax.naming.LinkRef" sous jboss 5.1.0.GA // par exemple, donc on fait le lookup pour voir final String jndiName; if (nameClassPair.getName().startsWith("java:")) { // pour glassfish v3 jndiName = nameClassPair.getName(); } else { jndiName = jndiPrefix + '/' + nameClassPair.getName(); } final Object value = initialContext.lookup(jndiName); if (value instanceof DataSource) { dataSources.put(jndiName, (DataSource) value); } } } catch (final NamingException e) { // le préfixe ("comp/env/jdbc", "/jdbc" ou "java:global/jdbc", etc) n'existe pas dans jndi, // (dans glassfish 3.0.1, c'est une NamingException et non une NameNotFoundException) return dataSources; } initialContext.close(); return dataSources; }
@Test public void testIt() throws Exception { final InitialContext ctx = getRemoteContext(); final ClassLoader current = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(Remote.class.getClassLoader()); Remote remote = (Remote) ctx.lookup( ARCHIVE_NAME + "/" + Bean.class.getSimpleName() + "!" + Remote.class.getName()); assertNotNull(remote); assertEquals("Echo: test", remote.echo("test")); remote = (Remote) ctx.lookup( ARCHIVE_NAME + "/" + Singleton.class.getSimpleName() + "!" + BinderRemote.class.getName()); assertNotNull(remote); assertEquals("Echo: test", remote.echo("test")); remote = (Remote) ctx.lookup( ARCHIVE_NAME + "/" + StatefulBean.class.getSimpleName() + "!" + Remote.class.getName()); assertNotNull(remote); assertEquals("Echo: test", remote.echo("test")); final Set<String> expected = new HashSet<String>(); expected.add(Bean.class.getSimpleName() + "!" + Remote.class.getName()); expected.add(Singleton.class.getSimpleName() + "!" + BinderRemote.class.getName()); expected.add(StatefulBean.class.getSimpleName() + "!" + Remote.class.getName()); NamingEnumeration<NameClassPair> e = ctx.list("test"); while (e.hasMore()) { NameClassPair binding = e.next(); if (!expected.remove(binding.getName())) { Assert.fail("unknown binding " + binding.getName()); } } if (!expected.isEmpty()) { Assert.fail("bindings not found " + expected); } } finally { ctx.close(); Thread.currentThread().setContextClassLoader(current); } }
public Collection getEnvProperties(String envSubDir, boolean sortProperties) throws Exception { ArrayList envListReturn = new ArrayList(); try { InitialContext EnvironmentContext = new InitialContext(); NamingEnumeration<NameClassPair> envList = EnvironmentContext.list("java:comp/env/" + envSubDir); if (envList != null) { NameClassPair item; while (envList.hasMore()) { item = envList.next(); envListReturn.add(item.getName()); } } } catch (NamingException NE) { NE.printStackTrace(); // Eccezione tracciata ma volutamente non rilancia. // Nel caso non trovasse variabili definite è corretto che torni una lista vuota. } if (sortProperties) Collections.sort(envListReturn); return envListReturn; }
private void initJNDI(Properties properties) throws NamingException { if (ctx != null) { try { ctx.close(); } catch (NamingException e) { // Utils.printTrace(e); throw e; } } // jndiProps.put("weblogic.jndi.responseReadTimeout", "100"); try { if (properties != null) { Hashtable<String, String> jndiProps = null; jndiProps = new Hashtable<String, String>(); jndiProps.put(Context.PROVIDER_URL, properties.getProperty(Context.PROVIDER_URL)); jndiProps.put( Context.SECURITY_CREDENTIALS, properties.getProperty(Context.SECURITY_CREDENTIALS)); jndiProps.put( Context.INITIAL_CONTEXT_FACTORY, properties.getProperty(Context.INITIAL_CONTEXT_FACTORY)); jndiProps.put( Context.SECURITY_PRINCIPAL, properties.getProperty(Context.SECURITY_PRINCIPAL)); jndiProps.put("dicated.connection", "true"); ctx = new InitialContext(jndiProps); } else ctx = new InitialContext(); Hashtable hashTable = ctx.getEnvironment(); Utils.printTrace(false); Utils.printMessage(hashTable.toString()); } catch (NamingException e) { // Utils.printTrace(false); // Utils.printTrace(e); // Utils.printMessage("! service stop !"); throw e; } // connect to the server try { NamingEnumeration<NameClassPair> list = null; // list all avaiable names if (this.appServiceProperties != null && "Y" .equals( this.appServiceProperties .getProperty("print.all.available.services") .toUpperCase())) { Utils.printMessage("======================================="); list = ctx.list(""); while (list.hasMore()) { try { NameClassPair tt = list.next(); Object ttt = ctx.lookup(tt.getName()); Utils.printMessage(ttt.getClass() + ":\t" + tt.getName()); } catch (Exception e) { Utils.printTrace(e); } } Utils.printMessage("======================================="); } // get user transaction userTransaction = (UserTransaction) ctx.lookup(RMIIQMAppModuleServicesConstants.RMIUserTransaction); // get general app service this.appModuleService = ctx.lookup(RMIIQMAppModuleServicesConstants.appModuleGeneralServiceName); // get lookup service this.appModuleLookupService = ctx.lookup(RMIIQMAppModuleServicesConstants.appModuleLookupServiceName); // get non temporal service this.appModuleNonTemporalService = ctx.lookup(RMIIQMAppModuleServicesConstants.appModuleNonTemporalServiceName); if (this.appServiceProperties != null && "Y" .equals( this.appServiceProperties .getProperty("print.service.available.methods") .toUpperCase())) { Utils.printMessage("================ this.appModuleService ================"); Utils.printMethods(this.appModuleService); Utils.printMessage("================ this.appModuleLookupService ================"); Utils.printMethods(this.appModuleLookupService); Utils.printMessage("================ this.appModuleNonTemporalService ================"); Utils.printMethods(this.appModuleNonTemporalService); } } catch (Exception e) { Utils.printTrace(e); Utils.printMessage(e); throw e; } }