001: /**
002: *
003: */package clime.messadmin.providers.displayformat;
004:
005: import java.io.IOException;
006: import java.io.PrintWriter;
007: import java.text.DateFormat;
008: import java.text.NumberFormat;
009: import java.text.SimpleDateFormat;
010: import java.util.Collection;
011: import java.util.Date;
012: import java.util.Iterator;
013: import java.util.Map;
014: import java.util.Set;
015:
016: import javax.servlet.ServletConfig;
017: import javax.servlet.ServletContext;
018: import javax.servlet.ServletException;
019: import javax.servlet.http.HttpServletRequest;
020: import javax.servlet.http.HttpServletResponse;
021:
022: import clime.messadmin.i18n.I18NSupport;
023: import clime.messadmin.model.IApplicationInfo;
024: import clime.messadmin.model.IServerInfo;
025: import clime.messadmin.model.ISessionInfo;
026: import clime.messadmin.providers.spi.DisplayFormatProvider;
027: import clime.messadmin.providers.spi.SerializableProvider;
028: import clime.messadmin.providers.spi.SizeOfProvider;
029: import clime.messadmin.utils.BytesFormat;
030:
031: /**
032: * @author Cédrik LIME
033: * @since 4.1
034: */
035: public class TextFormatProvider implements DisplayFormatProvider {
036: /**
037: * Modified ISO 8601 datetime format: {@value}
038: * @see <a href="http://www.w3.org/TR/NOTE-datetime">ISO 8601 / European Standard EN 28601 DateTime</a>
039: * @see <a href="http://www.ietf.org/rfc/rfc3339.txt">RFC 3399</a>
040: */
041: protected static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";//$NON-NLS-1$
042: private transient ServletConfig config;
043:
044: /**
045: *
046: */
047: public TextFormatProvider() {
048: super ();
049: }
050:
051: /* (non-Javadoc)
052: * @see clime.messadmin.providers.spi.BaseProvider#getPriority()
053: */
054: public int getPriority() {
055: return 0;
056: }
057:
058: /* (non-Javadoc)
059: * @see clime.messadmin.providers.spi.DisplayFormatProvider#getFormatID()
060: */
061: public String getFormatID() {
062: return "text";//$NON-NLS-1$
063: }
064:
065: /* (non-Javadoc)
066: * @see clime.messadmin.providers.spi.DisplayFormatProvider#getContentType()
067: */
068: public String getContentType() {
069: return "text/plain;charset=UTF-8";//$NON-NLS-1$
070: }
071:
072: /* (non-Javadoc)
073: * @see clime.messadmin.providers.spi.DisplayFormatProvider#init(javax.servlet.ServletConfig)
074: */
075: public void init(ServletConfig config) throws ServletException {
076: this .config = config;
077: }
078:
079: /**
080: * Returns this servlet's {@link ServletConfig} object.
081: *
082: * @return ServletConfig the <code>ServletConfig</code> object
083: * that initialized this servlet
084: */
085: public ServletConfig getServletConfig() {
086: return config;
087: }
088:
089: /**
090: * Returns a reference to the {@link ServletContext} in which this servlet
091: * is running. See {@link ServletConfig#getServletContext}.
092: *
093: * <p>This method is supplied for convenience. It gets the
094: * context from the servlet's <code>ServletConfig</code> object.
095: *
096: * @return ServletContext the <code>ServletContext</code> object
097: * passed to this servlet by the <code>init</code>
098: * method
099: */
100: public ServletContext getServletContext() {
101: ServletConfig sc = getServletConfig();
102: if (sc == null) {
103: throw new IllegalStateException(
104: "err.servlet_config_not_initialized");
105: }
106: return sc.getServletContext();
107: }
108:
109: /* (non-Javadoc)
110: * @see clime.messadmin.providers.spi.DisplayFormatProvider#preProcess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
111: */
112: public void preProcess(HttpServletRequest req,
113: HttpServletResponse resp) throws ServletException,
114: IOException {
115: // nothing
116: }
117:
118: /* (non-Javadoc)
119: * @see clime.messadmin.providers.spi.DisplayFormatProvider#displayWebAppsList(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
120: */
121: public void displayWebAppsList(HttpServletRequest req,
122: HttpServletResponse resp,
123: Set/*<IApplicationInfo>*/applicationInfos)
124: throws ServletException, IOException {
125: NumberFormat numberFormat = NumberFormat
126: .getNumberInstance(I18NSupport.getAdminLocale());
127: PrintWriter out = resp.getWriter();
128: writeMenu(req, resp, out);
129: out.println();
130: out.println("List of available applications:");
131: out.println();
132: Iterator iter = applicationInfos.iterator();
133: while (iter.hasNext()) {
134: IApplicationInfo app = (IApplicationInfo) iter.next();
135: writeMenu(req, resp, out, app);
136: out.println("Id:\t\t\t" + app.getInternalContextPath());
137: out.println("Context:\t\t" + app.getContextPath());
138: out.println("Name:\t\t\t" + app.getServletContextName());
139: out
140: .println("# active sessions:\t"
141: + numberFormat.format(app
142: .getActiveSessionsCount()));
143: out.println();
144: }
145: out.close();
146: }
147:
148: /* (non-Javadoc)
149: * @see clime.messadmin.providers.spi.DisplayFormatProvider#displaySessionsListPage(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String)
150: */
151: public void displaySessionsListPage(HttpServletRequest req,
152: HttpServletResponse resp, String sortBy, String orderBy,
153: IApplicationInfo webAppStats,
154: Collection/*<ISessionInfo>*/activeSessions,
155: Collection/*<String>*/passiveSessionsIds)
156: throws ServletException, IOException {
157: BytesFormat bytesFormat = new BytesFormat(I18NSupport
158: .getAdminLocale(), " ");//$NON-NLS-1$
159: NumberFormat numberFormat = NumberFormat
160: .getNumberInstance(I18NSupport.getAdminLocale());
161: DateFormat dateTimeFormat = DateFormat.getDateInstance(
162: DateFormat.DEFAULT, I18NSupport.getAdminLocale());
163: ((SimpleDateFormat) dateTimeFormat)
164: .applyPattern(DATE_TIME_PATTERN);
165: PrintWriter out = resp.getWriter();
166: writeMenu(req, resp, out);
167: writeMenu(req, resp, out, webAppStats);
168: out.println();
169: out.println("" + numberFormat.format(activeSessions.size())
170: + " active sessions for "
171: + webAppStats.getContextPath());
172: out.println("("
173: + numberFormat.format(passiveSessionsIds.size())
174: + " passivated sessions)");
175: out.println();
176: Iterator iter = activeSessions.iterator();
177: while (iter.hasNext()) {
178: ISessionInfo session = (ISessionInfo) iter.next();
179: writeMenu(req, resp, out, webAppStats, session);
180: out.println("Id:\t\t\t" + session.getId());
181: out.println("Locale:\t\t\t" + session.getGuessedLocale());
182: out.println("User:\t\t\t" + session.getGuessedUser());
183: out.println("Creation date:\t\t"
184: + dateTimeFormat.format(new Date(session
185: .getCreationTime())));
186: out.println("Last access time:\t"
187: + dateTimeFormat.format(new Date(session
188: .getLastAccessedTime())));
189: out.println("Idle time:\t\t" + session.getIdleTime());
190: out.println("TTL:\t\t\t" + session.getTTL());
191: out.println("Size:\t\t\t"
192: + bytesFormat.format(session.getSize()));
193: out.println();
194: }
195: out.close();
196: }
197:
198: /* (non-Javadoc)
199: * @see clime.messadmin.providers.spi.DisplayFormatProvider#displaySessionDetailPage(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String, java.lang.String)
200: */
201: public void displaySessionDetailPage(HttpServletRequest req,
202: HttpServletResponse resp, IApplicationInfo webAppStats,
203: ISessionInfo currentSession) throws ServletException,
204: IOException {
205: BytesFormat bytesFormat = new BytesFormat(I18NSupport
206: .getAdminLocale(), " ");//$NON-NLS-1$
207: NumberFormat numberFormat = NumberFormat
208: .getNumberInstance(I18NSupport.getAdminLocale());
209: PrintWriter out = resp.getWriter();
210: writeMenu(req, resp, out);
211: writeMenu(req, resp, out, webAppStats);
212: out.println();
213: out.println("Id:\t\t" + currentSession.getId());
214: out.println("Remote host:\t" + currentSession.getRemoteHost());
215: out.println("User:\t\t" + currentSession.getGuessedUser());
216: out.println("Locale:\t\t" + currentSession.getGuessedLocale());
217: out.println("# hits:\t\t"
218: + numberFormat.format(currentSession.getHits()));
219: out.println("Size:\t\t"
220: + bytesFormat.format(currentSession.getSize()));
221: out.println();
222: // Session attributes
223: Map attributes = currentSession.getAttributes();
224: out.println("# attributes:\t"
225: + numberFormat.format(attributes.size()));
226: Iterator iter = attributes.entrySet().iterator();
227: while (iter.hasNext()) {
228: Map.Entry entry = (Map.Entry) iter.next();
229: out.println("\tAttribute name:\t" + entry.getKey());
230: out.println("\tAttribute Class:\t"
231: + (entry.getValue() != null ? entry.getValue()
232: .getClass().getName() : ""));
233: out.println("\tAttribute size:\t"
234: + bytesFormat.format(SizeOfProvider.Util
235: .getObjectSize(entry.getValue(),
236: webAppStats.getClassLoader())));
237: if (!SerializableProvider.Util.isSerializable(entry
238: .getValue(), webAppStats.getClassLoader())) {
239: out.println("\tAttribute is not Serializable!");
240: }
241: out.println();
242: }
243: out.close();
244: }
245:
246: /* (non-Javadoc)
247: * @see clime.messadmin.providers.spi.DisplayFormatProvider#displayWebAppStatsPage(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String)
248: */
249: public void displayWebAppStatsPage(HttpServletRequest req,
250: HttpServletResponse resp, IApplicationInfo webAppStats)
251: throws ServletException, IOException {
252: BytesFormat bytesFormat = new BytesFormat(I18NSupport
253: .getAdminLocale(), " ");//$NON-NLS-1$
254: NumberFormat numberFormat = NumberFormat
255: .getNumberInstance(I18NSupport.getAdminLocale());
256: DateFormat dateTimeFormat = DateFormat.getDateInstance(
257: DateFormat.DEFAULT, I18NSupport.getAdminLocale());
258: ((SimpleDateFormat) dateTimeFormat)
259: .applyPattern(DATE_TIME_PATTERN);
260: PrintWriter out = resp.getWriter();
261: writeMenu(req, resp, out);
262: writeMenu(req, resp, out, webAppStats);
263: out.println();
264: out.println("Details for " + webAppStats.getContextPath()
265: + " (" + webAppStats.getServletContextName() + ')');
266: out.println();
267: out.println("# active sessions:\t"
268: + numberFormat.format(webAppStats
269: .getActiveSessionsCount()));
270: out.println("# passive sessions:\t"
271: + numberFormat.format(webAppStats
272: .getPassiveSessionsCount()));
273: out.println("Total sessions size:\t"
274: + bytesFormat.format(webAppStats
275: .getActiveSessionsSize()));
276: out.println("# created sessions:\t"
277: + numberFormat.format(webAppStats
278: .getTotalCreatedSessions()));
279: out.println("max concurrent sessions:\t"
280: + numberFormat.format(webAppStats
281: .getMaxConcurrentSessions()));
282: out.println();
283: out.println("# hits:\t\t\t"
284: + numberFormat.format(webAppStats.getHits()));
285: out.println("Mean requests size:\t"
286: + bytesFormat
287: .format(webAppStats.getRequestMeanLength()));
288: out.println("Total requests size:\t"
289: + bytesFormat.format(webAppStats
290: .getRequestTotalLength()));
291: out.println("Mean response size:\t"
292: + bytesFormat.format(webAppStats
293: .getResponseMeanLength()));
294: out.println("Total response size:\t"
295: + bytesFormat.format(webAppStats
296: .getResponseTotalLength()));
297: out.println();
298: // ServletContext attributes
299: Map attributes = webAppStats.getAttributes();
300: out.println("# attributes:\t"
301: + numberFormat.format(attributes.size()));
302: Iterator iter = attributes.entrySet().iterator();
303: while (iter.hasNext()) {
304: Map.Entry entry = (Map.Entry) iter.next();
305: out.println("\tAttribute name:\t" + entry.getKey());
306: out.println("\tAttribute Class:\t"
307: + (entry.getValue() != null ? entry.getValue()
308: .getClass().getName() : ""));
309: out.println("\tAttribute size:\t"
310: + bytesFormat.format(SizeOfProvider.Util
311: .getObjectSize(entry.getValue(),
312: webAppStats.getClassLoader())));
313: out.println();
314: }
315: out.close();
316: }
317:
318: /* (non-Javadoc)
319: * @see clime.messadmin.providers.spi.DisplayFormatProvider#displayServerInfosPage(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
320: */
321: public void displayServerInfosPage(HttpServletRequest req,
322: HttpServletResponse resp, IServerInfo serverInfo)
323: throws ServletException, IOException {
324: BytesFormat bytesFormat = new BytesFormat(I18NSupport
325: .getAdminLocale(), " ");//$NON-NLS-1$
326: NumberFormat numberFormat = NumberFormat
327: .getNumberInstance(I18NSupport.getAdminLocale());
328: DateFormat dateTimeFormat = DateFormat.getDateInstance(
329: DateFormat.DEFAULT, I18NSupport.getAdminLocale());
330: ((SimpleDateFormat) dateTimeFormat)
331: .applyPattern(DATE_TIME_PATTERN);
332: PrintWriter out = resp.getWriter();
333: writeMenu(req, resp, out);
334: out.println();
335: out.println("Server Information");
336: out.println();
337: out.println("Startup time:\t"
338: + dateTimeFormat.format(serverInfo.getStartupTime()));
339: out.println("# CPUs:\t\t"
340: + numberFormat.format(serverInfo.getCpuCount()));
341: out.println("Free memory:\t"
342: + bytesFormat.format(serverInfo.getFreeMemory()));
343: out.println("Total memory:\t"
344: + bytesFormat.format(serverInfo.getTotalMemory()));
345: if (serverInfo.getTotalMemory() >= 0) {
346: out.println("Max memory:\t"
347: + bytesFormat.format(serverInfo.getMaxMemory()));
348: }
349: out.close();
350: }
351:
352: protected void writeMenu(HttpServletRequest req,
353: HttpServletResponse resp, PrintWriter out) {
354: out.print("Server information:\t");
355: out.println(resp.encodeURL(req.getRequestURL().append(
356: "?action=serverInfos&format=").append(getFormatID())
357: .toString()));
358: out.print("Applications list:\t");
359: out.println(resp.encodeURL(req.getRequestURL().append(
360: "?action=webAppsList&format=").append(getFormatID())
361: .toString()));
362: }
363:
364: protected void writeMenu(HttpServletRequest req,
365: HttpServletResponse resp, PrintWriter out,
366: IApplicationInfo app) {
367: out.print("Application stats:\t");
368: out.println(resp.encodeURL(req.getRequestURL().append(
369: "?context=").append(app.getInternalContextPath())
370: .append("&action=webAppStats&format=").append(
371: getFormatID()).toString()));
372: out.print("Sessions list:\t");
373: out.println(resp.encodeURL(req.getRequestURL().append(
374: "?context=").append(app.getInternalContextPath())
375: .append("&action=sessionsList&format=").append(
376: getFormatID()).toString()));
377: }
378:
379: protected void writeMenu(HttpServletRequest req,
380: HttpServletResponse resp, PrintWriter out,
381: IApplicationInfo app, ISessionInfo session) {
382: out.print("Session details:\t");
383: out.println(resp.encodeURL(req.getRequestURL().append(
384: "?context=").append(app.getInternalContextPath())
385: .append("&action=sessionDetail&sessionId=").append(
386: session.getId()).append("&format=").append(
387: getFormatID()).toString()));
388: }
389: }
|