Coverage Report - net.sourceforge.pebble.PebbleContext
 
Classes in this File Line Coverage Branch Coverage Complexity
PebbleContext
46%
18/39
16%
1/6
1.4
 
 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  
 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  
 import org.springframework.context.ApplicationContext;
 39  
 
 40  
 import java.io.IOException;
 41  
 import java.io.InputStream;
 42  
 import java.util.Date;
 43  
 import java.util.Map;
 44  
 import java.util.Properties;
 45  
 
 46  
 /**
 47  
  * A bean representing the Pebble context.
 48  
  *
 49  
  * @author    Simon Brown
 50  
  */
 51  
 public class PebbleContext {
 52  
 
 53  
   /** the log used by this class */
 54  4
   private static Log log = LogFactory.getLog(PebbleContext.class);
 55  
 
 56  
   private Configuration configuration;
 57  
   private ApplicationContext applicationContext;
 58  
 
 59  
   private String buildVersion;
 60  
   private String buildDate;
 61  
 
 62  
   private static final String BUILD_VERSION_KEY = "build.version";
 63  
   private static final String BUILD_DATE_KEY = "build.date";
 64  
 
 65  
   /** the time that Pebble was started */
 66  
   private Date startTime;
 67  
 
 68  
   /** the directory where Pebble is deployed */
 69  
   private String webApplicationRoot;
 70  
 
 71  4
   private static final PebbleContext instance = new PebbleContext();
 72  
 
 73  
   public static PebbleContext getInstance() {
 74  14456
     return instance;
 75  
   }
 76  
 
 77  4
   private PebbleContext() {
 78  
     // and note when Pebble was started
 79  4
     this.startTime = new Date();
 80  
 
 81  
     try {
 82  4
       Properties buildProperties = new Properties();
 83  4
       InputStream in = getClass().getClassLoader().getResourceAsStream(
 84  
               "META-INF/maven/org.sourceforge.pebble/pebble/pom.properties");
 85  4
       if (in != null) {
 86  0
         buildProperties.load(in);
 87  0
         this.buildVersion = buildProperties.getProperty("version");
 88  
         // It's a little harder to do this in maven, seeing as it's not used anyway, commenting out.
 89  
         // this.buildDate = buildProperties.getProperty(BUILD_DATE_KEY);
 90  0
         in.close();
 91  
       } else {
 92  4
         log.warn("No maven pom.properties found, build version set for development");
 93  4
         this.buildVersion = "dev";
 94  
       }
 95  0
     } catch (IOException e) {
 96  0
       log.error(e.getMessage(), e);
 97  0
       e.printStackTrace();
 98  4
     }
 99  4
   }
 100  
 
 101  
   /**
 102  
    * Gets the version of Pebble being used.
 103  
    *
 104  
    * @return    the version number as a String
 105  
    */
 106  
   public String getBuildVersion() {
 107  0
     return this.buildVersion;
 108  
   }
 109  
 
 110  
   /**
 111  
    * Gets the date that Pebble was built.
 112  
    *
 113  
    * @return    the date as a String
 114  
    */
 115  
   public String getBuildDate() {
 116  0
     return this.buildDate;
 117  
   }
 118  
 
 119  
   /**
 120  
    * Gets the amount of time that Pebble has been running for.
 121  
    *
 122  
    * @return  a number of milliseconds
 123  
    */
 124  
   public RelativeDate getUptime() {
 125  0
     return new RelativeDate(new Date().getTime() - startTime.getTime());
 126  
   }
 127  
 
 128  
   public long getMemoryUsageInKB() {
 129  0
     return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024;
 130  
   }
 131  
 
 132  
   public long getTotalMemoryInKB() {
 133  0
     return Runtime.getRuntime().totalMemory() / 1024;
 134  
   }
 135  
 
 136  
   public Configuration getConfiguration() {
 137  4216
     return configuration;
 138  
   }
 139  
 
 140  
   public void setConfiguration(Configuration configuration) {
 141  4976
     this.configuration = configuration;
 142  4976
   }
 143  
 
 144  
   /**
 145  
    * Sets the directory where themes are located.
 146  
    *
 147  
    * @param webApplicationRoot    the absolute path to the webapp root on disk
 148  
    */
 149  
   public void setWebApplicationRoot(String webApplicationRoot) {
 150  0
     this.webApplicationRoot = webApplicationRoot;
 151  0
   }
 152  
 
 153  
   public String getWebApplicationRoot() {
 154  0
     return webApplicationRoot;
 155  
   }
 156  
 
 157  
   public void setApplicationContext(ApplicationContext applicationContext) {
 158  2560
     this.applicationContext = applicationContext;
 159  2560
   }
 160  
 
 161  
   public ApplicationContext getApplicationContext() {
 162  2720
     return applicationContext;
 163  
   }
 164  
 
 165  
   public <T> T getComponent(String name, Class<T> type) {
 166  0
     return applicationContext.getBean(name, type);
 167  
   }
 168  
 
 169  
   public <T> T getComponent(Class<T> type) {
 170  0
     Map<String, T> map = applicationContext.getBeansOfType(type);
 171  0
     if (map.isEmpty()) {
 172  0
       return null;
 173  0
     } else if (map.size() > 1) {
 174  0
       throw new IllegalArgumentException("Multiple beans of type " + type + " found.");
 175  
     } else {
 176  0
       return map.values().iterator().next();
 177  
     }
 178  
   }
 179  
 }