001: /**
002: *
003: */package clime.messadmin.providers.userdata;
004:
005: import java.text.NumberFormat;
006: import java.util.ArrayList;
007: import java.util.Collections;
008: import java.util.HashMap;
009: import java.util.Iterator;
010: import java.util.List;
011: import java.util.Map;
012:
013: import javax.servlet.ServletContext;
014: import javax.servlet.http.HttpSession;
015:
016: import clime.messadmin.i18n.I18NSupport;
017: import clime.messadmin.model.Application;
018: import clime.messadmin.model.RequestInfo;
019: import clime.messadmin.model.Server;
020: import clime.messadmin.model.Session;
021: import clime.messadmin.providers.spi.ApplicationDataProvider;
022: import clime.messadmin.providers.spi.BaseTabularDataProvider;
023: import clime.messadmin.providers.spi.SessionDataProvider;
024: import clime.messadmin.providers.spi.SizeOfProvider;
025: import clime.messadmin.servletstats.RequestInfoTimeComparator;
026: import clime.messadmin.servletstats.Utils;
027: import clime.messadmin.utils.BytesFormat;
028:
029: /**
030: * @author Cédrik LIME
031: */
032: public class ServletStatsDisplayer extends BaseTabularDataProvider
033: implements ApplicationDataProvider, SessionDataProvider {
034: private static final String BUNDLE_NAME = ServletStatsDisplayer.class
035: .getName();
036:
037: /**
038: *
039: */
040: public ServletStatsDisplayer() {
041: super ();
042: }
043:
044: /****************************************************************/
045: /* Common methods */
046: /****************************************************************/
047:
048: public String[] getTabularDataLabels(ClassLoader cl) {
049: return new String[] {
050: I18NSupport.getLocalizedMessage(BUNDLE_NAME, cl,
051: "label.url"),//$NON-NLS-1$
052: I18NSupport.getLocalizedMessage(BUNDLE_NAME, cl,
053: "label.n_errors"),//$NON-NLS-1$
054: I18NSupport.getLocalizedMessage(BUNDLE_NAME, cl,
055: "label.n_hits"),//$NON-NLS-1$
056: I18NSupport.getLocalizedMessage(BUNDLE_NAME, cl,
057: "label.used_time.mean"),//$NON-NLS-1$
058: I18NSupport.getLocalizedMessage(BUNDLE_NAME, cl,
059: "label.used_time.std_dev"),//$NON-NLS-1$
060: I18NSupport.getLocalizedMessage(BUNDLE_NAME, cl,
061: "label.used_time.total") //$NON-NLS-1$
062: };
063: }
064:
065: protected String getTableCaption(String[] labels,
066: Object[][] values, ClassLoader cl) {
067: NumberFormat numberFormatter = NumberFormat
068: .getNumberInstance(I18NSupport.getAdminLocale());
069: String caption = I18NSupport
070: .getLocalizedMessage(
071: BUNDLE_NAME,
072: cl,
073: "table.caption", new Object[] { numberFormatter.format(values.length) });//$NON-NLS-1$
074: return caption;
075: }
076:
077: protected String getDataTitle(Map data, ClassLoader cl) {
078: NumberFormat bytesFormatter = BytesFormat.getBytesInstance(
079: I18NSupport.getAdminLocale(), " ");//$NON-NLS-1$
080: long currentItemSize = SizeOfProvider.Util.getObjectSize(data,
081: cl);
082: String result = I18NSupport
083: .getLocalizedMessage(
084: BUNDLE_NAME,
085: cl,
086: "title", new Object[] { bytesFormatter.format(currentItemSize) });//$NON-NLS-1$
087: return result;
088: }
089:
090: protected Object[][] getTabularData(Map data) {
091: NumberFormat numberFormatter = NumberFormat
092: .getNumberInstance(I18NSupport.getAdminLocale());
093: List inputList = new ArrayList(data.values());
094: List resultList = new ArrayList(data.size());
095: // sort data by total used time
096: Collections.sort(inputList, new RequestInfoTimeComparator());
097: Iterator iter = inputList.iterator();
098: while (iter.hasNext()) {
099: RequestInfo request = (RequestInfo) iter.next();
100: resultList
101: .add(new Object[] {
102: request.getURL(),
103: request.getNErrors() > 0 ? "<span style=\"color: red; font-weight: bolder;\">" + numberFormatter.format(request.getNErrors()) + "</span>"//$NON-NLS-1$//$NON-NLS-2$
104: : numberFormatter.format(request
105: .getNErrors()),
106: numberFormatter.format(request.getHits()),
107: numberFormatter.format(request
108: .getMeanUsedTime())
109: + " ms",
110: numberFormatter.format(request
111: .getStdDevUsedTime())
112: + " ms",
113: numberFormatter.format(request
114: .getTotalUsedTime())
115: + " ms" });
116: }
117: Object[][] result = new Object[resultList.size()][];
118: return (Object[][]) resultList.toArray(result);
119: }
120:
121: /**
122: * {@inheritDoc}
123: */
124: public int getPriority() {
125: return 0;
126: }
127:
128: /****************************************************************/
129: /* Session methods */
130: /****************************************************************/
131:
132: public Object[][] getSessionTabularData(HttpSession httpSession) {
133: Session session = Server.getInstance().getApplication(
134: httpSession.getServletContext()).getSession(
135: httpSession.getId());
136: Map data = new HashMap(Utils.getPluginData(session));
137: Object[][] result = getTabularData(data);
138: return result;
139: }
140:
141: /**
142: * {@inheritDoc}
143: */
144: public String getSessionDataTitle(HttpSession httpSession) {
145: Application application = Server.getInstance().getApplication(
146: httpSession.getServletContext());
147: Session session = application.getSession(httpSession.getId());
148: Map data = new HashMap(Utils.getPluginData(session));
149: String result = getDataTitle(data, application
150: .getApplicationInfo().getClassLoader());
151: return result;
152: }
153:
154: /**
155: * {@inheritDoc}
156: */
157: public String getXHTMLSessionData(HttpSession session) {
158: try {
159: ClassLoader cl = getClassLoader(session);
160: String[] labels = getTabularDataLabels(cl);
161: Object[][] values = getSessionTabularData(session);
162: return buildXHTML(
163: labels,
164: values,
165: "extraSessionAttributesTable-" + getClass().getName(), getTableCaption(labels, values, cl));//$NON-NLS-1$
166: } catch (RuntimeException rte) {
167: return "Error in " + this .getClass().getName() + ": " + rte;
168: }
169: }
170:
171: /****************************************************************/
172: /* Application methods */
173: /****************************************************************/
174:
175: public Object[][] getApplicationTabularData(ServletContext context) {
176: Application application = Server.getInstance().getApplication(
177: context);
178: Map data = new HashMap(Utils.getPluginData(application));
179: Object[][] result = getTabularData(data);
180: return result;
181: }
182:
183: /**
184: * {@inheritDoc}
185: */
186: public String getApplicationDataTitle(ServletContext context) {
187: Application application = Server.getInstance().getApplication(
188: context);
189: Map data = new HashMap(Utils.getPluginData(application));
190: String result = getDataTitle(data, application
191: .getApplicationInfo().getClassLoader());
192: return result;
193: }
194:
195: /**
196: * {@inheritDoc}
197: */
198: public String getXHTMLApplicationData(ServletContext context) {
199: try {
200: ClassLoader cl = getClassLoader(context);
201: String[] labels = getTabularDataLabels(cl);
202: Object[][] values = getApplicationTabularData(context);
203: return buildXHTML(
204: labels,
205: values,
206: "extraApplicationAttributesTable-" + getClass().getName(), getTableCaption(labels, values, cl));//$NON-NLS-1$
207: } catch (RuntimeException rte) {
208: return "Error in " + this .getClass().getName() + ": " + rte;
209: }
210: }
211:
212: }
|