1 20 package org.apache.directory.ldapstudio.apacheds.configuration.model; 21 22 23 import java.io.BufferedReader ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.StringReader ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import javax.naming.NamingException ; 32 import javax.naming.directory.Attributes ; 33 import javax.naming.directory.BasicAttributes ; 34 35 import org.apache.directory.shared.ldap.ldif.LdifReader; 36 import org.apache.directory.shared.ldap.message.AttributesImpl; 37 import org.apache.directory.shared.ldap.util.StringTools; 38 import org.dom4j.Attribute; 39 import org.dom4j.Document; 40 import org.dom4j.Element; 41 import org.dom4j.io.SAXReader; 42 43 44 51 public class ServerConfigurationParser 52 { 53 64 public ServerConfiguration parse( String path ) throws ServerConfigurationParserException 65 { 66 try 67 { 68 SAXReader reader = new SAXReader(); 69 Document document = reader.read( path ); 70 71 ServerConfiguration serverConfiguration = new ServerConfiguration(); 72 serverConfiguration.setPath( path ); 73 74 parse( document, serverConfiguration ); 75 76 return serverConfiguration; 77 } 78 catch ( Exception e ) 79 { 80 if ( e instanceof ServerConfigurationParserException ) 81 { 82 throw ( ServerConfigurationParserException ) e; 83 } 84 else 85 { 86 ServerConfigurationParserException exception = new ServerConfigurationParserException( e.getMessage(), 87 e.getCause() ); 88 exception.setStackTrace( e.getStackTrace() ); 89 throw exception; 90 } 91 } 92 } 93 94 95 106 public ServerConfiguration parse( InputStream inputStream ) throws ServerConfigurationParserException 107 { 108 try 109 { 110 SAXReader reader = new SAXReader(); 111 Document document = reader.read( inputStream ); 112 113 ServerConfiguration serverConfiguration = new ServerConfiguration(); 114 115 parse( document, serverConfiguration ); 116 117 return serverConfiguration; 118 } 119 catch ( Exception e ) 120 { 121 if ( e instanceof ServerConfigurationParserException ) 122 { 123 throw ( ServerConfigurationParserException ) e; 124 } 125 else 126 { 127 ServerConfigurationParserException exception = new ServerConfigurationParserException( e.getMessage(), 128 e.getCause() ); 129 exception.setStackTrace( e.getStackTrace() ); 130 throw exception; 131 } 132 } 133 } 134 135 136 147 private void parse( Document document, ServerConfiguration serverConfiguration ) throws NumberFormatException , 148 BooleanFormatException, ServerConfigurationParserException 149 { 150 readEnvironmentBean( document, serverConfiguration ); 152 153 readConfigurationBean( document, serverConfiguration ); 155 } 156 157 158 166 private void readEnvironmentBean( Document document, ServerConfiguration serverConfiguration ) 167 { 168 Element environmentBean = getBeanElementById( document, "environment" ); 169 170 String principal = readEnvironmentBeanProperty( "java.naming.security.principal", environmentBean ); 172 if ( principal != null ) 173 { 174 serverConfiguration.setPrincipal( principal ); 175 } 176 177 String password = readEnvironmentBeanProperty( "java.naming.security.credentials", environmentBean ); 179 if ( password != null ) 180 { 181 serverConfiguration.setPassword( password ); 182 } 183 184 String binaryAttributes = readEnvironmentBeanProperty( "java.naming.ldap.attributes.binary", environmentBean ); 186 if ( binaryAttributes != null ) 187 { 188 String [] attributes = binaryAttributes.split( " " ); 189 190 for( String attribute : attributes) 191 { 192 serverConfiguration.addBinaryAttribute( attribute ); 193 } 194 } 195 } 196 197 198 208 private String readEnvironmentBeanProperty( String property, Element element ) 209 { 210 Element propertyElement = element.element( "property" ); 211 if ( propertyElement != null ) 212 { 213 Element propsElement = propertyElement.element( "props" ); 214 if ( propsElement != null ) 215 { 216 for ( Iterator i = propsElement.elementIterator( "prop" ); i.hasNext(); ) 217 { 218 Element propElement = ( Element ) i.next(); 219 Attribute keyAttribute = propElement.attribute( "key" ); 220 if ( keyAttribute != null && ( keyAttribute.getValue().equals( property ) ) ) 221 { 222 return propElement.getText(); 223 } 224 } 225 } 226 } 227 228 return null; 229 } 230 231 232 243 private void readConfigurationBean( Document document, ServerConfiguration serverConfiguration ) 244 throws NumberFormatException , BooleanFormatException, ServerConfigurationParserException 245 { 246 Element configurationBean = getBeanElementById( document, "configuration" ); 247 248 String ldapPort = readBeanProperty( "ldapPort", configurationBean ); 250 if ( ldapPort != null ) 251 { 252 serverConfiguration.setPort( Integer.parseInt( ldapPort ) ); 253 } 254 255 String synchPeriodMillis = readBeanProperty( "synchPeriodMillis", configurationBean ); 257 if ( synchPeriodMillis != null ) 258 { 259 serverConfiguration.setSynchronizationPeriod( Long.parseLong( synchPeriodMillis ) ); 260 } 261 262 String maxTimeLimit = readBeanProperty( "maxTimeLimit", configurationBean ); 264 if ( maxTimeLimit != null ) 265 { 266 serverConfiguration.setMaxTimeLimit( Integer.parseInt( maxTimeLimit ) ); 267 } 268 269 String maxSizeLimit = readBeanProperty( "maxSizeLimit", configurationBean ); 271 if ( maxSizeLimit != null ) 272 { 273 serverConfiguration.setMaxSizeLimit( Integer.parseInt( maxSizeLimit ) ); 274 } 275 276 String maxThreads = readBeanProperty( "maxThreads", configurationBean ); 278 if ( maxThreads != null ) 279 { 280 serverConfiguration.setMaxThreads( Integer.parseInt( maxThreads ) ); 281 } 282 283 String allowAnonymousAccess = readBeanProperty( "allowAnonymousAccess", configurationBean ); 285 if ( allowAnonymousAccess != null ) 286 { 287 serverConfiguration.setAllowAnonymousAccess( parseBoolean( allowAnonymousAccess ) ); 288 } 289 290 String accessControlEnabled = readBeanProperty( "accessControlEnabled", configurationBean ); 292 if ( accessControlEnabled != null ) 293 { 294 serverConfiguration.setEnableAccessControl( parseBoolean( accessControlEnabled ) ); 295 } 296 297 String enableNtp = readBeanProperty( "enableNtp", configurationBean ); 299 if ( enableNtp != null ) 300 { 301 serverConfiguration.setEnableNTP( parseBoolean( enableNtp ) ); 302 } 303 304 String enableKerberos = readBeanProperty( "enableKerberos", configurationBean ); 306 if ( enableKerberos != null ) 307 { 308 serverConfiguration.setEnableKerberos( parseBoolean( enableKerberos ) ); 309 } 310 311 String enableChangePassword = readBeanProperty( "enableChangePassword", configurationBean ); 313 if ( enableChangePassword != null ) 314 { 315 serverConfiguration.setEnableChangePassword( parseBoolean( enableChangePassword ) ); 316 } 317 318 String denormalizeOpAttrsEnabled = readBeanProperty( "denormalizeOpAttrsEnabled", configurationBean ); 320 if ( denormalizeOpAttrsEnabled != null ) 321 { 322 serverConfiguration.setDenormalizeOpAttr( parseBoolean( denormalizeOpAttrsEnabled ) ); 323 } 324 325 String systemPartitionConfiguration = readBeanProperty( "systemPartitionConfiguration", configurationBean ); 327 if ( systemPartitionConfiguration != null ) 328 { 329 Partition systemPartition = readPartition( document, systemPartitionConfiguration, true ); 330 if ( systemPartition != null ) 331 { 332 serverConfiguration.addPartition( systemPartition ); 333 } 334 } 335 else 336 { 337 throw new ServerConfigurationParserException( 338 "The Server Configuration does not contain a 'systemPartitionConfiguration' property." ); 339 } 340 341 readOtherPartitions( configurationBean, serverConfiguration ); 343 344 readInterceptors( configurationBean, serverConfiguration ); 346 347 readExtendedOperations( configurationBean, serverConfiguration ); 349 } 350 351 352 362 private void readOtherPartitions( Element configurationBean, ServerConfiguration serverConfiguration ) 363 throws NumberFormatException , BooleanFormatException 364 { 365 Element propertyElement = getBeanPropertyElement( "partitionConfigurations", configurationBean ); 366 if ( propertyElement != null ) 367 { 368 Element setElement = propertyElement.element( "set" ); 369 if ( setElement != null ) 370 { 371 for ( Iterator i = setElement.elementIterator( "ref" ); i.hasNext(); ) 372 { 373 Element element = ( Element ) i.next(); 374 Attribute beanAttribute = element.attribute( "bean" ); 375 if ( beanAttribute != null ) 376 { 377 Partition partition = readPartition( configurationBean.getDocument(), beanAttribute.getValue(), 378 false ); 379 if ( partition != null ) 380 { 381 serverConfiguration.addPartition( partition ); 382 } 383 } 384 } 385 } 386 } 387 } 388 389 390 403 private Partition readPartition( Document document, String id, boolean isSystemPartition ) 404 throws BooleanFormatException, NumberFormatException 405 { 406 Element partitionBean = getBeanElementById( document, id ); 407 if ( partitionBean != null ) 408 { 409 Partition partition = new Partition(); 410 partition.setSystemPartition( isSystemPartition ); 411 412 String name = readBeanProperty( "name", partitionBean ); 414 if ( name != null ) 415 { 416 partition.setName( name ); 417 } 418 419 String cacheSize = readBeanProperty( "cacheSize", partitionBean ); 421 if ( cacheSize != null ) 422 { 423 partition.setCacheSize( Integer.parseInt( cacheSize ) ); 424 } 425 426 String suffix = readBeanProperty( "suffix", partitionBean ); 428 if ( suffix != null ) 429 { 430 partition.setSuffix( suffix ); 431 } 432 433 String optimizerEnabled = readBeanProperty( "optimizerEnabled", partitionBean ); 435 if ( optimizerEnabled != null ) 436 { 437 partition.setEnableOptimizer( parseBoolean( optimizerEnabled ) ); 438 } 439 440 String synchOnWrite = readBeanProperty( "synchOnWrite", partitionBean ); 442 if ( synchOnWrite != null ) 443 { 444 partition.setSynchronizationOnWrite( parseBoolean( synchOnWrite ) ); 445 } 446 447 partition.setIndexedAttributes( readPartitionIndexedAttributes( partitionBean ) ); 449 450 partition.setContextEntry( readPartitionContextEntry( partitionBean ) ); 452 453 return partition; 454 } 455 456 return null; 457 } 458 459 460 468 private List <IndexedAttribute> readPartitionIndexedAttributes( Element partitionBean ) throws NumberFormatException 469 { 470 List <IndexedAttribute> indexedAttributes = new ArrayList <IndexedAttribute>(); 471 472 Element propertyElement = getBeanPropertyElement( "indexedAttributes", partitionBean ); 473 if ( propertyElement != null ) 474 { 475 Element setElement = propertyElement.element( "set" ); 476 if ( setElement != null ) 477 { 478 for ( Iterator i = setElement.elementIterator( "bean" ); i.hasNext(); ) 479 { 480 Element beanElement = ( Element ) i.next(); 481 IndexedAttribute ia = readIndexedAttribute( beanElement ); 482 if ( ia != null ) 483 { 484 indexedAttributes.add( ia ); 485 } 486 } 487 } 488 } 489 490 return indexedAttributes; 491 } 492 493 494 503 private IndexedAttribute readIndexedAttribute( Element beanElement ) throws NumberFormatException 504 { 505 Attribute classAttribute = beanElement.attribute( "class" ); 506 if ( classAttribute != null 507 && classAttribute.getValue().equals( 508 "org.apache.directory.server.core.partition.impl.btree.MutableIndexConfiguration" ) ) 509 { 510 String attributeId = readBeanProperty( "attributeId", beanElement ); 511 String cacheSize = readBeanProperty( "cacheSize", beanElement ); 512 if ( ( attributeId != null ) && ( cacheSize != null ) ) 513 { 514 return new IndexedAttribute( attributeId, Integer.parseInt( cacheSize ) ); 515 } 516 517 } 518 519 return null; 520 } 521 522 523 531 private Attributes readPartitionContextEntry( Element partitionBean ) 532 { 533 Element propertyElement = getBeanPropertyElement( "contextEntry", partitionBean ); 534 if ( propertyElement != null ) 535 { 536 Element valueElement = propertyElement.element( "value" ); 537 if ( valueElement != null ) 538 { 539 return readContextEntry( valueElement.getText() ); 540 } 541 } 542 543 return new BasicAttributes ( true ); 544 } 545 546 547 554 private Attributes readContextEntry( String text ) 555 { 556 StringReader strIn = new StringReader ( text ); 557 BufferedReader in = new BufferedReader ( strIn ); 558 559 String line = null; 560 Attributes attributes = new AttributesImpl( true ); 561 562 try 563 { 564 while ( ( line = ( ( BufferedReader ) in ).readLine() ) != null ) 565 { 566 if ( line.length() == 0 ) 567 { 568 continue; 569 } 570 571 String addedLine = line.trim(); 572 573 if ( StringTools.isEmpty( addedLine ) ) 574 { 575 continue; 576 } 577 578 javax.naming.directory.Attribute attribute = LdifReader.parseAttributeValue( addedLine ); 579 javax.naming.directory.Attribute oldAttribute = attributes.get( attribute.getID() ); 580 581 if ( oldAttribute != null ) 582 { 583 try 584 { 585 oldAttribute.add( attribute.get() ); 586 attributes.put( oldAttribute ); 587 } 588 catch ( NamingException ne ) 589 { 590 } 592 } 593 else 594 { 595 attributes.put( attribute ); 596 } 597 } 598 } 599 catch ( IOException ioe ) 600 { 601 } 603 604 return attributes; 605 } 606 607 608 616 private void readInterceptors( Element configurationBean, ServerConfiguration serverConfiguration ) 617 { 618 Element propertyElement = getBeanPropertyElement( "interceptorConfigurations", configurationBean ); 619 if ( propertyElement != null ) 620 { 621 Element listElement = propertyElement.element( "list" ); 622 if ( listElement != null ) 623 { 624 for ( Iterator i = listElement.elementIterator( "bean" ); i.hasNext(); ) 625 { 626 Interceptor interceptor = readInterceptor( ( Element ) i.next() ); 627 if ( interceptor != null ) 628 { 629 serverConfiguration.addInterceptor( interceptor ); 630 } 631 } 632 } 633 } 634 } 635 636 637 645 private Interceptor readInterceptor( Element element ) 646 { 647 Attribute classAttribute = element.attribute( "class" ); 648 if ( classAttribute != null 649 && classAttribute.getValue().equals( 650 "org.apache.directory.server.core.configuration.MutableInterceptorConfiguration" ) ) 651 { 652 String name = readBeanProperty( "name", element ); 653 654 for ( Iterator i = element.elementIterator( "property" ); i.hasNext(); ) 655 { 656 Element propertyElement = ( Element ) i.next(); 657 Attribute nameAttribute = propertyElement.attribute( "name" ); 658 if ( nameAttribute != null && ( nameAttribute.getValue().equals( "interceptor" ) ) ) 659 { 660 Element beanElement = propertyElement.element( "bean" ); 661 if ( beanElement != null ) 662 { 663 Attribute beanClassAttribute = beanElement.attribute( "class" ); 664 if ( beanClassAttribute != null ) 665 { 666 Interceptor interceptor = new Interceptor( name ); 667 interceptor.setClassType( beanClassAttribute.getValue() ); 668 return interceptor; 669 } 670 } 671 } 672 } 673 } 674 675 return null; 676 } 677 678 679 687 private void readExtendedOperations( Element configurationBean, ServerConfiguration serverConfiguration ) 688 { 689 Element propertyElement = getBeanPropertyElement( "extendedOperationHandlers", configurationBean ); 690 if ( propertyElement != null ) 691 { 692 Element listElement = propertyElement.element( "list" ); 693 if ( listElement != null ) 694 { 695 for ( Iterator i = listElement.elementIterator( "bean" ); i.hasNext(); ) 696 { 697 ExtendedOperation extendedOperation = readExtendedOperation( ( Element ) i.next() ); 698 if ( extendedOperation != null ) 699 { 700 serverConfiguration.addExtendedOperation( extendedOperation ); 701 } 702 } 703 } 704 } 705 } 706 707 708 716 private ExtendedOperation readExtendedOperation( Element element ) 717 { 718 Attribute classAttribute = element.attribute( "class" ); 719 if ( classAttribute != null ) 720 { 721 return new ExtendedOperation( classAttribute.getValue() ); 722 } 723 724 return null; 725 } 726 727 728 738 private Element getBeanElementById( Document document, String id ) 739 { 740 for ( Iterator i = document.getRootElement().elementIterator( "bean" ); i.hasNext(); ) 741 { 742 Element element = ( Element ) i.next(); 743 Attribute idAttribute = element.attribute( "id" ); 744 if ( idAttribute != null && ( idAttribute.getValue().equals( id ) ) ) 745 { 746 return element; 747 } 748 } 749 750 return null; 751 } 752 753 754 764 private String readBeanProperty( String property, Element element ) 765 { 766 Element propertyElement = getBeanPropertyElement( property, element ); 767 if ( propertyElement != null ) 768 { 769 Attribute valueAttribute = propertyElement.attribute( "value" ); 770 if ( valueAttribute != null ) 771 { 772 return valueAttribute.getValue(); 773 } 774 775 Attribute refAttribute = propertyElement.attribute( "ref" ); 776 if ( refAttribute != null ) 777 { 778 return refAttribute.getValue(); 779 } 780 } 781 782 return null; 783 } 784 785 786 796 private Element getBeanPropertyElement( String property, Element element ) 797 { 798 for ( Iterator i = element.elementIterator( "property" ); i.hasNext(); ) 799 { 800 Element propertyElement = ( Element ) i.next(); 801 Attribute nameAttribute = propertyElement.attribute( "name" ); 802 if ( nameAttribute != null && ( nameAttribute.getValue().equals( property ) ) ) 803 { 804 return propertyElement; 805 } 806 } 807 808 return null; 809 } 810 811 812 822 private boolean parseBoolean( String s ) throws BooleanFormatException 823 { 824 if ( "true".equals( s ) ) 825 { 826 return true; 827 } 828 else if ( "false".equals( s ) ) 829 { 830 return false; 831 } 832 else 833 { 834 throw new BooleanFormatException( "The String '" + s + "' could not be parsed as a boolean." ); 835 } 836 } 837 838 845 private class BooleanFormatException extends Exception 846 { 847 848 private static final long serialVersionUID = -6426955193802317452L; 849 850 851 857 public BooleanFormatException( String message ) 858 { 859 super( message ); 860 } 861 } 862 } 863 | Popular Tags |