Coverage Report - pt.digitalis.dif.dem.managers.impl.RegistrationManagerImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RegistrationManagerImpl
0%
0/80
0%
0/18
1,37
 
 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.managers.impl;
 7  
 
 8  
 import java.util.HashMap;
 9  
 import java.util.Map;
 10  
 import java.util.Properties;
 11  
 
 12  
 import pt.digitalis.dif.dem.Entity;
 13  
 import pt.digitalis.dif.dem.interfaces.IStage;
 14  
 import pt.digitalis.dif.dem.managers.IRegistrationManager;
 15  
 import pt.digitalis.dif.dem.objects.ILicense;
 16  
 import pt.digitalis.dif.dem.objects.LicenseEditionType;
 17  
 import pt.digitalis.dif.exception.manager.RegistrationManagerException;
 18  
 import pt.digitalis.dif.ioc.DIFIoCRegistry;
 19  
 import pt.digitalis.dif.startup.DIFGeneralConfigurationParameters;
 20  
 import pt.digitalis.utils.config.IConfigurations;
 21  
 
 22  
 import com.google.inject.Inject;
 23  
 
 24  
 /**
 25  
  * Manages the DEM Entities registration process.
 26  
  * 
 27  
  * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
 28  
  * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a>
 29  
  * @created 2007/06/01
 30  
  */
 31  0
 public class RegistrationManagerImpl implements IRegistrationManager {
 32  
 
 33  
     /** Missing registrable information tag */
 34  0
     static private String MISSING_REGISTRABLE_INFO = "DIF auto-generated: Missing registrable information!";
 35  
 
 36  
     /** Map of registrations */
 37  0
     static private Map<String, ILicense> registrations = new HashMap<String, ILicense>();
 38  
 
 39  
     /** ID of the config area where the Repository will manage the registrations */
 40  
     final static String REPOSITORY_CONFIG_ID = "dif2";
 41  
 
 42  
     /** ID that identifies the key */
 43  
     final static String REPOSITORY_KEY_ID = "key";
 44  
 
 45  
     /** ID that identifies the name */
 46  
     final static String REPOSITORY_NAME_ID = "name";
 47  
 
 48  
     /** ID of the section where the Repository will manage the registrations */
 49  
     final static String REPOSITORY_SECTION_ID = "Registrations";
 50  
 
 51  
     /** The registration persistence repository. */
 52  
     @Inject
 53  
     IConfigurations configRepository;
 54  
 
 55  
     /**
 56  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#addToRegistry(pt.digitalis.dif.dem.Entity,
 57  
      *      java.lang.String, java.lang.String, boolean)
 58  
      */
 59  
     public void addToRegistry(Entity type, String entityID, String name, boolean registrable)
 60  
     {
 61  
 
 62  0
         String id = Entity.getID(type, entityID.toLowerCase());
 63  
 
 64  0
         ILicense license = DIFIoCRegistry.getRegistry().getImplementation(ILicense.class);
 65  0
         license.setName(name);
 66  0
         license.setRegistrable(registrable);
 67  
 
 68  
         // Checks if there are persistent configurations in the repository
 69  0
         license = checkRepositoryForPersistentRegistration(id, license);
 70  
 
 71  0
         registrations.put(id, license);
 72  0
     }
 73  
 
 74  
     /**
 75  
      * Checks the persistent repository for saved registrations.
 76  
      * 
 77  
      * @param id
 78  
      *            the entity id
 79  
      * @param license
 80  
      *            the default registration record
 81  
      * @return the updated registration record
 82  
      */
 83  
     private ILicense checkRepositoryForPersistentRegistration(String id, ILicense license)
 84  
     {
 85  
 
 86  0
         Properties props = configRepository.readConfiguration(REPOSITORY_CONFIG_ID, REPOSITORY_SECTION_ID);
 87  
 
 88  
         // Tries to get the registrations from the persistence configuration
 89  0
         Object savedKey = props.get(id + "." + REPOSITORY_KEY_ID);
 90  
 
 91  0
         if (savedKey != null)
 92  0
             license.register(savedKey.toString(), id);
 93  
 
 94  0
         return license;
 95  
     }
 96  
 
 97  
     /**
 98  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#getApplicationEdition(java.lang.String)
 99  
      */
 100  
     public LicenseEditionType getApplicationEdition(String applicationID)
 101  
     {
 102  0
         return getRegistrationRecord(Entity.APPLICATION, applicationID).getEdition();
 103  
     }
 104  
 
 105  
     /**
 106  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#getProviderEdition(java.lang.String)
 107  
      */
 108  
     public LicenseEditionType getProviderEdition(String providerID)
 109  
     {
 110  0
         return getRegistrationRecord(Entity.PROVIDER, providerID).getEdition();
 111  
     }
 112  
 
 113  
     /**
 114  
      * Utility accessor for the registration object for any entity. It provides the search and initialization for first
 115  
      * time access. Private use only since this is a contained helper class that encapsulates all specifics about entity
 116  
      * registration.
 117  
      * 
 118  
      * @param type
 119  
      *            the entity type to get
 120  
      * @param id
 121  
      *            the id of the Entity
 122  
      * @return the registration record for the entity
 123  
      */
 124  
     private ILicense getRegistrationRecord(Entity type, String id)
 125  
     {
 126  0
         String entityID = Entity.getID(type, id);
 127  0
         ILicense license = registrations.get(entityID);
 128  
 
 129  0
         if (license == null)
 130  
         {
 131  
             // The DIF DEM rules state that missing registration information translates to a public registration.
 132  
             // If the CodeGen and DEM parsing utility has not provided any registration information then this is a
 133  
             // unregistrable entity. Create a new license accordingly.
 134  
 
 135  0
             license = DIFIoCRegistry.getRegistry().getImplementation(ILicense.class);
 136  0
             license.setName(MISSING_REGISTRABLE_INFO);
 137  0
             license.setRegistrable(false);
 138  
 
 139  
             // Updates the Map with the registered record. This will only persist in memory until a register/unregister
 140  
             // command is issued. Only then it will be considered a true registration license. This one is temporary.
 141  0
             registrations.put(entityID, license);
 142  
         }
 143  
 
 144  0
         return license;
 145  
     }
 146  
 
 147  
     /**
 148  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#getServiceEdition(java.lang.String)
 149  
      */
 150  
     public LicenseEditionType getServiceEdition(String serviceID)
 151  
     {
 152  0
         return getRegistrationRecord(Entity.SERVICE, serviceID).getEdition();
 153  
     }
 154  
 
 155  
     /**
 156  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#getStageClientName(pt.digitalis.dif.dem.interfaces.IStage)
 157  
      */
 158  
     public String getStageClientName(IStage stage)
 159  
     {
 160  0
         String result = getRegistrationRecord(Entity.STAGE, stage.getID()).getName();
 161  0
         if (MISSING_REGISTRABLE_INFO.equals(result))
 162  
         {
 163  0
             result = getRegistrationRecord(Entity.SERVICE, stage.getService().getID()).getName();
 164  0
             if (MISSING_REGISTRABLE_INFO.equals(result))
 165  
             {
 166  0
                 result = getRegistrationRecord(Entity.APPLICATION, stage.getService().getApplication().getID())
 167  0
                         .getName();
 168  0
                 if (MISSING_REGISTRABLE_INFO.equals(result))
 169  
                 {
 170  0
                     result = getRegistrationRecord(Entity.PROVIDER,
 171  0
                             stage.getService().getApplication().getProvider().getID()).getName();
 172  0
                     if (MISSING_REGISTRABLE_INFO.equals(result))
 173  
                     {
 174  0
                         result = DIFGeneralConfigurationParameters.getInstance().getClient();
 175  
                     }
 176  
                 }
 177  
             }
 178  
         }
 179  0
         return result;
 180  
     }
 181  
 
 182  
     /**
 183  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#getStageEdition(java.lang.String)
 184  
      */
 185  
     public LicenseEditionType getStageEdition(String stageID)
 186  
     {
 187  0
         return getRegistrationRecord(Entity.STAGE, stageID).getEdition();
 188  
     }
 189  
 
 190  
     /**
 191  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#isApplicationRegistered(java.lang.String)
 192  
      */
 193  
     public boolean isApplicationRegistered(String applicationID)
 194  
     {
 195  0
         return getRegistrationRecord(Entity.APPLICATION, applicationID).isRegistered();
 196  
     }
 197  
 
 198  
     /**
 199  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#isApplicationRegistrable(java.lang.String)
 200  
      */
 201  
     public boolean isApplicationRegistrable(String applicationID)
 202  
     {
 203  0
         return getRegistrationRecord(Entity.APPLICATION, applicationID).isRegistrable();
 204  
     }
 205  
 
 206  
     /**
 207  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#isProviderRegistered(java.lang.String)
 208  
      */
 209  
     public boolean isProviderRegistered(String providerID)
 210  
     {
 211  0
         return getRegistrationRecord(Entity.PROVIDER, providerID).isRegistered();
 212  
     }
 213  
 
 214  
     /**
 215  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#isProviderRegistrable(java.lang.String)
 216  
      */
 217  
     public boolean isProviderRegistrable(String providerID)
 218  
     {
 219  0
         return getRegistrationRecord(Entity.PROVIDER, providerID).isRegistrable();
 220  
     }
 221  
 
 222  
     /**
 223  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#isServiceRegistered(java.lang.String)
 224  
      */
 225  
     public boolean isServiceRegistered(String serviceID)
 226  
     {
 227  0
         return getRegistrationRecord(Entity.SERVICE, serviceID).isRegistered();
 228  
     }
 229  
 
 230  
     /**
 231  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#isServiceRegistrable(java.lang.String)
 232  
      */
 233  
     public boolean isServiceRegistrable(String serviceID)
 234  
     {
 235  0
         return getRegistrationRecord(Entity.SERVICE, serviceID).isRegistrable();
 236  
     }
 237  
 
 238  
     /**
 239  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#isStageRegistered(java.lang.String)
 240  
      */
 241  
     public boolean isStageRegistered(String stageId)
 242  
     {
 243  0
         return getRegistrationRecord(Entity.STAGE, stageId).isRegistered();
 244  
     }
 245  
 
 246  
     /**
 247  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#isStageRegistrable(java.lang.String)
 248  
      */
 249  
     public boolean isStageRegistrable(String stageId)
 250  
     {
 251  0
         return getRegistrationRecord(Entity.STAGE, stageId).isRegistrable();
 252  
     }
 253  
 
 254  
     /**
 255  
      * Persists the given registration to the repository
 256  
      * 
 257  
      * @param id
 258  
      *            the id of the Entity
 259  
      * @param license
 260  
      *            the registration license
 261  
      * @return T if the operation was successful
 262  
      */
 263  
     private boolean persistRegistration(String id, ILicense license)
 264  
     {
 265  
 
 266  0
         Properties props = new Properties();
 267  
 
 268  0
         props.put(id + "." + REPOSITORY_NAME_ID, license.getName() != null ? license.getName() : "");
 269  
 
 270  0
         if (license.getKey() == null)
 271  0
             props.put(id + "." + REPOSITORY_KEY_ID, "");
 272  
         else
 273  0
             props.put(id + "." + REPOSITORY_KEY_ID, license.getKey());
 274  
 
 275  0
         return configRepository.writeConfiguration(REPOSITORY_CONFIG_ID, REPOSITORY_SECTION_ID, props);
 276  
     }
 277  
 
 278  
     /**
 279  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#registerApplication(java.lang.String, java.lang.String,
 280  
      *      java.lang.String)
 281  
      */
 282  
     public boolean registerApplication(String applicationID, String name, String key)
 283  
             throws RegistrationManagerException
 284  
     {
 285  0
         return registerEntity(Entity.APPLICATION, applicationID, name, key);
 286  
     }
 287  
 
 288  
     /**
 289  
      * Registers an entity
 290  
      * 
 291  
      * @param type
 292  
      *            the type of the entity
 293  
      * @param entityID
 294  
      *            the id of the entity
 295  
      * @param name
 296  
      *            the name to register to
 297  
      * @param key
 298  
      *            the key to register
 299  
      * @return T if the registration was successful
 300  
      * @throws RegistrationManagerException
 301  
      *             if a error occurrs in registration manager
 302  
      */
 303  
     private boolean registerEntity(Entity type, String entityID, String name, String key)
 304  
             throws RegistrationManagerException
 305  
     {
 306  0
         String id = Entity.getID(type, entityID);
 307  0
         ILicense license = getRegistrationRecord(type, entityID);
 308  
 
 309  0
         if (!license.isRegistrable())
 310  0
             throw new RegistrationManagerException("You are trying to register a unregistrable entity");
 311  
 
 312  0
         license.setName(name);
 313  0
         license.register(key, id);
 314  
 
 315  
         // Updates the Map with the registered record
 316  0
         registrations.put(id, license);
 317  
 
 318  
         // Persists to the Repository
 319  0
         persistRegistration(id, license);
 320  
 
 321  0
         return license.isRegistered();
 322  
     }
 323  
 
 324  
     /**
 325  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#registerProvider(java.lang.String, java.lang.String,
 326  
      *      java.lang.String)
 327  
      */
 328  
     public boolean registerProvider(String providerID, String name, String key) throws RegistrationManagerException
 329  
     {
 330  0
         return registerEntity(Entity.PROVIDER, providerID, name, key);
 331  
     }
 332  
 
 333  
     /**
 334  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#registerService(java.lang.String, java.lang.String,
 335  
      *      java.lang.String)
 336  
      */
 337  
     public boolean registerService(String serviceID, String name, String key) throws RegistrationManagerException
 338  
     {
 339  0
         return registerEntity(Entity.SERVICE, serviceID, name, key);
 340  
     }
 341  
 
 342  
     /**
 343  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#registerStage(java.lang.String, java.lang.String,
 344  
      *      java.lang.String)
 345  
      */
 346  
     public boolean registerStage(String stageId, String name, String key) throws RegistrationManagerException
 347  
     {
 348  0
         return registerEntity(Entity.STAGE, stageId, name, key);
 349  
     }
 350  
 
 351  
     /**
 352  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#unregisterApplication(java.lang.String)
 353  
      */
 354  
     public void unregisterApplication(String applicationID)
 355  
     {
 356  0
         unregisterEntity(Entity.APPLICATION, applicationID);
 357  0
     }
 358  
 
 359  
     /**
 360  
      * Unregisters an entity
 361  
      * 
 362  
      * @param type
 363  
      *            the type of the entity
 364  
      * @param entityID
 365  
      *            the id of the entity
 366  
      */
 367  
     private void unregisterEntity(Entity type, String entityID)
 368  
     {
 369  0
         String id = Entity.getID(type, entityID);
 370  0
         ILicense license = getRegistrationRecord(type, entityID);
 371  
 
 372  0
         license.unregister();
 373  
 
 374  
         // Updates the Map with the registered record
 375  0
         registrations.put(id, license);
 376  
 
 377  
         // Persists to the Repository
 378  0
         persistRegistration(id, license);
 379  0
     }
 380  
 
 381  
     /**
 382  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#unregisterProvider(java.lang.String)
 383  
      */
 384  
     public void unregisterProvider(String providerID)
 385  
     {
 386  0
         unregisterEntity(Entity.PROVIDER, providerID);
 387  0
     }
 388  
 
 389  
     /**
 390  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#unregisterService(java.lang.String)
 391  
      */
 392  
     public void unregisterService(String serviceID)
 393  
     {
 394  0
         unregisterEntity(Entity.SERVICE, serviceID);
 395  0
     }
 396  
 
 397  
     /**
 398  
      * @see pt.digitalis.dif.dem.managers.IRegistrationManager#unregisterStage(java.lang.String)
 399  
      */
 400  
     public void unregisterStage(String stageId)
 401  
     {
 402  0
         unregisterEntity(Entity.STAGE, stageId);
 403  0
     }
 404  
 }