View Javadoc

1   /**
2    * - Digitalis Internal Framework v2.0 - (C) 2007, Digitalis Informatica. Distribuicao e Gestao de Informatica, Lda.
3    * Estrada de Paco de Arcos num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999
4    * http://www.digitalis.pt
5    */
6   package pt.digitalis.dif.dem;
7   
8   import java.util.HashMap;
9   import java.util.HashSet;
10  import java.util.Map;
11  import java.util.Set;
12  
13  import pt.digitalis.dif.dem.interfaces.IApplication;
14  import pt.digitalis.dif.dem.interfaces.IDEMRegistry;
15  import pt.digitalis.dif.dem.interfaces.IEntity;
16  import pt.digitalis.dif.dem.interfaces.IProvider;
17  import pt.digitalis.dif.dem.interfaces.IService;
18  import pt.digitalis.dif.dem.interfaces.IStage;
19  import pt.digitalis.dif.utils.logging.DIFLogger;
20  import pt.digitalis.utils.bytecode.holders.ClassHolder;
21  import pt.digitalis.utils.inspection.exception.ResourceNotFoundException;
22  
23  /**
24   * DIF entity model (DEM) Registry class. This class is populated by the code generation module. This module reads the
25   * DEM annotations in classpath's classes, generates the appropriate code and populates the Registry. Since only one DEM
26   * Registry is needed, the class implements the Singleton pattern. <b>Note to class maintainers:</b> all the attribute
27   * accesses - even for setting/removing values - should be made through the supplied inspectors
28   * <code>getProviders()</code>, <code>getApplications()</code>, <code>getServices()</code> and <code>getStages()</code>.
29   * Accessing directly to the attributes does not guarantee that they are properly initialized. This need is due to the
30   * fact that the code generation module inserts the proper attribute initialization code in the aforementioned getters,
31   * so accessing through them is the only way to guarantee that they contain the proper data.
32   * 
33   * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a>
34   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
35   * @created 2007/04/24
36   */
37  public class DEMRegistryImpl implements IDEMRegistry {
38  
39      /** The registered applications container (the key is the ID). */
40      static private Map<String, IApplication> applications = new HashMap<String, IApplication>();
41  
42      /** The default provider. */
43      static private String defaultProvider;
44  
45      /** The overridden registered stages container. */
46      static private Set<String> overridenStages = new HashSet<String>();
47  
48      /** The registered providers container (the key is the ID). */
49      static private Map<String, IProvider> providers = new HashMap<String, IProvider>();
50  
51      /** The registry object. */
52      static private DEMRegistryImpl registry = new DEMRegistryImpl();
53  
54      /** The registered services container (the key is the ID). */
55      static private Map<String, IService> services = new HashMap<String, IService>();
56  
57      /** The registered stages container (the key is the ID). */
58      static private Map<String, IStage> stages = new HashMap<String, IStage>();
59  
60      /**
61       * Registers an application.
62       * 
63       * @param clazz
64       *            the application class to register
65       * @return the added application
66       */
67      static public IApplication addApplication(ClassHolder clazz)
68      {
69          IApplication application;
70  
71          try
72          {
73              application = (IApplication) clazz.getClassInstance();
74              applications.put(application.getID(), application);
75  
76              return application;
77  
78          }
79          catch (ClassNotFoundException e)
80          {
81              e.printStackTrace();
82  
83              return null;
84          }
85          catch (InstantiationException e)
86          {
87              e.printStackTrace();
88  
89              return null;
90          }
91          catch (IllegalAccessException e)
92          {
93              e.printStackTrace();
94  
95              return null;
96          }
97          catch (ResourceNotFoundException e)
98          {
99              e.printStackTrace();
100 
101             return null;
102         }
103     }
104 
105     /**
106      * Registers a provider.
107      * 
108      * @param entityType
109      *            the entity Type to be added
110      * @param clazz
111      *            the provider class to register
112      * @return the added entity
113      */
114     static public IEntity addEntity(Entity entityType, ClassHolder clazz)
115     {
116 
117         if (entityType.equals(Entity.PROVIDER))
118             return addProvider(clazz);
119         if (entityType.equals(Entity.APPLICATION))
120             return addApplication(clazz);
121         if (entityType.equals(Entity.SERVICE))
122             return addService(clazz);
123         if (entityType.equals(Entity.STAGE))
124             return addStage(clazz);
125 
126         return null;
127     }
128 
129     // --- Maintenance methods for DIF's private use ---
130 
131     /**
132      * Registers a provider.
133      * 
134      * @param clazz
135      *            the provider class to register
136      * @return the added provider
137      */
138     static public IProvider addProvider(ClassHolder clazz)
139     {
140         IProvider provider;
141 
142         try
143         {
144             provider = (IProvider) clazz.getClassInstance();
145             providers.put(provider.getID(), provider);
146 
147             return provider;
148 
149         }
150         catch (ClassNotFoundException e)
151         {
152             e.printStackTrace();
153 
154             return null;
155         }
156         catch (InstantiationException e)
157         {
158             e.printStackTrace();
159 
160             return null;
161         }
162         catch (IllegalAccessException e)
163         {
164             e.printStackTrace();
165 
166             return null;
167         }
168         catch (ResourceNotFoundException e)
169         {
170             e.printStackTrace();
171 
172             return null;
173         }
174 
175     }
176 
177     /**
178      * Registers a service.
179      * 
180      * @param clazz
181      *            the service class to register
182      * @return the added service
183      */
184     static public IService addService(ClassHolder clazz)
185     {
186         IService service;
187 
188         try
189         {
190             service = (IService) clazz.getClassInstance();
191             services.put(service.getID(), service);
192 
193             return service;
194 
195         }
196         catch (ClassNotFoundException e)
197         {
198             e.printStackTrace();
199 
200             return null;
201         }
202         catch (InstantiationException e)
203         {
204             e.printStackTrace();
205 
206             return null;
207         }
208         catch (IllegalAccessException e)
209         {
210             e.printStackTrace();
211 
212             return null;
213         }
214         catch (ResourceNotFoundException e)
215         {
216             e.printStackTrace();
217 
218             return null;
219         }
220 
221     }
222 
223     /**
224      * Registers a stage.
225      * 
226      * @param clazz
227      *            the stage class to register
228      * @return the added stage
229      */
230     static public IStage addStage(ClassHolder clazz)
231     {
232         IStage stage;
233 
234         try
235         {
236             stage = (IStage) clazz.getClassInstance();
237 
238             // Stage override logic: Once a stage has been overridden it it not added. This must be done this way since
239             // we cannot be sure of the order in which the stages are added to the registry
240             if (!overridenStages.contains(stage.getID()))
241                 stages.put(stage.getID(), stage);
242 
243             // If the stage should override another one...
244             String overrideStageList = stage.getOverridesStageID();
245             if (overrideStageList != null)
246             {
247                 String[] overrideStageArray = overrideStageList.split(",");
248 
249                 for (String overrideStage: overrideStageArray)
250                 {
251                     overrideStage = overrideStage.trim().toLowerCase();
252 
253                     DIFLogger.getLogger().debug(
254                             "Stage \"" + stage.getName() + " (" + stage.getID() + ")\" has overriden stage \""
255                                     + overrideStage + "\"");
256 
257                     stages.put(overrideStage, stage);
258                     overridenStages.add(overrideStage);
259                 }
260             }
261 
262             return stage;
263 
264         }
265         catch (ClassNotFoundException e)
266         {
267             e.printStackTrace();
268 
269             return null;
270         }
271         catch (InstantiationException e)
272         {
273             e.printStackTrace();
274 
275             return null;
276         }
277         catch (IllegalAccessException e)
278         {
279             e.printStackTrace();
280 
281             return null;
282         }
283         catch (ResourceNotFoundException e)
284         {
285             e.printStackTrace();
286 
287             return null;
288         }
289 
290     }
291 
292     /**
293      * Returns the map of all registered applications.
294      * 
295      * @return the map of all registered applications
296      */
297     public static Map<String, IApplication> getApplications()
298     {
299         return applications;
300     }
301 
302     /**
303      * Returns the default provider.
304      * 
305      * @return the default provider
306      */
307     static public IProvider getDefaultProvider()
308     {
309         return providers.get(getDefaultProviderID());
310     }
311 
312     /**
313      * Returns the default provider id.
314      * 
315      * @return the default provider id
316      */
317     static public String getDefaultProviderID()
318     {
319         if (defaultProvider == null)
320             // If no default provider has been provided use the first existent one
321             defaultProvider = providers.keySet().iterator().next();
322 
323         return defaultProvider;
324     }
325 
326     /**
327      * Returns the map of all registered providers.
328      * 
329      * @return the map of all registered providers
330      */
331     public static Map<String, IProvider> getProviders()
332     {
333         return providers;
334     }
335 
336     /**
337      * Returns the DEMRegistryImpl instance (Singleton pattern).
338      * 
339      * @return the registry object
340      */
341     static public DEMRegistryImpl getRegistry()
342     {
343         return registry;
344     }
345 
346     /**
347      * Returns the map of all services registered.
348      * 
349      * @return the map of all services registered
350      */
351     public static Map<String, IService> getServices()
352     {
353         return services;
354     }
355 
356     // --- General use public methods (accessible through IDEMRegistry) ---
357 
358     /**
359      * Returns the map of all stages registered.
360      * 
361      * @return the map of all stages registered
362      */
363     static public Map<String, IStage> getStages()
364     {
365         return stages;
366     }
367 
368     /**
369      * Returns the default provider.
370      * 
371      * @param providerID
372      *            the new default provider
373      */
374     static public void setDefaultProvider(String providerID)
375     {
376         defaultProvider = providerID;
377     }
378 
379     /**
380      * Private constructor to prevent multiple instantiation (Singleton pattern).
381      */
382     private DEMRegistryImpl()
383     {}
384 
385     /** @see pt.digitalis.dif.dem.interfaces.IDEMRegistry#getApplication(java.lang.String) */
386     public IApplication getApplication(String applicationID)
387     {
388         if (applicationID == null)
389             return null;
390         else
391             return applications.get(applicationID.toLowerCase());
392     }
393 
394     /** @see pt.digitalis.dif.dem.interfaces.IDEMRegistry#getProvider(java.lang.String) */
395     public IProvider getProvider(String providerID)
396     {
397         if (providerID == null)
398             return null;
399         else
400             return providers.get((providerID.toLowerCase()));
401     }
402 
403     /** @see pt.digitalis.dif.dem.interfaces.IDEMRegistry#getService(java.lang.String) */
404     public IService getService(String serviceID)
405     {
406         if (serviceID == null)
407             return null;
408         else
409             return services.get(serviceID.toLowerCase());
410     }
411 
412     /** @see pt.digitalis.dif.dem.interfaces.IDEMRegistry#getStage(java.lang.String) */
413     public IStage getStage(String stageID)
414     {
415         if (stageID == null)
416             return null;
417         else
418             return stages.get(stageID.toLowerCase());
419     }
420 }