directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1027240 - in /directory/apacheds/branches/apacheds-config/server-config/src: main/java/org/apache/directory/server/config/ test/java/org/apache/directory/server/config/
Date Mon, 25 Oct 2010 19:15:18 GMT
Author: elecharny
Date: Mon Oct 25 19:15:18 2010
New Revision: 1027240

URL: http://svn.apache.org/viewvc?rev=1027240&view=rev
Log:
o Added a ConfigurationException
o Started to add Logs and error handling

Added:
    directory/apacheds/branches/apacheds-config/server-config/src/main/java/org/apache/directory/server/config/ConfigurationException.java
Modified:
    directory/apacheds/branches/apacheds-config/server-config/src/main/java/org/apache/directory/server/config/ConfigPartitionReader.java
    directory/apacheds/branches/apacheds-config/server-config/src/test/java/org/apache/directory/server/config/ConfigPartitionReaderTest.java

Modified: directory/apacheds/branches/apacheds-config/server-config/src/main/java/org/apache/directory/server/config/ConfigPartitionReader.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-config/server-config/src/main/java/org/apache/directory/server/config/ConfigPartitionReader.java?rev=1027240&r1=1027239&r2=1027240&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-config/server-config/src/main/java/org/apache/directory/server/config/ConfigPartitionReader.java
(original)
+++ directory/apacheds/branches/apacheds-config/server-config/src/main/java/org/apache/directory/server/config/ConfigPartitionReader.java
Mon Oct 25 19:15:18 2010
@@ -89,6 +89,7 @@ import org.apache.directory.shared.ldap.
 import org.apache.directory.shared.ldap.entry.EntryAttribute;
 import org.apache.directory.shared.ldap.entry.StringValue;
 import org.apache.directory.shared.ldap.entry.Value;
+import org.apache.directory.shared.ldap.exception.LdapException;
 import org.apache.directory.shared.ldap.filter.EqualityNode;
 import org.apache.directory.shared.ldap.filter.PresenceNode;
 import org.apache.directory.shared.ldap.filter.SearchScope;
@@ -592,6 +593,10 @@ public class ConfigPartitionReader
     */
     
     
+    /**
+     * Fnd the upper objectclass in a hierarchy. All the inherited ObjectClasses
+     * will be removed.
+     */
     private ObjectClass findObjectClass( EntryAttribute objectClass ) throws Exception
     {
         Set<ObjectClass> candidates = new HashSet<ObjectClass>();
@@ -639,6 +644,7 @@ public class ConfigPartitionReader
         // The remaining OC in the candidates set is the one we are looking for
         ObjectClass result = candidates.toArray( new ObjectClass[]{} )[0];
         
+        LOG.debug( "The top level object class is {}", result.getName() );
         return result;
     }
     
@@ -969,46 +975,83 @@ public class ConfigPartitionReader
     }
     
     
-
     /**
-     * Read some configuration element from the DIT using its name 
-     *
-     * @throws Exception
+     * Helper method to print a list of AT's names.
      */
