001: /*
002: * This is free software, licensed under the Gnu Public License (GPL)
003: * get a copy from <http://www.gnu.org/licenses/gpl.html>
004: * @version $Id: SystemInfoCommand.java,v 1.6 2005/12/14 10:53:03 hzeller Exp $
005: * @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a>
006: */
007: package henplus.commands;
008:
009: import henplus.AbstractCommand;
010: import henplus.HenPlus;
011: import henplus.SQLSession;
012: import henplus.util.ListMap;
013: import henplus.view.Column;
014: import henplus.view.ColumnMetaData;
015: import henplus.view.TableRenderer;
016: import henplus.view.util.Formatter;
017:
018: import java.util.Iterator;
019: import java.util.Map;
020:
021: /**
022: * Prints out some system information.<br>
023: * Created on: Mar 23, 2004<br>
024: */
025: public final class SystemInfoCommand extends AbstractCommand {
026:
027: private static final String CMD = "system-info";
028: private static final int ONE_KILOBYTE = 1024;
029:
030: private double _memoryUsedBefore = 0.0;
031:
032: // =================== rendering =============
033:
034: private final static ColumnMetaData[] DESC_META;
035: static {
036: DESC_META = new ColumnMetaData[2];
037: DESC_META[0] = new ColumnMetaData("System Property",
038: ColumnMetaData.ALIGN_LEFT);
039: DESC_META[1] = new ColumnMetaData("Value",
040: ColumnMetaData.ALIGN_RIGHT);
041: }
042:
043: // =======================================
044:
045: public SystemInfoCommand() {
046: super ();
047: }
048:
049: /* (non-Javadoc)
050: * @see henplus.Command#getCommandList()
051: */
052: public String[] getCommandList() {
053: return new String[] { CMD };
054: }
055:
056: /* (non-Javadoc)
057: * @see henplus.Command#participateInCommandCompletion()
058: */
059: public boolean participateInCommandCompletion() {
060: return false;
061: }
062:
063: /* (non-Javadoc)
064: * @see henplus.Command#execute(henplus.SQLSession, java.lang.String, java.lang.String)
065: */
066: public int execute(SQLSession session, String command,
067: String parameters) {
068:
069: final Map info = new ListMap();
070: info.put("Java Version", System.getProperty("java.version"));
071: info.put("Java VM", System.getProperty("java.vm.info"));
072: info.put("Java Home", System.getProperty("java.home"));
073: info.put("Java Vendor", System.getProperty("java.vendor"));
074:
075: final StringBuffer osInfo = new StringBuffer();
076: osInfo.append(System.getProperty("os.name"));
077: osInfo.append(" ");
078: osInfo.append(System.getProperty("os.version"));
079: osInfo.append(" ");
080: osInfo.append(System.getProperty("os.arch"));
081: info.put("Operating System", osInfo.toString());
082:
083: info.put("Default File Encoding", System
084: .getProperty("file.encoding"));
085:
086: //-- make sure we get almost reliable memory usage information.
087: System.gc();
088: System.gc();
089: System.gc();
090:
091: Runtime rt = Runtime.getRuntime();
092: double totalMemory = rt.totalMemory() / ONE_KILOBYTE;
093: double freeMemory = rt.freeMemory() / ONE_KILOBYTE;
094: double maxMemory = rt.maxMemory() / ONE_KILOBYTE;
095: double memoryUsed = totalMemory - freeMemory;
096: double diffMemory = memoryUsed - _memoryUsedBefore;
097: _memoryUsedBefore = memoryUsed;
098:
099: info.put("Max memory [KB]", Formatter
100: .formatNumber(maxMemory, 2));
101: info.put("Allocated memory [KB]", Formatter.formatNumber(
102: totalMemory, 2));
103: info.put("Free memory [KB]", Formatter.formatNumber(freeMemory,
104: 2));
105: info.put("Used memory [KB]", Formatter.formatNumber(memoryUsed,
106: 2));
107: info.put("Diff. of used memory (now-before) [KB]", Formatter
108: .formatNumber(diffMemory, 2));
109:
110: renderInfo(info);
111:
112: return SUCCESS;
113: }
114:
115: public boolean requiresValidSession(String cmd) {
116: return false;
117: }
118:
119: /* (non-Javadoc)
120: * @see henplus.Command#getShortDescription()
121: */
122: public String getShortDescription() {
123: return "print out some system information like memory usage.";
124: }
125:
126: /* (non-Javadoc)
127: * @see henplus.Command#getSynopsis(java.lang.String)
128: */
129: public String getSynopsis(String cmd) {
130: return CMD;
131: }
132:
133: /* (non-Javadoc)
134: * @see henplus.Command#getLongDescription(java.lang.String)
135: */
136: public String getLongDescription(String cmd) {
137: return "\tPrint out some system information like memory usage.";
138: }
139:
140: // ================== rendering ================
141:
142: private void renderInfo(Map info) {
143:
144: TableRenderer table = new TableRenderer(DESC_META, HenPlus
145: .out());
146:
147: Iterator iter = info.keySet().iterator();
148: while (iter.hasNext()) {
149: Object key = iter.next();
150: Object value = info.get(key);
151:
152: Column[] row = new Column[2];
153: row[0] = new Column(key.toString());
154: // don't call toString() on the value as it might be null
155: row[1] = new Column((String) value);
156:
157: table.addRow(row);
158: }
159: table.closeTable();
160: }
161: }
|