Example usage for javax.naming NamingEnumeration hasMore

List of usage examples for javax.naming NamingEnumeration hasMore

Introduction

In this page you can find the example usage for javax.naming NamingEnumeration hasMore.

Prototype

public boolean hasMore() throws NamingException;

Source Link

Document

Determines whether there are any more elements in the enumeration.

Usage

From source file:de.micromata.genome.util.runtime.jndi.JndiDumper.java

public static void dumpJndiBinding(InitialContext initialContext, String parentKey, Binding binding,
        StringBuilder sb, String indent) throws NamingException {
    try {//from   ww w. j a  v a2  s .co  m
        Object obj = binding.getObject();
        ClassLoader bindClassLoader = obj.getClass().getClassLoader();
        sb.append(indent).append(parentKey + "/" + binding.getName()).append("(")
                .append(System.identityHashCode(binding)).append(", cl: ").append(bindClassLoader).append("): ")
                .append(binding.toString());
        if (obj instanceof Context) {

            sb.append("\n");
            Context ctx = (Context) obj;
            indent += "  ";
            NamingEnumeration<Binding> bindings = ctx.listBindings("");
            while (bindings.hasMore()) {
                Binding cbinding = bindings.next();
                dumpJndiBinding(initialContext, parentKey + "/" + binding.getName(), cbinding, sb, indent);
            }
        } else if (obj instanceof Reference) {
            Reference ref = (Reference) obj;
            //      binding.get
            //      ref.
            sb.append("\n");
        } else if (obj instanceof Session) {
            Session sess = (Session) obj;
            sb.append("\n");
        } else if (obj instanceof DataSource) {
            DataSource ds = (DataSource) obj;
            if (ds instanceof BasicDataSource) {
                BasicDataSource dbds = (BasicDataSource) ds;
                sb.append(" '" + dbds.getUrl() + "'");
            }
            sb.append("\n");
        } else if (obj != null) {
            Class<? extends Object> clazz = obj.getClass();

            sb.append(" unkown type: " + clazz);
            sb.append("\n");
        }
    } catch (NamingException ex) {
        sb.append("Error access binding: " + binding.getName() + ": " + ex.getMessage());
    }
}

From source file:io.lavagna.service.Ldap.java

private static List<SearchResult> search(DirContext dctx, String base, String filter, SearchControls sc)
        throws NamingException {
    List<SearchResult> res = new ArrayList<>();
    NamingEnumeration<SearchResult> search = dctx.search(base, filter, sc);
    while (search.hasMore()) {
        res.add(search.next());//from www. j av a2s. c  o m
    }
    return res;
}

From source file:org.apache.cxf.sts.claims.LdapUtils.java

public static List<String> getAttributeOfEntries(LdapTemplate ldapTemplate, String baseDN, String objectClass,
        String filterAttributeName, String filterAttributeValue, String searchAttribute) {

    List<String> ldapAttributes = null;

    AttributesMapper mapper = new AttributesMapper() {
        public Object mapFromAttributes(Attributes attrs) throws NamingException {
            NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll();
            while (attrEnum.hasMore()) {
                return (String) attrEnum.next().get();
            }//w  w w  .  j  a v  a  2 s.c  om
            return null;
        }
    };

    String[] searchAttributes = new String[] { searchAttribute };

    List<?> result = null;
    AndFilter filter = new AndFilter();
    filter.and(new EqualsFilter("objectclass", objectClass))
            .and(new EqualsFilter(filterAttributeName, filterAttributeValue));

    result = ldapTemplate.search((baseDN == null) ? "" : baseDN, filter.toString(),
            SearchControls.SUBTREE_SCOPE, searchAttributes, mapper);
    if (result != null && result.size() > 0) {
        ldapAttributes = CastUtils.cast((List<?>) result);
    }

    return ldapAttributes;
}

From source file:org.apache.cxf.sts.claims.LdapUtils.java

public static Map<String, Attribute> getAttributesOfEntry(LdapTemplate ldapTemplate, String baseDN,
        String objectClass, String filterAttributeName, String filterAttributeValue,
        String[] searchAttributes) {

    Map<String, Attribute> ldapAttributes = null;

    AttributesMapper mapper = new AttributesMapper() {
        public Object mapFromAttributes(Attributes attrs) throws NamingException {
            Map<String, Attribute> map = new HashMap<String, Attribute>();
            NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll();
            while (attrEnum.hasMore()) {
                Attribute att = attrEnum.next();
                map.put(att.getID(), att);
            }/*w  ww  . j a  v a 2  s.c om*/
            return map;
        }
    };

    List<?> result = null;
    AndFilter filter = new AndFilter();
    filter.and(new EqualsFilter("objectclass", objectClass))
            .and(new EqualsFilter(filterAttributeName, filterAttributeValue));

    result = ldapTemplate.search((baseDN == null) ? "" : baseDN, filter.toString(),
            SearchControls.SUBTREE_SCOPE, searchAttributes, mapper);
    if (result != null && result.size() > 0) {
        ldapAttributes = CastUtils.cast((Map<?, ?>) result.get(0));
    }

    return ldapAttributes;
}

