Coverage Report - net.sourceforge.pebble.Configuration
 
Classes in this File Line Coverage Branch Coverage Complexity
Configuration
62%
41/66
40%
8/20
1.48
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, 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  
 
 33  
 package net.sourceforge.pebble;
 34  
 
 35  
 import net.sourceforge.pebble.dao.DAOFactory;
 36  
 import net.sourceforge.pebble.dao.file.FileDAOFactory;
 37  
 import net.sourceforge.pebble.security.SecurityRealm;
 38  
 import org.apache.commons.logging.Log;
 39  
 import org.apache.commons.logging.LogFactory;
 40  
 
 41  
 /**
 42  
  * A bean representing configurable properties for Pebble.
 43  
  *
 44  
  * @author    Simon Brown
 45  
  */
 46  
 public class Configuration {
 47  
 
 48  
   /** the log used by this class */
 49  4
   private static Log log = LogFactory.getLog(Configuration.class);
 50  
 
 51  4976
   private String dataDirectory = evaluateDirectory("${user.home}/pebble");
 52  
   private String url;
 53  
   private String secureUrl;
 54  4976
   private boolean multiBlog = false;
 55  4976
   private boolean virtualHostingEnabled = false;
 56  4976
   private boolean userThemesEnabled = true;
 57  4976
   private String smtpHost = "java:comp/env/mail/Session";
 58  4976
   private long fileUploadSize = 2048;
 59  4976
   private long fileUploadQuota = -1;
 60  4976
   private DAOFactory daoFactory = new FileDAOFactory();
 61  
   private SecurityRealm securityRealm;
 62  
 
 63  4976
   public Configuration() {
 64  4976
   }
 65  
 
 66  
   public String getUrl() {
 67  1288
     return url;
 68  
   }
 69  
 
 70  
   public void setUrl(String s) {
 71  5124
     this.url = s;
 72  
 
 73  5124
     if (url != null && !(url.length() == 0) && !url.endsWith("/")) {
 74  8
       url += "/";
 75  
     }
 76  5124
   }
 77  
 
 78  
   public String getDomainName() {
 79  
     // and set the domain name
 80  24
     String url = PebbleContext.getInstance().getConfiguration().getUrl();
 81  24
     int index = url.indexOf("://");
 82  24
     String domainName = url.substring(index+3);
 83  24
     index = domainName.indexOf("/");
 84  24
     domainName = domainName.substring(0, index);
 85  
 
 86  24
     if (domainName.indexOf(":") > -1) {
 87  
       // the domain name still has a port number so remove it
 88  4
       domainName = domainName.substring(0, domainName.indexOf(":"));
 89  
     }
 90  
 
 91  24
     return domainName;
 92  
   }
 93  
 
 94  
   public String getSecureUrl() {
 95  0
     if (secureUrl != null && secureUrl.length() > 0) {
 96  0
       return secureUrl;
 97  
     } else {
 98  0
       return url;
 99  
     }
 100  
   }
 101  
 
 102  
   public void setSecureUrl(String s) {
 103  0
     this.secureUrl = s;
 104  
 
 105  0
     if (secureUrl != null && !(secureUrl.length() == 0) && !secureUrl.endsWith("/")) {
 106  0
       secureUrl += "/";
 107  
     }
 108  0
   }
 109  
 
 110  
   public String getSmtpHost() {
 111  28
     return smtpHost;
 112  
   }
 113  
 
 114  
   public void setSmtpHost(String smtpHost) {
 115  0
     this.smtpHost = smtpHost;
 116  0
   }
 117  
 
 118  
   public long getFileUploadSize() {
 119  0
     return fileUploadSize;
 120  
   }
 121  
 
 122  
   public void setFileUploadSize(long fileUploadSize) {
 123  0
     this.fileUploadSize = fileUploadSize;
 124  0
   }
 125  
 
 126  
   public long getFileUploadQuota() {
 127  32
     return fileUploadQuota;
 128  
   }
 129  
 
 130  
   public void setFileUploadQuota(long fileUploadQuota) {
 131  0
     this.fileUploadQuota = fileUploadQuota;
 132  0
   }
 133  
 
 134  
   public DAOFactory getDaoFactory() {
 135  0
     return daoFactory;
 136  
   }
 137  
 
 138  
   public void setDaoFactory(DAOFactory daoFactory) {
 139  0
     this.daoFactory = daoFactory;
 140  0
   }
 141  
 
 142  
   public String getDataDirectory() {
 143  216
     return dataDirectory;
 144  
   }
 145  
 
 146  
   public void setDataDirectory(String dataDirectory) {
 147  4976
     this.dataDirectory = evaluateDirectory(dataDirectory);
 148  4976
   }
 149  
 
 150  
   public boolean isMultiBlog() {
 151  0
     return multiBlog;
 152  
   }
 153  
 
 154  
   public void setMultiBlog(boolean multiBlog) {
 155  0
     this.multiBlog = multiBlog;
 156  0
   }
 157  
 
 158  
 
 159  
   public boolean isVirtualHostingEnabled() {
 160  140
     return virtualHostingEnabled;
 161  
   }
 162  
 
 163  
   public void setVirtualHostingEnabled(boolean virtualHostingEnabled) {
 164  0
     this.virtualHostingEnabled = virtualHostingEnabled;
 165  0
   }
 166  
 
 167  
   public SecurityRealm getSecurityRealm() {
 168  56
     return securityRealm;
 169  
   }
 170  
 
 171  
   public void setSecurityRealm(SecurityRealm securityRealm) {
 172  2552
     this.securityRealm = securityRealm;
 173  2552
   }
 174  
 
 175  
   /**
 176  
    * Replaces ${some.property} at the start of the string with the value
 177  
    * from System.getProperty(some.property).
 178  
    *
 179  
    * @param s   the String to transform
 180  
    * @return  a new String, or the same String if it doesn't start with a
 181  
    *          property name delimited by ${...}
 182  
    */
 183  
   private String evaluateDirectory(String s) {
 184  9952
     log.debug("Raw string is " + s);
 185  9952
     if (s.startsWith("${")) {
 186  4976
       int index = s.indexOf("}");
 187  4976
       String propertyName = s.substring(2, index);
 188  4976
       String propertyValue = System.getProperty(propertyName);
 189  4976
       log.debug(propertyName + " = " + propertyValue);
 190  4976
       return propertyValue + s.substring(index+1);
 191  
     } else {
 192  4976
       return s;
 193  
     }
 194  
   }
 195  
 
 196  
   /**
 197  
    * Determines whether user themes are enabled.
 198  
    *
 199  
    * @return    true if user themes are enabled, false otherwise
 200  
    */
 201  
   public boolean isUserThemesEnabled() {
 202  0
     return userThemesEnabled;
 203  
   }
 204  
 
 205  
   /**
 206  
    * Sets whether user themes are enabled.
 207  
    *
 208  
    * @param userThemesEnabled   true if user themes are enabled,
 209  
    *                            false otherwise
 210  
    */
 211  
   public void setUserThemesEnabled(boolean userThemesEnabled) {
 212  0
     this.userThemesEnabled = userThemesEnabled;
 213  0
   }
 214  
 }