2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

Clover coverage report - Cactus 1.8dev for J2EE API 1.3
Coverage timestamp: Sun Mar 26 2006 18:50:18 BRT
file stats: LOC: 197   Methods: 5
NCLOC: 94   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ConfigurationInitializer.java 40% 48.1% 80% 50%
coverage coverage
 1   
 /* 
 2   
  * ========================================================================
 3   
  * 
 4   
  * Copyright 2003-2004 The Apache Software Foundation.
 5   
  *
 6   
  * Licensed under the Apache License, Version 2.0 (the "License");
 7   
  * you may not use this file except in compliance with the License.
 8   
  * 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, software
 13   
  * distributed under the License is distributed on an "AS IS" BASIS,
 14   
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15   
  * See the License for the specific language governing permissions and
 16   
  * limitations under the License.
 17   
  * 
 18   
  * ========================================================================
 19   
  */
 20   
 package org.apache.cactus.internal.configuration;
 21   
 
 22   
 import java.io.FileInputStream;
 23   
 import java.io.IOException;
 24   
 
 25   
 import java.util.Enumeration;
 26   
 import java.util.MissingResourceException;
 27   
 import java.util.PropertyResourceBundle;
 28   
 import java.util.ResourceBundle;
 29   
 
 30   
 import org.apache.cactus.internal.util.ClassLoaderUtils;
 31   
 import org.apache.cactus.util.ChainedRuntimeException;
 32   
 
 33   
 /**
 34   
  * Read Cactus configuration files and set the properties found as
 35   
  * System properties.
 36   
  *
 37   
  * @version $Id: ConfigurationInitializer.java 239016 2004-06-27 15:23:30Z vmassol $
 38   
  */
 39   
 public class ConfigurationInitializer
 40   
 {
 41   
     /**
 42   
      * Name of the Cactus configuration file if cactus is to look for it in
 43   
      * the classpath.
 44   
      */
 45   
     private static final String DEFAULT_CONFIG_NAME = "cactus";
 46   
 
 47   
     /**
 48   
      * Name of the java property for specifying the location of the cactus
 49   
      * configuration file. This overrides any cactus configuration file that is
 50   
      * put in the classpath.
 51   
      */
 52   
     private static final String CACTUS_CONFIG_PROPERTY = "cactus.config";
 53   
 
 54   
     /**
 55   
      * Name of the Cactus property that points to a properties file
 56   
      * containing logging configuration.
 57   
      */
 58   
     private static final String CACTUS_LOGGING_CONFIG_PROPERTY = 
 59   
         "cactus.logging.config";
 60   
 
 61   
     /**
 62   
      * Have the Cactus configuration files been initialized?
 63   
      */
 64   
     private static boolean isInitialized;
 65   
     
 66   
     /**
 67   
      * Read Cactus configuration files.
 68   
      * 
 69   
      * @param isReinitialization if true then force a re-read of the Cactus 
 70   
      *        configuration files
 71   
      */
 72  4
     public static synchronized void initialize(boolean isReinitialization)
 73   
     {
 74  4
         if (!isInitialized)
 75   
         {    
 76  1
             initializeConfig(isReinitialization);
 77  1
             initializeLoggingConfig(isReinitialization);
 78  1
             isInitialized = true;
 79   
         }
 80   
     }
 81   
 
 82   
     /**
 83   
      * Read Cactus configuration files.
 84   
      */
 85  4
     public static synchronized void initialize()
 86   
     {
 87  4
         initialize(false);
 88   
     }
 89   
     
 90   
     /**
 91   
      * Initialize general cactus configuration. Read the cactus configuration 
 92   
      * file from the java property defined on the command line 
 93   
      * (named CACTUS_CONFIG_PROPERTY) and if none has been defined tries to 
 94   
      * read the DEFAULT_CONFIG_NAME file from the classpath. All properties 
 95   
      * found are exported as java system properties.
 96   
      * 
 97   
      * @param isReinitialization if true then force a re-read of the Cactus 
 98   
      *        configuration files
 99   
      */
 100  1
     private static void initializeConfig(boolean isReinitialization)
 101   
     {
 102  1
         ResourceBundle config;
 103   
 
 104   
         // Has the user passed the location of the cactus configuration
 105   
         // file as a java property
 106  1
         String configOverride = System.getProperty(CACTUS_CONFIG_PROPERTY);
 107   
 
 108  1
         if (configOverride == null)
 109   
         {
 110   
             // Try to read the default cactus configuration file from the
 111   
             // classpath
 112  1
             try
 113   
             {
 114  1
                 config = ClassLoaderUtils.loadPropertyResourceBundle(
 115   
                     DEFAULT_CONFIG_NAME, ConfigurationInitializer.class);
 116   
             }
 117   
             catch (MissingResourceException e)
 118   
             {
 119   
                 // Cannot find cactus properties file. Do nothing.
 120  1
                 return;
 121   
             }
 122   
         }
 123   
         else
 124   
         {
 125   
             // Try to read from specified properties file
 126  0
             try
 127   
             {
 128  0
                 config = new PropertyResourceBundle(
 129   
                     new FileInputStream(configOverride));
 130   
             }
 131   
             catch (IOException e)
 132   
             {
 133  0
                 throw new ChainedRuntimeException(
 134   
                     "Cannot read cactus configuration file ["
 135   
                     + configOverride + "]", e);
 136   
             }
 137   
         }
 138   
 
 139  0
         addSystemProperties(config, isReinitialization);
 140   
     }
 141   
 
 142   
     /**
 143   
      * Initialize logging configuration.
 144   
      * 
 145   
      * @param isReinitialization if true then force a re-read of the Cactus 
 146   
      *        configuration files
 147   
      */
 148  1
     private static void initializeLoggingConfig(boolean isReinitialization)
 149   
     {
 150  1
         String logConfig = System.getProperty(CACTUS_LOGGING_CONFIG_PROPERTY);
 151  1
         if (logConfig != null)
 152   
         {
 153  0
             ResourceBundle bundle;
 154  0
             try
 155   
             {
 156  0
                 bundle = new PropertyResourceBundle(
 157   
                     new FileInputStream(logConfig));
 158   
             } 
 159   
             catch (IOException e)
 160   
             {
 161  0
                 throw new ChainedRuntimeException("Failed to load logging "
 162   
                     + "configuration file [" + logConfig + "]");
 163   
             }
 164  0
             addSystemProperties(bundle, isReinitialization);
 165   
         }
 166   
     }
 167   
 
 168   
     /**
 169   
      * Add all properties found in the resource bundle as system
 170   
      * properties.
 171   
      *
 172   
      * @param theBundle the resource bundle containing the properties to
 173   
      *        set as system properties
 174   
      * @param isReinitialization if true then force a re-read of the Cactus 
 175   
      *        configuration files
 176   
      */
 177  0
     private static void addSystemProperties(ResourceBundle theBundle,
 178   
             boolean isReinitialization)
 179   
     {
 180  0
         Enumeration keys = theBundle.getKeys();
 181   
 
 182  0
         while (keys.hasMoreElements())
 183   
         {
 184  0
             String key = (String) keys.nextElement();
 185   
 
 186   
             // Only set the system property if it does not already exist.
 187   
             // This allows system properties defined on the command line
 188   
             // override Cactus properties located in the Cactus configuration
 189   
             // files.
 190  0
             if ((System.getProperty(key) == null) || isReinitialization)
 191   
             {
 192  0
                 System.setProperty(key, theBundle.getString(key));
 193   
             }
 194   
         }
 195   
     }
 196   
 }
 197