KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > context > support > RequestHandledEvent


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.context.support;
18
19 import org.springframework.context.ApplicationEvent;
20
21 /**
22  * Event raised when a request is handled within an ApplicationContext.
23  *
24  * <p>Supported by Spring's own FrameworkServlet (through a specific
25  * ServletRequestHandledEvent subclass), but can also be raised by any
26  * other web component. Used, for example, by Spring's out-of-the-box
27  * PerformanceMonitorListener.
28  *
29  * @author Rod Johnson
30  * @author Juergen Hoeller
31  * @since January 17, 2001
32  * @see ServletRequestHandledEvent
33  * @see PerformanceMonitorListener
34  * @see org.springframework.web.servlet.FrameworkServlet
35  * @see org.springframework.context.ApplicationContext#publishEvent
36  */

37 public class RequestHandledEvent extends ApplicationEvent {
38
39     /** Session id that applied to the request, if any */
40     private String JavaDoc sessionId;
41
42     /** Usually the UserPrincipal */
43     private String JavaDoc userName;
44
45     /** Request processing time */
46     private final long processingTimeMillis;
47
48     /** Cause of failure, if any */
49     private Throwable JavaDoc failureCause;
50
51
52     /**
53      * Create a new RequestHandledEvent with session information.
54      * @param source the component that published the event
55      * @param sessionId the id of the HTTP session, if any
56      * @param userName the name of the user that was associated with the
57      * request, if any (usually the UserPrincipal)
58      * @param processingTimeMillis the processing time of the request in milliseconds
59      */

60     public RequestHandledEvent(Object JavaDoc source, String JavaDoc sessionId, String JavaDoc userName, long processingTimeMillis) {
61         super(source);
62         this.sessionId = sessionId;
63         this.userName = userName;
64         this.processingTimeMillis = processingTimeMillis;
65     }
66
67     /**
68      * Create a new RequestHandledEvent with session information.
69      * @param source the component that published the event
70      * @param sessionId the id of the HTTP session, if any
71      * @param userName the name of the user that was associated with the
72      * request, if any (usually the UserPrincipal)
73      * @param processingTimeMillis the processing time of the request in milliseconds
74      * @param failureCause the cause of failure, if any
75      */

76     public RequestHandledEvent(
77             Object JavaDoc source, String JavaDoc sessionId, String JavaDoc userName, long processingTimeMillis, Throwable JavaDoc failureCause) {
78
79         this(source, sessionId, userName, processingTimeMillis);
80         this.failureCause = failureCause;
81     }
82
83
84     /**
85      * Return the processing time of the request in milliseconds.
86      */

87     public long getProcessingTimeMillis() {
88         return processingTimeMillis;
89     }
90
91     /**
92      * Return the id of the HTTP session, if any.
93      */

94     public String JavaDoc getSessionId() {
95         return sessionId;
96     }
97
98     /**
99      * 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 JavaDoc getUserName() {
104         return 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 JavaDoc getFailureCause() {
118         return failureCause;
119     }
120
121
122     /**
123      * Return a short description of this event, only involving
124      * the most important context data.
125      */

126     public String JavaDoc getShortDescription() {
127         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
128         sb.append("session=[").append(this.sessionId).append("]; ");
129         sb.append("user=[").append(this.userName).append("]; ");
130         return sb.toString();
131     }
132
133     /**
134      * Return a full description of this event, involving
135      * all available context data.
136      */

137     public String JavaDoc getDescription() {
138         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
139         sb.append("session=[").append(this.sessionId).append("]; ");
140         sb.append("user=[").append(this.userName).append("]; ");
141         sb.append("time=[").append(this.processingTimeMillis).append("ms]; ");
142         sb.append("status=[");
143         if (!wasFailure()) {
144             sb.append("OK");
145         }
146         else {
147             sb.append("failed: ").append(this.failureCause);
148         }
149         sb.append(']');
150         return sb.toString();
151     }
152
153     public String JavaDoc toString() {
154         return ("RequestHandledEvent: " + getDescription());
155     }
156
157 }
158
Popular Tags