KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > model > SchemaPool


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
21 package org.apache.directory.ldapstudio.schemas.model;
22
23
24 import java.io.File JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.FileReader JavaDoc;
27 import java.io.FileWriter JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
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 /**
49  * This class represents the Schema Pool.
50  *
51  * A pool of schema is a common repository for all the currently loaded
52  * schemas in LDAP Studio Schemas Editor Plugin.
53  */

54 public class SchemaPool implements SchemaListener
55 {
56     /** The logger */
57     private static Logger logger = Logger.getLogger( SchemaPool.class );
58
59     /** The SchemaPool instance */
60     private static SchemaPool schemaPool;
61
62     /** The Listeners List */
63     private List JavaDoc<PoolListener> listeners;
64
65     /** The Schema List */
66     private List JavaDoc<Schema> schemaList;
67
68     /** The Attribute Type List */
69     private List JavaDoc<AttributeType> attributeTypes;
70
71     /** The Object Class List */
72     private List JavaDoc<ObjectClass> objectClasses;
73
74     /** The Attribute Type Map*/
75     private Map JavaDoc<String JavaDoc, AttributeType> attributeTypesMap;
76
77     /** The Object Class Map*/
78     private Map JavaDoc<String JavaDoc, ObjectClass> objectClassesMap;
79
80     /** The Schemas Tag */
81     private static final String JavaDoc SCHEMAS_TAG = "Schemas"; //$NON-NLS-1$
82

83     /** The Schema Tag */
84     private static final String JavaDoc SCHEMA_TAG = "Schema"; //$NON-NLS-1$
85

86     /** the Path Tag */
87     private static final String JavaDoc PATH_TAG = "path"; //$NON-NLS-1$
88

89
90     /**
91      * Creates a new instance of SchemaPool.
92      */

93     private SchemaPool()
94     {
95         listeners = new ArrayList JavaDoc<PoolListener>();
96         schemaList = new ArrayList JavaDoc<Schema>();
97         attributeTypes = new ArrayList JavaDoc<AttributeType>();
98         objectClasses = new ArrayList JavaDoc<ObjectClass>();
99         attributeTypesMap = new HashMap JavaDoc<String JavaDoc, AttributeType>();
100         objectClassesMap = new HashMap JavaDoc<String JavaDoc, ObjectClass>();
101     }
102
103
104     /**
105      * Returns the unique initialized pool.
106      *
107      * @return
108      * the pool
109      */

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     /**
126      * Loads the Schemas (Core and User Schemas)
127      */

128     private void loadSchemas()
129     {
130         schemaPool.loadCoreSchemas();
131         schemaPool.loadUserSchemas();
132     }
133
134
135     /**
136      * Loads the Core Schemas Files.
137      */

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     /**
154      * Loads the User Schemas Files.
155      */

156     public void loadUserSchemas()
157     {
158         try
159         {
160             FileReader JavaDoc reader = new FileReader JavaDoc( 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 ) ), //$NON-NLS-1$
168
Schema.SchemaType.userSchema, false );
169                 }
170                 catch ( SchemaCreationException e )
171                 {
172                     logger.debug( "Error loading schema " + child.getString( PATH_TAG ) + " in the pool." ); //$NON-NLS-1$ //$NON-NLS-2$
173
}
174
175             }
176         }
177         catch ( FileNotFoundException JavaDoc e )
178         {
179             logger.debug( "Error when loading previously opened schemas.", e ); //$NON-NLS-1$
180
}
181         catch ( WorkbenchException e )
182         {
183             logger.debug( "Error when loading previously opened schemas.", e ); //$NON-NLS-1$
184
}
185     }
186
187
188     /**
189      * Saves the User Schemas Paths to a XML File.
190      */

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 JavaDoc writer = new FileWriter JavaDoc( getSchemaPoolFile() );
207             memento.save( writer );
208             writer.close();
209         }
210         catch ( IOException JavaDoc e )
211         {
212             logger.debug( "Error when saving opened schemas.", e ); //$NON-NLS-1$
213
}
214     }
215
216
217     /**
218      * Loads the Core Schemas From the plugin's Bundle.
219      */

