1 20 21 package org.apache.directory.ldapstudio.schemas.model; 22 23 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.FileReader ; 27 import java.io.FileWriter ; 28 import java.io.IOException ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.util.ArrayList ; 32 import java.util.HashMap ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 import org.apache.directory.ldapstudio.schemas.Activator; 37 import org.apache.directory.ldapstudio.schemas.PluginConstants; 38 import org.apache.directory.ldapstudio.schemas.model.LDAPModelEvent.Reason; 39 import org.apache.directory.ldapstudio.schemas.model.Schema.SchemaType; 40 import org.apache.log4j.Logger; 41 import org.eclipse.core.runtime.Platform; 42 import org.eclipse.jface.preference.IPreferenceStore; 43 import org.eclipse.ui.IMemento; 44 import org.eclipse.ui.WorkbenchException; 45 import org.eclipse.ui.XMLMemento; 46 47 48 54 public class SchemaPool implements SchemaListener 55 { 56 57 private static Logger logger = Logger.getLogger( SchemaPool.class ); 58 59 60 private static SchemaPool schemaPool; 61 62 63 private List <PoolListener> listeners; 64 65 66 private List <Schema> schemaList; 67 68 69 private List <AttributeType> attributeTypes; 70 71 72 private List <ObjectClass> objectClasses; 73 74 75 private Map <String , AttributeType> attributeTypesMap; 76 77 78 private Map <String , ObjectClass> objectClassesMap; 79 80 81 private static final String SCHEMAS_TAG = "Schemas"; 83 84 private static final String SCHEMA_TAG = "Schema"; 86 87 private static final String PATH_TAG = "path"; 89 90 93 private SchemaPool() 94 { 95 listeners = new ArrayList <PoolListener>(); 96 schemaList = new ArrayList <Schema>(); 97 attributeTypes = new ArrayList <AttributeType>(); 98 objectClasses = new ArrayList <ObjectClass>(); 99 attributeTypesMap = new HashMap <String , AttributeType>(); 100 objectClassesMap = new HashMap <String , ObjectClass>(); 101 } 102 103 104 110 public static SchemaPool getInstance() 111 { 112 if ( schemaPool == null ) 113 { 114 schemaPool = new SchemaPool(); 115 synchronized ( schemaPool ) 116 { 117 schemaPool.loadSchemas(); 118 } 119 } 120 121 return schemaPool; 122 } 123 124 125 128 private void loadSchemas() 129 { 130 schemaPool.loadCoreSchemas(); 131 schemaPool.loadUserSchemas(); 132 } 133 134 135 138 private void loadCoreSchemas() 139 { 140 IPreferenceStore store = Activator.getDefault().getPreferenceStore(); 141 boolean useSpecificCore = store.getBoolean( PluginConstants.PREFS_SCHEMAS_EDITOR_SPECIFIC_CORE ); 142 if ( useSpecificCore ) 143 { 144 schemaPool.loadCoreSchemasFromSpecifiedLocation(); 145 } 146 else 147 { 148 schemaPool.loadCoreSchemasFromBundle(); 149 } 150 } 151 152 153 156 public void loadUserSchemas() 157 { 158 try 159 { 160 FileReader reader = new FileReader ( getSchemaPoolFile() ); 161 XMLMemento memento = XMLMemento.createReadRoot( reader ); 162 IMemento[] children = memento.getChildren( SCHEMA_TAG ); 163 for ( IMemento child : children ) 164 { 165 try 166 { 167 addSchema( Schema.localPathToURL( child.getString( PATH_TAG ) ), Schema.SchemaType.userSchema, false ); 169 } 170 catch ( SchemaCreationException e ) 171 { 172 logger.debug( "Error loading schema " + child.getString( PATH_TAG ) + " in the pool." ); } 174 175 } 176 } 177 catch ( FileNotFoundException e ) 178 { 179 logger.debug( "Error when loading previously opened schemas.", e ); } 181 catch ( WorkbenchException e ) 182 { 183 logger.debug( "Error when loading previously opened schemas.", e ); } 185 } 186 187 188 191 public void saveUserSchemasPaths() 192 { 193 XMLMemento memento = XMLMemento.createWriteRoot( SCHEMAS_TAG ); 194 195 for ( Schema schema : schemaList ) 196 { 197 if ( ( schema.type == Schema.SchemaType.userSchema ) && ( schema.getURL() != null ) ) 198 { 199 IMemento child = memento.createChild( SCHEMA_TAG ); 200 child.putString( PATH_TAG, schema.getURL().getPath() ); 201 } 202 } 203 204 try 205 { 206 FileWriter writer = new FileWriter ( getSchemaPoolFile() ); 207 memento.save( writer ); 208 writer.close(); 209 } 210 catch ( IOException e ) 211 { 212 logger.debug( "Error when saving opened schemas.", e ); } 214 } 215 216 217 220 private void loadCoreSchemasFromBundle() 221 { 222 URL urlApache = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/apache.schema" ); URL urlApachedns = Platform.getBundle( Activator.PLUGIN_ID ) 224 .getResource( "ressources/schemas/apachedns.schema" ); URL urlApachemeta = Platform.getBundle( Activator.PLUGIN_ID ).getResource( 226 "ressources/schemas/apachemeta.schema" ); URL urlAutofs = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/autofs.schema" ); URL urlCollective = Platform.getBundle( Activator.PLUGIN_ID ).getResource( 229 "ressources/schemas/collective.schema" ); URL urlCorba = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/corba.schema" ); URL urlCore = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/core.schema" ); URL urlCosine = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/cosine.schema" ); URL urlDhcp = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/dhcp.schema" ); URL urlInetorgperson = Platform.getBundle( Activator.PLUGIN_ID ).getResource( 235 "ressources/schemas/inetorgperson.schema" ); URL urlJava = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/java.schema" ); URL urlKrb5kdc = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/krb5kdc.schema" ); URL urlNis = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/nis.schema" ); URL urlSamba = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/samba.schema" ); URL urlSystem = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/system.schema" ); 242 schemaPool.addSchema( urlApache, SchemaType.coreSchema, false ); 243 schemaPool.addSchema( urlApachedns, SchemaType.coreSchema, false ); 244 schemaPool.addSchema( urlApachemeta, SchemaType.coreSchema, false ); 245 schemaPool.addSchema( urlAutofs, SchemaType.coreSchema, false ); 246 schemaPool.addSchema( urlCollective, SchemaType.coreSchema, false ); 247 schemaPool.addSchema( urlCorba, SchemaType.coreSchema, false ); 248 schemaPool.addSchema( urlCore, SchemaType.coreSchema, false ); 249 schemaPool.addSchema( urlCosine, SchemaType.coreSchema, false ); 250 schemaPool.addSchema( urlDhcp, SchemaType.coreSchema, false ); 251 schemaPool.addSchema( urlInetorgperson, SchemaType.coreSchema, false ); 252 schemaPool.addSchema( urlJava, SchemaType.coreSchema, false ); 253 schemaPool.addSchema( urlKrb5kdc, SchemaType.coreSchema, false ); 254 schemaPool.addSchema( urlNis, SchemaType.coreSchema, false ); 255 schemaPool.addSchema( urlSamba, SchemaType.coreSchema, false ); 256 schemaPool.addSchema( urlSystem, SchemaType.coreSchema, false ); 257 } 258 259 260 263 private void loadCoreSchemasFromSpecifiedLocation() 264 { 265 IPreferenceStore store = Activator.getDefault().getPreferenceStore(); 266 String specificPath = store.getString( PluginConstants.PREFS_SCHEMAS_EDITOR_SPECIFIC_CORE_DIRECTORY ); 267 268 File dir = new File ( specificPath ); 269 String sCurPath = dir.getAbsolutePath() + File.separator; 270 String [] safiles = dir.list(); 272 if ( safiles != null ) 273 { 274 for ( String safile : safiles ) 275 { 276 File curFile = new File ( sCurPath + safile ); 277 if ( !curFile.isDirectory() ) 278 { 279 try 280 { 281 URL fileURL = curFile.toURL(); 282 if ( Schema.URLtoFileName( fileURL ) != null ) 283 { 284 schemaPool.addSchema( fileURL, Schema.SchemaType.coreSchema, false ); 285 } 286 } 287 catch ( MalformedURLException e ) 288 { 289 logger.debug( "error whith the content of the specified core schema directory" ); } 291 } 292 } 293 } 294 } 295 296 297 303 public Schema[] getSchemas() 304 { 305 return schemaList.toArray( new Schema[0] ); 306 } 307 308 309 315 public List <ObjectClass> getObjectClasses() 316 { 317 return objectClasses; 318 } 319 320 321 327 public List <AttributeType> getAttributeTypes() 328 { 329 return attributeTypes; 330 } 331 332 333 339 public Map <String , ObjectClass> getObjectClassesAsMap() 340 { 341 return objectClassesMap; 342 } 343 344 345 351 public Map <String , AttributeType> getAttributeTypesAsMap() 352 { 353 return attributeTypesMap; 354 } 355 356 357 363 public Map <String , SchemaElement> getSchemaElements() 364 { 365 Map <String , SchemaElement> elementsTable = new HashMap <String , SchemaElement>(); 366 367 elementsTable.putAll( attributeTypesMap ); 368 elementsTable.putAll( objectClassesMap ); 369 370 return elementsTable; 371 } 372 373 374 382 public Schema getSchema( String name ) 383 { 384 for ( Schema schema : schemaList ) 385 { 386 if ( schema.getName().equals( name ) ) 387 { 388 return schema; 389 } 390 } 391 392 return null; 393 } 394 395 396 404 public boolean containsSchema( String name ) 405 { 406 return ( getSchema( name ) != null ); 407 } 408 409 410 418 public boolean containsSchema( Schema schema ) 419 { 420 return schemaList.contains( schema ); 421 } 422 423 424 432 public boolean containsObjectClass( String name ) 433 { 434 return objectClassesMap.containsKey( name.toLowerCase() ); 435 } 436 437 438 446 public boolean containsAttributeType( String name ) 447 { 448 return attributeTypesMap.containsKey( name.toLowerCase() ); 449 } 450 451 452 460 public boolean containsSchemaElement( String name ) 461 { 462 return getSchemaElements().containsKey( name.toLowerCase() ); 463 } 464 465 466 474 public boolean containsObjectClass( ObjectClass objectClass ) 475 { 476 return objectClasses.contains( objectClass ); 477 } 478 479 480 488 public boolean containsAttributeType( AttributeType attributeType ) 489 { 490 return attributeTypes.contains( attributeType ); 491 } 492 493 494 502 public boolean containsSchemaElement( SchemaElement schemaElement ) 503 { 504 return getSchemaElements().containsKey( schemaElement ); 505 } 506 507 508 516 public ObjectClass getObjectClass( String name ) 517 { 518 519 return objectClassesMap.get( name.toLowerCase() ); 520 } 521 522 523 531 public AttributeType getAttributeType( String name ) 532 { 533 return attributeTypesMap.get( name.toLowerCase() ); 534 } 535 536 537 546 private void addSchema( Schema s, boolean notifyListeners ) 547 { 548 if ( s != null ) 549 { 550 if ( !containsSchema( s.getName() ) ) 551 { 552 schemaList.add( s ); 553 s.addListener( this ); 554 555 AttributeType[] ats = s.getAttributeTypesAsArray(); 556 for ( AttributeType at : ats ) 557 { 558 addAttributeType( at ); 559 } 560 561 ObjectClass[] ocs = s.getObjectClassesAsArray(); 562 for ( ObjectClass oc : ocs ) 563 { 564 addObjectClass( oc ); 565 } 566 567 if ( notifyListeners ) 568 { 569 notifyChanged( LDAPModelEvent.Reason.SchemaAdded, s ); 570 } 571 } 572 } 573 } 574 575 576 582 public void addNewSchema( String name ) 583 { 584 addSchema( new Schema( SchemaType.userSchema, name ), true ); 585 } 586 587 588 594 public void addSchema( URL url ) 595 { 596 addSchema( url, SchemaType.userSchema, true ); 597 } 598 599 600 608 618 private void addSchema( URL url, SchemaType type, boolean notifyListeners ) 619 { 620 try 621 { 622 addSchema( new Schema( url, type ), notifyListeners ); 623 } 624 catch ( SchemaCreationException e ) 625 { 626 e.printStackTrace(); 628 } 629 } 630 631 632 638 public void removeSchema( Schema s ) 639 { 640 removeSchema( s, true ); 641 } 642 643 644 652 private void removeSchema( Schema s, boolean notifyListeners ) 653 { 654 if ( s != null ) 655 { 656 schemaList.remove( s ); 657 s.removeListener( this ); 658 s.closeAssociatedEditors(); 659 660 AttributeType[] ats = s.getAttributeTypesAsArray(); 661 for ( AttributeType at : ats ) 662 { 663 removeAttributeType( at ); 664 } 665 666 ObjectClass[] ocs = s.getObjectClassesAsArray(); 667 for ( ObjectClass oc : ocs ) 668 { 669 removeObjectClass( oc ); 670 } 671 672 if ( notifyListeners ) 673 { 674 notifyChanged( LDAPModelEvent.Reason.SchemaRemoved, s ); 675 } 676 } 677 } 678 679 680 684 public void saveAll() throws Exception 685 { 686 saveAll( false ); 687 } 688 689 690 695 public void saveAll( boolean askForConfirmation ) throws Exception 696 { 697 for ( Schema schema : schemaList ) 698 { 699 schema.save( askForConfirmation ); 700 } 701 } 702 703 704 707 public void reload() 708 { 709 Schema[] schemas = schemaList.toArray( new Schema[0] ); 711 for ( int i = 0; i < schemas.length; i++ ) 712 { 713 if ( schemas[i].type == SchemaType.coreSchema ) 714 { 715 removeSchema( schemas[i], false ); 716 } 717 } 718 719 schemaPool.loadCoreSchemas(); 721 722 for ( PoolListener listener : listeners ) 724 { 725 listener.poolChanged( this, new LDAPModelEvent( Reason.PoolReloaded ) ); 726 } 727 } 728 729 730 736 public void addListener( PoolListener listener ) 737 { 738 if ( !listeners.contains( listener ) ) 739 listeners.add( listener ); 740 } 741 742 743 749 public void removeListener( PoolListener listener ) 750 { 751 listeners.remove( listener ); 752 } 753 754 755 763 private void notifyChanged( LDAPModelEvent.Reason reason, Schema sc ) 764 { 765 for ( PoolListener listener : listeners ) 766 { 767 try 768 { 769 listener.poolChanged( this, new LDAPModelEvent( reason, sc ) ); 770 } 771 catch ( Exception e ) 772 { 773 logger.debug( "error when notifying " + listener + " of pool modification" ); } 775 } 776 } 777 778 779 782 public void schemaChanged( Schema originatingSchema, LDAPModelEvent e ) 783 { 784 switch ( e.getReason() ) 785 { 786 case ATAdded: 787 case OCAdded: 788 elementAdded( e ); 789 break; 790 791 case ATModified: 792 attributeTypeModified( e ); 793 break; 794 795 case OCModified: 796 objectClassModified( e ); 797 break; 798 799 case ATRemoved: 800 case OCRemoved: 801 elementRemoved( e ); 802 break; 803 804 default: 805 break; 806 } 807 808 for ( PoolListener listener : listeners ) 809 { 810 try 811 { 812 listener.poolChanged( this, e ); 813 } 814 catch ( Exception e1 ) 815 { 816 logger.debug( "error when notifying " + listener + " of pool modification" ); } 818 } 819 } 820 821 822 828 private void elementAdded( LDAPModelEvent e ) 829 { 830 SchemaElement schemaElement = ( SchemaElement ) e.getNewValue(); 831 832 if ( schemaElement instanceof AttributeType ) 833 { 834 addAttributeType( ( AttributeType ) schemaElement ); 835 } 836 else if ( schemaElement instanceof ObjectClass ) 837 { 838 addObjectClass( ( ObjectClass ) schemaElement ); 839 } 840 } 841 842 843 849 private void attributeTypeModified( LDAPModelEvent e ) 850 { 851 AttributeType oldAttributeType = ( AttributeType ) e.getOldValue(); 852 AttributeType newAttributeType = ( AttributeType ) e.getNewValue(); 853 854 String [] names = oldAttributeType.getNames(); 855 for ( int j = 0; j < names.length; j++ ) 856 { 857 attributeTypesMap.remove( names[j].toLowerCase() ); 858 } 859 attributeTypesMap.remove( oldAttributeType.getOid() ); 860 861 names = newAttributeType.getNames(); 862 for ( int j = 0; j < names.length; j++ ) 863 { 864 attributeTypesMap.put( names[j].toLowerCase(), newAttributeType ); 865 } 866 attributeTypesMap.put( newAttributeType.getOid(), newAttributeType ); 867 } 868 869 870 876 private void objectClassModified( LDAPModelEvent e ) 877 { 878 ObjectClass oldObjectClass = ( ObjectClass ) e.getOldValue(); 879 ObjectClass newObjectClass = ( ObjectClass ) e.getNewValue(); 880 881 String [] names = oldObjectClass.getNames(); 882 for ( int j = 0; j < names.length; j++ ) 883 { 884 objectClassesMap.remove( names[j].toLowerCase() ); 885 } 886 objectClassesMap.remove( oldObjectClass.getOid() ); 887 888 names = newObjectClass.getNames(); 889 for ( int j = 0; j < names.length; j++ ) 890 { 891 objectClassesMap.put( names[j].toLowerCase(), newObjectClass ); 892 } 893 objectClassesMap.put( newObjectClass.getOid(), newObjectClass ); 894 } 895 896 897 903 private void elementRemoved( LDAPModelEvent e ) 904 { 905 SchemaElement schemaElement = ( SchemaElement ) e.getOldValue(); 906 907 if ( schemaElement instanceof AttributeType ) 908 { 909 removeAttributeType( ( AttributeType ) schemaElement ); 910 } 911 else if ( schemaElement instanceof ObjectClass ) 912 { 913 removeObjectClass( ( ObjectClass ) schemaElement ); 914 } 915 } 916 917 918 924 private void addAttributeType( AttributeType at ) 925 { 926 String [] names = at.getNames(); 927 for ( int j = 0; j < names.length; j++ ) 928 { 929 attributeTypesMap.put( names[j].toLowerCase(), at ); 930 } 931 attributeTypesMap.put( at.getOid(), at ); 932 attributeTypes.add( at ); 933 } 934 935 936 942 private void removeAttributeType( AttributeType at ) 943 { 944 String [] names = at.getNames(); 945 for ( int j = 0; j < names.length; j++ ) 946 { 947 attributeTypesMap.remove( names[j].toLowerCase() ); 948 } 949 attributeTypesMap.remove( at.getOid() ); 950 attributeTypes.remove( at ); 951 } 952 953 954 960 private void addObjectClass( ObjectClass oc ) 961 { 962 String [] names = oc.getNames(); 963 for ( int j = 0; j < names.length; j++ ) 964 { 965 objectClassesMap.put( names[j].toLowerCase(), oc ); 966 } 967 objectClassesMap.put( oc.getOid(), oc ); 968 objectClasses.add( oc ); 969 } 970 971 972 978 private void removeObjectClass( ObjectClass oc ) 979 { 980 String [] names = oc.getNames(); 981 for ( int j = 0; j < names.length; j++ ) 982 { 983 objectClassesMap.remove( names[j].toLowerCase() ); 984 } 985 objectClassesMap.remove( oc.getOid() ); 986 objectClasses.remove( oc ); 987 } 988 989 990 996 private File getSchemaPoolFile() 997 { 998 return Activator.getDefault().getStateLocation().append( "schemaPool.xml" ).toFile(); } 1000} 1001 | Popular Tags |