001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.context.support;
018:
019: import org.springframework.context.ApplicationEvent;
020:
021: /**
022: * Event raised when a request is handled within an ApplicationContext.
023: *
024: * <p>Supported by Spring's own FrameworkServlet (through a specific
025: * ServletRequestHandledEvent subclass), but can also be raised by any
026: * other web component. Used, for example, by Spring's out-of-the-box
027: * PerformanceMonitorListener.
028: *
029: * @author Rod Johnson
030: * @author Juergen Hoeller
031: * @since January 17, 2001
032: * @see ServletRequestHandledEvent
033: * @see PerformanceMonitorListener
034: * @see org.springframework.web.servlet.FrameworkServlet
035: * @see org.springframework.context.ApplicationContext#publishEvent
036: */
037: public class RequestHandledEvent extends ApplicationEvent {
038:
039: /** Session id that applied to the request, if any */
040: private String sessionId;
041:
042: /** Usually the UserPrincipal */
043: private String userName;
044:
045: /** Request processing time */
046: private final long processingTimeMillis;
047:
048: /** Cause of failure, if any */
049: private Throwable failureCause;
050:
051: /**
052: * Create a new RequestHandledEvent with session information.
053: * @param source the component that published the event
054: * @param sessionId the id of the HTTP session, if any
055: * @param userName the name of the user that was associated with the
056: * request, if any (usually the UserPrincipal)
057: * @param processingTimeMillis the processing time of the request in milliseconds
058: */
059: public RequestHandledEvent(Object source, String sessionId,
060: String userName, long processingTimeMillis) {
061: super (source);
062: this .sessionId = sessionId;
063: this .userName = userName;
064: this .processingTimeMillis = processingTimeMillis;
065: }
066:
067: /**
068: * Create a new RequestHandledEvent with session information.
069: * @param source the component that published the event
070: * @param sessionId the id of the HTTP session, if any
071: * @param userName the name of the user that was associated with the
072: * request, if any (usually the UserPrincipal)
073: * @param processingTimeMillis the processing time of the request in milliseconds
074: * @param failureCause the cause of failure, if any
075: */
076: public RequestHandledEvent(Object source, String sessionId,
077: String userName, long processingTimeMillis,
078: Throwable failureCause) {
079:
080: this (source, sessionId, userName, processingTimeMillis);
081: this .failureCause = failureCause;
082: }
083:
084: /**
085: * Return the processing time of the request in milliseconds.
086: */
087: public long getProcessingTimeMillis() {
088: return this .processingTimeMillis;
089: }
090:
091: /**
092: * Return the id of the HTTP session, if any.
093: */
094: public String getSessionId() {
095: return this .sessionId;
096: }
097:
098: /**
099: * Return the name of the user that was associated with the request
100: * (usually the UserPrincipal).
101: * @see javax.servlet.http.HttpServletRequest#getUserPrincipal()
102: */
103: public String getUserName() {
104: return this .userName;
105: }
106:
107: /**
108: * Return whether the request failed.
109: */
110: public boolean wasFailure() {
111: return (this .failureCause != null);
112: }
113:
114: /**
115: * Return the cause of failure, if any.
116: */
117: public Throwable getFailureCause() {
118: return this .failureCause;
119: }
120:
121: /**
122: * Return a short description of this event, only involving
123: * the most important context data.
124: */
125: public String getShortDescription() {
126: StringBuffer sb = new StringBuffer();
127: sb.append("session=[").append(this .sessionId).append("]; ");
128: sb.append("user=[").append(this .userName).append("]; ");
129: return sb.toString();
130: }
131:
132: /**
133: * Return a full description of this event, involving
134: * all available context data.
135: */
136: public String getDescription() {
137: StringBuffer sb = new StringBuffer();
138: sb.append("session=[").append(this .sessionId).append("]; ");
139: sb.append("user=[").append(this .userName).append("]; ");
140: sb.append("time=[").append(this .processingTimeMillis).append(
141: "ms]; ");
142: sb.append("status=[");
143: if (!wasFailure()) {
144: sb.append("OK");
145: } else {
146: sb.append("failed: ").append(this .failureCause);
147: }
148: sb.append(']');
149: return sb.toString();
150: }
151:
152: public String toString() {
153: return ("RequestHandledEvent: " + getDescription());
154: }
155:
156: }
|