/** * Enumerates the names bound in the named context, along with the class * names of objects bound to them. The contents of any subcontexts are * not included. * <p> * If a binding is added to or removed from this context, its effect on * an enumeration previously returned is undefined. * * @param name the name of the context to list * @return an enumeration of the names and class names of the bindings in * this context. Each element of the enumeration is of type NameClassPair. * @exception NamingException if a naming exception is encountered */ @Override public NamingEnumeration<NameClassPair> list(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) { return new NamingContextEnumeration(bindings.values().iterator()); } NamingEntry entry = bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException (sm.getString("namingContext.nameNotBound", name, name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException (sm.getString("namingContext.contextExpected")); } return ((Context) entry.value).list(name.getSuffix(1)); }
/** * Enumerates the names bound in the named context, along with the class * names of objects bound to them. The contents of any subcontexts are not * included. * <p> * If a binding is added to or removed from this context, its effect on an * enumeration previously returned is undefined. * * @param name * the name of the context to list * @return an enumeration of the names and class names of the bindings in * this context. Each element of the enumeration is of type * NameClassPair. * @exception NamingException * if a naming exception is encountered */ @Override public NamingEnumeration<NameClassPair> list(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) { return new NamingContextEnumeration(bindings.values().iterator()); } NamingEntry entry = bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException(sm.getString("namingContext.nameNotBound", name, name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException(sm.getString("namingContext.contextExpected")); } return ((Context) entry.value).list(name.getSuffix(1)); }
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { Properties p = new Properties(); p.put("java.naming.factory.initial", "org.apache.openejb.client.RemoteInitialContextFactory"); p.put("java.naming.provider.url", "http://localhost:8080/tomee/ejb"); // user and pass optional // p.put("java.naming.security.principal", "myuser"); // p.put("java.naming.security.credentials", "mypass"); InitialContext ctx = new InitialContext(p); for (NameClassPair pair : Collections.list(ctx.list(""))) { System.out.println(pair.getName()); System.out.println(pair.getClassName()); } // ejbBankAccount = (BankAccountRemote) ctx.lookup("BankAccountEJB"); ejbBankAccount = (BankAccountRemote) ctx.lookup("BankAccountEJBRemote"); ejbBankAccount.transfer(BigDecimal.valueOf(100), 1, 2); System.out.println("EJB OK"); } catch (Exception ex) { ex.printStackTrace(); } }
/** * Enumerates the names bound in the named context, along with the class * names of objects bound to them. The contents of any subcontexts are * not included. * <p> * If a binding is added to or removed from this context, its effect on * an enumeration previously returned is undefined. * * @param name the name of the context to list * @return an enumeration of the names and class names of the bindings in * this context. Each element of the enumeration is of type NameClassPair. * @throws NamingException if a jndi exception is encountered */ @Override public NamingEnumeration<NameClassPair> list(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) { name = name.getSuffix(1); } if (name.isEmpty()) { return new NamingContextEnumeration(new ArrayList<>(bindings.values())); } NamingEntry entry = bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException(SM.getString("namingContext.nameNotBound", name, name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException(SM.getString("namingContext.contextExpected")); } return ((Context) entry.value).list(name.getSuffix(1)); }
public <T> Map<String, T> findByTypeWithName(Class<T> type) { Map<String, T> answer = new LinkedHashMap<String, T>(); try { NamingEnumeration<NameClassPair> list = getContext().list(""); while (list.hasMore()) { NameClassPair pair = list.next(); Object instance = context.lookup(pair.getName()); if (type.isInstance(instance)) { answer.put(pair.getName(), type.cast(instance)); } } } catch (NamingException e) { // ignore } return answer; }
public <T> Set<T> findByType(Class<T> type) { Set<T> answer = new LinkedHashSet<T>(); try { NamingEnumeration<NameClassPair> list = getContext().list(""); while (list.hasMore()) { NameClassPair pair = list.next(); Object instance = context.lookup(pair.getName()); if (type.isInstance(instance)) { answer.add(type.cast(instance)); } } } catch (NamingException e) { // ignore } return answer; }
@Override public NamingEnumeration<NameClassPair> list(final Name name) throws NamingException { final Name searchName = validateName(name); // If null, it means we don't know how to handle this -> throw to the // parent if (searchName == null) { return parent.list(name); } else if (searchName.isEmpty()) { // listing this context return new Names(bindings); } // Perhaps 'name' names a context final Object target = lookup(name); if (target instanceof Context) { return ((Context) target).list(""); } throw new NotContextException(name + " cannot be listed"); }
private static String contextDump(Context ctx, String root, boolean recurse, int level) { StringBuilder sb = new StringBuilder(); try { for( NamingEnumeration<NameClassPair> list = ctx.list(root); list.hasMore(); ) { NameClassPair nc = list.next(); for(int t=0; t<level; ++t) sb.append('\t'); sb.append(nc); sb.append("line.separator"); if(recurse) { String childPath = root.length() > 0 ? root + "/" + nc.getName() : nc.getName(); contextDump(ctx, childPath, recurse, level+1); } } } catch(NamingException nX) { sb.append(nX.getMessage()); } return sb.toString(); }
@Test public void testRunning() throws Exception { DirContext ctx = getContext(); HashSet<String> set = new HashSet<>(); NamingEnumeration<NameClassPair> list = ctx.list("ou=system"); while (list.hasMore()) { NameClassPair ncp = list.next(); set.add(ncp.getName()); } Assert.assertTrue(set.contains("uid=admin")); Assert.assertTrue(set.contains("ou=users")); Assert.assertTrue(set.contains("ou=groups")); Assert.assertTrue(set.contains("ou=configuration")); Assert.assertTrue(set.contains("prefNodeName=sysPrefRoot")); }
@Test public void testRunning() throws Exception { Hashtable<String, String> env = new Hashtable<>(); env.put(Context.PROVIDER_URL, "ldap://localhost:1024"); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL); env.put(Context.SECURITY_CREDENTIALS, CREDENTIALS); DirContext ctx = new InitialDirContext(env); HashSet<String> set = new HashSet<>(); NamingEnumeration<NameClassPair> list = ctx.list("ou=system"); while (list.hasMore()) { NameClassPair ncp = list.next(); set.add(ncp.getName()); } Assert.assertTrue(set.contains("uid=admin")); Assert.assertTrue(set.contains("ou=users")); Assert.assertTrue(set.contains("ou=groups")); Assert.assertTrue(set.contains("ou=configuration")); Assert.assertTrue(set.contains("prefNodeName=sysPrefRoot")); }
/** * {@inheritDoc} */ public NamingEnumeration<NameClassPair> list(Name name) throws NamingException { if (name.isEmpty()) { try { return new NameClassPairEnumeration(registry.list()); } catch (RemoteException e) { throw (NamingException) newNamingException(e) .fillInStackTrace(); } } Object obj = lookup(name); if (obj instanceof Context) { try { return ((Context) obj).list(""); //$NON-NLS-1$ } finally { ((Context) obj).close(); } } // jndi.80=Name specifies an object that is not a context: {0} throw new NotContextException(Messages.getString("jndi.80", name)); //$NON-NLS-1$ }
/** * Create an XML description of a {@link javax.naming.Context} from the server. * @param outOfScope <code>true</code> if the class for this context is not in the classpath. * @return An XML string describing the current RMI context. * @throws NamingException If the RMI context could not be accessed. */ private String getContextDescription(boolean outOfScope) throws NamingException { StringBuilder dsc = getHeader(); dsc.append("<context name=\"").append(rmiRegistryContext.getNameInNamespace()).append("\">\n"); NamingEnumeration<NameClassPair> ne = rmiRegistryContext.list(""); while (ne.hasMore()) { NameClassPair nc = ne.next(); dsc.append(" <name value=\"").append(nc.getName()); if (nc.isRelative()) dsc.append("\" relative=\"").append(nc.isRelative()); dsc.append("\">\n"); dsc.append(" <class name=\"").append(nc.getClassName()).append("\""); if (outOfScope) dsc.append(" inscope=\"false\""); dsc.append("/>\n </name>\n"); } dsc.append("</context>"); return dsc.toString(); }
/** * {@inheritDoc} */ public NamingEnumeration<NameClassPair> list(Name name) throws NamingException { if (!(name instanceof CompositeName)) { // jndi.26=URL context can't accept non-composite name: {0} throw new InvalidNameException(Messages.getString("jndi.26", name)); //$NON-NLS-1$ } if (name.size() == 1) { return list(name.get(0)); } Context context = getContinuationContext(name); try { return context.list(name.getSuffix(1)); } finally { context.close(); } }
public void testAttributeDefinition() throws NamingException { addMoreAttributeData(); MockLdapContext mockContext = new MockLdapContext(context, null, ""); Attribute attr = new LdapAttribute("objectclass", mockContext); DirContext attributeDefinition = attr.getAttributeDefinition(); NamingEnumeration<NameClassPair> ne = attributeDefinition.list(""); assertFalse(ne.hasMore()); try { ne = attributeDefinition.list("invalid"); fail("Should throw NameNotFoundException"); } catch (NameNotFoundException e) { // Expected. } Attributes schemaAttributes = attributeDefinition.getAttributes(""); assertEquals(7, schemaAttributes.size()); assertEquals("1.3.6.1.4.1.1466.115.121.1.38", schemaAttributes.get( "syntax").get()); assertEquals("objectClass", schemaAttributes.get("name").get()); assertEquals("2.5.4.0", schemaAttributes.get("numericoid").get()); assertEquals("userApplications", schemaAttributes.get("usage").get()); assertEquals("objectIdentifierMatch", schemaAttributes.get("equality") .get()); }
public void testSyntaxDefinition() throws NamingException { addMoreAttributeData(); MockLdapContext mockContext = new MockLdapContext(context, null, ""); Attribute attr = new LdapAttribute("objectclass", mockContext); DirContext attributeDefinition = attr.getAttributeSyntaxDefinition(); NamingEnumeration<NameClassPair> ne = attributeDefinition.list(""); assertFalse(ne.hasMore()); try { ne = attributeDefinition.list("invalid"); fail("Should throw NameNotFoundException"); } catch (NameNotFoundException e) { // Expected. } Attributes schemaAttributes = attributeDefinition.getAttributes(""); assertEquals(3, schemaAttributes.size()); assertEquals("system", schemaAttributes.get("x-schema").get()); assertEquals("true", schemaAttributes.get("x-is-human-readable").get()); assertEquals("1.3.6.1.4.1.1466.115.121.1.38", schemaAttributes.get( "numericoid").get()); }
void passivate() throws NamingException { if( embedded ) { // Clear embedded JNDI NamingEnumeration<NameClassPair> names = initialContext.list( "" ); while( names.hasMore() ) { initialContext.unbind( names.next().getName() ); } // Set java.naming.provider.url System property back if( previousUrl == null ) { System.clearProperty( Context.PROVIDER_URL ); } else { System.setProperty( Context.PROVIDER_URL, previousUrl ); } } initialContext.close(); }
/** * Enumerates the names bound in the named context, along with the class * names of objects bound to them. * * @param name the name of the context to list * @return an enumeration of the names and class names of the bindings in * this context. Each element of the enumeration is of type NameClassPair. * @exception NamingException if a naming exception is encountered */ @Override public NamingEnumeration<NameClassPair> list(String name) throws NamingException { if (log.isDebugEnabled()) { log.debug(sm.getString("selectorContext.methodUsingString", "list", name)); } return getBoundContext().list(parseName(name)); }
/** * Enumerates the names bound in the named context, along with the class * names of objects bound to them. * * @param name the name of the context to list * @return an enumeration of the names and class names of the bindings in * this context. Each element of the enumeration is of type NameClassPair. * @exception NamingException if a naming exception is encountered */ @Override public NamingEnumeration<NameClassPair> list(String name) throws NamingException { if (!aliases.isEmpty()) { AliasResult result = findAlias(name); if (result.dirContext != null) { return result.dirContext.list(result.aliasName); } } // Next do a standard lookup List<NamingEntry> bindings = doListBindings(name); // Check the alternate locations List<NamingEntry> altBindings = null; String resourceName = "/META-INF/resources" + name; for (DirContext altDirContext : altDirContexts) { if (altDirContext instanceof BaseDirContext) { altBindings = ((BaseDirContext) altDirContext).doListBindings(resourceName); } if (altBindings != null) { if (bindings == null) { bindings = altBindings; } else { bindings.addAll(altBindings); } } } if (bindings != null) { return new NamingContextEnumeration(bindings.iterator()); } // Really not found throw new NameNotFoundException( sm.getString("resources.notFound", name)); }
/** * List children of this collection. The names given are relative to this * URI's path. The full uri of the children is then : path + "/" + name. */ public Enumeration<String> list() throws IOException { if (!connected) { connect(); } if ((resource == null) && (collection == null)) { throw new FileNotFoundException( getURL() == null ? "null" : getURL().toString()); } Vector<String> result = new Vector<String>(); if (collection != null) { try { NamingEnumeration<NameClassPair> enumeration = collection.list("/"); UEncoder urlEncoder = new UEncoder(UEncoder.SafeCharsSet.WITH_SLASH); while (enumeration.hasMoreElements()) { NameClassPair ncp = enumeration.nextElement(); String s = ncp.getName(); result.addElement( urlEncoder.encodeURL(s, 0, s.length()).toString()); } } catch (NamingException e) { // Unexpected exception throw new FileNotFoundException( getURL() == null ? "null" : getURL().toString()); } } return result.elements(); }
private void tldScanResourcePathsWebInf(DirContext resources, String rootPath, Set tldPaths) throws IOException { if (log.isTraceEnabled()) { log.trace(" Scanning TLDs in " + rootPath + " subdirectory"); } try { NamingEnumeration items = resources.list(rootPath); while (items.hasMoreElements()) { NameClassPair item = (NameClassPair) items.nextElement(); String resourcePath = rootPath + "/" + item.getName(); if (!resourcePath.endsWith(".tld") && (resourcePath.startsWith("/WEB-INF/classes") || resourcePath.startsWith("/WEB-INF/lib"))) { continue; } if (resourcePath.endsWith(".tld")) { if (log.isTraceEnabled()) { log.trace(" Adding path '" + resourcePath + "'"); } tldPaths.add(resourcePath); } else { tldScanResourcePathsWebInf(resources, resourcePath, tldPaths); } } } catch (NamingException e) { ; // Silent catch: it's valid that no /WEB-INF directory exists } }
/** * List children of this collection. The names given are relative to this * URI's path. The full uri of the children is then : path + "/" + name. */ public Enumeration list() throws IOException { if (!connected) { connect(); } if ((resource == null) && (collection == null)) { throw new FileNotFoundException(); } Vector result = new Vector(); if (collection != null) { try { NamingEnumeration enumeration = context.list(getURL().getFile()); while (enumeration.hasMoreElements()) { NameClassPair ncp = (NameClassPair) enumeration.nextElement(); result.addElement(ncp.getName()); } } catch (NamingException e) { // Unexpected exception throw new FileNotFoundException(); } } return result.elements(); }
/** * List children of this collection. The names given are relative to this * URI's path. The full uri of the children is then : path + "/" + name. */ public Enumeration list() throws IOException { if (!connected) { connect(); } if ((resource == null) && (collection == null)) { throw new FileNotFoundException(); } Vector result = new Vector(); if (collection != null) { try { NamingEnumeration _enum = context.list(getURL().getFile()); while (_enum.hasMoreElements()) { NameClassPair ncp = (NameClassPair) _enum.nextElement(); result.addElement(ncp.getName()); } } catch (NamingException e) { // Unexpected exception throw new FileNotFoundException(); } } return result.elements(); }
/** * Enumerates the names bound in the named context, along with the class * names of objects bound to them. * * @param name * the name of the context to list * @return an enumeration of the names and class names of the bindings in * this context. Each element of the enumeration is of type * NameClassPair. * @exception NamingException * if a naming exception is encountered */ @Override public NamingEnumeration<NameClassPair> list(String name) throws NamingException { if (!aliases.isEmpty()) { AliasResult result = findAlias(name); if (result.dirContext != null) { return result.dirContext.list(result.aliasName); } } // Next do a standard lookup List<NamingEntry> bindings = doListBindings(name); // Check the alternate locations List<NamingEntry> altBindings = null; String resourceName = "/META-INF/resources" + name; for (DirContext altDirContext : altDirContexts) { if (altDirContext instanceof BaseDirContext) { altBindings = ((BaseDirContext) altDirContext).doListBindings(resourceName); } if (altBindings != null) { if (bindings == null) { bindings = altBindings; } else { bindings.addAll(altBindings); } } } if (bindings != null) { return new NamingContextEnumeration(bindings.iterator()); } // Really not found throw new NameNotFoundException(sm.getString("resources.notFound", name)); }
/** * List children of this collection. The names given are relative to this * URI's path. The full uri of the children is then : path + "/" + name. */ public Enumeration<String> list() throws IOException { if (!connected) { connect(); } if ((resource == null) && (collection == null)) { throw new FileNotFoundException(getURL() == null ? "null" : getURL().toString()); } Vector<String> result = new Vector<String>(); if (collection != null) { try { NamingEnumeration<NameClassPair> enumeration = collection.list("/"); UEncoder urlEncoder = new UEncoder(UEncoder.SafeCharsSet.WITH_SLASH); while (enumeration.hasMoreElements()) { NameClassPair ncp = enumeration.nextElement(); String s = ncp.getName(); result.addElement(urlEncoder.encodeURL(s, 0, s.length()).toString()); } } catch (NamingException e) { // Unexpected exception throw new FileNotFoundException(getURL() == null ? "null" : getURL().toString()); } } return result.elements(); }
private static void dumpTreeEntry(NamingEnumeration<NameClassPair> list, String s) throws NamingException { System.out.println("\ndump " + s); while (list.hasMore()) { NameClassPair ncp = list.next(); System.out.println(ncp.toString()); if (s.length() == 0) { dumpJndi(ncp.getName()); } else { dumpJndi(s + "/" + ncp.getName()); } } }
@Override public NamingEnumeration<NameClassPair> list(String root) throws NamingException { if (logger.isDebugEnabled()) { logger.debug("Listing name/class pairs under [" + root + "]"); } return new NameClassPairEnumeration(this, root); }
/** * In this test we are trying do a osgi:service list() and test the NamingEnumeration object returned. */ @Test(dependsOnMethods = "testOSGIUrlListWithUnregisteredService") public void testOSGIUrlWithServiceList() throws NamingException { FooService fooService1 = new FooServiceImpl1(); FooService fooService2 = new FooServiceImpl2(); ServiceRegistration<FooService> fooService1Registration = bundleContext.registerService( FooService.class, fooService1, null); ServiceRegistration<FooService> fooService2Registration = bundleContext.registerService( FooService.class, fooService2, null); Context context = jndiContextManager.newInitialContext(); NamingEnumeration<NameClassPair> namingEnumeration = context.list("osgi:service/org.wso2.carbon.jndi.osgi.services.FooService"); assertTrue(namingEnumeration.hasMoreElements()); NameClassPair nameClassPair = namingEnumeration.nextElement(); assertNotNull(nameClassPair, "No NameClassPair returned fo service :" + FooService.class); assertEquals(nameClassPair.getName(), String.valueOf(fooService1Registration.getReference().getProperty(Constants.SERVICE_ID))); assertTrue(namingEnumeration.hasMoreElements()); fooService1Registration.unregister(); fooService2Registration.unregister(); }