public NameClassPair next() { String name = (String) domains.nextElement(); Object value = ctxMap.get(name); String className = value.getClass().getName(); NameClassPair pair = new NameClassPair(name, className); return pair; }
assertTrue("Correct DataSource registered", context1.lookup("java:comp/env/jdbc/myds") == ds); assertTrue("Correct Object registered", context1.lookup("myobject") == obj); NamingEnumeration<?> bindingEnum = context3.listBindings(""); while (bindingEnum.hasMoreElements()) { Binding binding = (Binding) bindingEnum.nextElement(); bindingMap.put(binding.getName(), binding); Map<String, Binding> subBindingMap = new HashMap<>(); NamingEnumeration<?> subBindingEnum = jdbcContext.listBindings(""); while (subBindingEnum.hasMoreElements()) { Binding binding = (Binding) subBindingEnum.nextElement(); subBindingMap.put(binding.getName(), binding); while (pairEnum.hasMore()) { NameClassPair pair = (NameClassPair) pairEnum.next(); pairMap.put(pair.getName(), pair.getClassName()); while (subPairEnum.hasMoreElements()) { NameClassPair pair = (NameClassPair) subPairEnum.next(); subPairMap.put(pair.getName(), pair.getClassName()); while (pairEnum.hasMore()) { NameClassPair pair = (NameClassPair) pairEnum.next(); pairMap.put(pair.getName(), pair.getClassName());
private void addEntries(final ModelNode current, final Context context) throws NamingException { final NamingEnumeration<NameClassPair> entries = context.list(""); while (entries.hasMore()) { final NameClassPair pair = entries.next(); final ModelNode node = current.get(pair.getName()); node.get("class-name").set(pair.getClassName()); try { final Object value; if(context instanceof NamingContext) { value = ((NamingContext)context).lookup(pair.getName(), false); } else { value = context.lookup(pair.getName()); NamingLogger.ROOT_LOGGER.failedToLookupJndiViewValue(pair.getName(),e); NamingLogger.ROOT_LOGGER.failedToLookupJndiViewValue(pair.getName(),e);
String itemName = is.readUTF(); String itemClass = is.readUTF(); final NameClassPair nameClassPair = new NameClassPair(itemName, itemClass, true); final CompositeName inNamespace = (CompositeName) compositeName.clone(); inNamespace.add(itemName); nameClassPair.setNameInNamespace(inNamespace.toString()); results.add(nameClassPair);
public Object nextElement() { String name = (String) namesEnum.nextElement(); String className = bindings.get(name).getClass().getName(); return new NameClassPair(name, className); }
public static void printSearchEnumeration(NamingEnumeration retEnum) { try { while (retEnum.hasMore()) { SearchResult sr = (SearchResult) retEnum.next(); System.out.println(">>" + sr.getNameInNamespace()); Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "uid=administrator,cn=admins,cn=city"); NamingEnumeration answer = ctx.search("", null, ctl); ctx.close();
@Override public T nextElement() { if (names.hasMoreElements()) { String name = (String) names.nextElement(); String className = bindings.get(name).getClass().getName(); return (T) (new NameClassPair(name, className)); } else { return null; } }
import javax.naming.*; ... public static Map toMap(Context ctx) throws NamingException { String namespace = ctx instanceof InitialContext ? ctx.getNameInNamespace() : ""; HashMap<String, Object> map = new HashMap<String, Object>(); log.info("> Listing namespace: " + namespace); NamingEnumeration<NameClassPair> list = ctx.list(namespace); while (list.hasMoreElements()) { NameClassPair next = list.next(); String name = next.getName(); String jndiPath = namespace + name; Object lookup; try { log.info("> Looking up name: " + jndiPath); Object tmp = ctx.lookup(jndiPath); if (tmp instanceof Context) { lookup = toMap((Context) tmp); } else { lookup = tmp.toString(); } } catch (Throwable t) { lookup = t.getMessage(); } map.put(name, lookup); } return map; }
object = resources.lookup(source); } catch (NamingException e) { resources.createSubcontext(dest); } catch (NamingException e) { errorList.put (dest, Integer.valueOf(WebdavStatus.SC_CONFLICT)); return false; NamingEnumeration<NameClassPair> enumeration = resources.list(source); while (enumeration.hasMoreElements()) { NameClassPair ncPair = enumeration.nextElement(); String childDest = dest; if (!"/".equals(childDest)) childDest += "/"; childDest += ncPair.getName(); String childSrc = source; if (!"/".equals(childSrc)) childSrc += "/"; childSrc += ncPair.getName(); copyResource(resources, errorList, childSrc, childDest); errorList.put (dest, Integer.valueOf(WebdavStatus.SC_INTERNAL_SERVER_ERROR)); return false;
public static Map<String, String> listToMap(NamingEnumeration enumeration) { Map<String, String> result = new HashMap<String, String>(); while (enumeration.hasMoreElements()) { NameClassPair nameClassPair = (NameClassPair) enumeration.nextElement(); String name = nameClassPair.getName(); result.put(name, nameClassPair.getClassName()); } return result; }
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); props.put(Context.PROVIDER_URL, url); props.put(Context.SECURITY_PRINCIPAL, principalName); props.put(Context.SECURITY_CREDENTIALS, params.get("password")); // secretpwd if (url.toUpperCase().startsWith("LDAPS://")) { ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration<SearchResult> results = context.search(toDC(domainName),"(& (userPrincipalName="+principalName+")(objectClass=user))", ctrls); if(!results.hasMore()) throw new AuthenticationException("Principal name not found"); SearchResult result = results.next(); System.out.println("distinguisedName: " + result.getNameInNamespace() ); // CN=Firstname Lastname,OU=Mycity,DC=mydomain,DC=com
/** * Print the contents of a JNDI context recursively * * @param indent indentation string * @param ctx the JNDI context * @exception NamingException thrown if any problems occur */ private void showTree(String indent, Context ctx, int depth, int maxDepth) throws NamingException { if( depth == maxDepth ) return; NamingEnumeration enumeration = ctx.list(""); while (enumeration.hasMoreElements()) { NameClassPair ncp = (NameClassPair)enumeration.next(); System.out.println(indent+ncp); if (ncp.getClassName().indexOf("Context") != -1) showTree(indent+ncp.getName()+"/", (Context)ctx.lookup(ncp.getName()), depth+1, maxDepth); } }
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); props.put(Context.PROVIDER_URL, "ldap://LDAPSERVER:PORT"); props.put(Context.SECURITY_PRINCIPAL, "uid=adminuser,ou=special users,o=xx.com");//adminuser - User with special priviledge, dn user props.put(Context.SECURITY_CREDENTIALS, "adminpassword");//dn user password javax.naming.directory.SearchResult result = answers.nextElement(); String user = result.getNameInNamespace();
public Object next() throws NamingException { if ( !hasMore() ) { throw new NoSuchElementException(); } final String name = (String) m_names.next(); Object object = m_bindings.get( name ); if ( !m_returnBindings ) { return new NameClassPair( name, object.getClass().getName() ); } else { return new Binding( name, resolve( name, object ) ); } }
private JndiInputFile findNextFile() throws NamingException { while (!enumerationDeque.isEmpty() && enumerationDeque.peek().hasMore()) { NameClassPair next = enumerationDeque.peek().next(); Object entry = dirContextDeque.peek().lookup(next.getName()); if (entry instanceof DirContext) { dirContextDeque.push((DirContext) entry); enumerationDeque.push(((DirContext) entry).list("/")); fullPath.add(next.getName()); } else { return new JndiInputFile(entry, String.format("%s/%s", Joiner.on("/").skipNulls().join(fullPath), next.getName())); } } if (!enumerationDeque.isEmpty()) { enumerationDeque.pop(); dirContextDeque.pop(); fullPath.remove(fullPath.size() - 1); return findNextFile(); } else { return null; } } }
/** * print out a naming enumeration * @param namingEnumeration * @throws NamingException */ public static void printNamingEnumeration(NamingEnumeration<?> namingEnumeration) throws NamingException { while (namingEnumeration.hasMore()) { Object nextElement = namingEnumeration.next(); if (nextElement instanceof Attribute) { Attribute attribute = (Attribute)nextElement; printAttribute(attribute); } else if (nextElement instanceof SearchResult) { SearchResult searchResult = (SearchResult)nextElement; System.out.println("Search result: " + searchResult.getNameInNamespace()); Attributes attributes = searchResult.getAttributes(); printAttributes(attributes); } else if (nextElement instanceof NameClassPair) { NameClassPair nameClassPair = (NameClassPair)namingEnumeration.nextElement(); System.out.println("Name class pair: " + nameClassPair.getClassName() + ", " + nameClassPair.getNameInNamespace()); } else { throw new RuntimeException("Not expecting type: " + nextElement); } } }
private static void printSchema(String contextName, DirContext schemaContext) throws NameNotFoundException, NamingException { outstream.println(); NamingEnumeration<NameClassPair> schemaList = schemaContext.list(contextName); while (schemaList.hasMore()) { NameClassPair ncp = schemaList.nextElement(); printObject(contextName, ncp.getName(), schemaContext); outstream.println(); } outstream.println(); }
@SuppressWarnings( "rawtypes" ) private void getPaths( String rootName, Map<String, Attributes> childs, List<String> paths ) throws Exception { NamingEnumeration ne = getInitialContext().list( rootName ); while ( ne.hasMore() ) { NameClassPair nameCP = (NameClassPair) ne.next(); childs.put( nameCP.getName() + "," + rootName, getInitialContext().getAttributes( nameCP.getName() + "," + rootName ) ); getPaths( nameCP.getName() + "," + rootName, childs, paths ); paths.add( nameCP.getName() + "," + rootName ); } }
public synchronized void destroySubcontextWithChilds(String name) throws NamingException { NamingEnumeration<NameClassPair> list = ctx.list(name); try { while (list.hasMore()) destroySubcontextWithChilds(list.next().getNameInNamespace()); } finally { LdapUtils.safeClose(list); } ctx.destroySubcontext(name); }