001: /*
002: * (C) Copyright 2000 - 2003 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.portlet.jvminfo;
021:
022: import java.io.IOException;
023:
024: import java.util.Enumeration;
025: import java.util.Properties;
026:
027: import javax.portlet.PortletConfig;
028: import javax.portlet.PortletException;
029: import javax.portlet.RenderRequest;
030: import javax.portlet.RenderResponse;
031: import javax.portlet.WindowState;
032:
033: import com.nabhinc.portlet.base.BasePortlet;
034:
035: /**
036: * Creates a table showing JVM properties on the server.
037: *
038: * @author Padmanabh Dabke
039: * (c) 2003 Nabh Information Systems, Inc. All Rights Reserved.
040: */
041: public class JVMInfoPortlet extends BasePortlet {
042:
043: private final int MAX_LENGTH = 50;
044:
045: private String jpCache = "No Info";
046:
047: /**
048: * Do nothing constructor.
049: */
050: public JVMInfoPortlet() {
051:
052: }
053:
054: public void init(PortletConfig config) throws PortletException {
055: super .init(config);
056:
057: }
058:
059: public void doView(RenderRequest request, RenderResponse response)
060: throws PortletException, IOException {
061:
062: setContent(request);
063: response.getWriter().write(jpCache);
064:
065: }
066:
067: /**
068: * Checks the length of the string. If it is greater than
069: * 100 chars, divides it.
070: * @return java.lang.String
071: * @param orig java.lang.String
072: */
073: private String formatContent(String orig) {
074:
075: if (orig == null)
076: return null;
077:
078: if (orig.length() <= MAX_LENGTH)
079: return orig;
080:
081: return orig.substring(0, MAX_LENGTH - 3) + " ...";
082:
083: }
084:
085: private void setContent(RenderRequest request) {
086:
087: WindowState state = request.getWindowState();
088:
089: StringBuffer sb = new StringBuffer();
090: sb
091: .append("<table width=\"100%\" cellpadding=\"4\" cellspacing=\"0\">");
092: sb
093: .append("<tr><td class=\"portlet-section-header\">Property Name</td><td class=\"portlet-section-header\" align=\"center\">Value</td></tr>");
094: Runtime jvm = Runtime.getRuntime();
095:
096: sb
097: .append("<tr><td class=\"portlet-section-body\">Free Memory (in bytes)</td><td class=\"portlet-section-body\">");
098: sb.append(Long.toString(jvm.freeMemory()));
099: sb.append("</td></tr>");
100:
101: sb
102: .append("<tr><td class=\"portlet-section-alternate\">Total Memory (in bytes)</td><td class=\"portlet-section-alternate\">");
103: sb.append(Long.toString(jvm.totalMemory()));
104: sb.append("</td></tr>");
105:
106: //get the system properties
107:
108: Properties props = System.getProperties();
109: Enumeration enumProps = props.propertyNames();
110:
111: String currentStyle = "portlet-section-body";
112: boolean styleFlag = true;
113:
114: try {
115: while (enumProps.hasMoreElements()) {
116: Object key = enumProps.nextElement();
117:
118: if (!(key instanceof String)) {
119: continue;
120: }
121:
122: Object value = props.getProperty(key.toString());
123: sb.append("<tr><td class=\"" + currentStyle + "\">");
124: sb.append(key.toString());
125: sb.append("</td><td class=\"" + currentStyle + "\">");
126: if (state.equals(WindowState.MAXIMIZED))
127: sb.append(value.toString());
128: else
129: sb.append(formatContent(value.toString()));
130:
131: sb.append("</td></tr>");
132:
133: if (styleFlag) {
134: styleFlag = false;
135: currentStyle = "portlet-section-alternate";
136: } else {
137: styleFlag = true;
138: currentStyle = "portlet-section-body";
139: }
140:
141: }
142: sb.append("</table>");
143: } catch (Throwable t) {
144: t.printStackTrace();
145: }
146:
147: jpCache = sb.toString();
148:
149: }
150:
151: }
|