View Javadoc

1   /*
2    *   @(#) $Id: StartupConfiguration.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  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Set;
27  
28  import javax.naming.directory.Attributes;
29  
30  import org.apache.ldap.server.authn.AnonymousAuthenticator;
31  import org.apache.ldap.server.authn.AuthenticationService;
32  import org.apache.ldap.server.authn.SimpleAuthenticator;
33  import org.apache.ldap.server.authz.AuthorizationService;
34  import org.apache.ldap.server.exception.ExceptionService;
35  import org.apache.ldap.server.jndi.ContextFactoryService;
36  import org.apache.ldap.server.normalization.NormalizationService;
37  import org.apache.ldap.server.operational.OperationalAttributeService;
38  import org.apache.ldap.server.schema.SchemaService;
39  import org.apache.ldap.server.schema.bootstrap.ApacheSchema;
40  import org.apache.ldap.server.schema.bootstrap.BootstrapSchema;
41  import org.apache.ldap.server.schema.bootstrap.CoreSchema;
42  import org.apache.ldap.server.schema.bootstrap.CosineSchema;
43  import org.apache.ldap.server.schema.bootstrap.InetorgpersonSchema;
44  import org.apache.ldap.server.schema.bootstrap.JavaSchema;
45  import org.apache.ldap.server.schema.bootstrap.SystemSchema;
46  
47  /***
48   * A {@link Configuration} that starts up ApacheDS.
49   * 
50   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
51   * @version $Rev: 264732 $, $Date: 2005-08-30 04:04:51 -0400 (Tue, 30 Aug 2005) $
52   */
53  public class StartupConfiguration extends Configuration
54  {
55      private static final long serialVersionUID = 4826762196566871677L;
56  
57      private File workingDirectory = new File( "server-work" );
58      private boolean allowAnonymousAccess = true; // allow by default
59      private Set authenticatorConfigurations; // Set<AuthenticatorConfiguration>
60      private List interceptorConfigurations; // Set<InterceptorConfiguration>
61      
62      private Set bootstrapSchemas; // Set<BootstrapSchema>
63      private Set contextPartitionConfigurations = new HashSet(); // Set<ContextPartitionConfiguration>
64      private List testEntries = new ArrayList(); // List<Attributes>
65      
66      /***
67       * Creates a new instance with default settings.
68       */
69      public StartupConfiguration()
70      {
71          setDefaultAuthenticatorConfigurations();
72          setDefaultBootstrapSchemas();
73          setDefaultInterceptorConfigurations();
74      }
75  
76      /***
77       * Creates a new instance with default settings that operates on the
78       * {@link ContextFactoryService} with the specified ID.
79       */
80      public StartupConfiguration( String instanceId )
81      {
82          setDefaultAuthenticatorConfigurations();
83          setDefaultBootstrapSchemas();
84          setDefaultInterceptorConfigurations();
85          setInstanceId( instanceId );
86      }
87  
88      private void setDefaultAuthenticatorConfigurations()
89      {
90          Set set; 
91          
92          // Set default authenticator configurations
93          set = new HashSet();
94          
95          MutableAuthenticatorConfiguration authCfg;
96  
97          // Anonymous
98          authCfg = new MutableAuthenticatorConfiguration();
99          authCfg.setName( "Anonymous" );
100         authCfg.setAuthenticator( new AnonymousAuthenticator() );
101         set.add( authCfg );
102 
103         // Simple
104         authCfg = new MutableAuthenticatorConfiguration();
105         authCfg.setName( "Simple" );
106         authCfg.setAuthenticator( new SimpleAuthenticator() );
107         set.add( authCfg );
108         
109         setAuthenticatorConfigurations( set );
110     }
111 
112     private void setDefaultBootstrapSchemas()
113     {
114         Set set;
115         // Set default bootstrap schemas
116         set = new HashSet();
117         
118         set.add( new CoreSchema() );
119         set.add( new CosineSchema() );        
120         set.add( new ApacheSchema() );        
121         set.add( new InetorgpersonSchema() );        
122         set.add( new JavaSchema() );        
123         set.add( new SystemSchema() );
124         
125         setBootstrapSchemas( set );
126     }
127 
128     private void setDefaultInterceptorConfigurations()
129     {
130         // Set default interceptor chains
131         InterceptorConfiguration interceptorCfg;
132         List list = new ArrayList();
133         
134         interceptorCfg = new MutableInterceptorConfiguration();
135         interceptorCfg.setName( "normalizationService" );
136         interceptorCfg.setInterceptor( new NormalizationService() );
137         list.add( interceptorCfg );
138         
139         interceptorCfg = new MutableInterceptorConfiguration();
140         interceptorCfg.setName( "authenticationService" );
141         interceptorCfg.setInterceptor( new AuthenticationService() );
142         list.add( interceptorCfg );
143         
144         interceptorCfg = new MutableInterceptorConfiguration();
145         interceptorCfg.setName( "authorizationService" );
146         interceptorCfg.setInterceptor( new AuthorizationService() );
147         list.add( interceptorCfg );
148         
149         interceptorCfg = new MutableInterceptorConfiguration();
150         interceptorCfg.setName( "exceptionService" );
151         interceptorCfg.setInterceptor( new ExceptionService() );
152         list.add( interceptorCfg );
153         
154         interceptorCfg = new MutableInterceptorConfiguration();
155         interceptorCfg.setName( "schemaService" );
156         interceptorCfg.setInterceptor( new SchemaService() );
157         list.add( interceptorCfg );
158         
159         interceptorCfg = new MutableInterceptorConfiguration();
160         interceptorCfg.setName( "operationalAttributeService" );
161         interceptorCfg.setInterceptor( new OperationalAttributeService() );
162         list.add( interceptorCfg );
163         
164         setInterceptorConfigurations( list );
165     }
166 
167     /***
168      * Returns {@link AuthenticatorConfiguration}s to use for authenticating clients.
169      */
170     public Set getAuthenticatorConfigurations()
171     {
172         return ConfigurationUtil.getClonedSet( authenticatorConfigurations );
173     }
174 
175     /***
176      * Sets {@link AuthenticatorConfiguration}s to use for authenticating clients.
177      */
178     protected void setAuthenticatorConfigurations( Set authenticatorConfigurations )
179     {
180         Set newSet = ConfigurationUtil.getTypeSafeSet(
181                 authenticatorConfigurations, AuthenticatorConfiguration.class );
182         
183         Set names = new HashSet();
184         Iterator i = newSet.iterator();
185         while( i.hasNext() )
186         {
187             AuthenticatorConfiguration cfg = ( AuthenticatorConfiguration ) i.next();
188             cfg.validate();
189             
190             String name = cfg.getName();
191             if( names.contains( name ) )
192             {
193                 throw new ConfigurationException( "Duplicate authenticator name: " + name );
194             }
195             names.add( name );
196         }
197         
198         this.authenticatorConfigurations = newSet;
199     }
200 
201     /***
202      * Returns {@link BootstrapSchema}s to load while bootstrapping.
203      */
204     public Set getBootstrapSchemas()
205     {
206         return ConfigurationUtil.getClonedSet( bootstrapSchemas );
207     }
208 
209     /***
210      * Sets {@link BootstrapSchema}s to load while bootstrapping.
211      */
212     protected void setBootstrapSchemas( Set bootstrapSchemas )
213     {
214         this.bootstrapSchemas = ConfigurationUtil.getTypeSafeSet(
215                 bootstrapSchemas, BootstrapSchema.class );
216     }
217 
218     /***
219      * Returns {@link ContextPartitionConfiguration}s to configure context partitions.
220      */
221     public Set getContextPartitionConfigurations()
222     {
223         return ConfigurationUtil.getClonedSet( contextPartitionConfigurations );
224     }
225 
226     /***
227      * Sets {@link ContextPartitionConfiguration}s to configure context partitions.
228      */
229     protected void setContextPartitionConfigurations( Set contextParitionConfigurations )
230     {
231         Set newSet = ConfigurationUtil.getTypeSafeSet(
232                 contextParitionConfigurations, ContextPartitionConfiguration.class );
233         
234         Set names = new HashSet();
235         Iterator i = newSet.iterator();
236         while( i.hasNext() )
237         {
238             ContextPartitionConfiguration cfg = ( ContextPartitionConfiguration ) i.next();
239             cfg.validate();
240 
241             String name = cfg.getName();
242             if( names.contains( name ) )
243             {
244                 throw new ConfigurationException( "Duplicate partition name: " + name );
245             }
246             names.add( name );
247         }
248         
249         this.contextPartitionConfigurations = newSet;
250     }
251 
252     /***
253      * Returns <tt>true</tt> if anonymous access is allowed.
254      */
255     public boolean isAllowAnonymousAccess()
256     {
257         return allowAnonymousAccess;
258     }
259 
260     /***
261      * Sets whether to allow anonymous access or not
262      */
263     protected void setAllowAnonymousAccess( boolean enableAnonymousAccess )
264     {
265         this.allowAnonymousAccess = enableAnonymousAccess;
266     }
267 
268     /***
269      * Returns interceptor chain.
270      */
271     public List getInterceptorConfigurations()
272     {
273         return ConfigurationUtil.getClonedList( interceptorConfigurations );
274     }
275 
276     /***
277      * Sets interceptor chain.
278      */
279     protected void setInterceptorConfigurations( List interceptorConfigurations )
280     {
281         List newList = ConfigurationUtil.getTypeSafeList(
282                 interceptorConfigurations, InterceptorConfiguration.class );
283         
284         Set names = new HashSet();
285         Iterator i = newList.iterator();
286         while( i.hasNext() )
287         {
288             InterceptorConfiguration cfg = ( InterceptorConfiguration ) i.next();
289             cfg.validate();
290 
291             String name = cfg.getName();
292             if( names.contains( name ) )
293             {
294                 throw new ConfigurationException( "Duplicate interceptor name: " + name );
295             }
296             names.add( name );
297         }
298 
299         this.interceptorConfigurations = interceptorConfigurations;
300     }
301 
302     /***
303      * Returns test directory entries({@link Attributes}) to be loaded while
304      * bootstrapping.
305      */
306     public List getTestEntries()
307     {
308         return ConfigurationUtil.getClonedAttributesList( testEntries );
309     }
310 
311     /***
312      * Sets test directory entries({@link Attributes}) to be loaded while
313      * bootstrapping.
314      */
315     protected void setTestEntries( List testEntries )
316     {
317          testEntries = ConfigurationUtil.getClonedAttributesList(
318                 ConfigurationUtil.getTypeSafeList( testEntries, Attributes.class ) );
319          
320          Iterator i = testEntries.iterator();
321          while( i.hasNext() )
322          {
323              Attributes entry = ( Attributes ) i.next();
324              if( entry.get( "dn" ) == null )
325              {
326                  throw new ConfigurationException( "Test entries must have DN attributes" );
327              }
328          }
329 
330          this.testEntries = testEntries;
331     }
332 
333     /***
334      * Returns working directory (counterpart of <tt>var/lib</tt>).
335      */
336     public File getWorkingDirectory()
337     {
338         return workingDirectory;
339     }
340 
341     /***
342      * Sets working directory (counterpart of <tt>var/lib</tt>).
343      */
344     protected void setWorkingDirectory( File workingDirectory )
345     {
346         workingDirectory.mkdirs();
347         if( !workingDirectory.exists() )
348         {
349             throw new ConfigurationException( "Working directory '" + workingDirectory + "' doesn't exist." );
350         }
351         if( !workingDirectory.isDirectory() )
352         {
353             throw new ConfigurationException( "Working directory '" + workingDirectory + "' is not a directory." );
354         }
355 
356         this.workingDirectory = workingDirectory;
357     }
358     
359     public void validate()
360     {
361         setWorkingDirectory( workingDirectory );
362     }
363 }