001: /*
002: * Copyright (c) 2006 Your Corporation. All Rights Reserved.
003: */
004:
005: /*
006: * Created by IntelliJ IDEA.
007: * User: Jacques
008: * Date: Jan 1, 2006
009: * Time: 2:44:27 AM
010: */
011: package com.technoetic.xplanner;
012:
013: import java.io.File;
014: import java.io.IOException;
015: import java.sql.SQLException;
016: import java.text.DateFormat;
017: import java.util.Date;
018: import java.util.Iterator;
019: import java.util.Map;
020: import java.util.Properties;
021: import javax.servlet.ServletContext;
022:
023: import net.sf.hibernate.HibernateException;
024: import net.sf.hibernate.Session;
025: import net.sf.hibernate.SessionFactory;
026: import org.apache.commons.collections.map.ListOrderedMap;
027: import org.apache.commons.lang.StringUtils;
028: import org.springframework.orm.hibernate.HibernateCallback;
029: import org.springframework.orm.hibernate.HibernateTemplate;
030: import org.springframework.web.context.ServletContextAware;
031: import com.tacitknowledge.util.migration.jdbc.AutopatchSupport;
032:
033: import com.technoetic.xplanner.upgrade.XPlannerMigrationLauncherFactory;
034: import com.technoetic.xplanner.util.LogUtil;
035: import com.technoetic.xplanner.db.hsqldb.HsqlServer;
036:
037: /** @noinspection StringContatenationInLoop,MagicNumber*/
038: public class SystemInfo implements ServletContextAware {
039: static final long MEGABYTE = 1048576L;
040: private Properties properties;
041: private ServletContext servletContext;
042: private SessionFactory sessionFactory;
043:
044: public Properties getProperties() {
045: return properties;
046: }
047:
048: public void setProperties(Properties properties) {
049: this .properties = properties;
050: }
051:
052: public void setSessionFactory(SessionFactory sessionFactory) {
053: this .sessionFactory = sessionFactory;
054: }
055:
056: public static Map getSystemProperties() {
057: Properties sysProps = System.getProperties();
058: Map props = new ListOrderedMap();
059: props.put("System Date", DateFormat.getDateInstance().format(
060: new Date()));
061: props.put("System Time", DateFormat.getTimeInstance().format(
062: new Date()));
063: props.put("Current directory", getCurrentDirectory());
064:
065: props.put("Java Version", sysProps.getProperty("java.version"));
066: props.put("Java Vendor", sysProps.getProperty("java.vendor"));
067: props.put("JVM Version", sysProps
068: .getProperty("java.vm.specification.version"));
069: props.put("JVM Vendor", sysProps
070: .getProperty("java.vm.specification.vendor"));
071: props.put("JVM Implementation Version", sysProps
072: .getProperty("java.vm.version"));
073: props.put("Java Runtime", sysProps
074: .getProperty("java.runtime.name"));
075: props.put("Java VM", sysProps.getProperty("java.vm.name"));
076:
077: props.put("User Name", sysProps.getProperty("user.name"));
078: props.put("User Timezone", sysProps
079: .getProperty("user.timezone"));
080:
081: props.put("Operating System", sysProps.getProperty("os.name")
082: + " " + sysProps.getProperty("os.version"));
083: props.put("OS Architecture", sysProps.getProperty("os.arch"));
084:
085: return props;
086: }
087:
088: private static String getCurrentDirectory() {
089: try {
090: return new File(".").getCanonicalPath();
091: } catch (IOException e) {
092: // Should not happen
093: return "<undefined>";
094: }
095: }
096:
097: public Map getJVMStatistics() {
098: Map stats = new ListOrderedMap();
099: stats.put("Total Memory", "" + getTotalMemory() + "MB");
100: stats.put("Free Memory", "" + getFreeMemory() + "MB");
101: stats.put("Used Memory", "" + getUsedMemory() + "MB");
102: return stats;
103: }
104:
105: public Map getBuildInfo() {
106: Map stats = new ListOrderedMap();
107: stats.put("Version", properties
108: .getProperty(XPlannerProperties.XPLANNER_VERSION_KEY));
109: stats
110: .put(
111: "Build Date",
112: properties
113: .getProperty(XPlannerProperties.XPLANNER_BUILD_DATE_KEY));
114: stats
115: .put(
116: "Build Revision",
117: properties
118: .getProperty(XPlannerProperties.XPLANNER_BUILD_REVISION_KEY));
119: stats.put("Build Package", properties.getProperty(
120: XPlannerProperties.XPLANNER_BUILD_PACKAGE_KEY, "War"));
121: return stats;
122: }
123:
124: public Map getDatabaseInfo() {
125: Map props = new ListOrderedMap();
126: props.put("Dialect", properties
127: .getProperty("hibernate.dialect"));
128: props.put("Driver", properties
129: .getProperty("hibernate.connection.driver_class"));
130: props.put("Driver Version", getDriverVersion());
131: props.put("Database Vendor", getDatabaseVendor());
132: props.put("Database Version", getDatabaseVersion());
133: props.put("Database Name", properties
134: .getProperty("hibernate.connection.dbname"));
135: props.put("Database Url", properties
136: .getProperty(XPlannerProperties.CONNECTION_URL_KEY));
137: if (HsqlServer.isLocalPublicDatabaseStarted()) {
138: props.put("Database File", HsqlServer.getInstance()
139: .getDbPath());
140: }
141: props.put("Database User Name", properties
142: .getProperty("hibernate.connection.username"));
143: props
144: .put(
145: "Database User Password",
146: isEmpty(properties
147: .getProperty("hibernate.connection.password")) ? "[not set]"
148: : "******");
149: props.put("Database Patch Level", getDatabasePatchLevel());
150: return props;
151: }
152:
153: private String getDatabaseVendor() {
154: return (String) new HibernateTemplate(sessionFactory)
155: .execute(new HibernateCallback() {
156: public Object doInHibernate(Session session)
157: throws HibernateException, SQLException {
158: return session.connection().getMetaData()
159: .getDatabaseProductName();
160: }
161: });
162: }
163:
164: private String getDatabaseVersion() {
165: return (String) new HibernateTemplate(sessionFactory)
166: .execute(new HibernateCallback() {
167: public Object doInHibernate(Session session)
168: throws HibernateException, SQLException {
169: return session.connection().getMetaData()
170: .getDatabaseProductVersion();
171: }
172: });
173: }
174:
175: private String getDriverVersion() {
176: return (String) new HibernateTemplate(sessionFactory)
177: .execute(new HibernateCallback() {
178: public Object doInHibernate(Session session)
179: throws HibernateException, SQLException {
180: return session.connection().getMetaData()
181: .getDriverVersion();
182: }
183: });
184: }
185:
186: private static String getDatabasePatchLevel() {
187: //noinspection CatchGenericClass,OverlyBroadCatchBlock
188: try {
189: AutopatchSupport autopatchSupport = new AutopatchSupport(
190: new XPlannerMigrationLauncherFactory(),
191: XPlannerMigrationLauncherFactory.SYSTEM_NAME);
192: return "" + autopatchSupport.getPatchLevel();
193: } catch (Exception e) {
194: return "Unknown (Exception during retrieval: "
195: + e.getMessage() + ")";
196: }
197: }
198:
199: public Map getAppServerInfo() {
200: Map props = new ListOrderedMap();
201: props.put("Application Server", servletContext.getServerInfo());
202: props.put("Servlet Version", servletContext.getMajorVersion()
203: + "." + servletContext.getMinorVersion());
204: return props;
205: }
206:
207: private static boolean isEmpty(String value) {
208: return value == null || "".equals(value);
209: }
210:
211: public long getTotalMemory() {
212: return Runtime.getRuntime().totalMemory() / MEGABYTE;
213: }
214:
215: public long getFreeMemory() {
216: return Runtime.getRuntime().freeMemory() / MEGABYTE;
217: }
218:
219: public long getUsedMemory() {
220: return getTotalMemory() - getFreeMemory();
221: }
222:
223: public void setServletContext(ServletContext servletContext) {
224: this .servletContext = servletContext;
225: LogUtil.getLogger().info(
226: "*********************** XPLANNER INFO ************************\n"
227: + toString());
228: }
229:
230: public String toString() {
231: StringBuffer buf = new StringBuffer();
232: buf.append(propertiesMapToString("Build", getBuildInfo()));
233: buf
234: .append(propertiesMapToString("Database",
235: getDatabaseInfo()));
236: buf.append(propertiesMapToString("App Server",
237: getAppServerInfo()));
238: buf.append(propertiesMapToString("System",
239: getSystemProperties()));
240: return buf.toString();
241: }
242:
243: private static String propertiesMapToString(String mapName,
244: Map properties) {
245: StringBuffer buf = new StringBuffer();
246: buf.append(mapName);
247: buf.append(":\n");
248:
249: Iterator iterator = properties.keySet().iterator();
250: while (iterator.hasNext()) {
251: String name = (String) iterator.next();
252: String value = (String) properties.get(name);
253: buf.append(" ");
254: buf.append(StringUtils.rightPad(name + ":", 30));
255: buf.append(value).append("\n");
256: }
257: buf.append("\n");
258: return buf.toString();
259: }
260:
261: }
|