View Javadoc

1   /*
2    *   @(#) $Id: ContextPartitionConfiguration.java 264732 2005-08-30 08:04:51Z akarasulu $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.ldap.server.configuration;
20  
21  
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.Set;
25  
26  import javax.naming.Name;
27  import javax.naming.NamingException;
28  import javax.naming.directory.Attributes;
29  import javax.naming.directory.BasicAttributes;
30  
31  import org.apache.ldap.common.name.LdapName;
32  import org.apache.ldap.common.schema.Normalizer;
33  import org.apache.ldap.server.partition.ContextPartition;
34  import org.apache.ldap.server.partition.impl.btree.jdbm.JdbmContextPartition;
35  import org.apache.ldap.server.schema.MatchingRuleRegistry;
36  
37  
38  /***
39   * A configuration for {@link ContextPartition}.
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   * @version $Rev: 264732 $, $Date: 2005-08-30 04:04:51 -0400 (Tue, 30 Aug 2005) $
43   */
44  public class ContextPartitionConfiguration
45  {
46      /*** The name of reserved system partition */
47      public static final String SYSTEM_PARTITION_NAME = "system";
48  
49      private String name;
50      private String suffix;
51      private Set indexedAttributes = new HashSet(); // Set<String>
52      private Attributes contextEntry = new BasicAttributes( true );
53      private ContextPartition contextPartition = new JdbmContextPartition();
54      
55      /***
56       * Creates a new instance.
57       */
58      protected ContextPartitionConfiguration()
59      {
60      }
61      
62      /***
63       * Returns user-defined name of the {@link ContextPartition} that
64       * this configuration configures.
65       */
66      public String getName()
67      {
68          return name;
69      }
70      
71      /***
72       * Sets user-defined name of the {@link ContextPartition} that
73       * this configuration configures.
74       */
75      protected void setName( String name )
76      {
77          // TODO name can be a directory name.
78          name = name.trim();
79          this.name = name;
80      }
81  
82      /***
83       * Returns the set of attribute type strings to create an index on.
84       */
85      public Set getIndexedAttributes()
86      {
87          return ConfigurationUtil.getClonedSet( indexedAttributes );
88      }
89      
90      /***
91       * Sets the set of attribute type strings to create an index on.
92       */
93      protected void setIndexedAttributes( Set indexedAttributes )
94      {
95          Set newIndexedAttributes = ConfigurationUtil.getTypeSafeSet(
96                  indexedAttributes, String.class );
97  
98          Iterator i = newIndexedAttributes.iterator();
99          while( i.hasNext() )
100         {
101             String attribute = ( String ) i.next();
102             // TODO Attribute name must be normalized and validated
103             newIndexedAttributes.add( attribute );
104         }
105         this.indexedAttributes = newIndexedAttributes;
106     }
107     
108     /***
109      * Returns the {@link ContextPartition} that this configuration configures.
110      */
111     public ContextPartition getContextPartition()
112     {
113         return contextPartition;
114     }
115     
116     /***
117      * Sets the {@link ContextPartition} that this configuration configures.
118      */
119     protected void setContextPartition( ContextPartition partition )
120     {
121         if( partition == null )
122         {
123             throw new NullPointerException( "partition" );
124         }
125         this.contextPartition = partition;
126     }
127     
128     /***
129      * Returns root entry that will be added to the {@link ContextPartition}
130      * after it is initialized.
131      */
132     public Attributes getContextEntry()
133     {
134         return ( Attributes ) contextEntry.clone();
135     }
136     
137     /***
138      * Sets root entry that will be added to the {@link ContextPartition}
139      * after it is initialized.
140      */
141     protected void setContextEntry( Attributes rootEntry )
142     {
143         this.contextEntry = ( Attributes ) rootEntry.clone();
144     }
145     
146     /***
147      * Returns the suffix of the {@link ContextPartition}.
148      */
149     public String getSuffix()
150     {
151         return suffix;
152     }
153     
154     /***
155      * Returns the normalized suffix of the {@link ContextPartition}.
156      */
157     public Name getNormalizedSuffix( MatchingRuleRegistry matchingRuleRegistry ) throws NamingException
158     {
159         return getNormalizedSuffix( matchingRuleRegistry.lookup( "distinguishedNameMatch" ).getNormalizer() );
160     }
161     
162     /***
163      * Returns the normalized suffix of the {@link ContextPartition}.
164      */
165     public Name getNormalizedSuffix( Normalizer normalizer ) throws NamingException
166     {
167         return new LdapName( normalizer.normalize( suffix ).toString() );
168     }
169     
170     /***
171      * Sets the suffix of the {@link ContextPartition}.
172      */
173     protected void setSuffix( String suffix )
174     {
175         suffix = suffix.trim();
176         try
177         {
178             new LdapName( suffix );
179         }
180         catch( NamingException e )
181         {
182             throw new ConfigurationException( "Failed to normalize the suffix: " + suffix );
183         }
184         this.suffix = suffix;
185     }
186     
187     
188     /***
189      * Validates this configuration.
190      * 
191      * @throws ConfigurationException if this configuration is not valid
192      */
193     public void validate()
194     {
195         if( getName() == null || getName().length() == 0 )
196         {
197             throw new ConfigurationException( "Name is not specified." );
198         }
199 
200         if( getSuffix() == null )
201         {
202             throw new ConfigurationException( "Suffix is not specified." );
203         }
204     }
205 }