File Source: WebloggerConfig.java

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   *  contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  
    19  package org.apache.roller.weblogger.config;
    20  
    21  import java.io.File;
    22  import java.io.FileInputStream;
    23  import java.io.InputStream;
    24  import java.util.Enumeration;
    25  import java.util.Properties;
    26  import org.apache.commons.logging.Log;
    27  import org.apache.commons.logging.LogFactory;
    28  import org.apache.log4j.PropertyConfigurator;
    29  import org.apache.roller.util.PropertyExpander;
    30  
    31  
    32  /**
    33   * This is the single entry point for accessing configuration properties in Roller.
    34   */
    35  public class WebloggerConfig {
    36      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.config.WebloggerConfig__static_init
              * 
              *  Presumptions:
              *    java.lang.Class:forName(...)@57 != null
              *    java.lang.System.out != null
              *    java.util.Properties:keys(...)@116 != null
              *    org.apache.commons.logging.LogFactory:getLog(...)@44 != null
              * 
              *  Postconditions:
              *    "="._tainted == 0
              *    "Roller Weblogger: Failed to load custom properties from "._tainted == 0
              *    "Roller Weblogger: Successfully loaded custom properties from "._tainted == 0
              *    config == &new Properties(WebloggerConfig__static_init#1)
              *    custom_config == &".roller-custom.properties"
              *    custom_config_file in Addr_Set{null,&new File(WebloggerConfig__static_init#2)}
              *    custom_jvm_param == &"roller.custom.config"
              *    default_config == &".org.apache.roller.weblogger.config.roller.properties"
              *    (soft) log != null
              *    new File(WebloggerConfig__static_init#2) num objects <= 1
              *    ...
              * 
              *  Test Vectors:
              *    java.io.File:exists(...)@78: {0}, {1}
              *    java.lang.Class:getResourceAsStream(...)@64: Addr_Set{null}, Inverse{null}
              *    java.lang.String:length(...)@74: {0}, {1..232-1}
              *    java.lang.System:getProperty(...)@73: Addr_Set{null}, Inverse{null}
              *    java.util.Properties:get(...)@92: Addr_Set{null}, Inverse{null}
              */
    37      private static String default_config = "/org/apache/roller/weblogger/config/roller.properties";
    38      private static String custom_config = "/roller-custom.properties";
    39      private static String custom_jvm_param = "roller.custom.config";
    40      private static File custom_config_file = null;
    41  
    42      private static Properties config;
    43  
    44      private static Log log = LogFactory.getLog(WebloggerConfig.class);
    45      
    46  
    47      /*
    48       * Static block run once at class loading
    49       *
    50       * We load the default properties and any custom properties we find
    51       */
    52      static {
    53          config = new Properties();
    54  
    55          try {
    56              // we'll need this to get at our properties files in the classpath
    57              Class config_class = Class.forName("org.apache.roller.weblogger.config.WebloggerConfig");
    58  
    59              // first, lets load our default properties
    60              InputStream is = config_class.getResourceAsStream(default_config);
    61              config.load(is);
    62  
    63              // now, see if we can find our custom config
    64              is = config_class.getResourceAsStream(custom_config);
    65              if(is != null) {
    66                  config.load(is);
    67                  System.out.println("Roller Weblogger: Successfully loaded custom properties file from classpath");
    68              } else {
    69                  System.out.println("Roller Weblogger: No custom properties file found in classpath");
    70              }
    71  
    72              // finally, check for an external config file
    73              String env_file = System.getProperty(custom_jvm_param);
    74              if(env_file != null && env_file.length() > 0) {
    75                  custom_config_file = new File(env_file);
    76  
    77                  // make sure the file exists, then try and load it
+   78                  if(custom_config_file != null && custom_config_file.exists()) {
    79                      is = new FileInputStream(custom_config_file);
    80                      config.load(is);
    81                      System.out.println("Roller Weblogger: Successfully loaded custom properties from "+
    82                              custom_config_file.getAbsolutePath());
    83                  } else {
    84                      System.out.println("Roller Weblogger: Failed to load custom properties from "+
    85                              custom_config_file.getAbsolutePath());
    86                  }
    87  
    88              } 
    89  
    90              // Now expand system properties for properties in the config.expandedProperties list,
    91              // replacing them by their expanded values.
    92              String expandedPropertiesDef = (String) config.get("config.expandedProperties");
    93              if (expandedPropertiesDef != null) {
    94                  String[] expandedProperties = expandedPropertiesDef.split(",");
+   95                  for (int i = 0; i < expandedProperties.length; i++) {
+   96                      String propName = expandedProperties[i].trim();
    97                      String initialValue = (String) config.get(propName);
    98                      if (initialValue != null) {
    99                          String expandedValue = PropertyExpander.expandSystemProperties(initialValue);
   100                          config.put(propName,expandedValue);
   101                      }
   102                  }
   103              }
   104              
   105              // initialize logging subsystem via WebloggerConfig
   106              Properties log4jprops = new Properties();
   107              PropertyConfigurator.configure(WebloggerConfig.getPropertiesStartingWith("log4j."));
   108              
   109              // finally we can start logging...
   110  
   111              // some debugging for those that want it
   112              if(log.isDebugEnabled()) {
   113                  log.debug("WebloggerConfig looks like this ...");
   114  
   115                  String key = null;
   116                  Enumeration keys = config.keys();
   117                  while(keys.hasMoreElements()) {
   118                      key = (String) keys.nextElement();
   119                      log.debug(key+"="+config.getProperty(key));
   120                  }
   121              }
   122  
   123          } catch (Exception e) {
   124              e.printStackTrace();
   125          }
   126  
   127      }
   128  
   129  
   130      // no, you may not instantiate this class :p
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.config.WebloggerConfig()
              */
   131      private WebloggerConfig() {}
   132  
   133  
   134      /**
   135       * Retrieve a property value
   136       * @param     key Name of the property
   137       * @return    String Value of property requested, null if not found
   138       */
   139      public static String getProperty(String key) {
                 /* 
    P/P           *  Method: String getProperty(String)
                  * 
                  *  Preconditions:
                  *    config != null
                  *    log != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   140          log.debug("Fetching property ["+key+"="+config.getProperty(key)+"]");
   141          String value = config.getProperty(key);
   142          return value == null ? value : value.trim();
   143      }
   144      
   145      /**
   146       * Retrieve a property value
   147       * @param     key Name of the property
   148       * @param     defaultValue Default value of property if not found     
   149       * @return    String Value of property requested or defaultValue
   150       */
   151      public static String getProperty(String key, String defaultValue) {
                 /* 
    P/P           *  Method: String getProperty(String, String)
                  * 
                  *  Preconditions:
                  *    config != null
                  *    log != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.util.Properties:getProperty(...)@153: Inverse{null}, Addr_Set{null}
                  */
   152          log.debug("Fetching property ["+key+"="+config.getProperty(key)+",defaultValue="+defaultValue+"]");
   153          String value = config.getProperty(key);
   154          if(value == null)
   155            return defaultValue;
   156          
   157          return value.trim();
   158      }
   159  
   160      /**
   161       * Retrieve a property as a boolean ... defaults to false if not present.
   162       */
   163      public static boolean getBooleanProperty(String name) {
                 /* 
    P/P           *  Method: bool getBooleanProperty(String)
                  * 
                  *  Preconditions:
                  *    config != null
                  *    log != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   164          return getBooleanProperty(name,false);
   165      }
   166  
   167      /**
   168       * Retrieve a property as a boolean ... with specified default if not present.
   169       */
   170      public static boolean getBooleanProperty(String name, boolean defaultValue) {
   171          // get the value first, then convert
                 /* 
    P/P           *  Method: bool getBooleanProperty(String, bool)
                  * 
                  *  Preconditions:
                  *    config != null
                  *    log != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   172          String value = WebloggerConfig.getProperty(name);
   173  
   174          if(value == null)
   175              return defaultValue;
   176  
   177          return (new Boolean(value)).booleanValue();
   178      }
   179  
   180      /**
   181       * Retrieve a property as an int ... defaults to 0 if not present.
   182       */
   183      public static int getIntProperty(String name) {
                 /* 
    P/P           *  Method: int getIntProperty(String)
                  * 
                  *  Preconditions:
                  *    config != null
                  *    log != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   184          return getIntProperty(name, 0);
   185      }
   186  
   187      /**
   188       * Retrieve a property as a int ... with specified default if not present.
   189       */
   190      public static int getIntProperty(String name, int defaultValue) {
   191          // get the value first, then convert
                 /* 
    P/P           *  Method: int getIntProperty(String, int)
                  * 
                  *  Preconditions:
                  *    config != null
                  *    log != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   192          String value = WebloggerConfig.getProperty(name);
   193  
   194          if (value == null)
   195              return defaultValue;
   196  
   197          return (new Integer(value)).intValue();
   198      }
   199  
   200      /**
   201       * Retrieve all property keys
   202       * @return Enumeration A list of all keys
   203       **/
   204      public static Enumeration keys() {
                 /* 
    P/P           *  Method: Enumeration keys()
                  * 
                  *  Preconditions:
                  *    config != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   205          return config.keys();
   206      }
   207      
   208      
   209      /**
   210       * Get properties starting with a specified string.
   211       */
   212      public static Properties getPropertiesStartingWith(String startingWith) {
                 /* 
    P/P           *  Method: Properties getPropertiesStartingWith(String)
                  * 
                  *  Preconditions:
                  *    config != null
                  * 
                  *  Presumptions:
                  *    java.util.Properties:keys(...)@214 != null
                  * 
                  *  Postconditions:
                  *    return_value == &new Properties(getPropertiesStartingWith#1)
                  *    new Properties(getPropertiesStartingWith#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.util.Enumeration:hasMoreElements(...)@214: {0}, {1}
                  */
   213          Properties props = new Properties();
   214          for (Enumeration it = config.keys(); it.hasMoreElements();) {
   215              String key = (String)it.nextElement();
   216              props.put(key, config.get(key));
   217          }
   218          return props;
   219      }
   220      
   221  
   222      /**
   223       * Set the "uploads.dir" property at runtime.
   224       * <p />
   225       * Properties are meant to be read-only, but we make this exception because  
   226       * we know that some people are still writing their uploads to the webapp  
   227       * context and we can only get that path at runtime (and for unit testing).
   228       * <p />
   229       * This property is *not* persisted in any way.
   230       */
   231      public static void setUploadsDir(String path) {
   232          // only do this if the user wants to use the webapp context
                 /* 
    P/P           *  Method: void setUploadsDir(String)
                  * 
                  *  Preconditions:
                  *    config != null
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@233: {0}, {1}
                  */
   233          if("${webapp.context}".equals(config.getProperty("uploads.dir")))
   234              config.setProperty("uploads.dir", path);
   235      }
   236      
   237      /**
   238       * Set the "themes.dir" property at runtime.
   239       * <p />
   240       * Properties are meant to be read-only, but we make this exception because  
   241       * we know that some people are still using their themes in the webapp  
   242       * context and we can only get that path at runtime (and for unit testing).
   243       * <p />
   244       * This property is *not* persisted in any way.
   245       */
   246      public static void setThemesDir(String path) {
   247          // only do this if the user wants to use the webapp context
                 /* 
    P/P           *  Method: void setThemesDir(String)
                  * 
                  *  Preconditions:
                  *    config != null
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@248: {0}, {1}
                  */
   248          if("${webapp.context}".equals(config.getProperty("themes.dir")))
   249              config.setProperty("themes.dir", path);
   250      }
   251      
   252  }








SofCheck Inspector Build Version : 2.18479
WebloggerConfig.java 2009-Jan-02 14:25:10
WebloggerConfig.class 2009-Sep-04 03:12:32