You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ka...@apache.org on 2010/08/24 19:39:37 UTC
svn commit: r988631 - in /directory:
apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/
clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/
Author: kayyagari
Date: Tue Aug 24 17:39:37 2010
New Revision: 988631
URL: http://svn.apache.org/viewvc?rev=988631&view=rev
Log:
o a schema loader based on the network connection
o associated test case
Added:
directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/NetworkSchemaLoaderTest.java
directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/NetworkSchemaLoader.java
Added: directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/NetworkSchemaLoaderTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/NetworkSchemaLoaderTest.java?rev=988631&view=auto
==============================================================================
--- directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/NetworkSchemaLoaderTest.java (added)
+++ directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/NetworkSchemaLoaderTest.java Tue Aug 24 17:39:37 2010
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.directory.shared.client.api;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import org.apache.directory.ldap.client.api.LdapConnection;
+import org.apache.directory.ldap.client.api.LdapNetworkConnection;
+import org.apache.directory.ldap.client.api.NetworkSchemaLoader;
+import org.apache.directory.server.annotations.CreateLdapServer;
+import org.apache.directory.server.annotations.CreateTransport;
+import org.apache.directory.server.core.integ.AbstractLdapTestUnit;
+import org.apache.directory.server.core.integ.FrameworkRunner;
+import org.apache.directory.shared.ldap.schema.SchemaManager;
+import org.apache.directory.shared.ldap.schema.manager.impl.DefaultSchemaManager;
+import org.apache.directory.shared.ldap.util.LdapExceptionUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * A test class for NetworkSchemaLoader.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(FrameworkRunner.class)
+@CreateLdapServer(transports =
+ { @CreateTransport(protocol = "LDAP"), @CreateTransport(protocol = "LDAPS") })
+public class NetworkSchemaLoaderTest extends AbstractLdapTestUnit
+{
+ @Test
+ public void testNetworkSchemaLoader() throws Exception
+ {
+ LdapConnection connection = new LdapNetworkConnection( "localhost", ldapServer.getPort() );
+ connection.bind( "uid=admin,ou=system", "secret" );
+
+ NetworkSchemaLoader loader = new NetworkSchemaLoader( connection );
+
+ SchemaManager sm = new DefaultSchemaManager( loader );
+
+ boolean loaded = sm.loadAllEnabled();
+
+ if ( !loaded )
+ {
+ fail( "Schema load failed : " + LdapExceptionUtils.printErrors( sm.getErrors() ) );
+ }
+
+ assertTrue( sm.getRegistries().getAttributeTypeRegistry().contains( "cn" ) );
+ }
+
+}
Added: directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/NetworkSchemaLoader.java
URL: http://svn.apache.org/viewvc/directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/NetworkSchemaLoader.java?rev=988631&view=auto
==============================================================================
--- directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/NetworkSchemaLoader.java (added)
+++ directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/NetworkSchemaLoader.java Tue Aug 24 17:39:37 2010
@@ -0,0 +1,332 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.directory.ldap.client.api;
+
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.directory.shared.ldap.cursor.Cursor;
+import org.apache.directory.shared.ldap.entry.Entry;
+import org.apache.directory.shared.ldap.exception.LdapException;
+import org.apache.directory.shared.ldap.filter.SearchScope;
+import org.apache.directory.shared.ldap.message.Response;
+import org.apache.directory.shared.ldap.message.SearchResultEntry;
+import org.apache.directory.shared.ldap.name.DN;
+import org.apache.directory.shared.ldap.schema.registries.AbstractSchemaLoader;
+import org.apache.directory.shared.ldap.schema.registries.Schema;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * A schema loader which uses LdapConnection to load schema.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class NetworkSchemaLoader extends AbstractSchemaLoader
+{
+ /** the connection to the ldap server */
+ private LdapConnection connection;
+
+ private static final String SCHEMA_BASE = "ou=schema";
+
+ private static final String FILTER = "(objectClass=*)";
+
+ private static final Logger LOG = LoggerFactory.getLogger( NetworkSchemaLoader.class );
+
+
+ /**
+ *
+ * Creates a new instance of NetworkSchemaLoader.
+ *
+ * @param connection
+ * @throws Exception if the connection is not authenticated or if there are any problems
+ * while loading the schema entries
+ */
+ public NetworkSchemaLoader( LdapConnection connection ) throws Exception
+ {
+ if ( !connection.isAuthenticated() )
+ {
+ throw new IllegalArgumentException( "connection is not authenticated" );
+ }
+
+ this.connection = connection;
+ initializeSchemas();
+ }
+
+
+ private void initializeSchemas() throws Exception
+ {
+ List<Entry> schemaEntries = searchSchemaObjects( SCHEMA_BASE, "(objectClass=metaSchema)" );
+
+ LOG.debug( "initializing schemas {}", schemaEntries );
+ for ( Entry entry : schemaEntries )
+ {
+ Schema schema = getSchema( entry );
+ schemaMap.put( schema.getSchemaName(), schema );
+ }
+ }
+
+
+ /**
+ * searches with ONE LEVEL scope under the given DN and retrieves all the schema objects
+ *
+ * @param baseDn the DN of the schema entry under which the schema objects are present
+ * e.x ou=attributeTypes,cn=apache,ou=schema
+ * @param filter optional search filter, if null the default fileter {@link #FILTER} is used
+ * @return a list of entries of the schema objects
+ * @throws LdapException
+ */
+ private List<Entry> searchSchemaObjects( String baseDn, String filter ) throws LdapException
+ {
+ try
+ {
+ LOG.debug( "searching under the dn {} for schema objects", baseDn );
+
+ List<Entry> entries = new ArrayList<Entry>();
+
+ if ( filter == null )
+ {
+ filter = FILTER;
+ }
+
+ Cursor<Response> cursor = connection.search( new DN( baseDn ), filter, SearchScope.ONELEVEL, "*", "+" );
+
+ while ( cursor.next() )
+ {
+ Entry entry = ( ( SearchResultEntry ) cursor.get() ).getEntry();
+ entries.add( entry );
+ }
+
+ cursor.close();
+
+ return entries;
+ }
+ catch ( LdapException e )
+ {
+ throw e;
+ }
+ catch ( Exception e )
+ {
+ throw new LdapException( e );
+ }
+ }
+
+
+ /**
+ * @see #searchSchemaObjects(String, String)
+ */
+ private List<Entry> searchSchemaObjects( String baseDn ) throws LdapException
+ {
+ return searchSchemaObjects( baseDn, FILTER );
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public List<Entry> loadAttributeTypes( Schema... schemas ) throws LdapException, IOException
+ {
+ List<Entry> atEntries = new ArrayList<Entry>();
+
+ for ( Schema s : schemas )
+ {
+ List<Entry> entries = searchSchemaObjects( "ou=attributeTypes,cn=" + s.getSchemaName() + "," + SCHEMA_BASE );
+ atEntries.addAll( entries );
+ }
+
+ return atEntries;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public List<Entry> loadComparators( Schema... schemas ) throws LdapException, IOException
+ {
+ List<Entry> comparatorsEntries = new ArrayList<Entry>();
+
+ for ( Schema s : schemas )
+ {
+ List<Entry> entries = searchSchemaObjects( "ou=comparators,cn=" + s.getSchemaName() + "," + SCHEMA_BASE );
+ comparatorsEntries.addAll( entries );
+ }
+
+ return comparatorsEntries;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public List<Entry> loadDitContentRules( Schema... schemas ) throws LdapException, IOException
+ {
+ List<Entry> ditContentRulesEntries = new ArrayList<Entry>();
+
+ for ( Schema s : schemas )
+ {
+ List<Entry> entries = searchSchemaObjects( "ou=ditContentRules,cn=" + s.getSchemaName() + "," + SCHEMA_BASE );
+ ditContentRulesEntries.addAll( entries );
+ }
+
+ return ditContentRulesEntries;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public List<Entry> loadDitStructureRules( Schema... schemas ) throws LdapException, IOException
+ {
+ List<Entry> ditStructureRulesEntries = new ArrayList<Entry>();
+
+ for ( Schema s : schemas )
+ {
+ List<Entry> entries = searchSchemaObjects( "ou=ditStructureRules,cn=" + s.getSchemaName() + ","
+ + SCHEMA_BASE );
+ ditStructureRulesEntries.addAll( entries );
+ }
+
+ return ditStructureRulesEntries;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public List<Entry> loadMatchingRuleUses( Schema... schemas ) throws LdapException, IOException
+ {
+ List<Entry> matchingRuleUsesEntries = new ArrayList<Entry>();
+
+ for ( Schema s : schemas )
+ {
+ List<Entry> entries = searchSchemaObjects( "ou=matchingRuleUse,cn=" + s.getSchemaName() + "," + SCHEMA_BASE );
+ matchingRuleUsesEntries.addAll( entries );
+ }
+
+ return matchingRuleUsesEntries;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public List<Entry> loadMatchingRules( Schema... schemas ) throws LdapException, IOException
+ {
+ List<Entry> matchingRulesEntries = new ArrayList<Entry>();
+
+ for ( Schema s : schemas )
+ {
+ List<Entry> entries = searchSchemaObjects( "ou=matchingRules,cn=" + s.getSchemaName() + "," + SCHEMA_BASE );
+ matchingRulesEntries.addAll( entries );
+ }
+
+ return matchingRulesEntries;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public List<Entry> loadNameForms( Schema... schemas ) throws LdapException, IOException
+ {
+ List<Entry> nameFormsEntries = new ArrayList<Entry>();
+
+ for ( Schema s : schemas )
+ {
+ List<Entry> entries = searchSchemaObjects( "ou=nameForms,cn=" + s.getSchemaName() + "," + SCHEMA_BASE );
+ nameFormsEntries.addAll( entries );
+ }
+
+ return nameFormsEntries;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public List<Entry> loadNormalizers( Schema... schemas ) throws LdapException, IOException
+ {
+ List<Entry> normalizersEntries = new ArrayList<Entry>();
+
+ for ( Schema s : schemas )
+ {
+ List<Entry> entries = searchSchemaObjects( "ou=normalizers,cn=" + s.getSchemaName() + "," + SCHEMA_BASE );
+ normalizersEntries.addAll( entries );
+ }
+
+ return normalizersEntries;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public List<Entry> loadObjectClasses( Schema... schemas ) throws LdapException, IOException
+ {
+ List<Entry> objectClassesEntries = new ArrayList<Entry>();
+
+ for ( Schema s : schemas )
+ {
+ List<Entry> entries = searchSchemaObjects( "ou=objectClasses,cn=" + s.getSchemaName() + "," + SCHEMA_BASE );
+ objectClassesEntries.addAll( entries );
+ }
+
+ return objectClassesEntries;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public List<Entry> loadSyntaxCheckers( Schema... schemas ) throws LdapException, IOException
+ {
+ List<Entry> syntaxCheckersEntries = new ArrayList<Entry>();
+
+ for ( Schema s : schemas )
+ {
+ List<Entry> entries = searchSchemaObjects( "ou=syntaxCheckers,cn=" + s.getSchemaName() + "," + SCHEMA_BASE );
+ syntaxCheckersEntries.addAll( entries );
+ }
+
+ return syntaxCheckersEntries;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public List<Entry> loadSyntaxes( Schema... schemas ) throws LdapException, IOException
+ {
+ List<Entry> syntaxesEntries = new ArrayList<Entry>();
+
+ for ( Schema s : schemas )
+ {
+ List<Entry> entries = searchSchemaObjects( "ou=syntaxes,cn=" + s.getSchemaName() + "," + SCHEMA_BASE );
+ syntaxesEntries.addAll( entries );
+ }
+
+ return syntaxesEntries;
+ }
+
+}