001: /**
002: *
003: */package clime.messadmin.providers.lifecycle;
004:
005: import javax.servlet.ServletContext;
006: import javax.servlet.http.HttpServletRequest;
007: import javax.servlet.http.HttpServletResponse;
008:
009: import clime.messadmin.filter.MessAdminRequestWrapper;
010: import clime.messadmin.filter.MessAdminResponseWrapper;
011: import clime.messadmin.model.Application;
012: import clime.messadmin.model.Request;
013: import clime.messadmin.model.RequestInfo;
014: import clime.messadmin.model.Server;
015: import clime.messadmin.model.Session;
016: import clime.messadmin.providers.spi.RequestExceptionProvider;
017: import clime.messadmin.providers.spi.RequestLifeCycleProvider;
018: import clime.messadmin.servletstats.Utils;
019:
020: /**
021: * Collects statistics on Servlets
022: * @author Cédrik LIME
023: */
024: public class ServletStatsGatherer implements RequestLifeCycleProvider,
025: RequestExceptionProvider {
026: private Request request = new Request(null);
027:
028: /**
029: *
030: */
031: public ServletStatsGatherer() {
032: super ();
033: }
034:
035: /**
036: * {@inheritDoc}
037: */
038: public void requestInitialized(HttpServletRequest httpRequest,
039: HttpServletResponse httpResponse,
040: ServletContext servletContext) {
041: // Application
042: Application application = Server.getInstance().getApplication(
043: servletContext);
044: final String servletPath = httpRequest.getContextPath()
045: + httpRequest.getServletPath();
046: RequestInfo requestInfo = (RequestInfo) Utils.getPluginData(
047: application).get(servletPath);
048: if (requestInfo == null) {
049: requestInfo = new RequestInfo(servletPath);
050: Utils.getPluginData(application).put(servletPath,
051: requestInfo);
052: }
053: request.requestInitialized(requestInfo, httpRequest,
054: servletContext);
055: // Session
056: if (httpRequest.getSession(false) != null) {
057: Session session = application.getSession(httpRequest
058: .getSession(false).getId());
059: requestInfo = (RequestInfo) Utils.getPluginData(session)
060: .get(servletPath);
061: if (requestInfo == null) {
062: requestInfo = new RequestInfo(servletPath);
063: Utils.getPluginData(session).put(servletPath,
064: requestInfo);
065: }
066: request.requestInitialized(requestInfo, httpRequest,
067: servletContext);
068: }
069: }
070:
071: /**
072: * {@inheritDoc}
073: */
074: public void requestDestroyed(HttpServletRequest httpRequest,
075: HttpServletResponse httpResponse,
076: ServletContext servletContext) {
077: // Application
078: Application application = Server.getInstance().getApplication(
079: servletContext);
080: final String servletPath = httpRequest.getContextPath()
081: + httpRequest.getServletPath();
082: RequestInfo requestInfo = (RequestInfo) Utils.getPluginData(
083: application).get(servletPath);
084: if (requestInfo == null) {
085: requestInfo = new RequestInfo(servletPath);
086: Utils.getPluginData(application).put(servletPath,
087: requestInfo);
088: }
089: request
090: .requestDestroyed(requestInfo,
091: (MessAdminRequestWrapper) httpRequest,
092: (MessAdminResponseWrapper) httpResponse,
093: servletContext);
094: // Session
095: if (httpRequest.getSession(false) != null) {
096: Session session = application.getSession(httpRequest
097: .getSession(false).getId());
098: requestInfo = (RequestInfo) Utils.getPluginData(session)
099: .get(servletPath);
100: if (requestInfo == null) {
101: // session was created with this hit
102: requestInfo = new RequestInfo(servletPath);
103: Utils.getPluginData(session).put(servletPath,
104: requestInfo);
105: request.requestInitialized(requestInfo, httpRequest,
106: servletContext);
107: }
108: request.requestDestroyed(requestInfo,
109: (MessAdminRequestWrapper) httpRequest,
110: (MessAdminResponseWrapper) httpResponse,
111: servletContext);
112: }
113: }
114:
115: /**
116: * {@inheritDoc}
117: */
118: public void requestException(Exception e,
119: HttpServletRequest httpRequest,
120: HttpServletResponse httpResponse,
121: ServletContext servletContext) {
122: requestDestroyed(httpRequest, httpResponse, servletContext);
123: // Application
124: Application application = Server.getInstance().getApplication(
125: servletContext);
126: final String servletPath = httpRequest.getContextPath()
127: + httpRequest.getServletPath();
128: RequestInfo requestInfo = (RequestInfo) Utils.getPluginData(
129: application).get(servletPath);
130: if (requestInfo == null) {
131: requestInfo = new RequestInfo(servletPath);
132: Utils.getPluginData(application).put(servletPath,
133: requestInfo);
134: }
135: request
136: .requestException(requestInfo, e,
137: (MessAdminRequestWrapper) httpRequest,
138: (MessAdminResponseWrapper) httpResponse,
139: servletContext);
140: // Session
141: if (httpRequest.getSession(false) != null) {
142: Session session = application.getSession(httpRequest
143: .getSession(false).getId());
144: requestInfo = (RequestInfo) Utils.getPluginData(session)
145: .get(servletPath);
146: if (requestInfo == null) {
147: requestInfo = new RequestInfo(servletPath);
148: Utils.getPluginData(session).put(servletPath,
149: requestInfo);
150: }
151: request.requestException(requestInfo, e,
152: (MessAdminRequestWrapper) httpRequest,
153: (MessAdminResponseWrapper) httpResponse,
154: servletContext);
155: }
156: }
157:
158: /**
159: * {@inheritDoc}
160: */
161: public int getPriority() {
162: // no need for a priority, really
163: return 0;
164: }
165:
166: }
|