001: /**
002: *
003: */package clime.messadmin.model;
004:
005: import java.io.IOException;
006: import java.io.InputStream;
007: import java.util.ArrayList;
008: import java.util.Date;
009: import java.util.Enumeration;
010: import java.util.HashMap;
011: import java.util.Iterator;
012: import java.util.List;
013: import java.util.Map;
014: import java.util.Properties;
015: import java.util.Set;
016:
017: import javax.servlet.ServletContext;
018: import javax.servlet.http.HttpSession;
019:
020: import clime.messadmin.model.stats.MinMaxTracker;
021: import clime.messadmin.providers.ProviderUtils;
022: import clime.messadmin.providers.spi.ApplicationDataProvider;
023: import clime.messadmin.providers.spi.SizeOfProvider;
024: import clime.messadmin.utils.SessionUtils;
025: import clime.messadmin.utils.SimpleEntry;
026:
027: /**
028: * Contener for WebApp-related statistics
029: * @author Cédrik LIME
030: */
031: public class ApplicationInfo implements IApplicationInfo {
032: private final ServletContext servletContext;
033: private ClassLoader classLoader;
034:
035: protected final RequestInfo cumulativeRequestStats = new RequestInfo(
036: null);
037:
038: protected final long startupTime = System.currentTimeMillis();
039: protected MinMaxTracker activeSessions = new MinMaxTracker(0, 0, 0);
040: protected volatile int passiveSessionsCount = 0;
041: protected volatile long totalCreatedSessions = 0;//simple cumulative counter
042: protected volatile int hits = 0;//number of hits
043:
044: // cache from HttpServletRequest
045: protected volatile String contextPath;
046:
047: /**
048: *
049: */
050: public ApplicationInfo(ServletContext servletContext) {
051: super ();
052: this .servletContext = servletContext;
053: // for (int i = 0; i < responseStatus.length; ++i) {
054: // responseStatus[i] = new HitsCounter();
055: // }
056: }
057:
058: public ClassLoader getClassLoader() {
059: return classLoader;
060: }
061:
062: void setClassLoader(ClassLoader cl) {
063: classLoader = cl;
064: }
065:
066: public String getInternalContextPath() {
067: String context = SessionUtils.getContext(servletContext);
068: return context;
069: }
070:
071: /** {@inheritDoc} */
072: public int getNErrors() {
073: return cumulativeRequestStats.getNErrors();
074: }
075:
076: /** {@inheritDoc} */
077: public ErrorData getLastError() {
078: return cumulativeRequestStats.getLastError();
079: }
080:
081: /**
082: * {@inheritDoc}
083: */
084: public int getHits() {
085: return hits;
086: }
087:
088: /**
089: * {@inheritDoc}
090: */
091: public long getMaxConcurrentSessions() {
092: return activeSessions.getMax(); //concurrentSessions.getMax();
093: }
094:
095: /**
096: * {@inheritDoc}
097: */
098: public Date getMaxConcurrentSessionsDate() {
099: return activeSessions.getMaxAccessTime(); //concurrentSessions.getMaxAccessTime();
100: }
101:
102: /**
103: * {@inheritDoc}
104: */
105: public long getTotalCreatedSessions() {
106: return totalCreatedSessions;
107: }
108:
109: /**
110: * {@inheritDoc}
111: */
112: public long getRequestTotalLength() {
113: return cumulativeRequestStats.getRequestTotalLength();
114: }
115:
116: /**
117: * {@inheritDoc}
118: */
119: public long getRequestMaxLength() {
120: return cumulativeRequestStats.getRequestMaxLength();
121: }
122:
123: /**
124: * {@inheritDoc}
125: */
126: public Date getRequestMaxLengthDate() {
127: return cumulativeRequestStats.getRequestMaxLengthDate();
128: }
129:
130: /**
131: * {@inheritDoc}
132: */
133: public double getRequestMeanLength() {
134: return cumulativeRequestStats.getRequestMeanLength();
135: }
136:
137: /**
138: * {@inheritDoc}
139: */
140: public double getRequestStdDevLength() {
141: return cumulativeRequestStats.getRequestStdDevLength();
142: }
143:
144: /**
145: * {@inheritDoc}
146: */
147: public long getResponseTotalLength() {
148: return cumulativeRequestStats.getResponseTotalLength();
149: }
150:
151: /**
152: * {@inheritDoc}
153: */
154: public long getResponseMaxLength() {
155: return cumulativeRequestStats.getResponseMaxLength();
156: }
157:
158: /**
159: * {@inheritDoc}
160: */
161: public Date getResponseMaxLengthDate() {
162: return cumulativeRequestStats.getResponseMaxLengthDate();
163: }
164:
165: /**
166: * {@inheritDoc}
167: */
168: public double getResponseMeanLength() {
169: return cumulativeRequestStats.getResponseMeanLength();
170: }
171:
172: /**
173: * {@inheritDoc}
174: */
175: public double getResponseStdDevLength() {
176: return cumulativeRequestStats.getResponseStdDevLength();
177: }
178:
179: /**
180: * {@inheritDoc}
181: */
182: public int getActiveSessionsCount() {
183: return (int) activeSessions.getLastValue();
184: }
185:
186: /**
187: * {@inheritDoc}
188: */
189: public int getPassiveSessionsCount() {
190: return passiveSessionsCount;
191: }
192:
193: /** {@inheritDoc} */
194: public long getActiveSessionsSize() {
195: /* Copy all sessions' attributes in a temporary structure, to avoid concurent modifications problems */
196: Set sessions = Server.getInstance().getApplication(
197: servletContext).getActiveSessionInfos();
198: List sessionsAttributes = new ArrayList(sessions.size());
199: final long shellSize = SizeOfProvider.Util.getObjectSize(
200: sessionsAttributes, classLoader);
201: Iterator iter = sessions.iterator();
202: // for each session
203: while (iter.hasNext()) {
204: HttpSession httpSession = ((SessionInfo) iter.next())
205: .getHttpSession();
206: try {
207: Map attributes = new HashMap();
208: Enumeration enumeration = httpSession
209: .getAttributeNames();
210: // for each session attribute
211: while (enumeration.hasMoreElements()) {
212: String name = (String) enumeration.nextElement();
213: Object attribute = httpSession.getAttribute(name);
214: attributes.put(name, attribute);
215: }
216: sessionsAttributes.add(attributes);
217: } catch (IllegalStateException ise) {
218: // Session is invalidated: don't count anything
219: }
220: }
221: return SizeOfProvider.Util.getObjectSize(sessionsAttributes,
222: classLoader)
223: - shellSize;
224: }
225:
226: /**
227: * {@inheritDoc}
228: */
229: public Date getStartupTime() {
230: return new Date(startupTime);
231: }
232:
233: /**
234: * {@inheritDoc}
235: */
236: public long getUsedTimeTotal() {
237: return cumulativeRequestStats.getTotalUsedTime();
238: }
239:
240: /**
241: * {@inheritDoc}
242: */
243: public long getUsedTimeMax() {
244: return cumulativeRequestStats.getMaxUsedTime();
245: }
246:
247: /**
248: * {@inheritDoc}
249: */
250: public Date getUsedTimeMaxDate() {
251: return cumulativeRequestStats.getMaxUsedTimeDate();
252: }
253:
254: /**
255: * {@inheritDoc}
256: */
257: public double getUsedTimeMean() {
258: return cumulativeRequestStats.getMeanUsedTime();
259: }
260:
261: /**
262: * {@inheritDoc}
263: */
264: public double getUsedTimeStdDev() {
265: return cumulativeRequestStats.getStdDevUsedTime();
266: }
267:
268: /**
269: * {@inheritDoc}
270: */
271: public List getApplicationSpecificData() {
272: Iterator iter = ProviderUtils.getProviders(
273: ApplicationDataProvider.class, classLoader).iterator();
274: List result = new ArrayList();
275: while (iter.hasNext()) {
276: ApplicationDataProvider ad = (ApplicationDataProvider) iter
277: .next();
278: try {
279: String title = ad
280: .getApplicationDataTitle(servletContext);
281: String xhtml = ad
282: .getXHTMLApplicationData(servletContext);
283: if (title != null && xhtml != null) {
284: result.add(new SimpleEntry(title, xhtml));
285: }
286: } catch (RuntimeException rte) {
287: result
288: .add(new SimpleEntry(ad.getClass().getName(),
289: rte));
290: }
291: }
292: return result;
293: }
294:
295: /**
296: * {@inheritDoc}
297: */
298: public String getContextPath() {
299: return contextPath;
300: }
301:
302: /**
303: * {@inheritDoc}
304: */
305: public String getServerInfo() {
306: return servletContext.getServerInfo();
307: }
308:
309: /**
310: * {@inheritDoc}
311: */
312: public String getServletContextName() {
313: String name = servletContext.getServletContextName();
314: if (name == null) {
315: // try to fetch information from META-INF/MANIFEST.MF
316: InputStream is = servletContext
317: .getResourceAsStream("/META-INF/MANIFEST.MF");//$NON-NLS-1$
318: if (is != null) {
319: Properties manifest = new Properties();
320: try {
321: manifest.load(is);
322: is.close();
323: String appName = manifest
324: .getProperty("Implementation-Title");//$NON-NLS-1$
325: String appVersion = manifest
326: .getProperty("Implementation-Version");//$NON-NLS-1$
327: if (appName != null && appVersion != null) {
328: name = appName + '/' + appVersion;
329: }
330: } catch (IOException ioe) {
331: // ignore
332: }
333: }
334: }
335: return (name == null) ? "" : name;//$NON-NLS-1$
336: }
337:
338: /**
339: * {@inheritDoc}
340: */
341: public String getInitParameter(String name) {
342: return servletContext.getInitParameter(name);
343: }
344:
345: /**
346: * {@inheritDoc}
347: */
348: public Map getInitParameters() {
349: Map result = new HashMap();
350: Enumeration enumeration = servletContext
351: .getInitParameterNames();
352: while (enumeration.hasMoreElements()) {
353: String name = (String) enumeration.nextElement();
354: String value = servletContext.getInitParameter(name);
355: result.put(name, value);
356: }
357: return result;
358: }
359:
360: /**
361: * {@inheritDoc}
362: */
363: public Map getAttributes() {
364: Map result = new HashMap();
365: Enumeration enumeration = servletContext.getAttributeNames();
366: while (enumeration.hasMoreElements()) {
367: String name = (String) enumeration.nextElement();
368: Object value = servletContext.getAttribute(name);
369: result.put(name, value);
370: }
371: return result;
372: }
373:
374: /**
375: * {@inheritDoc}
376: */
377: public Object getAttribute(String name) {
378: return servletContext.getAttribute(name);
379: }
380:
381: /**
382: * {@inheritDoc}
383: */
384: public void setAttribute(String name, Object object) {
385: servletContext.setAttribute(name, object);
386: }
387:
388: /**
389: * {@inheritDoc}
390: */
391: public void removeAttribute(String name) {
392: servletContext.removeAttribute(name);
393: }
394:
395: }
|