| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 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 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public class PebbleContext { |
| 52 | |
|
| 53 | |
|
| 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 | |
|
| 66 | |
private Date startTime; |
| 67 | |
|
| 68 | |
|
| 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 | |
|
| 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 | |
|
| 89 | |
|
| 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 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
public String getBuildVersion() { |
| 107 | 0 | return this.buildVersion; |
| 108 | |
} |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public String getBuildDate() { |
| 116 | 0 | return this.buildDate; |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 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 | |
|
| 146 | |
|
| 147 | |
|
| 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 | |
} |