File Source: configuration.java

     1  /*
     2   * Copyright (c) 2003-2006, Simon Brown
     3   * All rights reserved.
     4   *
     5   * Redistribution and use in source and binary forms, with or without
     6   * modification, are permitted provided that the following conditions are met:
     7   *
     8   *   - Redistributions of source code must retain the above copyright
     9   *     notice, this list of conditions and the following disclaimer.
    10   *
    11   *   - Redistributions in binary form must reproduce the above copyright
    12   *     notice, this list of conditions and the following disclaimer in
    13   *     the documentation and/or other materials provided with the
    14   *     distribution.
    15   *
    16   *   - Neither the name of Pebble nor the names of its contributors may
    17   *     be used to endorse or promote products derived from this software
    18   *     without specific prior written permission.
    19   *
    20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    30   * POSSIBILITY OF SUCH DAMAGE.
    31   */
    32  package net.sourceforge.pebble;
    33  
    34  import net.sourceforge.pebble.dao.DAOFactory;
    35  import net.sourceforge.pebble.dao.file.FileDAOFactory;
    36  import net.sourceforge.pebble.security.SecurityRealm;
    37  import org.apache.commons.logging.Log;
    38  import org.apache.commons.logging.LogFactory;
    39  
    40  /**
    41   * A bean representing configurable properties for Pebble.
    42   *
    43   * @author    Simon Brown
    44   */
    45  public class Configuration {
    46  
    47    /** the log used by this class */
           /* 
    P/P     *  Method: net.sourceforge.pebble.Configuration__static_init
            * 
            *  Postconditions:
            *    init'ed(log)
            */
    48    private static Log log = LogFactory.getLog(Configuration.class);
    49  
    50    private String dataDirectory = "${user.home}/pebble";
    51    private String url;
    52    private String secureUrl;
    53    private boolean multiBlog = false;
    54    private boolean virtualHostingEnabled = false;
    55    private boolean userThemesEnabled = true;
    56    private String smtpHost = "java:comp/env/mail/Session";
    57    private long fileUploadSize = 2048;
    58    private long fileUploadQuota = -1;
    59    private DAOFactory daoFactory = new FileDAOFactory();
    60    private SecurityRealm securityRealm;
    61  
           /* 
    P/P     *  Method: void net.sourceforge.pebble.Configuration()
            * 
            *  Postconditions:
            *    this.daoFactory == &new FileDAOFactory(Configuration#1)
            *    this.dataDirectory == &"${user.home}.pebble"
            *    this.fileUploadQuota == -1
            *    this.fileUploadSize == 2_048
            *    this.multiBlog == 0
            *    this.virtualHostingEnabled == 0
            *    this.smtpHost == &"java:comp.env.mail.Session"
            *    this.userThemesEnabled == 1
            *    new FileDAOFactory(Configuration#1) num objects == 1
            *    new FileBlogEntryDAO(FileDAOFactory#1) num objects == 1
            *    ...
            */
    62    public Configuration() {
    63    }
    64  
    65    public String getUrl() {
             /* 
    P/P       *  Method: String getUrl()
              * 
              *  Preconditions:
              *    init'ed(this.url)
              * 
              *  Postconditions:
              *    return_value == this.url
              *    init'ed(return_value)
              */
    66      return url;
    67    }
    68  
    69    public void setUrl(String s) {
             /* 
    P/P       *  Method: void setUrl(String)
              * 
              *  Postconditions:
              *    init'ed(this.url)
              * 
              *  Test Vectors:
              *    s: Addr_Set{null}, Inverse{null}
              *    java.lang.String:endsWith(...)@72: {1}, {0}
              *    java.lang.String:length(...)@72: {0}, {1..232-1}
              */
    70      this.url = s;
    71  
    72      if (url != null && !(url.length() == 0) && !url.endsWith("/")) {
    73        url += "/";
    74      }
    75    }
    76  
    77    public String getDomainName() {
    78      // and set the domain name
             /* 
    P/P       *  Method: String getDomainName()
              * 
              *  Presumptions:
              *    java.lang.String:indexOf(...)@80 <= 232-4
              * 
              *  Postconditions:
              *    return_value != null
              * 
              *  Test Vectors:
              *    java.lang.String:indexOf(...)@85: {-231..-1}, {0..232-1}
              * 
              *  Presumptions:
              *    net.sourceforge.pebble.PebbleContext:getConfiguration(...).url@79 != null
              *    net.sourceforge.pebble.PebbleContext:getConfiguration(...)@79 != null
              *    net.sourceforge.pebble.PebbleContext:getInstance(...)@79 != null
              */
    79      String url = PebbleContext.getInstance().getConfiguration().getUrl();
    80      int index = url.indexOf("://");
    81      String domainName = url.substring(index+3);
    82      index = domainName.indexOf("/");
    83      domainName = domainName.substring(0, index);
    84  
    85      if (domainName.indexOf(":") > -1) {
    86        // the domain name still has a port number so remove it
    87        domainName = domainName.substring(0, domainName.indexOf(":"));
    88      }
    89  
    90      return domainName;
    91    }
    92  
    93    public String getSecureUrl() {
             /* 
    P/P       *  Method: String getSecureUrl()
              * 
              *  Preconditions:
              *    init'ed(this.secureUrl)
              *    (soft) init'ed(this.url)
              * 
              *  Postconditions:
              *    return_value == One-of{this.secureUrl, this.url}
              *    (soft) init'ed(return_value)
              * 
              *  Test Vectors:
              *    this.secureUrl: Addr_Set{null}, Inverse{null}
              *    java.lang.String:length(...)@94: {0}, {1..232-1}
              */
    94      if (secureUrl != null && secureUrl.length() > 0) {
    95        return secureUrl;
    96      } else {
    97        return url;
    98      }
    99    }
   100  
   101    public void setSecureUrl(String s) {
             /* 
    P/P       *  Method: void setSecureUrl(String)
              * 
              *  Postconditions:
              *    init'ed(this.secureUrl)
              * 
              *  Test Vectors:
              *    s: Addr_Set{null}, Inverse{null}
              *    java.lang.String:endsWith(...)@104: {1}, {0}
              *    java.lang.String:length(...)@104: {0}, {1..232-1}
              */
   102      this.secureUrl = s;
   103  
   104      if (secureUrl != null && !(secureUrl.length() == 0) && !secureUrl.endsWith("/")) {
   105        secureUrl += "/";
   106      }
   107    }
   108  
   109    public String getSmtpHost() {
             /* 
    P/P       *  Method: String getSmtpHost()
              * 
              *  Preconditions:
              *    init'ed(this.smtpHost)
              * 
              *  Postconditions:
              *    return_value == this.smtpHost
              *    init'ed(return_value)
              */
   110      return smtpHost;
   111    }
   112  
   113    public void setSmtpHost(String smtpHost) {
             /* 
    P/P       *  Method: void setSmtpHost(String)
              * 
              *  Postconditions:
              *    this.smtpHost == smtpHost
              *    init'ed(this.smtpHost)
              */
   114      this.smtpHost = smtpHost;
   115    }
   116  
   117    public long getFileUploadSize() {
             /* 
    P/P       *  Method: long getFileUploadSize()
              * 
              *  Preconditions:
              *    init'ed(this.fileUploadSize)
              * 
              *  Postconditions:
              *    return_value == this.fileUploadSize
              *    init'ed(return_value)
              */
   118      return fileUploadSize;
   119    }
   120  
   121    public void setFileUploadSize(long fileUploadSize) {
             /* 
    P/P       *  Method: void setFileUploadSize(long)
              * 
              *  Postconditions:
              *    this.fileUploadSize == fileUploadSize
              *    init'ed(this.fileUploadSize)
              */
   122      this.fileUploadSize = fileUploadSize;
   123    }
   124  
   125    public long getFileUploadQuota() {
             /* 
    P/P       *  Method: long getFileUploadQuota()
              * 
              *  Preconditions:
              *    init'ed(this.fileUploadQuota)
              * 
              *  Postconditions:
              *    return_value == this.fileUploadQuota
              *    init'ed(return_value)
              */
   126      return fileUploadQuota;
   127    }
   128  
   129    public void setFileUploadQuota(long fileUploadQuota) {
             /* 
    P/P       *  Method: void setFileUploadQuota(long)
              * 
              *  Postconditions:
              *    this.fileUploadQuota == fileUploadQuota
              *    init'ed(this.fileUploadQuota)
              */
   130      this.fileUploadQuota = fileUploadQuota;
   131    }
   132  
   133    public DAOFactory getDaoFactory() {
             /* 
    P/P       *  Method: DAOFactory getDaoFactory()
              * 
              *  Preconditions:
              *    init'ed(this.daoFactory)
              * 
              *  Postconditions:
              *    return_value == this.daoFactory
              *    init'ed(return_value)
              */
   134      return daoFactory;
   135    }
   136  
   137    public void setDaoFactory(DAOFactory daoFactory) {
             /* 
    P/P       *  Method: void setDaoFactory(DAOFactory)
              * 
              *  Postconditions:
              *    this.daoFactory == daoFactory
              *    init'ed(this.daoFactory)
              */
   138      this.daoFactory = daoFactory;
   139    }
   140  
   141    public String getDataDirectory() {
             /* 
    P/P       *  Method: String getDataDirectory()
              * 
              *  Preconditions:
              *    init'ed(this.dataDirectory)
              * 
              *  Postconditions:
              *    return_value == this.dataDirectory
              *    init'ed(return_value)
              */
   142      return dataDirectory;
   143    }
   144  
   145    public void setDataDirectory(String dataDirectory) {
             /* 
    P/P       *  Method: void setDataDirectory(String)
              * 
              *  Preconditions:
              *    dataDirectory != null
              *    log != null
              * 
              *  Postconditions:
              *    this.dataDirectory != null
              */
   146      this.dataDirectory = evaluateDirectory(dataDirectory);
   147    }
   148  
   149    public boolean isMultiBlog() {
             /* 
    P/P       *  Method: bool isMultiBlog()
              * 
              *  Preconditions:
              *    init'ed(this.multiBlog)
              * 
              *  Postconditions:
              *    return_value == this.multiBlog
              *    init'ed(return_value)
              */
   150      return multiBlog;
   151    }
   152  
   153    public void setMultiBlog(boolean multiBlog) {
             /* 
    P/P       *  Method: void setMultiBlog(bool)
              * 
              *  Postconditions:
              *    this.multiBlog == multiBlog
              *    init'ed(this.multiBlog)
              */
   154      this.multiBlog = multiBlog;
   155    }
   156  
   157  
   158    public boolean isVirtualHostingEnabled() {
             /* 
    P/P       *  Method: bool isVirtualHostingEnabled()
              * 
              *  Preconditions:
              *    init'ed(this.virtualHostingEnabled)
              * 
              *  Postconditions:
              *    return_value == this.virtualHostingEnabled
              *    init'ed(return_value)
              */
   159      return virtualHostingEnabled;
   160    }
   161  
   162    public void setVirtualHostingEnabled(boolean virtualHostingEnabled) {
             /* 
    P/P       *  Method: void setVirtualHostingEnabled(bool)
              * 
              *  Postconditions:
              *    this.virtualHostingEnabled == virtualHostingEnabled
              *    init'ed(this.virtualHostingEnabled)
              */
   163      this.virtualHostingEnabled = virtualHostingEnabled;
   164    }
   165  
   166    public SecurityRealm getSecurityRealm() {
             /* 
    P/P       *  Method: SecurityRealm getSecurityRealm()
              * 
              *  Preconditions:
              *    init'ed(this.securityRealm)
              * 
              *  Postconditions:
              *    return_value == this.securityRealm
              *    init'ed(return_value)
              */
   167      return securityRealm;
   168    }
   169  
   170    public void setSecurityRealm(SecurityRealm securityRealm) {
             /* 
    P/P       *  Method: void setSecurityRealm(SecurityRealm)
              * 
              *  Postconditions:
              *    this.securityRealm == securityRealm
              *    init'ed(this.securityRealm)
              */
   171      this.securityRealm = securityRealm;
   172    }
   173  
   174    /**
   175     * Replaces ${some.property} at the start of the string with the value
   176     * from System.getProperty(some.property).
   177     *
   178     * @param s   the String to transform
   179     * @return  a new String, or the same String if it doesn't start with a
   180     *          property name delimited by ${...}
   181     */
   182    private String evaluateDirectory(String s) {
             /* 
    P/P       *  Method: String evaluateDirectory(String)
              * 
              *  Preconditions:
              *    log != null
              *    s != null
              * 
              *  Presumptions:
              *    java.lang.String:indexOf(...)@185 <= 232-2
              * 
              *  Postconditions:
              *    return_value != null
              * 
              *  Test Vectors:
              *    java.lang.String:startsWith(...)@184: {0}, {1}
              */
   183      log.debug("Raw string is " + s);
   184      if (s.startsWith("${")) {
   185        int index = s.indexOf("}");
   186        String propertyName = s.substring(2, index);
   187        String propertyValue = System.getProperty(propertyName);
   188        log.debug(propertyName + " = " + propertyValue);
   189        return propertyValue + s.substring(index+1);
   190      } else {
   191        return s;
   192      }
   193    }
   194  
   195    /**
   196     * Determines whether user themes are enabled.
   197     *
   198     * @return    true if user themes are enabled, false otherwise
   199     */
   200    public boolean isUserThemesEnabled() {
             /* 
    P/P       *  Method: bool isUserThemesEnabled()
              * 
              *  Preconditions:
              *    init'ed(this.userThemesEnabled)
              * 
              *  Postconditions:
              *    return_value == this.userThemesEnabled
              *    init'ed(return_value)
              */
   201      return userThemesEnabled;
   202    }
   203  
   204    /**
   205     * Sets whether user themes are enabled.
   206     *
   207     * @param userThemesEnabled   true if user themes are enabled,
   208     *                            false otherwise
   209     */
   210    public void setUserThemesEnabled(boolean userThemesEnabled) {
             /* 
    P/P       *  Method: void setUserThemesEnabled(bool)
              * 
              *  Postconditions:
              *    this.userThemesEnabled == userThemesEnabled
              *    init'ed(this.userThemesEnabled)
              */
   211      this.userThemesEnabled = userThemesEnabled;
   212    }
   213  }








SofCheck Inspector Build Version : 2.22510
configuration.java 2010-Jun-25 19:40:34
configuration.class 2010-Jul-19 20:23:40