List of usage examples for javax.naming NamingEnumeration hasMore
public boolean hasMore() throws NamingException;
From source file:com.jaeksoft.searchlib.util.ActiveDirectory.java
public static final Attributes getAttributes(NamingEnumeration<SearchResult> result) throws NamingException { if (result == null) return null; if (!result.hasMore()) return null; SearchResult rs = (SearchResult) result.next(); return rs.getAttributes(); }
From source file:org.wso2.carbon.uuf.core.API.java
/** * Returns a map of service implementation class names and instances of all OSGi services for the given service * class name.// ww w.j a v a 2 s. c om * * @param serviceClassName service class name * @return a map of implementation class and instances */ public static Map<String, Object> getOSGiServices(String serviceClassName) { try { Context context = new InitialContext(); NamingEnumeration<Binding> enumeration = context.listBindings("osgi:service/" + serviceClassName); Map<String, Object> services = new HashMap<>(); while (enumeration.hasMore()) { Binding binding = enumeration.next(); services.put(binding.getClassName(), binding.getObject()); } return services; } catch (NamingException e) { throw new UUFException( "Cannot create the initial context when calling OSGi service '" + serviceClassName + "'."); } }
From source file:org.projectforge.business.ldap.LdapUtils.java
public static String[] getAttributeStringValues(final Attributes attributes, final String attrId) throws NamingException { final Attribute attr = attributes.get(attrId); if (attr == null) { return null; }//from ww w . ja v a 2s . c o m final NamingEnumeration<?> enumeration = attr.getAll(); final List<String> list = new ArrayList<String>(); while (enumeration.hasMore() == true) { final Object attrValue = enumeration.next(); if (attrValue == null) { list.add(null); } list.add(String.valueOf(attrValue)); } return list.toArray(new String[list.size()]); }
From source file:org.pentaho.di.trans.steps.mailvalidator.MailValidation.java
private static ArrayList<String> getMX(String hostName) throws NamingException { // Perform a DNS lookup for MX records in the domain Hashtable<String, String> env = new Hashtable<String, String>(); env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); DirContext ictx = new InitialDirContext(env); Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" }); Attribute attr = attrs.get("MX"); // if we don't have an MX record, try the machine itself if ((attr == null) || (attr.size() == 0)) { attrs = ictx.getAttributes(hostName, new String[] { "A" }); attr = attrs.get("A"); if (attr == null) { throw new NamingException(BaseMessages.getString(PKG, "MailValidator.NoMatchName", hostName)); }// w ww .ja va 2 s.c om } // Huzzah! we have machines to try. Return them as an array list // NOTE: We SHOULD take the preference into account to be absolutely // correct. This is left as an exercise for anyone who cares. ArrayList<String> res = new ArrayList<String>(); NamingEnumeration<?> en = attr.getAll(); while (en.hasMore()) { String x = (String) en.next(); String[] f = x.split(" "); if (f[1].endsWith(".")) { f[1] = f[1].substring(0, (f[1].length() - 1)); } res.add(f[1]); } return res; }
From source file:com.ktds.ldap.populator.LdapTestUtils.java
/** * Clear the directory sub-tree starting with the node represented by the * supplied distinguished name./*w w w . j ava2 s. c o m*/ * * @param ctx The DirContext to use for cleaning the tree. * @param name the distinguished name of the root node. * @throws NamingException if anything goes wrong removing the sub-tree. */ public static void clearSubContexts(DirContext ctx, Name name) throws NamingException { NamingEnumeration enumeration = null; try { enumeration = ctx.listBindings(name); while (enumeration.hasMore()) { Binding element = (Binding) enumeration.next(); Name childName = LdapUtils.newLdapName(element.getName()); childName = LdapUtils.prepend(childName, name); try { ctx.unbind(childName); } catch (ContextNotEmptyException e) { clearSubContexts(ctx, childName); ctx.unbind(childName); } } } catch (NamingException e) { LOGGER.debug("Error cleaning sub-contexts", e); } finally { try { enumeration.close(); } catch (Exception e) { // Never mind this } } }
From source file:org.geoserver.security.ldap.LDAPTestUtils.java
/** * Clear the directory sub-tree starting with the node represented by the * supplied distinguished name.//from www . ja va 2s.c o m * * @param ctx The DirContext to use for cleaning the tree. * @param name the distinguished name of the root node. * @throws NamingException if anything goes wrong removing the sub-tree. */ public static void clearSubContexts(DirContext ctx, Name name) throws NamingException { NamingEnumeration enumeration = null; try { enumeration = ctx.listBindings(name); while (enumeration.hasMore()) { Binding element = (Binding) enumeration.next(); DistinguishedName childName = new DistinguishedName(element.getName()); childName.prepend((DistinguishedName) name); try { ctx.destroySubcontext(childName); } catch (ContextNotEmptyException e) { clearSubContexts(ctx, childName); ctx.destroySubcontext(childName); } } } catch (NamingException e) { e.printStackTrace(); } finally { try { enumeration.close(); } catch (Exception e) { // Never mind this } } }
From source file:security.AuthenticationManager.java
public static Map<String, String> getUserAttributes(DirContext ctx, String searchBase, String userName, String principalDomain, String... attributeNames) throws NamingException { if (StringUtils.isBlank(userName)) { throw new IllegalArgumentException("Username and password can not be blank."); }/*from ww w . jav a 2s . c om*/ if (attributeNames.length == 0) { return Collections.emptyMap(); } Attributes matchAttr = new BasicAttributes(true); BasicAttribute basicAttr = new BasicAttribute("userPrincipalName", userName + principalDomain); matchAttr.put(basicAttr); NamingEnumeration<? extends SearchResult> searchResult = ctx.search(searchBase, matchAttr, attributeNames); if (ctx != null) { ctx.close(); } Map<String, String> result = new HashMap<>(); if (searchResult.hasMore()) { NamingEnumeration<? extends Attribute> attributes = searchResult.next().getAttributes().getAll(); while (attributes.hasMore()) { Attribute attr = attributes.next(); String attrId = attr.getID(); String attrValue = (String) attr.get(); result.put(attrId, attrValue); } } return result; }
From source file:org.jkcsoft.java.util.JndiHelper.java
public static Map getUserInfo(BehavioralContext ctx, String userName) throws NamingException { Map infoMap = null;//from w w w .j a v a 2s . c o m Configuration cfg = ctx.getConfig(); // String searchRelativeDc = cfg.getString(Constants.KEY_AD_USER_NODE_DN); String theFilter = LDAP_USER_SAMACCOUNTNAME + "=" + userName; List theAttrsList = new Vector(Arrays.asList(ldapUserAttrs)); theAttrsList.addAll(Arrays.asList(ldapTopAttrs)); int countLimit = 1000; int timeLimitMillis = 30000; boolean returnObject = false; boolean derefObj = true; SearchControls scs = new SearchControls(SearchControls.SUBTREE_SCOPE, countLimit, timeLimitMillis, (String[]) theAttrsList.toArray(new String[0]), returnObject, derefObj); DirContext rootCtx = getTsessAccountContext(ctx); try { log.debug("Search params name[" + searchRelativeDc + "] " + "filter[" + theFilter + "] controls[" + scs + "]"); NamingEnumeration results = rootCtx.search(searchRelativeDc, theFilter, scs); if (results == null || !results.hasMore()) throw new NamingException("User LDAP entry not found"); SearchResult searchResult = ((SearchResult) results.next()); if (searchResult == null) throw new NamingException("User LDAP entry not found"); if (log.isTraceEnabled()) { logLdap(log, 0, 0, searchResult); } Attributes userLdapAttrs = searchResult.getAttributes(); infoMap = new HashMap(); for (Iterator attrIter = theAttrsList.iterator(); attrIter.hasNext();) { loadMap(infoMap, userLdapAttrs, (String) attrIter.next()); } } finally { safeClose(rootCtx); } return infoMap; }
From source file:com.ironiacorp.persistence.datasource.HibernateConfigurationUtil.java
/** * Retrieve the data sources registered at JNDI in the given context. * //w ww . j av a 2s .co m * @param namingContext * Start point context. * * @return A collection of the data sources found within the context. */ private static Collection<String> getAvailableDataSources(Context namingContext) { Collection<String> datasources = new ArrayList<String>(); Class<?> type = null; // Acquire global JNDI resources if available try { type = Class.forName("xyz"); } catch (ClassNotFoundException e) { } try { NamingEnumeration<Binding> items = namingContext.listBindings(""); while (items.hasMore()) { Binding item = (Binding) items.next(); if (item.getObject() instanceof Context) { datasources.addAll(getAvailableDataSources((Context) item.getObject())); } else { if (type.isInstance(item.getObject())) { datasources.add(item.getName()); } } } } catch (Throwable t) { } return datasources; }
From source file:org.zanata.ZanataInit.java
private static void list(Context ctx, String indent, StringBuffer buffer, boolean verbose) { ClassLoader loader = Thread.currentThread().getContextClassLoader(); try {/* w w w .j ava 2 s . c om*/ NamingEnumeration<NameClassPair> ne = ctx.list(""); while (ne.hasMore()) { NameClassPair pair = ne.next(); String name = pair.getName(); String className = pair.getClassName(); boolean recursive = false; boolean isLinkRef = false; boolean isProxy = false; Class<?> c = null; try { c = loader.loadClass(className); if (Context.class.isAssignableFrom(c)) { recursive = true; } if (LinkRef.class.isAssignableFrom(c)) { isLinkRef = true; } isProxy = Proxy.isProxyClass(c); } catch (ClassNotFoundException cnfe) { // If this is a $Proxy* class its a proxy if (className.startsWith("$Proxy")) { isProxy = true; // We have to get the class from the binding try { Object p = ctx.lookup(name); c = p.getClass(); } catch (NamingException e) { Throwable t = e.getRootCause(); if (t instanceof ClassNotFoundException) { // Get the class name from the exception msg String msg = t.getMessage(); if (msg != null) { // Reset the class name to the CNFE class className = msg; } } } } } buffer.append(indent).append(" +- ").append(name); // Display reference targets if (isLinkRef) { // Get the try { Object obj = ctx.lookupLink(name); LinkRef link = (LinkRef) obj; buffer.append("[link -> "); buffer.append(link.getLinkName()); buffer.append(']'); } catch (Throwable t) { buffer.append("invalid]"); } } // Display proxy interfaces if (isProxy) { buffer.append(" (proxy: ").append(pair.getClassName()); if (c != null) { Class<?>[] ifaces = c.getInterfaces(); buffer.append(" implements "); for (Class<?> iface : ifaces) { buffer.append(iface); buffer.append(','); } buffer.setCharAt(buffer.length() - 1, ')'); } else { buffer.append(" implements ").append(className).append(")"); } } else if (verbose) { buffer.append(" (class: ").append(pair.getClassName()).append(")"); } buffer.append('\n'); if (recursive) { try { Object value = ctx.lookup(name); if (value instanceof Context) { Context subctx = (Context) value; list(subctx, indent + " | ", buffer, verbose); } else { buffer.append(indent).append(" | NonContext: ").append(value); buffer.append('\n'); } } catch (Throwable t) { buffer.append("Failed to lookup: ").append(name).append(", errmsg=").append(t.getMessage()); buffer.append('\n'); } } } ne.close(); } catch (NamingException ne) { buffer.append("error while listing context ").append(ctx.toString()).append(": ") .append(ne.toString(true)); } }