KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > SampleRequestListener


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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.apache.cocoon;
18
19 import org.apache.avalon.framework.logger.AbstractLogEnabled;
20 import org.apache.cocoon.environment.Environment;
21 import org.apache.cocoon.environment.ObjectModelHelper;
22 import org.apache.cocoon.environment.Request;
23
24 /**
25  * This is called from with {@link Cocoon#process(Environment)}, before any
26  * requests are passed onto the {@link Processor} and after the request has been
27  * processed. It allows all requests to be logged or monitored.
28  * NB Cocoon does not require an instance of this Component to function, but if
29  * there is one it will be used.
30  */

31 public class SampleRequestListener extends AbstractLogEnabled implements RequestListener {
32
33     /**
34      * <p>In this method you can call, for example:
35      * <code>Request req=ObjectModelHelper.getRequest(env.getObjectModel());</code>
36      * And then, you could use the following:
37      * <ul>
38      * <li>req.getRequestURI()</li>
39      * <li>req.getQueryString()</li>
40      * <li>req.getSession().getId()</li>
41      * <li>req.getLocale().getLanguage().toString()</li>
42      * </ul>
43      * <p>
44      * @param environment as supplied to {@link Processor#process(Environment)}
45      * from within {@link Cocoon#process(Environment)}.
46      */

47     public void onRequestStart(Environment environment){
48         Request req = ObjectModelHelper.getRequest(environment.getObjectModel());
49         getLogger().info(req.getRequestURI() + " started");
50     }
51
52     /**
53      * <p>This method is called when a request has completed. This method is called before the
54      * response is committed.
55      * @param environment as supplied to {@link Processor#process(Environment)}
56      * from within {@link Cocoon#process(Environment)}.
57      */

58     public void onRequestEnd(Environment environment) {
59         Request req = ObjectModelHelper.getRequest(environment.getObjectModel());
60         getLogger().info(req.getRequestURI() + " ended");
61     }
62     /**
63      * <p>This method is called when an exception has occurred processing the request.
64      * @param environment as supplied to {@link Processor#process(Environment)}
65      * from within {@link Cocoon#process(Environment)}.
66      * @param throwable the error that occurred processing the request.
67      */

68     public void onRequestException(Environment environment, Throwable JavaDoc throwable) {
69         Request req = ObjectModelHelper.getRequest(environment.getObjectModel());
70         getLogger().info(req.getRequestURI() + " failed with " + throwable.getMessage());
71     }
72 }
73
74
Popular Tags