Search in sources :

Example 1 with CaseInsensitiveTreeSet

use of com.sun.identity.common.CaseInsensitiveTreeSet in project OpenAM by OpenRock.

the class SMSFlatFileObjectBase method searchOrgs.

private Set<String> searchOrgs(SSOToken token, String objName, String filter, int numOfEntries, boolean sortResults, boolean ascendingOrder, boolean recursive, String serviceName, String attrName, Set values) throws SMSException, SSOException {
    // Check the args
    if ((objName == null) || (objName.length() == 0) || (filter == null) || (filter.length() == 0) || (numOfEntries < 0)) {
        throw new IllegalArgumentException("SMSFlatFileObject.searchOrganizationNames(): " + "One or more arguments is null or empty: " + "objName [" + objName == null ? "null" : objName + "] filter ]" + filter == null ? "null" : filter + "]");
    }
    // For org search the filter prefix would be "o="
    // However for root realm it would be "ou=" when search is performed
    String fPrefix = "o=";
    String sidFilter = null;
    // construct the filename filter
    if ((serviceName != null) && (attrName != null) && (values != null) && !values.isEmpty()) {
        sidFilter = serviceName + "-" + attrName + "=" + values.iterator().next();
        if (objName.equalsIgnoreCase(mRootDN)) {
            fPrefix = "ou=";
        }
    }
    Set<String> subentries = null;
    if (sortResults) {
        subentries = new CaseInsensitiveTreeSet(ascendingOrder);
    } else {
        subentries = new CaseInsensitiveHashSet();
    }
    try {
        Set entries = getSubEntries(objName, fPrefix + filter, sidFilter, false, numOfEntries, sortResults, ascendingOrder);
        // to make it a full DN
        for (Iterator i = entries.iterator(); i.hasNext(); ) {
            String suborg = (String) i.next();
            subentries.add(fPrefix + suborg + "," + objName);
        }
        if (recursive) {
            // Get the list if sub-orgs and search
            Set<String> subOrgs = new HashSet();
            if (!filter.equals("*") || (sidFilter != null)) {
                Set ssubOrgs = getSubEntries(objName, fPrefix + "*", null, false, 0, sortResults, ascendingOrder);
                for (Iterator i = ssubOrgs.iterator(); i.hasNext(); ) {
                    String suborg = (String) i.next();
                    subOrgs.add(fPrefix + suborg + "," + objName);
                }
            } else {
                subOrgs.addAll(subentries);
            }
            for (String subOrgName : subOrgs) {
                int reqEntries = (numOfEntries == 0) ? numOfEntries : numOfEntries - subentries.size();
                if (numOfEntries < 0) {
                    break;
                }
                Set<String> subsubentries = searchOrgs(token, subOrgName, filter, reqEntries, sortResults, ascendingOrder, recursive, serviceName, attrName, values);
                subentries.addAll(subsubentries);
            }
        }
    } catch (ServiceNotFoundException e) {
        // return empty set if object does not exist. 
        subentries = new CaseInsensitiveHashSet<>();
    }
    if (mDebug.messageEnabled()) {
        mDebug.message("SMSFlatFileObject:searchOrgs " + "search " + filter + " for " + objName + " returned " + subentries.size() + " items");
    }
    return (subentries);
}
Also used : CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) HashSet(java.util.HashSet) CaseInsensitiveTreeSet(com.sun.identity.common.CaseInsensitiveTreeSet) Set(java.util.Set) ServiceNotFoundException(com.sun.identity.sm.ServiceNotFoundException) Iterator(java.util.Iterator) CaseInsensitiveTreeSet(com.sun.identity.common.CaseInsensitiveTreeSet) CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) HashSet(java.util.HashSet)

Example 2 with CaseInsensitiveTreeSet

use of com.sun.identity.common.CaseInsensitiveTreeSet in project OpenAM by OpenRock.

the class SMSFlatFileObject method getSubEntries.

/**
     * Real routine to get sub entries, used by subEntries() and 
     * schemaSubEntries(). 
     *
     * @throws ServiceNotFoundException if the configuration object is 
     * not found.
     * @throws SchemaException if a sub directory name is not in the 
     * expected "ou=..." format.
     */
