KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > apacheds > configuration > model > ServerConfigurationWriter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. 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,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20 package org.apache.directory.ldapstudio.apacheds.configuration.model;
21
22
23 import java.io.BufferedWriter JavaDoc;
24 import java.io.FileWriter JavaDoc;
25
26 import javax.naming.NamingEnumeration JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import javax.naming.directory.Attribute JavaDoc;
29 import javax.naming.directory.Attributes JavaDoc;
30 import javax.xml.transform.Transformer JavaDoc;
31 import javax.xml.transform.TransformerException JavaDoc;
32 import javax.xml.transform.TransformerFactory JavaDoc;
33 import javax.xml.transform.stream.StreamSource JavaDoc;
34
35 import org.apache.directory.ldapstudio.apacheds.configuration.Activator;
36 import org.dom4j.Document;
37 import org.dom4j.DocumentHelper;
38 import org.dom4j.Element;
39 import org.dom4j.io.DocumentResult;
40 import org.dom4j.io.DocumentSource;
41
42
43 /**
44  * This class represents the Server Configuration Writer. It can be used to save a 'server.xml' file from.
45  *
46  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
47  * @version $Rev$, $Date$
48  */

49 public class ServerConfigurationWriter
50 {
51     /**
52      * Writes the Server Configuration to disk.
53      *
54      * @param serverConfiguration
55      * the Server Configuration
56      * @throws ServerConfigurationWriterException
57      * if an error occurrs when writing the Server Configuration file
58      */

59     public void write( ServerConfiguration serverConfiguration ) throws ServerConfigurationWriterException
60     {
61         try
62         {
63             BufferedWriter JavaDoc outFile = new BufferedWriter JavaDoc( new FileWriter JavaDoc( serverConfiguration.getPath() ) );
64
65             Document document = DocumentHelper.createDocument();
66             Element root = document.addElement( "beans" );
67
68             // Environment Bean
69
createEnvironmentBean( root, serverConfiguration );
70
71             // Configuration Bean
72
createConfigurationBean( root, serverConfiguration );
73
74             // System Partition Configuration Bean
75
createSystemPartitionConfigurationBean( root, serverConfiguration );
76
77             // User Partitions Beans
78
createUserPartitionsConfigurationsBean( root, serverConfiguration );
79
80             // CustomEditors Bean
81
createCustomEditorsBean( root );
82
83             Document stylizedDocuement = styleDocument( document );
84             stylizedDocuement.addDocType( "beans", "-//SPRING//DTD BEAN//EN",
85                 "http://www.springframework.org/dtd/spring-beans.dtd" );
86             outFile.write( stylizedDocuement.asXML() );
87             outFile.close();
88         }
89         catch ( Exception JavaDoc e )
90         {
91             ServerConfigurationWriterException exception = new ServerConfigurationWriterException( e.getMessage(), e
92                 .getCause() );
93             exception.setStackTrace( e.getStackTrace() );
94             throw exception;
95         }
96     }
97
98
99     /**
100      * Creates the Environment Bean
101      *
102      * @param root
103      * the root Element
104      * @param serverConfiguration
105      * the Server Configuration
106      */

107     private void createEnvironmentBean( Element root, ServerConfiguration serverConfiguration )
108     {
109         Element environmentBean = root.addElement( "bean" );
110         environmentBean.addAttribute( "id", "environment" );
111         environmentBean.addAttribute( "class", "org.springframework.beans.factory.config.PropertiesFactoryBean" );
112
113         Element propertyElement = environmentBean.addElement( "property" );
114         propertyElement.addAttribute( "name", "properties" );
115         Element propsElement = propertyElement.addElement( "props" );
116
117         // Key 'java.naming.security.authentication'
118
Element propElement = propsElement.addElement( "prop" );
119         propElement.addAttribute( "key", "java.naming.security.authentication" );
120         propElement.setText( "simple" );
121
122         // Key 'java.naming.security.principal'
123
propElement = propsElement.addElement( "prop" );
124         propElement.addAttribute( "key", "java.naming.security.principal" );
125         propElement.setText( serverConfiguration.getPrincipal() );
126
127         // Key 'java.naming.security.credentials'
128
propElement = propsElement.addElement( "prop" );
129         propElement.addAttribute( "key", "java.naming.security.credentials" );
130         propElement.setText( serverConfiguration.getPassword() );
131
132         // Key 'java.naming.ldap.attributes.binary'
133
if ( !serverConfiguration.getBinaryAttributes().isEmpty() )
134         {
135             propElement = propsElement.addElement( "prop" );
136             propElement.addAttribute( "key", "java.naming.ldap.attributes.binary" );
137             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
138             for ( String JavaDoc attribute : serverConfiguration.getBinaryAttributes() )
139             {
140                 sb.append( attribute );
141                 sb.append( " " );
142             }
143             String JavaDoc attributes = sb.toString();
144             propElement.setText( attributes.substring( 0, attributes.length() - 1 ) );
145         }
146
147     }
148
149
150     /**
151      * Creates the Configuration Bean.
152      *
153      * @param root
154      * the root Element
155      * @param serverConfiguration
156      * the Server Configuration
157      */

158     private void createConfigurationBean( Element root, ServerConfiguration serverConfiguration )
159     {
160         Element configurationBean = root.addElement( "bean" );
161         configurationBean.addAttribute( "id", "configuration" );
162         configurationBean.addAttribute( "class",
163             "org.apache.directory.server.configuration.MutableServerStartupConfiguration" );
164
165         // Working directory
166
Element propertyElement = configurationBean.addElement( "property" );
167         propertyElement.addAttribute( "name", "workingDirectory" );
168         propertyElement.addAttribute( "value", "example.com" ); // Ask Alex about this value.
169

170         // SynchPeriodMillis
171
propertyElement = configurationBean.addElement( "property" );
172         propertyElement.addAttribute( "name", "synchPeriodMillis" );
173         propertyElement.addAttribute( "value", "" + serverConfiguration.getSynchronizationPeriod() );
174
175         // MaxTimeLimit
176
propertyElement = configurationBean.addElement( "property" );
177         propertyElement.addAttribute( "name", "maxTimeLimit" );
178         propertyElement.addAttribute( "value", "" + serverConfiguration.getMaxTimeLimit() );
179
180         // MaxSizeLimit
181
propertyElement = configurationBean.addElement( "property" );
182         propertyElement.addAttribute( "name", "maxSizeLimit" );
183         propertyElement.addAttribute( "value", "" + serverConfiguration.getMaxSizeLimit() );
184
185         // MaxThreads
186
propertyElement = configurationBean.addElement( "property" );
187         propertyElement.addAttribute( "name", "maxThreads" );
188         propertyElement.addAttribute( "value", "" + serverConfiguration.getMaxThreads() );
189
190         // AllowAnonymousAccess
191
propertyElement = configurationBean.addElement( "property" );
192         propertyElement.addAttribute( "name", "allowAnonymousAccess" );
193         propertyElement.addAttribute( "value", "" + serverConfiguration.isAllowAnonymousAccess() );
194
195         // AccessControlEnabled
196
propertyElement = configurationBean.addElement( "property" );
197         propertyElement.addAttribute( "name", "accessControlEnabled" );
198         propertyElement.addAttribute( "value", "" + serverConfiguration.isEnableAccessControl() );
199
200         // Enable NTP
201
propertyElement = configurationBean.addElement( "property" );
202         propertyElement.addAttribute( "name", "enableNtp" );
203         propertyElement.addAttribute( "value", "" + serverConfiguration.isEnableNTP() );
204
205         // EnableKerberos
206
propertyElement = configurationBean.addElement( "property" );
207         propertyElement.addAttribute( "name", "enableKerberos" );
208         propertyElement.addAttribute( "value", "" + serverConfiguration.isEnableKerberos() );
209
210         // EnableChangePassword
211
propertyElement = configurationBean.addElement( "property" );
212         propertyElement.addAttribute( "name", "enableChangePassword" );
213         propertyElement.addAttribute( "value", "" + serverConfiguration.isEnableChangePassword() );
214
215         // DenormalizeOpAttrsEnabled
216
propertyElement = configurationBean.addElement( "property" );
217         propertyElement.addAttribute( "name", "denormalizeOpAttrsEnabled" );
218         propertyElement.addAttribute( "value", "" + serverConfiguration.isDenormalizeOpAttr() );
219
220         // LdapPort
221
propertyElement = configurationBean.addElement( "property" );
222         propertyElement.addAttribute( "name", "ldapPort" );
223         propertyElement.addAttribute( "value", "" + serverConfiguration.getPort() );
224
225         // SystemPartitionConfiguration
226
propertyElement = configurationBean.addElement( "property" );
227         propertyElement.addAttribute( "name", "systemPartitionConfiguration" );
228         propertyElement.addAttribute( "ref", "systemPartitionConfiguration" );
229
230         // PartitionConfigurations
231
propertyElement = configurationBean.addElement( "property" );
232         propertyElement.addAttribute( "name", "partitionConfigurations" );
233         if ( serverConfiguration.getPartitions().size() > 1 )
234         {
235             Element setElement = propertyElement.addElement( "set" );
236             int partitionCounter = 1;
237             for ( Partition partition : serverConfiguration.getPartitions() )
238             {
239                 if ( !partition.isSystemPartition() )
240                 {
241                     setElement.addElement( "ref" ).addAttribute( "bean", "partition-" + partitionCounter );
242                     partitionCounter++;
243                 }
244             }
245         }
246
247         // ExtendedOperationHandlers
248
propertyElement = configurationBean.addElement( "property" );
249         propertyElement.addAttribute( "name", "extendedOperationHandlers" );
250         if ( serverConfiguration.getExtendedOperations().size() > 1 )
251         {
252             Element listElement = propertyElement.addElement( "list" );
253             for ( ExtendedOperation extendedOperation : serverConfiguration.getExtendedOperations() )
254             {
255                 listElement.addElement( "bean" ).addAttribute( "class", extendedOperation.getClassType() );
256             }
257         }
258
259         // InterceptorConfigurations
260
propertyElement = configurationBean.addElement( "property" );
261         propertyElement.addAttribute( "name", "interceptorConfigurations" );
262         if ( serverConfiguration.getInterceptors().size() > 1 )
263         {
264             Element listElement = propertyElement.addElement( "list" );
265             for ( Interceptor interceptor : serverConfiguration.getInterceptors() )
266             {
267                 Element interceptorBeanElement = listElement.addElement( "bean" );
268                 interceptorBeanElement.addAttribute( "class",
269                     "org.apache.directory.server.core.configuration.MutableInterceptorConfiguration" );
270
271                 Element interceptorPropertyElement = interceptorBeanElement.addElement( "property" );
272                 interceptorPropertyElement.addAttribute( "name", "name" );
273                 interceptorPropertyElement.addAttribute( "value", interceptor.getName() );
274
275                 interceptorPropertyElement = interceptorBeanElement.addElement( "property" );
276                 interceptorPropertyElement.addAttribute( "name", "interceptor" );
277                 interceptorPropertyElement.addElement( "bean" ).addAttribute( "class",
278                     ( interceptor.getClassType() == null ? "" : interceptor.getClassType() ) );
279             }
280         }
281
282     }
283
284
285     /**
286      * Creates the SystemPartitionConfiguration Bean.
287      *
288      * @param root
289      * the root Element
290      * @param serverConfiguration
291      * the Server Configuration
292      */

293     private void createSystemPartitionConfigurationBean( Element root, ServerConfiguration serverConfiguration )
294     {
295         Partition systemPartition = null;
296         for ( Partition partition : serverConfiguration.getPartitions() )
297         {
298             if ( partition.isSystemPartition() )
299             {
300                 systemPartition = partition;
301                 break;
302             }
303         }
304
305         if ( systemPartition != null )
306         {
307             createPartitionConfigurationBean( root, systemPartition, "systemPartitionConfiguration" );
308         }
309     }
310
311
312     /**
313      * Creates the UserPartitionConfigurations Bean.
314      *
315      * @param root
316      * the root Element
317      * @param serverConfiguration
318      * the Server Configuration
319      */

320     private void createUserPartitionsConfigurationsBean( Element root, ServerConfiguration serverConfiguration )
321     {
322         int counter = 1;
323         for ( Partition partition : serverConfiguration.getPartitions() )
324         {
325             if ( !partition.isSystemPartition() )
326             {
327                 createPartitionConfigurationBean( root, partition, "partition-" + counter );
328                 counter++;
329             }
330         }
331     }
332
333
334     /**
335      * Creates a Partition Configuration Bean.
336      *
337      * @param root
338      * the root Element
339      * @param partition
340      * the Partition
341      * @param name
342      * the name to use
343      */

344     private void createPartitionConfigurationBean( Element root, Partition partition, String JavaDoc name )
345     {
346         Element partitionBean = root.addElement( "bean" );
347         partitionBean.addAttribute( "id", name );
348         partitionBean.addAttribute( "class",
349             "org.apache.directory.server.core.partition.impl.btree.MutableBTreePartitionConfiguration" );
350
351         // Name
352
Element propertyElement = partitionBean.addElement( "property" );
353         propertyElement.addAttribute( "name", "name" );
354         propertyElement.addAttribute( "value", partition.getName() );
355
356         // CacheSize
357
propertyElement = partitionBean.addElement( "property" );
358         propertyElement.addAttribute( "name", "cacheSize" );
359         propertyElement.addAttribute( "value", "" + partition.getCacheSize() );
360
361         // Suffix
362
propertyElement = partitionBean.addElement( "property" );
363         propertyElement.addAttribute( "name", "suffix" );
364         propertyElement.addAttribute( "value", partition.getSuffix() );
365
366         // OptimizerEnabled
367
propertyElement = partitionBean.addElement( "property" );
368         propertyElement.addAttribute( "name", "optimizerEnabled" );
369         propertyElement.addAttribute( "value", "" + partition.isEnableOptimizer() );
370
371         // SynchOnWrite
372
propertyElement = partitionBean.addElement( "property" );
373         propertyElement.addAttribute( "name", "synchOnWrite" );
374         propertyElement.addAttribute( "value", "" + partition.isSynchronizationOnWrite() );
375
376         // Indexed Attributes
377
propertyElement = partitionBean.addElement( "property" );
378         propertyElement.addAttribute( "name", "indexedAttributes" );
379         if ( partition.getIndexedAttributes().size() > 1 )
380         {
381             Element setElement = propertyElement.addElement( "set" );
382             for ( IndexedAttribute indexedAttribute : partition.getIndexedAttributes() )
383             {
384                 Element beanElement = setElement.addElement( "bean" );
385                 beanElement.addAttribute( "class",
386                     "org.apache.directory.server.core.partition.impl.btree.MutableIndexConfiguration" );
387
388                 // AttributeID
389
Element beanPropertyElement = beanElement.addElement( "property" );
390                 beanPropertyElement.addAttribute( "name", "attributeId" );
391                 beanPropertyElement.addAttribute( "value", indexedAttribute.getAttributeId() );
392
393                 // CacheSize
394
beanPropertyElement = beanElement.addElement( "property" );
395                 beanPropertyElement.addAttribute( "name", "cacheSize" );
396                 beanPropertyElement.addAttribute( "value", "" + indexedAttribute.getCacheSize() );
397             }
398         }
399
400         // ContextEntry
401
propertyElement = partitionBean.addElement( "property" );
402         propertyElement.addAttribute( "name", "contextEntry" );
403         if ( partition.getContextEntry() != null )
404         {
405             Element valueElement = propertyElement.addElement( "value" );
406
407             Attributes JavaDoc contextEntry = partition.getContextEntry();
408             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
409             NamingEnumeration JavaDoc<? extends Attribute JavaDoc> ne = contextEntry.getAll();
410             while ( ne.hasMoreElements() )
411             {
412                 Attribute JavaDoc attribute = ( Attribute JavaDoc ) ne.nextElement();
413                 try
414                 {
415                     NamingEnumeration JavaDoc<?> values = attribute.getAll();
416                     while ( values.hasMoreElements() )
417                     {
418                         sb.append( attribute.getID() + ": " + values.nextElement() + "\n" );
419                     }
420                 }
421                 catch ( NamingException JavaDoc e )
422                 {
423                 }
424             }
425
426             valueElement.setText( sb.toString() );
427         }
428     }
429
430
431     /**
432      * Creates the Custom Editors Bean.
433      *
434      * @param root
435      * the root Element
436      */

437     private void createCustomEditorsBean( Element root )
438     {
439         Element customEditorsBean = root.addElement( "bean" );
440         customEditorsBean.addAttribute( "class", "org.springframework.beans.factory.config.CustomEditorConfigurer" );
441         Element propertyElement = customEditorsBean.addElement( "property" );
442         propertyElement.addAttribute( "name", "customEditors" );
443         Element mapElement = propertyElement.addElement( "map" );
444         Element entryElement = mapElement.addElement( "entry" );
445         entryElement.addAttribute( "key", "javax.naming.directory.Attributes" );
446         Element entryBeanElement = entryElement.addElement( "bean" );
447         entryBeanElement.addAttribute( "class",
448             "org.apache.directory.server.core.configuration.AttributesPropertyEditor" );
449     }
450
451
452     /**
453      * XML Pretty Printer XSLT Tranformation
454      *
455      * @param document
456      * the Dom4j Document
457      * @return
458      * the stylized Document
459      * @throws TransformerException
460      */

461     private Document styleDocument( Document document ) throws TransformerException JavaDoc
462     {
463         // load the transformer using JAXP
464
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
465         Transformer JavaDoc transformer = null;
466
467         transformer = factory
468             .newTransformer( new StreamSource JavaDoc( Activator.class.getResourceAsStream( "template.xslt" ) ) );
469
470         // now lets style the given document
471
DocumentSource source = new DocumentSource( document );
472         DocumentResult result = new DocumentResult();
473
474         transformer.transform( source, result );
475
476         // return the transformed document
477
Document transformedDoc = result.getDocument();
478         return transformedDoc;
479     }
480 }
481
Popular Tags