Coverage Report - pt.digitalis.dif.startup.DIFStartupConfiguration
 
Classes in this File Line Coverage Branch Coverage Complexity
DIFStartupConfiguration
0%
0/61
0%
0/18
1,812
 
 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.startup;
 7  
 
 8  
 import java.io.InputStream;
 9  
 import java.util.Properties;
 10  
 
 11  
 import pt.digitalis.dif.dem.objects.messages.MessagesLocation;
 12  
 import pt.digitalis.dif.utils.logging.DIFLogger;
 13  
 import pt.digitalis.log.LogLevel;
 14  
 import pt.digitalis.utils.ioc.IoCImplementations;
 15  
 import pt.digitalis.utils.ioc.ModuleParser;
 16  
 
 17  
 /**
 18  
  * This class represents the basic startup configuration DIF needs. In this phase it can't rely on several
 19  
  * features/resources since they have not yet been started Note: There is no support for changes in this configuration
 20  
  * in runtime. Only by updating the configuration file and then restarting the DIF will this be reflected. This is a
 21  
  * design decision since these parameters define several resources that are mandatory for the startup of the DIF itself
 22  
  * and set the ground for all its infrastructure, like, for instance the IoC controller.
 23  
  * 
 24  
  * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
 25  
  * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a>
 26  
  * @created Oct 2, 2007
 27  
  */
 28  0
 public class DIFStartupConfiguration {
 29  
 
 30  
     /**
 31  
      * The file name of the properties file that contains the startup parameters. Note: It's called dif2 for the
 32  
      * eventuality of the existence of dif 1.x instances in the same App. Server. this way they will not collide.
 33  
      */
 34  
     static final public String CONFIGURATION_FILE = "dif2.properties";
 35  
 
 36  
     /** The DEMO Mode property */
 37  
     static private Boolean demoMode;
 38  
 
 39  
     /** The developer Mode property */
 40  
     static private Boolean developerMode;
 41  
 
 42  
     /** The IoC engine to use: Property "iocimplementation" */
 43  
     static private IoCImplementations iocImplementation;
 44  
 
 45  
     /** The Log Level: Property "loglevel" */
 46  
     static private LogLevel logLevel;
 47  
 
 48  
     /** The Messages Location: Property "messagelocation" */
 49  
     static private MessagesLocation messagesLocation;
 50  
 
 51  
     /** The Module Parsing method: Property "moduleparser" */
 52  
     static private ModuleParser moduleParser;
 53  
 
 54  
     /** The "print stack trace" property. */
 55  0
     static private Boolean printStackTrace = true;
 56  
 
 57  
     /** The testing Mode property */
 58  
     static private Boolean testingMode;
 59  
 
 60  
     /** Static initializer */
 61  
     static
 62  
     {
 63  0
         Properties parameters = new Properties();
 64  
         try
 65  
         {
 66  
             /*
 67  
              * IMPLEMENTATION NOTE: Must use the Thread context class loader since inside Surefire the system
 68  
              * classLoader does not have all classpath entries
 69  
              */
 70  
 
 71  
             // Load the configuration file from the classPath
 72  0
             InputStream configsFile = Thread.currentThread().getContextClassLoader()
 73  0
                     .getResourceAsStream(CONFIGURATION_FILE);
 74  
 
 75  0
             if (configsFile != null)
 76  
             {
 77  
 
 78  0
                 parameters.load(configsFile);
 79  
 
 80  0
                 iocImplementation = IoCImplementations.getImplementationByName(loadProperty(parameters,
 81  0
                         "iocimplementation", IoCImplementations.GUICE.toString()));
 82  
 
 83  0
                 logLevel = LogLevel.getLevelByName(loadProperty(parameters, "loglevel", LogLevel.WARN.toString()));
 84  0
                 developerMode = loadProperty(parameters, "developerMode", false);
 85  0
                 demoMode = loadProperty(parameters, "demoMode", false);
 86  0
                 testingMode = loadProperty(parameters, "testingMode", false);
 87  
 
 88  0
                 moduleParser = ModuleParser.getParsingMethodByName(loadProperty(parameters, "moduleparser",
 89  0
                         ModuleParser.FAST.toString()));
 90  0
                 messagesLocation = MessagesLocation.getMessageLocationByName(loadProperty(parameters,
 91  0
                         "messagelocation", MessagesLocation.MESSAGES_FOLDER.toString()));
 92  0
                 printStackTrace = loadProperty(parameters, "printStackTrace", true);
 93  
             }
 94  
             else
 95  0
                 setDefaultConfigurations();
 96  
 
 97  
         }
 98  0
         catch (Exception e)
 99  
         {
 100  0
             setDefaultConfigurations();
 101  0
             DIFLogger.getLogger().warn("dif2.properties unavailable! Reverting to defaults...");
 102  
         }
 103  
     }
 104  
 
 105  
     /**
 106  
      * Inspector for the 'demoMode' attribute.
 107  
      * 
 108  
      * @return the demoMode value
 109  
      */
 110  
     public static Boolean getDemoMode()
 111  
     {
 112  0
         return demoMode;
 113  
     }
 114  
 
 115  
     /**
 116  
      * Inspector for the 'developerMode' attribute.
 117  
      * 
 118  
      * @return the developerMode value
 119  
      */
 120  
     public static Boolean getDeveloperMode()
 121  
     {
 122  0
         return developerMode;
 123  
     }
 124  
 
 125  
     /**
 126  
      * @return the iocImplementation
 127  
      */
 128  
     static public IoCImplementations getIoCImplementation()
 129  
     {
 130  0
         return iocImplementation;
 131  
     }
 132  
 
 133  
     /**
 134  
      * @return the logLevel
 135  
      */
 136  
     static public LogLevel getLogLevel()
 137  
     {
 138  0
         return logLevel;
 139  
     }
 140  
 
 141  
     /**
 142  
      * @return the messagesLocation
 143  
      */
 144  
     static public MessagesLocation getMessagesLocation()
 145  
     {
 146  0
         return messagesLocation;
 147  
     }
 148  
 
 149  
     /**
 150  
      * @return the moduleParser
 151  
      */
 152  
     static public ModuleParser getModuleParser()
 153  
     {
 154  0
         return moduleParser;
 155  
     }
 156  
 
 157  
     /**
 158  
      * Inspector for the 'testingMode' attribute.
 159  
      * 
 160  
      * @return the testingMode value
 161  
      */
 162  
     public static Boolean getTestingMode()
 163  
     {
 164  0
         return testingMode;
 165  
     }
 166  
 
 167  
     /**
 168  
      * Inspector for the 'printStackTrace' attribute.
 169  
      * 
 170  
      * @return the printStackTrace attribute value
 171  
      */
 172  
     static public Boolean isPrintStackTrace()
 173  
     {
 174  0
         return printStackTrace;
 175  
     }
 176  
 
 177  
     /**
 178  
      * @return T is the instance is in production mode. Production mode is the default mode when none of the other modes
 179  
      *         are active (development, testing or demo)
 180  
      */
 181  
     public static boolean isProductionMode()
 182  
     {
 183  0
         return !getDemoMode() && !getDeveloperMode() && !getTestingMode();
 184  
     }
 185  
 
 186  
     /**
 187  
      * Overloads the 'loadProperty' method for boolean values.
 188  
      * 
 189  
      * @param prop
 190  
      *            the Properties to search
 191  
      * @param key
 192  
      *            the key to search
 193  
      * @param defaultValue
 194  
      *            the default value when not found
 195  
      * @return the read value or the default if not found
 196  
      */
 197  
     static Boolean loadProperty(Properties prop, String key, boolean defaultValue)
 198  
     {
 199  0
         String value = prop.getProperty(key);
 200  
 
 201  0
         if (value == null)
 202  0
             return defaultValue;
 203  
         else
 204  
         {
 205  0
             if (value.toLowerCase().equals("false") || value.toLowerCase().equals("f"))
 206  0
                 return false;
 207  0
             else if (value.toLowerCase().equals("true") || value.toLowerCase().equals("t"))
 208  0
                 return true;
 209  
             else
 210  0
                 throw new RuntimeException("Invalid configuration value for property '" + key
 211  0
                         + "'! Unable to proceed...\nValid values are: 'true', 'T', 'false' and 'F'.");
 212  
         }
 213  
     }
 214  
 
 215  
     /**
 216  
      * Searches properties for a given key
 217  
      * 
 218  
      * @param prop
 219  
      *            the Properties to search
 220  
      * @param key
 221  
      *            the key to search
 222  
      * @param defaultValue
 223  
      *            the default value when not found
 224  
      * @return the read value or the default if not found
 225  
      */
 226  
     static String loadProperty(Properties prop, String key, String defaultValue)
 227  
     {
 228  0
         String value = prop.getProperty(key);
 229  
 
 230  0
         if (value == null)
 231  0
             return defaultValue;
 232  
         else
 233  0
             return value;
 234  
     }
 235  
 
 236  
     /** */
 237  
     static private void setDefaultConfigurations()
 238  
     {
 239  0
         iocImplementation = IoCImplementations.GUICE;
 240  0
         logLevel = LogLevel.WARN;
 241  0
         developerMode = false;
 242  0
         demoMode = false;
 243  0
         testingMode = false;
 244  0
         moduleParser = ModuleParser.FAST;
 245  0
         messagesLocation = MessagesLocation.MESSAGES_FOLDER;
 246  0
         printStackTrace = true;
 247  0
     }
 248  
 
 249  
     /**
 250  
      * Modifier for the 'demoMode' attribute.
 251  
      * 
 252  
      * @param demoMode
 253  
      *            the new demoMode value to set
 254  
      */
 255  
     public static void setDemoMode(Boolean demoMode)
 256  
     {
 257  0
         DIFStartupConfiguration.demoMode = demoMode;
 258  0
     }
 259  
 
 260  
     /**
 261  
      * Modifier for the 'developerMode' attribute.
 262  
      * 
 263  
      * @param developerMode
 264  
      *            the new developerMode value to set
 265  
      */
 266  
     public static void setDeveloperMode(Boolean developerMode)
 267  
     {
 268  0
         DIFStartupConfiguration.developerMode = developerMode;
 269  0
     }
 270  
 
 271  
     /**
 272  
      * Modifier for the 'logLevel' attribute.
 273  
      * 
 274  
      * @param logLevel
 275  
      *            the new logLevel value to set
 276  
      */
 277  
     public static void setLogLevel(LogLevel logLevel)
 278  
     {
 279  0
         DIFStartupConfiguration.logLevel = logLevel;
 280  0
     }
 281  
 
 282  
     /**
 283  
      * Modifier for the 'testingMode' attribute.
 284  
      * 
 285  
      * @param testingMode
 286  
      *            the new testingMode value to set
 287  
      */
 288  
     public static void setTestingMode(Boolean testingMode)
 289  
     {
 290  0
         DIFStartupConfiguration.testingMode = testingMode;
 291  0
     }
 292  
 }