protected Set getSubEntries(String objName, String filter, String sidFilter, boolean isSubConfig, int numOfEntries, boolean sortResults, boolean ascendingOrder) throws SMSException {
    String objKey = objName.toLowerCase();
    Set subentries = null;
    // wait indefinitely for the read lock.
    mRWLock.readRequest();
    try {
        // Check if object exists.
        String filepath = mNameMap.getProperty(objKey);
        if (filepath == null) {
            String errmsg = "SMSFlatFileObjectBase.getSubEntries: " + objName + " : not found in objects map.";
            mDebug.warning(errmsg);
            throw new ServiceNotFoundException(errmsg);
        }
        File filehandle = new File(filepath);
        File parentDir = filehandle.getParentFile();
        if (!parentDir.isDirectory()) {
            String errmsg = "SMSFlatFileObject.getSubEntries: " + objName + " : " + filehandle.getPath() + " does not exist or is not a directory.";
            mDebug.error(errmsg);
            throw new ServiceNotFoundException(errmsg);
        }
        // Create file filter for filter and sid filter.
        FilenameFilter subentFileFilter = new FilenameFilter(filter);
        FilenameFilter sidFileFilter = null;
        if (sidFilter != null && sidFilter.length() > 0) {
            // are encoded. 
            if (isSubConfig) {
                sidFileFilter = new FilenameFilter(SMSEntry.ATTR_SERVICE_ID + "=" + sidFilter.toLowerCase());
            } else {
                sidFileFilter = new FilenameFilter(SMSEntry.ATTR_XML_KEYVAL + "=" + sidFilter.toLowerCase());
            }
        }
        // Create set for return, use sorted set if sortResults is true.
        if (sortResults) {
            subentries = new CaseInsensitiveTreeSet(ascendingOrder);
        } else {
            subentries = new CaseInsensitiveHashSet();
        }
        // Set all entries that match filter, and that match 
        // sunserviceid/sunxmlkeyvalye if sidFilter was not null.
        File[] subentriesFound = parentDir.listFiles(subentFileFilter);
        int numEntriesAdded = 0;
        boolean done = false;
        for (int i = 0; (i < subentriesFound.length) && !done; i++) {
            File[] sunserviceidFiles = null;
            if (sidFileFilter == null || ((sunserviceidFiles = subentriesFound[i].listFiles(sidFileFilter)) != null && sunserviceidFiles.length > 0)) {
                String filename = subentriesFound[i].getName();
                int equalSign = filename.indexOf('=');
                if (equalSign < 0 || equalSign == (filename.length() - 1)) {
                    String errmsg = "SMSFlatFileObject.getSubEntries: " + "Invalid sub entry name found: " + filename;
                    mDebug.error(errmsg);
                    throw new SchemaException(errmsg);
                }
                String subentryname = FileNameDecoder.decode(filename.substring(equalSign + 1));
                subentries.add(subentryname);
                numEntriesAdded++;
                // stop if number of entries requested has been reached.
                // if sort results, need to get the whole list first.
                done = !sortResults && (numOfEntries > 0) && (numEntriesAdded == numOfEntries);
            }
        }
        if (sortResults && (numOfEntries > 0)) {
            // remove extra entries from the bottom.
            while ((numEntriesAdded - numOfEntries) > 0) {
                Object l = ((CaseInsensitiveTreeSet) subentries).last();
                subentries.remove(l);
                numEntriesAdded--;
            }
        }
    } finally {
        mRWLock.readDone();
    }
    return subentries;
}
Also used : CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) SchemaException(com.sun.identity.sm.SchemaException) CaseInsensitiveTreeSet(com.sun.identity.common.CaseInsensitiveTreeSet) Set(java.util.Set) CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) ServiceNotFoundException(com.sun.identity.sm.ServiceNotFoundException) CaseInsensitiveTreeSet(com.sun.identity.common.CaseInsensitiveTreeSet) File(java.io.File)

Example 3 with CaseInsensitiveTreeSet

use of com.sun.identity.common.CaseInsensitiveTreeSet in project OpenAM by OpenRock.

