1 20 package org.apache.directory.ldapstudio.apacheds.configuration.model; 21 22 23 import java.io.BufferedWriter ; 24 import java.io.FileWriter ; 25 26 import javax.naming.NamingEnumeration ; 27 import javax.naming.NamingException ; 28 import javax.naming.directory.Attribute ; 29 import javax.naming.directory.Attributes ; 30 import javax.xml.transform.Transformer ; 31 import javax.xml.transform.TransformerException ; 32 import javax.xml.transform.TransformerFactory ; 33 import javax.xml.transform.stream.StreamSource ; 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 49 public class ServerConfigurationWriter 50 { 51 59 public void write( ServerConfiguration serverConfiguration ) throws ServerConfigurationWriterException 60 { 61 try 62 { 63 BufferedWriter outFile = new BufferedWriter ( new FileWriter ( serverConfiguration.getPath() ) ); 64 65 Document document = DocumentHelper.createDocument(); 66 Element root = document.addElement( "beans" ); 67 68 createEnvironmentBean( root, serverConfiguration ); 70 71 createConfigurationBean( root, serverConfiguration ); 73 74 createSystemPartitionConfigurationBean( root, serverConfiguration ); 76 77 createUserPartitionsConfigurationsBean( root, serverConfiguration ); 79 80 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 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 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 Element propElement = propsElement.addElement( "prop" ); 119 propElement.addAttribute( "key", "java.naming.security.authentication" ); 120 propElement.setText( "simple" ); 121 122 propElement = propsElement.addElement( "prop" ); 124 propElement.addAttribute( "key", "java.naming.security.principal" ); 125 propElement.setText( serverConfiguration.getPrincipal() ); 126 127 propElement = propsElement.addElement( "prop" ); 129 propElement.addAttribute( "key", "java.naming.security.credentials" ); 130 propElement.setText( serverConfiguration.getPassword() ); 131 132 if ( !serverConfiguration.getBinaryAttributes().isEmpty() ) 134 { 135 propElement = propsElement.addElement( "prop" ); 136 propElement.addAttribute( "key", "java.naming.ldap.attributes.binary" ); 137 StringBuffer sb = new StringBuffer (); 138 for ( String attribute : serverConfiguration.getBinaryAttributes() ) 139 { 140 sb.append( attribute ); 141 sb.append( " " ); 142 } 143 String attributes = sb.toString(); 144 propElement.setText( attributes.substring( 0, attributes.length() - 1 ) ); 145 } 146 147 } 148 149 150 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 Element propertyElement = configurationBean.addElement( "property" ); 167 propertyElement.addAttribute( "name", "workingDirectory" ); 168 propertyElement.addAttribute( "value", "example.com" ); 170 propertyElement = configurationBean.addElement( "property" ); 172 propertyElement.addAttribute( "name", "synchPeriodMillis" ); 173 propertyElement.addAttribute( "value", "" + serverConfiguration.getSynchronizationPeriod() ); 174 175 propertyElement = configurationBean.addElement( "property" ); 177 propertyElement.addAttribute( "name", "maxTimeLimit" ); 178 propertyElement.addAttribute( "value", "" + serverConfiguration.getMaxTimeLimit() ); 179 180 propertyElement = configurationBean.addElement( "property" ); 182 propertyElement.addAttribute( "name", "maxSizeLimit" ); 183 propertyElement.addAttribute( "value", "" + serverConfiguration.getMaxSizeLimit() ); 184 185 propertyElement = configurationBean.addElement( "property" ); 187 propertyElement.addAttribute( "name", "maxThreads" ); 188 propertyElement.addAttribute( "value", "" + serverConfiguration.getMaxThreads() ); 189 190 propertyElement = configurationBean.addElement( "property" ); 192 propertyElement.addAttribute( "name", "allowAnonymousAccess" ); 193 propertyElement.addAttribute( "value", "" + serverConfiguration.isAllowAnonymousAccess() ); 194 195 propertyElement = configurationBean.addElement( "property" ); 197 propertyElement.addAttribute( "name", "accessControlEnabled" ); 198 propertyElement.addAttribute( "value", "" + serverConfiguration.isEnableAccessControl() ); 199 200 propertyElement = configurationBean.addElement( "property" ); 202 propertyElement.addAttribute( "name", "enableNtp" ); 203 propertyElement.addAttribute( "value", "" + serverConfiguration.isEnableNTP() ); 204 205 propertyElement = configurationBean.addElement( "property" ); 207 propertyElement.addAttribute( "name", "enableKerberos" ); 208 propertyElement.addAttribute( "value", "" + serverConfiguration.isEnableKerberos() ); 209 210 propertyElement = configurationBean.addElement( "property" ); 212 propertyElement.addAttribute( "name", "enableChangePassword" ); 213 propertyElement.addAttribute( "value", "" + serverConfiguration.isEnableChangePassword() ); 214 215 propertyElement = configurationBean.addElement( "property" ); 217 propertyElement.addAttribute( "name", "denormalizeOpAttrsEnabled" ); 218 propertyElement.addAttribute( "value", "" + serverConfiguration.isDenormalizeOpAttr() ); 219 220 propertyElement = configurationBean.addElement( "property" ); 222 propertyElement.addAttribute( "name", "ldapPort" ); 223 propertyElement.addAttribute( "value", "" + serverConfiguration.getPort() ); 224 225 propertyElement = configurationBean.addElement( "property" ); 227 propertyElement.addAttribute( "name", "systemPartitionConfiguration" ); 228 propertyElement.addAttribute( "ref", "systemPartitionConfiguration" ); 229 230 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 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 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 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 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 344 private void createPartitionConfigurationBean( Element root, Partition partition, String 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 Element propertyElement = partitionBean.addElement( "property" ); 353 propertyElement.addAttribute( "name", "name" ); 354 propertyElement.addAttribute( "value", partition.getName() ); 355 356 propertyElement = partitionBean.addElement( "property" ); 358 propertyElement.addAttribute( "name", "cacheSize" ); 359 propertyElement.addAttribute( "value", "" + partition.getCacheSize() ); 360 361 propertyElement = partitionBean.addElement( "property" ); 363 propertyElement.addAttribute( "name", "suffix" ); 364 propertyElement.addAttribute( "value", partition.getSuffix() ); 365 366 propertyElement = partitionBean.addElement( "property" ); 368 propertyElement.addAttribute( "name", "optimizerEnabled" ); 369 propertyElement.addAttribute( "value", "" + partition.isEnableOptimizer() ); 370 371 propertyElement = partitionBean.addElement( "property" ); 373 propertyElement.addAttribute( "name", "synchOnWrite" ); 374 propertyElement.addAttribute( "value", "" + partition.isSynchronizationOnWrite() ); 375 376 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 Element beanPropertyElement = beanElement.addElement( "property" ); 390 beanPropertyElement.addAttribute( "name", "attributeId" ); 391 beanPropertyElement.addAttribute( "value", indexedAttribute.getAttributeId() ); 392 393 beanPropertyElement = beanElement.addElement( "property" ); 395 beanPropertyElement.addAttribute( "name", "cacheSize" ); 396 beanPropertyElement.addAttribute( "value", "" + indexedAttribute.getCacheSize() ); 397 } 398 } 399 400 propertyElement = partitionBean.addElement( "property" ); 402 propertyElement.addAttribute( "name", "contextEntry" ); 403 if ( partition.getContextEntry() != null ) 404 { 405 Element valueElement = propertyElement.addElement( "value" ); 406 407 Attributes contextEntry = partition.getContextEntry(); 408 StringBuffer sb = new StringBuffer (); 409 NamingEnumeration <? extends Attribute > ne = contextEntry.getAll(); 410 while ( ne.hasMoreElements() ) 411 { 412 Attribute attribute = ( Attribute ) ne.nextElement(); 413 try 414 { 415 NamingEnumeration <?> values = attribute.getAll(); 416 while ( values.hasMoreElements() ) 417 { 418 sb.append( attribute.getID() + ": " + values.nextElement() + "\n" ); 419 } 420 } 421 catch ( NamingException e ) 422 { 423 } 424 } 425 426 valueElement.setText( sb.toString() ); 427 } 428 } 429 430 431 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 461 private Document styleDocument( Document document ) throws TransformerException 462 { 463 TransformerFactory factory = TransformerFactory.newInstance(); 465 Transformer transformer = null; 466 467 transformer = factory 468 .newTransformer( new StreamSource ( Activator.class.getResourceAsStream( "template.xslt" ) ) ); 469 470 DocumentSource source = new DocumentSource( document ); 472 DocumentResult result = new DocumentResult(); 473 474 transformer.transform( source, result ); 475 476 Document transformedDoc = result.getDocument(); 478 return transformedDoc; 479 } 480 } 481 | Popular Tags |