Coverage Report - pt.digitalis.dif.dem.DEMRegistryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DEMRegistryImpl
0%
0/112
0%
0/56
3,889
 
 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  0
 public class DEMRegistryImpl implements IDEMRegistry {
 38  
 
 39  
     /** The registered applications container (the key is the ID). */
 40  0
     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  0
     static private Set<String> overridenStages = new HashSet<String>();
 47  
 
 48  
     /** The registered providers container (the key is the ID). */
 49  0
     static private Map<String, IProvider> providers = new HashMap<String, IProvider>();
 50  
 
 51  
     /** The registry object. */
 52  0
     static private DEMRegistryImpl registry = new DEMRegistryImpl();
 53  
 
 54  
     /** The registered services container (the key is the ID). */
 55  0
     static private Map<String, IService> services = new HashMap<String, IService>();
 56  
 
 57  
     /** The registered stages container (the key is the ID). */
 58  0
     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  0
             application = (IApplication) clazz.getClassInstance();
 74  0
             applications.put(application.getID(), application);
 75  
 
 76  0
             return application;
 77  
 
 78  
         }
 79  0
         catch (ClassNotFoundException e)
 80  
         {
 81  0
             e.printStackTrace();
 82  
 
 83  0
             return null;
 84  
         }
 85  0
         catch (InstantiationException e)
 86  
         {
 87  0
             e.printStackTrace();
 88  
 
 89  0
             return null;
 90  
         }
 91  0
         catch (IllegalAccessException e)
 92  
         {
 93  0
             e.printStackTrace();
 94  
 
 95  0
             return null;
 96  
         }
 97  0
         catch (ResourceNotFoundException e)
 98  
         {
 99  0
             e.printStackTrace();
 100  
 
 101  0
             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  0
         if (entityType.equals(Entity.PROVIDER))
 118  0
             return addProvider(clazz);
 119  0
         if (entityType.equals(Entity.APPLICATION))
 120  0
             return addApplication(clazz);
 121  0
         if (entityType.equals(Entity.SERVICE))
 122  0
             return addService(clazz);
 123  0
         if (entityType.equals(Entity.STAGE))
 124  0
             return addStage(clazz);
 125  
 
 126  0
         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  0
             provider = (IProvider) clazz.getClassInstance();
 145  0
             providers.put(provider.getID(), provider);
 146  
 
 147  0
             return provider;
 148  
 
 149  
         }
 150  0
         catch (ClassNotFoundException e)
 151  
         {
 152  0
             e.printStackTrace();
 153  
 
 154  0
             return null;
 155  
         }
 156  0
         catch (InstantiationException e)
 157  
         {
 158  0
             e.printStackTrace();
 159  
 
 160  0
             return null;
 161  
         }
 162  0
         catch (IllegalAccessException e)
 163  
         {
 164  0
             e.printStackTrace();
 165  
 
 166  0
             return null;
 167  
         }
 168  0
         catch (ResourceNotFoundException e)
 169  
         {
 170  0
             e.printStackTrace();
 171  
 
 172  0
             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  0
             service = (IService) clazz.getClassInstance();
 191  0
             services.put(service.getID(), service);
 192  
 
 193  0
             return service;
 194  
 
 195  
         }
 196  0
         catch (ClassNotFoundException e)
 197  
         {
 198  0
             e.printStackTrace();
 199  
 
 200  0
             return null;
 201  
         }
 202  0
         catch (InstantiationException e)
 203  
         {
 204  0
             e.printStackTrace();
 205  
 
 206  0
             return null;
 207  
         }
 208  0
         catch (IllegalAccessException e)
 209  
         {
 210  0
             e.printStackTrace();
 211  
 
 212  0
             return null;
 213  
         }
 214  0
         catch (ResourceNotFoundException e)
 215  
         {
 216  0
             e.printStackTrace();
 217  
 
 218  0
             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  0
             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  0
             if (!overridenStages.contains(stage.getID()))
 241  0
                 stages.put(stage.getID(), stage);
 242  
 
 243  
             // If the stage should override another one...
 244  0
             String overrideStageList = stage.getOverridesStageID();
 245  0
             if (overrideStageList != null)
 246  
             {
 247  0
                 String[] overrideStageArray = overrideStageList.split(",");
 248  
 
 249  0
                 for (String overrideStage: overrideStageArray)
 250  
                 {
 251  0
                     overrideStage = overrideStage.trim().toLowerCase();
 252  
 
 253  0
                     DIFLogger.getLogger().debug(
 254  0
                             "Stage \"" + stage.getName() + " (" + stage.getID() + ")\" has overriden stage \""
 255  0
                                     + overrideStage + "\"");
 256  
 
 257  0
                     stages.put(overrideStage, stage);
 258  0
                     overridenStages.add(overrideStage);
 259  
                 }
 260  
             }
 261  
 
 262  0
             return stage;
 263  
 
 264  
         }
 265  0
         catch (ClassNotFoundException e)
 266  
         {
 267  0
             e.printStackTrace();
 268  
 
 269  0
             return null;
 270  
         }
 271  0
         catch (InstantiationException e)
 272  
         {
 273  0
             e.printStackTrace();
 274  
 
 275  0
             return null;
 276  
         }
 277  0
         catch (IllegalAccessException e)
 278  
         {
 279  0
             e.printStackTrace();
 280  
 
 281  0
             return null;
 282  
         }
 283  0
         catch (ResourceNotFoundException e)
 284  
         {
 285  0
             e.printStackTrace();
 286  
 
 287  0
             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  0
         return applications;
 300  
     }
 301  
 
 302  
     /**
 303  
      * Returns the default provider.
 304  
      * 
 305  
      * @return the default provider
 306  
      */
 307  
     static public IProvider getDefaultProvider()
 308  
     {
 309  0
         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  0
         if (defaultProvider == null)
 320  
             // If no default provider has been provided use the first existent one
 321  0
             defaultProvider = providers.keySet().iterator().next();
 322  
 
 323  0
         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  0
         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  0
         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  0
         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  0
         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  0
         defaultProvider = providerID;
 377  0
     }
 378  
 
 379  
     /**
 380  
      * Private constructor to prevent multiple instantiation (Singleton pattern).
 381  
      */
 382  0
     private DEMRegistryImpl()
 383  0
     {}
 384  
 
 385  
     /** @see pt.digitalis.dif.dem.interfaces.IDEMRegistry#getApplication(java.lang.String) */
 386  
     public IApplication getApplication(String applicationID)
 387  
     {
 388  0
         if (applicationID == null)
 389  0
             return null;
 390  
         else
 391  0
             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  0
         if (providerID == null)
 398  0
             return null;
 399  
         else
 400  0
             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  0
         if (serviceID == null)
 407  0
             return null;
 408  
         else
 409  0
             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  0
         if (stageID == null)
 416  0
             return null;
 417  
         else
 418  0
             return stages.get(stageID.toLowerCase());
 419  
     }
 420  
 }