220     private void loadCoreSchemasFromBundle()
221     {
222         URL JavaDoc urlApache = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/apache.schema" ); //$NON-NLS-1$
223
URL JavaDoc urlApachedns = Platform.getBundle( Activator.PLUGIN_ID )
224             .getResource( "ressources/schemas/apachedns.schema" ); //$NON-NLS-1$
225
URL JavaDoc urlApachemeta = Platform.getBundle( Activator.PLUGIN_ID ).getResource(
226             "ressources/schemas/apachemeta.schema" ); //$NON-NLS-1$
227
URL JavaDoc urlAutofs = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/autofs.schema" ); //$NON-NLS-1$
228
URL JavaDoc urlCollective = Platform.getBundle( Activator.PLUGIN_ID ).getResource(
229             "ressources/schemas/collective.schema" ); //$NON-NLS-1$
230
URL JavaDoc urlCorba = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/corba.schema" ); //$NON-NLS-1$
231
URL JavaDoc urlCore = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/core.schema" ); //$NON-NLS-1$
232
URL JavaDoc urlCosine = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/cosine.schema" ); //$NON-NLS-1$
233
URL JavaDoc urlDhcp = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/dhcp.schema" ); //$NON-NLS-1$
234
URL JavaDoc urlInetorgperson = Platform.getBundle( Activator.PLUGIN_ID ).getResource(
235             "ressources/schemas/inetorgperson.schema" ); //$NON-NLS-1$
236
URL JavaDoc urlJava = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/java.schema" ); //$NON-NLS-1$
237
URL JavaDoc urlKrb5kdc = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/krb5kdc.schema" ); //$NON-NLS-1$
238
URL JavaDoc urlNis = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/nis.schema" ); //$NON-NLS-1$
239
URL JavaDoc urlSamba = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/samba.schema" ); //$NON-NLS-1$
240
URL JavaDoc urlSystem = Platform.getBundle( Activator.PLUGIN_ID ).getResource( "ressources/schemas/system.schema" ); //$NON-NLS-1$
241

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     /**
261      * Loads the Core Schemas Files from the Location specified in the preferences.
262      */

263     private void loadCoreSchemasFromSpecifiedLocation()
264     {
265         IPreferenceStore store = Activator.getDefault().getPreferenceStore();
266         String JavaDoc specificPath = store.getString( PluginConstants.PREFS_SCHEMAS_EDITOR_SPECIFIC_CORE_DIRECTORY );
267
268         File JavaDoc dir = new File JavaDoc( specificPath );
269         String JavaDoc sCurPath = dir.getAbsolutePath() + File.separator;
270         // Get the directorie entries
271
String JavaDoc[] safiles = dir.list();
272         if ( safiles != null )
273         {
274             for ( String JavaDoc safile : safiles )
275             {
276                 File JavaDoc curFile = new File JavaDoc( sCurPath + safile );
277                 if ( !curFile.isDirectory() )
278                 {
279                     try
280                     {
281                         URL JavaDoc fileURL = curFile.toURL();
282                         if ( Schema.URLtoFileName( fileURL ) != null )
283                         {
284                             schemaPool.addSchema( fileURL, Schema.SchemaType.coreSchema, false );
285                         }
286                     }
287                     catch ( MalformedURLException JavaDoc e )
288                     {
289                         logger.debug( "error whith the content of the specified core schema directory" ); //$NON-NLS-1$
290
}
291                 }
292             }
293         }
294     }
295
296
297     /**
298      * Returns all the schemas contained in the pool.
299      *
300      * @return
301      * the schemas stored in a Schema array
302      */

303     public Schema[] getSchemas()
304     {
305         return schemaList.toArray( new Schema[0] );
306     }
307
308
309     /**
310      * Returns the List of Object Classes.
311      *
312      * @return
313      * the List of Object Classes
314      */

315     public List JavaDoc<ObjectClass> getObjectClasses()
316     {
317         return objectClasses;
318     }
319
320
321     /**
322      * Return the List of Attribute Types.
323      *
324      * @return
325      * the List of Attribute Types
326      */

327     public List JavaDoc<AttributeType> getAttributeTypes()
328     {
329         return attributeTypes;
330     }
331
332
333     /**
334      * Returns the Object Classes as a Map.
335      *
336      * @return
337      * the Object Classes as a Map
338      */

339     public Map JavaDoc<String JavaDoc, ObjectClass> getObjectClassesAsMap()
340     {
341         return objectClassesMap;
342     }
343
344
345     /**
346      * Gets the Attribute Types as a Map.
347      *
348      * @return
349      * the Attribute Types as a Map
350      */

351     public Map JavaDoc<String JavaDoc, AttributeType> getAttributeTypesAsMap()
352     {
353         return attributeTypesMap;
354     }
355
356
357     /**
358      * Get the Schema Elements as a Map
359      *
360      * @return
361      * the Schema Elements as a Map
362      */

363     public Map JavaDoc<String JavaDoc, SchemaElement> getSchemaElements()
364     {
365         Map JavaDoc<String JavaDoc, SchemaElement> elementsTable = new HashMap JavaDoc<String JavaDoc, SchemaElement>();
366
367         elementsTable.putAll( attributeTypesMap );
368         elementsTable.putAll( objectClassesMap );
369
370         return elementsTable;
371     }
372
373
374     /**
375      * Returns the schema specified by the given name.
376      *
377      * @param name
378      * the name of the schema to find
379      * @return
380      * the schema specified by the given name.
381      */

382     public Schema getSchema( String JavaDoc 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     /**
397      * Tests if a schema with the given name is inside the pool.
398      *
399      * @param name
400      * the name of the schema
401      * @return
402      * true if inside
403      */

404     public boolean containsSchema( String JavaDoc name )
405     {
406         return ( getSchema( name ) != null );
407     }
408
409
410     /**
411      * Tests if the given schema is inside the pool.
412      *
413      * @param schema
414      * the name of the schema to test
415      * @return
416      * true if inside, false if not
417      */

418     public boolean containsSchema( Schema schema )
419     {
420         return schemaList.contains( schema );
421     }
422
423
424     /**
425      * Tests if an objectClass with the given name is inside the pool.
426      *
427      * @param name
428      * the name of the object class to test
429      * @return
430      * true if inside, false if not
431      */

432     public boolean containsObjectClass( String JavaDoc name )
433     {
434         return objectClassesMap.containsKey( name.toLowerCase() );
435     }
436
437
438     /**
439      * Tests if an attribute type with the given name is inside the pool.
440      *
441      * @param name
442      * the name of the attribute type to test
443      * @return
444      * true if inside, false if not
445      */

446     public boolean containsAttributeType( String JavaDoc name )
447     {
448         return attributeTypesMap.containsKey( name.toLowerCase() );
449     }
450
451
452     /**
453      * Tests if an element with the given name is inside the pool.
454      *
455      * @param name
456      * the name of the eleme,t to test
457      * @return
458      * true if inside, false if not
459      */

460     public boolean containsSchemaElement( String JavaDoc name )
461     {
462         return getSchemaElements().containsKey( name.toLowerCase() );
463     }
464
465
466     /**
467      * Tests if the given object class is inside the pool.
468      *
469      * @param objectClass
470      * the objectClass to test
471      * @return
472      * if inside the pool, false if not
473      */

474     public boolean containsObjectClass( ObjectClass objectClass )
475     {
476         return objectClasses.contains( objectClass );
477     }
478
479
480     /**
481      * Tests if the given attributeType is inside the pool.
482      *
483      * @param attributeType
484      * the attributeType to test
485      * @return
486      * if inside the pool, false if not
487      */

488     public boolean containsAttributeType( AttributeType attributeType )
489     {
490         return attributeTypes.contains( attributeType );
491     }
492
493
494     /**
495      * Tests if the given element exists in the pool.
496      *
497      * @param schemaElement
498      * the Schema Element to test
499      * @return
500      * if inside the pool, false if not
501      */

502     public boolean containsSchemaElement( SchemaElement schemaElement )
503     {
504         return getSchemaElements().containsKey( schemaElement );
505     }
506
507
508     /**
509      * Returns the object class corresponding to the given name.
510      *
511      * @param name
512      * the name of the object class to return
513      * @return
514      * null if the name is not mapped
515      */

516     public ObjectClass getObjectClass( String JavaDoc name )
517     {
518
519         return objectClassesMap.get( name.toLowerCase() );
520     }
521
522
523     /**
524      * Returns the attribute type corresponding to the given name.
525      *
526      * @param name
527      * the name of the attriute type to return
528      * @return
529      * null if the name is not mapped
530      */

531     public AttributeType getAttributeType( String JavaDoc name )
532     {
533         return attributeTypesMap.get( name.toLowerCase() );
534     }
535
536
537     /**
538      * Adds a schema to the pool.
539      *
540      * @param s
541      * the schema to add
542      *
543      * @param notifyListeners
544      * a boolean to indicate if the listeners need to be notified
545      */

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     /**
577      * Adds a new schema to the Schema Pool.
578      *
579      * @param name
580      * the name of the new schema
581      */

582     public void addNewSchema( String JavaDoc name )
583     {
584         addSchema( new Schema( SchemaType.userSchema, name ), true );
585     }
586
587
588     /**
589      * Adds a schema to the Schema Pool.
590      *
591      * @param url
592      * the url of the schema
593      */

594     public void addSchema( URL JavaDoc url )
595     {
596         addSchema( url, SchemaType.userSchema, true );
597     }
598
599
600     /**
601      * Adds an already existing schema into the pool (load the schema from a file)
602      * @param url the URL to the .schema file
603      * @param type the schema type
604      * @return the schema that has been added to the pool, null if not added or already in the pool
605      * @throws SchemaCreationException if no schema was found at the specified
606      * URL or if any error occurs during its initialization.
607      */

608     /**
609      * Adds a schema to the Schema Pool.
610      *
611      * @param url
612      * the url of the schema
613      * @param type
614      * the type of the schema
615      * @param notifyListeners
616      * true if the listeners should be notified
617      */

618     private void addSchema( URL JavaDoc url, SchemaType type, boolean notifyListeners )
619     {
620         try
621         {
622             addSchema( new Schema( url, type ), notifyListeners );
623         }
624         catch ( SchemaCreationException e )
625         {
626             // TODO Auto-generated catch block
627
e.printStackTrace();
628         }
629     }
630
631
632     /**
633      * Removes a schema from the pool.
634      *
635      * @param s
636      * the schema to be removed
637      */

638     public void removeSchema( Schema s )
639     {
640         removeSchema( s, true );
641     }
642
643
644     /**
645      * Removes a schema from the pool.
646      *
647      * @param s
648      * the schema to be removed
649      * @param notifyListeners
650      * a boolean to indicate if the listeners need to be notified
651      */

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     /**
681      * Saves all the schemas contained in the pool
682      * @throws Exception if error during the writting process
683      */

684     public void saveAll() throws Exception JavaDoc
685     {
686         saveAll( false );
687     }
688
689
690     /**
691      * Saves all the schemas contained in the pool
692      * @param askForConfirmation if true, will ask form confirmation before saving
693      * @throws Exception if error during the writting process
694      */

695     public void saveAll( boolean askForConfirmation ) throws Exception JavaDoc
696     {
697         for ( Schema schema : schemaList )
698         {
699             schema.save( askForConfirmation );
700         }
701     }
702
703
704     /**
705      * Reloads the Schema Pool.
706      */

707     public void reload()
708     {
709         // Removing Core Schemas
710
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         // Loading Schemas
720
schemaPool.loadCoreSchemas();
721
722         // Notifying Listeners
723
for ( PoolListener listener : listeners )
724         {
725             listener.poolChanged( this, new LDAPModelEvent( Reason.PoolReloaded ) );
726         }
727     }
728
729
730     /**
731      * Adds a listener to the Schema Pool.
732      *
733      * @param listener
734      * the listener to add
735      */

736     public void addListener( PoolListener listener )
737     {
738         if ( !listeners.contains( listener ) )
739             listeners.add( listener );
740     }
741
742
743     /**
744      * Removed a Lister from the Schema Pool.
745      *
746      * @param listener
747      * the listener to remove
748      */

749     public void removeListener( PoolListener listener )
750     {
751         listeners.remove( listener );
752     }
753
754
755     /**
756      * Notifies listeners of Schema Pool notification
757      *
758      * @param reason
759      * the reson
760      * @param sc
761      * the schema
762      */

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 JavaDoc e )
772             {
773                 logger.debug( "error when notifying " + listener + " of pool modification" ); //$NON-NLS-1$ //$NON-NLS-2$
774
}
775         }
776     }
777
778
779     /* (non-Javadoc)
780      * @see org.apache.directory.ldapstudio.schemas.model.SchemaListener#schemaChanged(org.apache.directory.ldapstudio.schemas.model.Schema, org.apache.directory.ldapstudio.schemas.model.LDAPModelEvent)
781      */

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 JavaDoc e1 )
815             {
816                 logger.debug( "error when notifying " + listener + " of pool modification" ); //$NON-NLS-1$ //$NON-NLS-2$
817
}
818         }
819     }
820
821
822     /**
823      * Updates the SchemaPool after an event of type ATAdded or case OCAdded.
824      *
825      * @param e
826      * the associated event
827      */

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     /**
844      * Updates the SchemaPool after an event of type ATModified.
845      *
846      * @param e
847      * the associated event
848      */