-    public List<AdsBaseBean> read( DN base, String name, SearchScope scope, boolean
mandatory ) throws Exception
+    private String dumpATs( Set<AttributeType> attributeTypes )
     {
-        System.out.println( "Reading from '" + base + "' entry " + name + ", mandatory :
 " + mandatory);
+        if ( ( attributeTypes == null ) || ( attributeTypes.size() == 0 ) )
+        {
+            return "";
+        }
         
-        // Search for the element starting at some point in the DIT
-        AttributeType adsdAt = schemaManager.getAttributeType( SchemaConstants.OBJECT_CLASS_AT
);
-        EqualityNode<?> filter = new EqualityNode( adsdAt, new StringValue( name )
);
-        SearchControls controls = new SearchControls();
-        controls.setSearchScope( scope.ordinal() );
-
-        IndexCursor<Long, Entry, Long> cursor = se.cursor( base,
-            AliasDerefMode.NEVER_DEREF_ALIASES, filter, controls );
-
-        if ( !cursor.next() )
+        StringBuilder sb = new StringBuilder();
+        boolean isFirst = true;
+        sb.append( '{' );
+        
+        for ( AttributeType attributeType : attributeTypes )
         {
-            if ( mandatory )
+            if ( isFirst )
             {
-                cursor.close();
-                
-                // the DirectoryService is mandatory so throwing exception
-                throw new Exception( "No directoryService instance was configured under the
DN "
-                    + configPartition.getSuffix() );
+                isFirst = false;
             }
             else
             {
-                return null;
+                sb.append( ", " );
             }
+            
+            sb.append( attributeType.getName() );
         }
+
+        sb.append( '}' );
+
+        
+        return sb.toString();
+    }
+
+    /**
+     * Read some configuration element from the DIT using its name 
+     */
+    private List<AdsBaseBean> read( DN baseDn, String name, SearchScope scope, boolean
mandatory ) throws ConfigurationException
+    {
+        LOG.debug( "Reading from '{}', entry {}", baseDn, name );
         
+        // Search for the element starting at some point in the DIT
+        // Prepare the search request
+        AttributeType adsdAt = schemaManager.getAttributeType( SchemaConstants.OBJECT_CLASS_AT
);
+        EqualityNode<?> filter = new EqualityNode( adsdAt, new StringValue( name )
);
+        SearchControls controls = new SearchControls();
+        controls.setSearchScope( scope.ordinal() );
+        IndexCursor<Long, Entry, Long> cursor = null;
+        
+        // Create a container for all the read beans
         List<AdsBaseBean> beans = new ArrayList<AdsBaseBean>();
 
-        // Loop on all the found elements
         try
         {
+            // Do the search
+            cursor = se.cursor( baseDn, AliasDerefMode.NEVER_DEREF_ALIASES, filter, controls
);
+    
+            // First, check if we have some entries to process.
+            if ( !cursor.next() )
+            {
+                if ( mandatory )
+                {
+                    cursor.close();
+                    
+                    // the requested element is mandatory so let's throw an exception
+                    String message = "No directoryService instance was configured under the
DN "
+                        + configPartition.getSuffix();
+                    LOG.error( message );
+                    throw new ConfigurationException( message );
+                }
+                else
+                {
+                    return null;
+                }
+            }
+
+            // Loop on all the found elements
             do
             {
                 ForwardIndexEntry<Long, Entry, Long> forwardEntry = ( ForwardIndexEntry<Long,
Entry, Long> ) cursor
@@ -1028,23 +1071,40 @@ public class ConfigPartitionReader
                 // Now, read the AttributeTypes and store the values into the bean fields
                 // The MAY
                 Set<AttributeType> mays = getAllMays( objectClass );
+                LOG.debug( "Fetching the following MAY attributes : {}", dumpATs( mays )
);
                 readFields( bean, entry, mays, OPTIONNAL );
                 
                 // The MUST
                 Set<AttributeType> musts = getAllMusts( objectClass );
+                LOG.debug( "Fetching the following MAY attributes : {}", dumpATs( musts )
);
                 readFields( bean, entry, musts, MANDATORY );
                 
+                // Done, we can add the bean into the list
                 beans.add( bean );
             }
             while ( cursor.next() );
         }
         catch ( Exception e )
         {
-            e.printStackTrace();
+            String message = "Cannot open a cursor to read the configuration on " + baseDn;
+            LOG.error( message );
+            throw new ConfigurationException( message );
         }
         finally
         {
-            cursor.close();
+            if ( cursor != null )
+            {
+                try
+                {
+                    cursor.close();
+                }
+                catch ( Exception e )
+                {
+                    // So ??? If the cursor can't be close, there is nothing we can do
+                    // but rethrow the exception
+                    throw new ConfigurationException( e.getMessage(), e.getCause() );
+                }
+            }
         }
         
         return beans;
@@ -1164,30 +1224,83 @@ public class ConfigPartitionReader
     }
     
     
-    public ConfigBean readConfig( DN base, String objectClass ) throws Exception
+    /**
+     * Read the configuration from the DIT, returning a bean containing all of it.
+     * 
+     * @param base The base DN in the DIT where the configuration is stored
+     * @return The Config bean, containing the whole configuration
+     * @throws ConfigurationException If we had some issue reading the configuration
+     */
+    public ConfigBean readConfig( String baseDn ) throws LdapException
     {
-        try
+        // The starting point is the DirectoryService element
+        return readConfig( new DN( baseDn ), ConfigSchemaConstants.ADS_DIRECTORY_SERVICE_OC.getValue()
);
+    }
+    
+    
+    /**
+     * Read the configuration from the DIT, returning a bean containing all of it.
+     * 
+     * @param base The base DN in the DIT where the configuration is stored
+     * @return The Config bean, containing the whole configuration
+     * @throws ConfigurationException If we had some issue reading the configuration
+     */
+    public ConfigBean readConfig( DN baseDn ) throws ConfigurationException
+    {
+        // The starting point is the DirectoryService element
+        return readConfig( baseDn, ConfigSchemaConstants.ADS_DIRECTORY_SERVICE_OC.getValue()
);
+    }
+    
+    
+    /**
+     * Read the configuration from the DIT, returning a bean containing all of it.
+     * 
+     * @param baseDn The base DN in the DIT where the configuration is stored
+     * @param objectClass The element to read from the DIT
+     * @return The bean containing the configuration for the required element
+     * @throws ConfigurationException
+     */
+    public ConfigBean readConfig( String baseDn, String objectClass ) throws LdapException
+    {
+        return readConfig( new DN( baseDn ), objectClass );
+    }
+    
+    
+    /**
+     * Read the configuration from the DIT, returning a bean containing all of it.
+     * 
+     * @param baseDn The base DN in the DIT where the configuration is stored
+     * @param objectClass The element to read from the DIT
+     * @return The bean containing the configuration for the required element
+     * @throws ConfigurationException
+     */
+    public ConfigBean readConfig( DN baseDn, String objectClass ) throws ConfigurationException
+    {
+        LOG.debug( "Reading configuration for the {} element, from {} ", objectClass, baseDn
);
+        ConfigBean configBean = new ConfigBean();
+        
+        if ( baseDn == null )
         {
-            ConfigBean configBean = new ConfigBean();
-            
-            if ( base == null )
-            {
-                base = configPartition.getSuffix();
-            }
-            
-            List<AdsBaseBean> beans = read( base, objectClass, SearchScope.ONELEVEL,
MANDATORY );
-            
-            System.out.println( beans.get( 0 ) );
-            
-            configBean.setDirectoryServiceBeans( beans );
-            
-            return configBean;
+            baseDn = configPartition.getSuffix();
         }
-        catch ( Exception e )
+        
+        List<AdsBaseBean> beans = read( baseDn, objectClass, SearchScope.ONELEVEL,
MANDATORY );
+        
+        if ( LOG.isDebugEnabled() )
         {
-            e.printStackTrace();
-            throw e;
+            if ( ( beans == null ) || ( beans.size() == 0 ) )
+            {
+                LOG.debug( "No {} element to read", objectClass );
+            }
+            else
+            {
+                LOG.debug( beans.get( 0 ).toString() );
+            }
         }
+        
+        configBean.setDirectoryServiceBeans( beans );
+        
+        return configBean;
     }
     
     

Added: directory/apacheds/branches/apacheds-config/server-config/src/main/java/org/apache/directory/server/config/ConfigurationException.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-config/server-config/src/main/java/org/apache/directory/server/config/ConfigurationException.java?rev=1027240&view=auto
==============================================================================
--- directory/apacheds/branches/apacheds-config/server-config/src/main/java/org/apache/directory/server/config/ConfigurationException.java
(added)
+++ directory/apacheds/branches/apacheds-config/server-config/src/main/java/org/apache/directory/server/config/ConfigurationException.java
Mon Oct 25 19:15:18 2010
@@ -0,0 +1,57 @@
+/*
+ *  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.server.config;
+
+import org.apache.directory.shared.ldap.exception.LdapException;
+
+/**
+ * An exception used when we cannot read the configuration, or when an error
+ * occured while reading it from the DIT.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ConfigurationException extends LdapException
+{
+    /** The serial version UUID */
+    private static final long serialVersionUID = 1L;
+
+    
+    /**
+     * Creates a new instance of ConfigurationException.
+     *
+     * @param message The exception message
+     */
+    public ConfigurationException( String message )
+    {
+        super( message );
+    }
+
+    
+    /**
+     * Creates a new instance of ConfigurationException.
+     *
+     * @param message The exception message
+     * @param cause the original cause
+     */
+    public ConfigurationException( String message, Throwable cause )
+    {
+        super( message, cause );
+    }
+}

Modified: directory/apacheds/branches/apacheds-config/server-config/src/test/java/org/apache/directory/server/config/ConfigPartitionReaderTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-config/server-config/src/test/java/org/apache/directory/server/config/ConfigPartitionReaderTest.java?rev=1027240&r1=1027239&r2=1027240&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-config/server-config/src/test/java/org/apache/directory/server/config/ConfigPartitionReaderTest.java
(original)
+++ directory/apacheds/branches/apacheds-config/server-config/src/test/java/org/apache/directory/server/config/ConfigPartitionReaderTest.java
Mon Oct 25 19:15:18 2010
@@ -130,7 +130,7 @@ public class ConfigPartitionReaderTest
         
         ConfigPartitionReader cpReader = new ConfigPartitionReader( configPartition, workDir
);
         
-        ConfigBean configBean = cpReader.readConfig( new DN( "ou=config" ), ConfigSchemaConstants.ADS_DIRECTORY_SERVICE_OC.getValue()
);
+        ConfigBean configBean = cpReader.readConfig( "ou=config" );
 
         assertNotNull( configBean );
         DirectoryServiceBean directoryServiceBean = (DirectoryServiceBean)configBean.getDirectoryServiceBeans().get(
0 );



Mime
View raw message