File Source: WebloggerRuntimeConfig.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.InputStream;
    22  import java.io.InputStreamReader;
    23  import java.io.StringWriter;
    24  import org.apache.commons.logging.Log;
    25  import org.apache.commons.logging.LogFactory;
    26  import org.apache.roller.weblogger.config.runtime.RuntimeConfigDefs;
    27  import org.apache.roller.weblogger.config.runtime.RuntimeConfigDefsParser;
    28  import org.apache.roller.weblogger.business.PropertiesManager;
    29  import org.apache.roller.weblogger.business.WebloggerFactory;
    30  import org.apache.roller.weblogger.pojos.RuntimeConfigProperty;
    31  
    32  
    33  /**
    34   * This class acts as a convenience gateway for getting property values
    35   * via the PropertiesManager.  We do this because most calls to the
    36   * PropertiesManager are just to get the value of a specific property and
    37   * thus the caller doesn't need the full RuntimeConfigProperty object.
    38   * 
    39   * We also provide some methods for converting to different data types.
    40   */
    41  public class WebloggerRuntimeConfig {
    42      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.config.WebloggerRuntimeConfig__static_init
              * 
              *  Postconditions:
              *    absoluteContextURL == null
              *    configDefs == null
              *    relativeContextURL == null
              *    init'ed(log)
              *    runtime_config == &".org.apache.roller.weblogger.config.runtimeConfigDefs.xml"
              */
    43      private static Log log = LogFactory.getLog(WebloggerRuntimeConfig.class);
    44      
    45      private static String runtime_config = "/org/apache/roller/weblogger/config/runtimeConfigDefs.xml";
    46      private static RuntimeConfigDefs configDefs = null;
    47      
    48      // special case for our context urls
    49      private static String relativeContextURL = null;
    50      private static String absoluteContextURL = null;
    51      
    52      
    53      // prevent instantiations
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.config.WebloggerRuntimeConfig()
              */
    54      private WebloggerRuntimeConfig() {}
    55      
    56      
    57      /**
    58       * Retrieve a single property from the PropertiesManager ... returns null
    59       * if there is an error
    60       **/
    61      public static String getProperty(String name) {
    62          
                 /* 
    P/P           *  Method: String getProperty(String)
                  * 
                  *  Preconditions:
                  *    log != null
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
                  * 
                  *  Presumptions:
                  *    getWeblogger(...).propertiesManager != null
                  *    pmgr.strategy != null
                  *    pmgr.strategy.emf != null
                  *    pmgr.strategy.threadLocalEntityManager != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    javax.persistence.EntityManager:find(...)@216: Addr_Set{null}, Inverse{null}
                  */
    63          String value = null;
    64          
    65          try {
    66              PropertiesManager pmgr = WebloggerFactory.getWeblogger().getPropertiesManager();
    67              RuntimeConfigProperty prop = pmgr.getProperty(name);
    68              if(prop != null) {
    69                  value = prop.getValue();
    70              }
    71          } catch(Exception e) {
    72              log.warn("Trouble accessing property: "+name, e);
    73          }
    74          
    75          log.debug("fetched property ["+name+"="+value+"]");
    76  
    77          return value;
    78      }
    79      
    80      
    81      /**
    82       * Retrieve a property as a boolean ... defaults to false if there is an error
    83       **/
    84      public static boolean getBooleanProperty(String name) {
    85          
    86          // get the value first, then convert
                 /* 
    P/P           *  Method: bool getBooleanProperty(String)
                  * 
                  *  Preconditions:
                  *    log != null
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    87          String value = WebloggerRuntimeConfig.getProperty(name);
    88          
    89          if(value == null)
    90              return false;
    91          
    92          return (new Boolean(value)).booleanValue();
    93      }
    94      
    95      
    96      /**
    97       * Retrieve a property as an int ... defaults to -1 if there is an error
    98       **/
    99      public static int getIntProperty(String name) {
   100          
   101          // get the value first, then convert
                 /* 
    P/P           *  Method: int getIntProperty(String)
                  * 
                  *  Preconditions:
                  *    log != null
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   102          String value = WebloggerRuntimeConfig.getProperty(name);
   103          
   104          if(value == null)
   105              return -1;
   106          
   107          int intval = -1;
   108          try {
   109              intval = Integer.parseInt(value);
   110          } catch(Exception e) {
   111              log.warn("Trouble converting to int: "+name, e);
   112          }
   113          
   114          return intval;
   115      }
   116      
   117      
   118      public static RuntimeConfigDefs getRuntimeConfigDefs() {
   119          
                 /* 
    P/P           *  Method: RuntimeConfigDefs getRuntimeConfigDefs()
                  * 
                  *  Preconditions:
                  *    init'ed(configDefs)
                  *    (soft) log != null
                  *    (soft) init'ed(runtime_config)
                  * 
                  *  Presumptions:
                  *    java.lang.Class:getResourceAsStream(...)@124 != null
                  * 
                  *  Postconditions:
                  *    configDefs == One-of{old configDefs, &new RuntimeConfigDefs(unmarshall#1)}
                  *    init'ed(configDefs)
                  *    return_value == configDefs
                  *    new ArrayList(RuntimeConfigDefs#1) num objects <= 1
                  *    new RuntimeConfigDefs(unmarshall#1) num objects <= 1
                  *    init'ed(new RuntimeConfigDefs(unmarshall#1).configDefs)
                  * 
                  *  Test Vectors:
                  *    configDefs: Inverse{null}, Addr_Set{null}
                  */
   120          if(configDefs == null) {
   121              
   122              // unmarshall the config defs file
   123              try {
   124                  InputStream is = 
   125                          WebloggerRuntimeConfig.class.getResourceAsStream(runtime_config);
   126                  
   127                  RuntimeConfigDefsParser parser = new RuntimeConfigDefsParser();
   128                  configDefs = parser.unmarshall(is);
   129                  
   130              } catch(Exception e) {
   131                  // error while parsing :(
   132                  log.error("Error parsing runtime config defs", e);
   133              }
   134              
   135          }
   136          
   137          return configDefs;
   138      }
   139      
   140      
   141      /**
   142       * Get the runtime configuration definitions XML file as a string.
   143       *
   144       * This is basically a convenience method for accessing this file.
   145       * The file itself contains meta-data about what configuration
   146       * properties we change at runtime via the UI and how to setup
   147       * the display for editing those properties.
   148       */
   149      public static String getRuntimeConfigDefsAsString() {
   150          
                 /* 
    P/P           *  Method: String getRuntimeConfigDefsAsString()
                  * 
                  *  Preconditions:
                  *    log != null
                  *    (soft) init'ed(runtime_config)
                  * 
                  *  Postconditions:
                  *    java.io.StringWriter:toString(...)._tainted == 0
                  *    return_value in Addr_Set{&java.io.StringWriter:toString(...),&""}
                  * 
                  *  Test Vectors:
                  *    java.io.InputStreamReader:read(...)@160: {-231..0}, {1..232-1}
                  */
   151          log.debug("Trying to load runtime config defs file");
   152          
   153          try {
   154              InputStreamReader reader =
   155                      new InputStreamReader(WebloggerConfig.class.getResourceAsStream(runtime_config));
   156              StringWriter configString = new StringWriter();
   157              
   158              char[] buf = new char[8196];
   159              int length = 0;
   160              while((length = reader.read(buf)) > 0)
   161                  configString.write(buf, 0, length);
   162              
   163              reader.close();
   164              
   165              return configString.toString();
   166          } catch(Exception e) {
   167              log.error("Error loading runtime config defs file", e);
   168          }
   169          
   170          return "";
   171      }
   172      
   173      
   174      /**
   175       * Special method which sets the non-persisted absolute url to this site.
   176       *
   177       * This property is *not* persisted in any way.
   178       */
   179      public static void setAbsoluteContextURL(String url) {
                 /* 
    P/P           *  Method: void setAbsoluteContextURL(String)
                  * 
                  *  Postconditions:
                  *    absoluteContextURL == url
                  *    init'ed(absoluteContextURL)
                  */
   180          absoluteContextURL = url;
   181      }
   182      
   183      
   184      /**
   185       * Get the absolute url to this site.
   186       *
   187       * This method will just return the value of the "site.absoluteurl"
   188       * property if it is set, otherwise it will return the non-persisted
   189       * value which is set by the InitFilter.
   190       */
   191      public static String getAbsoluteContextURL() {
   192          
   193          // db prop takes priority if it exists
                 /* 
    P/P           *  Method: String getAbsoluteContextURL()
                  * 
                  *  Preconditions:
                  *    log != null
                  *    (soft) init'ed(absoluteContextURL)
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
                  * 
                  *  Postconditions:
                  *    (soft) init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.lang.String:length(...)@195: {0}, {1..232-1}
                  */
   194          String absURL = getProperty("site.absoluteurl");
   195          if(absURL != null && absURL.trim().length() > 0) {
   196              return absURL;
   197          }
   198          
   199          return absoluteContextURL;
   200      }
   201      
   202      
   203      /**
   204       * Special method which sets the non-persisted relative url to this site.
   205       *
   206       * This property is *not* persisted in any way.
   207       */
   208      public static void setRelativeContextURL(String url) {
                 /* 
    P/P           *  Method: void setRelativeContextURL(String)
                  * 
                  *  Postconditions:
                  *    relativeContextURL == url
                  *    init'ed(relativeContextURL)
                  */
   209          relativeContextURL = url;
   210      }
   211      
   212      
   213      public static String getRelativeContextURL() {
                 /* 
    P/P           *  Method: String getRelativeContextURL()
                  * 
                  *  Preconditions:
                  *    init'ed(relativeContextURL)
                  * 
                  *  Postconditions:
                  *    return_value == relativeContextURL
                  *    init'ed(return_value)
                  */
   214          return relativeContextURL;
   215      }
   216      
   217      
   218      /**
   219       * Convenience method for Roller classes trying to determine if a given
   220       * weblog handle represents the front page blog.
   221       */
   222      public static boolean isFrontPageWeblog(String weblogHandle) {
   223          
                 /* 
    P/P           *  Method: bool isFrontPageWeblog(String)
                  * 
                  *  Preconditions:
                  *    log != null
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   224          String frontPageHandle = getProperty("site.frontpage.weblog.handle");
   225          
+  226          return (frontPageHandle.equals(weblogHandle));
   227      }
   228      
   229      
   230      /**
   231       * Convenience method for Roller classes trying to determine if a given
   232       * weblog handle represents the front page blog configured to render
   233       * site-wide data.
   234       */
   235      public static boolean isSiteWideWeblog(String weblogHandle) {
   236          
                 /* 
    P/P           *  Method: bool isSiteWideWeblog(String)
                  * 
                  *  Preconditions:
                  *    log != null
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   237          boolean siteWide = getBooleanProperty("site.frontpage.weblog.aggregated");
   238          
   239          return (isFrontPageWeblog(weblogHandle) && siteWide);
   240      }
   241      
   242  }








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