849     private void attributeTypeModified( LDAPModelEvent e )
850     {
851         AttributeType oldAttributeType = ( AttributeType ) e.getOldValue();
852         AttributeType newAttributeType = ( AttributeType ) e.getNewValue();
853
854         String JavaDoc[] 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     /**
871      * Updates the SchemaPool after an event of type OCModified.
872      *
873      * @param e
874      * the associated event
875      */

876     private void objectClassModified( LDAPModelEvent e )
877     {
878         ObjectClass oldObjectClass = ( ObjectClass ) e.getOldValue();
879         ObjectClass newObjectClass = ( ObjectClass ) e.getNewValue();
880
881         String JavaDoc[] 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     /**
898      * Updates the SchemaPool after an event of type ATRemoved or case OCRemoved.
899      *
900      * @param e
901      * the associated event
902      */

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     /**
919      * Adds an attribute type.
920      *
921      * @param at
922      * the attribute type to add
923      */

924     private void addAttributeType( AttributeType at )
925     {
926         String JavaDoc[] 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     /**
937      * Removes an attribute type.
938      *
939      * @param at
940      * the attribute type to remove
941      */

942     private void removeAttributeType( AttributeType at )
943     {
944         String JavaDoc[] 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     /**
955      * Adds an object class.
956      *
957      * @param oc
958      * the object class to add
959      */

960     private void addObjectClass( ObjectClass oc )
961     {
962         String JavaDoc[] 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     /**
973      * Removes an object class
974      *
975      * @param oc
976      * the object class to remove
977      */

978     private void removeObjectClass( ObjectClass oc )
979     {
980         String JavaDoc[] 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     /**
991      * Gets the Schema Pool File (where is store information about the loaded User Schemas).
992      *
993      * @return
994      * the Schema Pool File
995      */

996     private File JavaDoc getSchemaPoolFile()
997     {
998         return Activator.getDefault().getStateLocation().append( "schemaPool.xml" ).toFile(); //$NON-NLS-1$
999
}
1000}
1001
Popular Tags