the class SMSEnhancedFlatFileObject method getSubEntries.

/**
     * Real routine to get sub entries, used by subEntries() and 
     * schemaSubEntries(). 
     *
     * @throws ServiceNotFoundException if the configuration object is 
     * not found.
     * @throws SchemaException if a sub directory name is not in the 
     * expected "ou=..." format.
     */
protected Set getSubEntries(String objName, String filter, String sidFilter, boolean isSubConfig, int numOfEntries, boolean sortResults, boolean ascendingOrder) throws SMSException {
    String objKey = objName.toLowerCase();
    Set subentries = null;
    // wait indefinitely for the read lock.
    mRWLock.readRequest();
    try {
        SMSFlatFileTreeNode node = root.getChild(objKey);
        if (node == null) {
            String errmsg = "SMSEnhancedFlatFileObject.getSubEntries: " + objName + " : not found in objects map.";
            mDebug.warning(errmsg);
            throw new ServiceNotFoundException(errmsg);
        }
        // Create file filter for filter and sid filter.
        NodeNameFilter subEntNodeFilter = new NodeNameFilter(filter);
        NodeNameFilter sidNameFilter = getSidNodeFilter(sidFilter, isSubConfig);
        // Create set for return, use sorted set if sortResults is true.
        if (sortResults) {
            subentries = new CaseInsensitiveTreeSet(ascendingOrder);
        } else {
            subentries = new CaseInsensitiveHashSet();
        }
        // Set all entries that match filter, and that match 
        // sunserviceid/sunxmlkeyvalye if sidFilter was not null.
        Set subEntries = node.searchChildren(subEntNodeFilter, false);
        int numEntriesAdded = 0;
        int sz = subEntries.size();
        boolean done = false;
        for (Iterator i = subEntries.iterator(); i.hasNext() && !done; ) {
            SMSFlatFileTreeNode n = (SMSFlatFileTreeNode) i.next();
            String nodeDN = n.getName();
            boolean accept = (sidNameFilter == null);
            if (!accept) {
                Set sids = n.searchChildren(sidNameFilter, false);
                accept = (sids != null) && !sids.isEmpty();
            }
            if (accept) {
                int idx = nodeDN.indexOf('=');
                if ((idx == -1) || (idx == (nodeDN.length() - 1))) {
                    String errmsg = "SMSEnhancedFlatFileObject.getSubEntries: " + "Invalid sub entry name found: " + nodeDN;
                    mDebug.error(errmsg);
                    throw new SchemaException(errmsg);
                }
                String subentryname = FileNameDecoder.decode(nodeDN.substring(idx + 1));
                subentries.add(subentryname);
                numEntriesAdded++;
                // stop if number of entries requested has been reached.
                // if sort results, need to get the whole list first.
                done = !sortResults && (numOfEntries > 0) && (numEntriesAdded == numOfEntries);
            }
        }
        if (sortResults && (numOfEntries > 0)) {
            while ((numEntriesAdded - numOfEntries) > 0) {
                Object l = ((CaseInsensitiveTreeSet) subentries).last();
                subentries.remove(l);
                numEntriesAdded--;
            }
        }
    } finally {
        mRWLock.readDone();
    }
    return subentries;
}
Also used : CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) SchemaException(com.sun.identity.sm.SchemaException) CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) HashSet(java.util.HashSet) CaseInsensitiveTreeSet(com.sun.identity.common.CaseInsensitiveTreeSet) Set(java.util.Set) ServiceNotFoundException(com.sun.identity.sm.ServiceNotFoundException) Iterator(java.util.Iterator) CaseInsensitiveTreeSet(com.sun.identity.common.CaseInsensitiveTreeSet)

Aggregations

CaseInsensitiveHashSet (com.sun.identity.common.CaseInsensitiveHashSet)3 CaseInsensitiveTreeSet (com.sun.identity.common.CaseInsensitiveTreeSet)3 ServiceNotFoundException (com.sun.identity.sm.ServiceNotFoundException)3 Set (java.util.Set)3 SchemaException (com.sun.identity.sm.SchemaException)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 File (java.io.File)1