From source file:org.pepstock.jem.annotations.SetFields.java

private static void checkIfIsRightObject(String name, boolean isDD) throws NamingException {
    InitialContext ic = ContextUtils.getContext();
    NamingEnumeration<NameClassPair> list = ic.list(name);
    boolean isDataDescription = false;
    while (list.hasMore()) {
        NameClassPair pair = list.next();
        // checks if is datastream
        // only datastreams are changed
        if (pair instanceof DataStreamNameClassPair) {
            isDataDescription = true;// w ww . ja  v  a2s.c  o m
        }
    }
    if (isDD && !isDataDescription) {
        throw new NamingException(name + " is not a data description");
    }
    if (!isDD && isDataDescription) {
        throw new NamingException(name + " is not a data source");
    }
}

From source file:com.jaeksoft.searchlib.util.ActiveDirectory.java

public static void collectMemberOf(Attributes attrs, Collection<ADGroup> groups) throws NamingException {
    Attribute tga = attrs.get("memberOf");
    if (tga == null)
        return;/*w  w  w .ja  v a2s.  c  o m*/
    NamingEnumeration<?> membersOf = tga.getAll();
    while (membersOf.hasMore()) {
        Object memberObject = membersOf.next();
        groups.add(new ADGroup(memberObject.toString()));
    }
    membersOf.close();
}

From source file:org.codehaus.groovy.grails.plugins.springsecurity.ldap.LdapUtils.java

/**
 * Retrieves the LDAP attribute values for the specified name.
 * @param attributeName the LDAP attribute name
 * @return  the values or <code>null</code> if not available
 *///from w  w w  .  ja v a  2  s  .com
public static List<String> getAttributes(String attributeName) {
    Object currentUser = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    if (!(currentUser instanceof GrailsLdapUser)) {
        LOG.warn("Must be using an implementation of " + GrailsLdapUser.class.getName()
                + " to retrieve Attributes");
        return null;
    }

    List<String> values = new ArrayList<String>();
    NamingEnumeration<?> attributes = null;
    try {
        attributes = ((GrailsLdapUser) currentUser).getAttributes().get(attributeName).getAll();
        if (attributes != null) {
            while (attributes.hasMore()) {
                values.add(attributes.next().toString());
            }
        }
    } catch (NamingException e) {
        LOG.warn("Unable to retrieve attribute " + attributeName + " from the currently logged in user", e);
    } catch (RuntimeException e) {
        LOG.warn("Unable to retrieve attribute " + attributeName + " from the currently logged in user", e);
    }

    return values;
}

From source file:de.griffel.confluence.plugins.plantuml.DatasourceHelper.java

/**
 * Determines data sources configured in Tomcat.
 *
 * @return List of data source names//from   ww w  .j av  a  2s  . c o m
 */
public static List<String> listAvailableDataSources() {
    final List<String> results = new LinkedList<String>();

    // Workaround for classloader problems, see https://answers.atlassian.com/questions/6374/how-do-i-access-jndi-from-a-version-2-osgi-plugin
    final ClassLoader origCL = Thread.currentThread().getContextClassLoader();

    try {
        Thread.currentThread().setContextClassLoader(ComponentManager.class.getClassLoader());
        final InitialContext jndiContext = new InitialContext();
        final NamingEnumeration<Binding> bindings = jndiContext.listBindings(JDBC_CONTEXT);
        while (bindings != null && bindings.hasMore()) {
            results.add(bindings.next().getName());
        }
    } catch (NamingException e) {
        log.debug("NamingException listBindings", e);
        results.add(e.toString());
    } finally {
        Thread.currentThread().setContextClassLoader(origCL);
    }
    return results;
}

From source file:com.funambol.server.db.DataSourceContextHelperTest.java

/**
 * Usefull method for troubleshooting/*w ww.  j a va  2s .  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:org.springframework.ldap.test.unboundid.LdapTestUtils.java

/**
 * Clear the directory sub-tree starting with the node represented by the
 * supplied distinguished name./*ww w  .j av  a  2 s .co 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
        }
    }
}