Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
==================== DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved. Copyright 2011-2013 Tirasa. All rights reserved. The contents of this file are subject to the terms of the Common Development and Distribution License("CDDL") (the "License"). You may not use this file except in compliance with the License. You can obtain a copy of the License at https://oss.oracle.com/licenses/CDDL See the License for the specific language governing permissions and limitations under the License. When distributing the Covered Code, include this CDDL Header Notice in each file and include the License file at https://oss.oracle.com/licenses/CDDL. If applicable, add the following below this CDDL Header, with the fields enclosed by brackets [] replaced by your own identifying information: "Portions Copyrighted [year] [name of copyright owner]" ====================
 
 package org.connid.bundles.ad.schema;
 
 import java.util.List;
 import java.util.Set;
 
 class ADSchemaBuilder {
 
     private static final Log LOG = Log.getLog(ADSchemaBuilder.class);
 
     private ADConnection connection;
 
     private Schema schema;
 
     private static String[] ATTRIBUTES_TO_GET = {
         "maycontain",
         "systemmaycontain",
         "mustcontain",
         "systemmustcontain"};
 
     public ADSchemaBuilder(final ADConnection connection) {
         this. = connection;
     }
 
     public Schema getSchema() {
         if ( == null) {
             buildSchema();
         }
         return ;
     }
 
     private void buildSchema() {
         final SchemaBuilder schemaBld = new SchemaBuilder(ADConnector.class);
         build(.schemaBld);
         build(.schemaBld);
          = schemaBld.build();
     }
 
     private void build(final String onamefinal SchemaBuilder schemaBld) {
         final ADConfiguration conf = (ADConfiguration.getConfiguration();
 
         final StringBuilder filter = new StringBuilder();
 
         // -----------------------------------
         // Create search control
         // -----------------------------------
         final SearchControls searchCtls = LdapInternalSearch.createDefaultSearchControls();
 
         searchCtls.setSearchScope(.);
 
         searchCtls.setReturningAttributes();
         // -----------------------------------
 
         // -----------------------------------
        // Specify filter
        // -----------------------------------
        for (String oclass : oname.equalsIgnoreCase(.)
                ? conf.getAccountObjectClasses() : conf.getGroupObjectClasses()) {
            filter.append("(lDAPDisplayName=").append(oclass).append(")");
        }
        filter.insert(0, "(&(|").append(")(objectClass=classSchema))");
        // -----------------------------------
        final LdapContext ctx = .getInitialContext();
        final Set<StringschemaNames = new HashSet<String>();
        // Issue http://code.google.com/p/connid/issues/detail?id=24
        schemaNames.add(conf.getUidAttribute());
        final String schemaConfigurationPath = "CN=Schema,CN=Configuration";
        for (String suffix : conf.getBaseContextsToSynchronize()) {
            try {
                final NamingEnumeration<SearchResultoclasses = ctx.search(
                        schemaConfigurationPath + "," + suffix,
                        filter.toString(),
                        searchCtls);
                while (oclasses.hasMoreElements()) {
                    final SearchResult oclass = oclasses.next();
                    schemaNames.addAll(getObjectSchemaNames(oclass));
                }
            } catch (NamingException e) {
                .error(e"Error retrieving schema names from {0}"suffix);
            }
        }
        final ObjectClassInfoBuilder objClassBld = new ObjectClassInfoBuilder();
        // ObjectClass.ACCOUNT/ObjectClass.GROUP
        objClassBld.setType(oname);
        objClassBld.setContainer(false);
        objClassBld.addAllAttributeInfo(createAttrInfos(schemaNames));
        final ObjectClassInfo oci = objClassBld.build();
        schemaBld.defineObjectClass(oci);
    }
    private Set<StringgetObjectSchemaNames(final SearchResult oclass)
            throws NamingException {
        final Set<StringschemaNames = new HashSet<String>();
        for (String attrName : ) {
            final Attribute attr = oclass.getAttributes().get(attrName);
            if (attr != null) {
                final NamingEnumeration en = attr.getAll();
                while (en.hasMoreElements()) {
                    final String elem = (Stringen.nextElement();
                    if (StringUtil.isNotBlank(elem)) {
                        schemaNames.add(elem.trim());
                    }
                }
            }
        }
        return schemaNames;
    }
    private List<AttributeInfocreateAttrInfos(final Set<StringschemaNames) {
        final List<AttributeInfoinfos = new ArrayList<AttributeInfo>();
        for (String schemaName : schemaNames) {
            infos.add(handleAttribute(schemaName));
        }
        return infos;
    }
    private AttributeInfo handleAttribute(final String displayName) {
        final String IS_SINGLE_VALUE = "isSingleValued";
        final String SYSTEM_ONLY = "systemOnly";
        final String LDAP_DISPLAY_NAME = "lDAPDisplayName";
        final Set<Flagsflags = EnumSet.noneOf(Flags.class);
        boolean binary = .isBinarySyntax(displayName);
        boolean objectClass = displayName == null || "objectClass".equalsIgnoreCase(displayName);
        final LdapContext ctx = .getInitialContext();
        final String[] baseContexts = .getConfiguration().getBaseContextsToSynchronize();
        // ------------------------------
        // Search control
        // ------------------------------
        final SearchControls searchCtls = LdapInternalSearch.createDefaultSearchControls();
        searchCtls.setSearchScope(.);
        searchCtls.setReturningAttributes(new String[]{IS_SINGLE_VALUESYSTEM_ONLY});
        // ------------------------------
        int i = 0;
        Attributes attributes = null;
        while (attributes == null && i < baseContexts.length) {
            final StringBuilder dn = new StringBuilder();
            dn.append("cn=schema, cn=configuration,").append(baseContexts[i]);
            try {
                final NamingEnumeration<SearchResultresult = ctx.search(
                        dn.toString(),
                        LDAP_DISPLAY_NAME + "=" + displayName,
                        searchCtls);
                if (result != null && result.hasMoreElements()) {
                    attributes = result.next().getAttributes();
                }
            } catch (NamingException e) {
                .error(e"Error retrieving attributes for {0}"dn);
            }
            i++;
        }
        if (attributes != null) {
            final Attribute isSingle = attributes.get(IS_SINGLE_VALUE);
            try {
                if (isSingle == null || isSingle.get() == null || "false".equalsIgnoreCase(isSingle.get().toString())) {
                    flags.add(.);
                }
            } catch (NamingException e) {
                .error(e"Failure retrieving attribute " + IS_SINGLE_VALUE);
            }
            final Attribute systemOnly = attributes.get(SYSTEM_ONLY);
            try {
                if ((systemOnly != null
                        && systemOnly.get() != null
                        && "true".equalsIgnoreCase(systemOnly.get().toString()))
                        || objectClass) {
                    flags.add(.);
                    flags.add(.);
                }
            } catch (NamingException e) {
                .error(e"Failure retrieving attribute " + SYSTEM_ONLY);
            }
        }
        return AttributeInfoBuilder.build(
                displayName,
                binary ? byte[].class : String.class,
                flags);
    }
New to GrepCode? Check out our FAQ X