View Javadoc

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