pebblecontext.java

### File Source: pebblecontext.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.security.SecurityRealm;
        35  import net.sourceforge.pebble.util.RelativeDate;
        36  import org.apache.commons.logging.Log;
        37  import org.apache.commons.logging.LogFactory;
        38
        39  import java.io.IOException;
        40  import java.io.InputStream;
        41  import java.util.Date;
        42  import java.util.Properties;
        43
        44  /**
        45   * A bean representing the Pebble context.
        46   *
        47   * @author    Simon Brown
        48   */
        49  public class PebbleContext {
        50
        51    /** the log used by this class */
               /*
        P/P     *  Method: net.sourceforge.pebble.PebbleContext__static_init
                *
                *  Presumptions:
                *    org.apache.commons.logging.LogFactory:getLog(...)@52 != null
                *
                *  Postconditions:
                *    instance == &new PebbleContext(PebbleContext__static_init#1)
                *    (soft) log != null
                *    new Date(PebbleContext#1) num objects == 1
                *    new PebbleContext(PebbleContext__static_init#1) num objects == 1
                *    possibly_updated(instance.buildVersion)
                *    instance.startTime == &new Date(PebbleContext#1)
                */
        52    private static Log log = LogFactory.getLog(PebbleContext.class);
        53
        54    private Configuration configuration;
        55
        56    private String buildVersion;
        57    private String buildDate;
        58
        59    private static final String BUILD_VERSION_KEY = "build.version";
        60    private static final String BUILD_DATE_KEY = "build.date";
        61
        62    /** the time that Pebble was started */
        63    private Date startTime;
        64
        65    /** the directory where Pebble is deployed */
        66    private String webApplicationRoot;
        67
        68    private static final PebbleContext instance = new PebbleContext();
        69
        70    public static PebbleContext getInstance() {
                 /*
        P/P       *  Method: PebbleContext getInstance()
                  *
                  *  Postconditions:
                  *    return_value == &new PebbleContext(PebbleContext__static_init#1)
                  */
        71      return instance;
        72    }
        73
               /*
        P/P     *  Method: void net.sourceforge.pebble.PebbleContext()
                *
                *  Preconditions:
                *    (soft) log != null
                *
                *  Presumptions:
                *    java.lang.Class:getClassLoader(...)@80 != null
                *    java.lang.Object:getClass(...)@80 != null
                *
                *  Postconditions:
                *    possibly_updated(this.buildVersion)
                *    this.startTime == &new Date(PebbleContext#1)
                *    new Date(PebbleContext#1) num objects == 1
                *
                *  Test Vectors:
                *    java.lang.ClassLoader:getResourceAsStream(...)@80: Addr_Set{null}, Inverse{null}
                */
        74    private PebbleContext() {
        75      // and note when Pebble was started
        76      this.startTime = new Date();
        77
        78      try {
        79        Properties buildProperties = new Properties();
        80        InputStream in = getClass().getClassLoader().getResourceAsStream(
        81                "META-INF/maven/org.sourceforge.pebble/pebble/pom.properties");
        82        if (in != null) {
        83          buildProperties.load(in);
        84          this.buildVersion = buildProperties.getProperty("version");
        85          // It's a little harder to do this in maven, seeing as it's not used anyway, commenting out.
        86          // this.buildDate = buildProperties.getProperty(BUILD_DATE_KEY);
        87          in.close();
        88        } else {
        89          log.warn("No maven pom.properties found, build version set for development");
        90        }
        91      } catch (IOException e) {
        92        log.error(e.getMessage(), e);
        93        e.printStackTrace();
        94      }
        95    }
        96
        97    /**
        98     * Gets the version of Pebble being used.
        99     *
       100     * @return    the version number as a String
       101     */
       102    public String getBuildVersion() {
                 /*
        P/P       *  Method: String getBuildVersion()
                  *
                  *  Preconditions:
                  *    init'ed(this.buildVersion)
                  *
                  *  Postconditions:
                  *    return_value == this.buildVersion
                  *    init'ed(return_value)
                  */
       103      return this.buildVersion;
       104    }
       105
       106    /**
       107     * Gets the date that Pebble was built.
       108     *
       109     * @return    the date as a String
       110     */
       111    public String getBuildDate() {
                 /*
        P/P       *  Method: String getBuildDate()
                  *
                  *  Preconditions:
                  *    init'ed(this.buildDate)
                  *
                  *  Postconditions:
                  *    return_value == this.buildDate
                  *    init'ed(return_value)
                  */
       112      return this.buildDate;
       113    }
       114
       115    /**
       116     * Gets the amount of time that Pebble has been running for.
       117     *
       118     * @return  a number of milliseconds
       119     */
       120    public RelativeDate getUptime() {
                 /*
        P/P       *  Method: RelativeDate getUptime()
                  *
                  *  Preconditions:
                  *    this.startTime != null
                  *
                  *  Presumptions:
                  *    java.util.Date:getTime(...)@121 - java.util.Date:getTime(...)@121 in -264+1..263
                  *
                  *  Postconditions:
                  *    return_value == &new RelativeDate(getUptime#1)
                  *    new RelativeDate(getUptime#1) num objects == 1
                  *    (soft) init'ed(return_value.time)
                  */
       121      return new RelativeDate(new Date().getTime() - startTime.getTime());
       122    }
       123
       124    public long getMemoryUsageInKB() {
                 /*
        P/P       *  Method: long getMemoryUsageInKB()
                  *
                  *  Presumptions:
                  *    (-(java.lang.Runtime:freeMemory(...)@125 - java.lang.Runtime:totalMemory(...)@125))/1_024 in -27_021_597_764_222_975..27_021_597_764_222_975
                  *    java.lang.Runtime:getRuntime(...)@125 != null
                  *
                  *  Postconditions:
                  *    return_value in -27_021_597_764_222_975..27_021_597_764_222_975
                  */
       125      return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024;
       126    }
       127
       128    public long getTotalMemoryInKB() {
                 /*
        P/P       *  Method: long getTotalMemoryInKB()
                  *
                  *  Presumptions:
                  *    java.lang.Runtime:getRuntime(...)@129 != null
                  *    java.lang.Runtime:totalMemory(...)@129/1_024 in -9_007_199_254_740_992..18_014_398_509_481_983
                  *
                  *  Postconditions:
                  *    return_value in -9_007_199_254_740_992..18_014_398_509_481_983
                  */
       129      return Runtime.getRuntime().totalMemory() / 1024;
       130    }
       131
       132    public Configuration getConfiguration() {
                 /*
        P/P       *  Method: Configuration getConfiguration()
                  *
                  *  Preconditions:
                  *    init'ed(this.configuration)
                  *
                  *  Postconditions:
                  *    return_value == this.configuration
                  *    init'ed(return_value)
                  */
       133      return configuration;
       134    }
       135
       136    public void setConfiguration(Configuration configuration) {
                 /*
        P/P       *  Method: void setConfiguration(Configuration)
                  *
                  *  Postconditions:
                  *    this.configuration == configuration
                  *    init'ed(this.configuration)
                  */
       137      this.configuration = configuration;
       138    }
       139
       140    /**
       141     * Sets the directory where themes are located.
       142     *
       143     * @param webApplicationRoot    the absolute path to the webapp root on disk
       144     */
       145    public void setWebApplicationRoot(String webApplicationRoot) {
                 /*
        P/P       *  Method: void setWebApplicationRoot(String)
                  *
                  *  Postconditions:
                  *    this.webApplicationRoot == webApplicationRoot
                  *    init'ed(this.webApplicationRoot)
                  */
       146      this.webApplicationRoot = webApplicationRoot;
       147    }
       148
       149    public String getWebApplicationRoot() {
                 /*
        P/P       *  Method: String getWebApplicationRoot()
                  *
                  *  Preconditions:
                  *    init'ed(this.webApplicationRoot)
                  *
                  *  Postconditions:
                  *    return_value == this.webApplicationRoot
                  *    init'ed(return_value)
                  */
       150      return webApplicationRoot;
       151    }
       152
       153  }

SofCheck Inspector Build Version : 2.22510

pebblecontext.java | 2010-Jun-25 19:40:34
pebblecontext.class | 2010-Jul-19 20:23:40