joerg 2004/05/07 19:19:24
Modified: src/java/org/apache/cocoon Cocoon.java
. status.xml
Added: src/java/org/apache/cocoon RequestListener.java
src/samples/org/apache/cocoon SampleRequestListener.java
Log:
RequestListener hook added, that is called before and after a request is
processed. (thanks to Ralph Goers)
Revision Changes Path
1.24 +45 -1 cocoon-2.1/src/java/org/apache/cocoon/Cocoon.java
Index: Cocoon.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/Cocoon.java,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- Cocoon.java 10 Mar 2004 12:58:09 -0000 1.23
+++ Cocoon.java 8 May 2004 02:19:24 -0000 1.24
@@ -136,6 +136,9 @@
/** The source resolver */
protected SourceResolver sourceResolver;
+ /** An optional Avalon Component that is called before and after
processing all requests. */
+ protected RequestListener requestListener;
+
/**
* Creates a new <code>Cocoon</code> instance.
*
@@ -318,6 +321,10 @@
}
this.sourceResolver =
(SourceResolver)this.componentManager.lookup(SourceResolver.ROLE);
+
+ if (this.componentManager.hasComponent(RequestListener.ROLE)){
+ this.requestListener = (RequestListener)
this.componentManager.lookup(RequestListener.ROLE);
+ }
}
/** Dump System Properties */
@@ -480,6 +487,9 @@
this.threads = null;
if ( this.componentManager != null ) {
+ if ( this.requestListener!=null ){
+ this.componentManager.release(this.requestListener);
+ }
this.componentManager.release(this.threadSafeProcessor);
this.threadSafeProcessor = null;
@@ -615,12 +625,38 @@
this.debug(environment, false);
}
+
+ if (this.requestListener != null) {
+ try {
+ requestListener.onRequestStart(environment);
+ }
+ catch (Exception e) {
+ getLogger().error("Error encountered monitoring request
start: " + e.getMessage());
+ }
+ }
+
if (this.threadSafeProcessor != null) {
result = this.threadSafeProcessor.process(environment);
+ if (this.requestListener != null) {
+ try {
+ requestListener.onRequestEnd(environment);
+ }
+ catch (Exception e) {
+ getLogger().error("Error encountered monitoring
request start: " + e.getMessage());
+ }
+ }
} else {
Processor processor =
(Processor)this.componentManager.lookup(Processor.ROLE);
try {
result = processor.process(environment);
+ if (this.requestListener != null) {
+ try {
+ requestListener.onRequestEnd(environment);
+ }
+ catch (Exception e) {
+ getLogger().error("Error encountered monitoring
request start: " + e.getMessage());
+ }
+ }
}
finally {
this.componentManager.release(processor);
@@ -631,6 +667,14 @@
return result;
} catch (Exception any) {
+ if (this.requestListener != null) {
+ try {
+ requestListener.onRequestException(environment, any);
+ }
+ catch (Exception e) {
+ getLogger().error("Error encountered monitoring request
start: " + e.getMessage());
+ }
+ }
// reset response on error
environment.tryResetResponse();
throw any;
1.1
cocoon-2.1/src/java/org/apache/cocoon/RequestListener.java
Index: RequestListener.java
===================================================================
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cocoon;
import org.apache.avalon.framework.component.Component;
import org.apache.cocoon.environment.Environment;
/**
* This is called from with [EMAIL PROTECTED] Cocoon#process(Environment)},
before any
* requests are passed onto the [EMAIL PROTECTED] Processor} and after the
request has been
* processed. It allows all requests to be logged or monitored.
* NB Cocoon does not require an instance of this Component to function, but
if
* there is one it will be used.
*/
public interface RequestListener extends Component {
String ROLE = RequestListener.class.getName();
/**
* <p>In this method you can call, for example:
* <code>Request
req=ObjectModelHelper.getRequest(env.getObjectModel());</code>
* And then, you could use the following:
* <ul>
* <li>req.getRequestURI()</li>
* <li>req.getQueryString()</li>
* <li>req.getSession().getId()</li>
* <li>req.getLocale().getLanguage().toString()</li>
* </ul>
* <p>
* @param environment as supplied to [EMAIL PROTECTED]
Processor#process(Environment)}
* from within [EMAIL PROTECTED]
Cocoon#process(Environment)}.
*/
public void onRequestStart(Environment environment);
/**
* <p>This method is called when a request has completed. This method is
* called before the response is committed.
* @param environment as supplied to [EMAIL PROTECTED]
Processor#process(Environment)}
* from within [EMAIL PROTECTED]
Cocoon#process(Environment)}.
*/
public void onRequestEnd(Environment environment);
/**
* <p>This method is called when an exception has occurred processing the
request.
* @param environment as supplied to [EMAIL PROTECTED]
Processor#process(Environment)}
* from within [EMAIL PROTECTED]
Cocoon#process(Environment)}.
* @param throwable the error that occurred processing the request.
*/
public void onRequestException(Environment environment, Throwable
throwable);
}
1.316 +4 -1 cocoon-2.1/status.xml
Index: status.xml
===================================================================
RCS file: /home/cvs/cocoon-2.1/status.xml,v
retrieving revision 1.315
retrieving revision 1.316
diff -u -r1.315 -r1.316
--- status.xml 7 May 2004 18:32:44 -0000 1.315
+++ status.xml 8 May 2004 02:19:24 -0000 1.316
@@ -212,6 +212,9 @@
<changes>
<release version="@version@" date="@date@">
+ <action dev="JH" type="add" fixes-bug="28424" due-to="Ralph Goers"
due-to-email="[EMAIL PROTECTED]">
+ RequestListener hook added, that is called before and after a request
is processed.
+ </action>
<action dev="DC" type="update" due-to="Ralph Goers" fixes-bug="28704">
Enable XConfToolTask to modify multiple attributes. See the bug report
for description of use. Also enable suppression of comments.
1.1
cocoon-2.1/src/samples/org/apache/cocoon/SampleRequestListener.java
Index: SampleRequestListener.java
===================================================================
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cocoon;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.cocoon.environment.Environment;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
/**
* This is called from with [EMAIL PROTECTED] Cocoon#process(Environment)},
before any
* requests are passed onto the [EMAIL PROTECTED] Processor} and after the
request has been
* processed. It allows all requests to be logged or monitored.
* NB Cocoon does not require an instance of this Component to function, but
if
* there is one it will be used.
*/
public class SampleRequestListener extends AbstractLogEnabled implements
RequestListener {
/**
* <p>In this method you can call, for example:
* <code>Request
req=ObjectModelHelper.getRequest(env.getObjectModel());</code>
* And then, you could use the following:
* <ul>
* <li>req.getRequestURI()</li>
* <li>req.getQueryString()</li>
* <li>req.getSession().getId()</li>
* <li>req.getLocale().getLanguage().toString()</li>
* </ul>
* <p>
* @param environment as supplied to [EMAIL PROTECTED]
Processor#process(Environment)}
* from within [EMAIL PROTECTED]
Cocoon#process(Environment)}.
*/
public void onRequestStart(Environment environment){
Request req =
ObjectModelHelper.getRequest(environment.getObjectModel());
getLogger().info(req.getRequestURI() + " started");
}
/**
* <p>This method is called when a request has completed. This method is
called before the
* response is committed.
* @param environment as supplied to [EMAIL PROTECTED]
Processor#process(Environment)}
* from within [EMAIL PROTECTED]
Cocoon#process(Environment)}.
*/
public void onRequestEnd(Environment environment) {
Request req =
ObjectModelHelper.getRequest(environment.getObjectModel());
getLogger().info(req.getRequestURI() + " ended");
}
/**
* <p>This method is called when an exception has occurred processing the
request.
* @param environment as supplied to [EMAIL PROTECTED]
Processor#process(Environment)}
* from within [EMAIL PROTECTED]
Cocoon#process(Environment)}.
* @param throwable the error that occurred processing the request.
*/
public void onRequestException(Environment environment, Throwable
throwable) {
Request req =
ObjectModelHelper.getRequest(environment.getObjectModel());
getLogger().info(req.getRequestURI() + " failed with " +
throwable.getMessage());
}
}