horwat 2002/08/08 19:10:10 Modified: coyote/src/java/org/apache/coyote/tomcat5 CoyoteRequest.java LocalStrings.properties Log: Add servlet request attribute change event support. Revision Changes Path 1.2 +130 -6 jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat5/CoyoteRequest.java Index: CoyoteRequest.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat5/CoyoteRequest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- CoyoteRequest.java 4 Aug 2002 19:39:49 -0000 1.1 +++ CoyoteRequest.java 9 Aug 2002 02:10:10 -0000 1.2 @@ -91,6 +91,10 @@ import javax.servlet.ServletException; import javax.servlet.ServletInputStream; import javax.servlet.ServletRequest; +import javax.servlet.ServletRequestEvent; +import javax.servlet.ServletRequestListener; +import javax.servlet.ServletRequestAttributeEvent; +import javax.servlet.ServletRequestAttributeListener; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -105,6 +109,7 @@ import org.apache.catalina.Context; import org.apache.catalina.Globals; import org.apache.catalina.HttpRequest; +import org.apache.catalina.Logger; import org.apache.catalina.Manager; import org.apache.catalina.Realm; import org.apache.catalina.Session; @@ -214,6 +219,12 @@ /** + * List of read only attributes for this Request. + */ + private HashMap readOnlyAttributes = new HashMap(); + + + /** * The preferred Locales assocaited with this Request. */ protected ArrayList locales = new ArrayList(); @@ -1136,7 +1147,42 @@ * @param name Name of the request attribute to remove */ public void removeAttribute(String name) { - attributes.remove(name); + Object value = null; + boolean found = false; + + // Remove the specified attribute + synchronized (attributes) { + // Check for read only attribute + if (readOnlyAttributes.containsKey(name)) + return; + found = attributes.containsKey(name); + if (found) { + value = attributes.get(name); + attributes.remove(name); + } else { + return; + } + } + + // Notify interested application event listeners + Object listeners[] = context.getApplicationListeners(); + if ((listeners == null) || (listeners.length == 0)) + return; + ServletRequestAttributeEvent event = + new ServletRequestAttributeEvent(context.getServletContext(), + getRequest(), name, value); + for (int i = 0; i < listeners.length; i++) { + if (!(listeners[i] instanceof ServletRequestAttributeListener)) + continue; + ServletRequestAttributeListener listener = + (ServletRequestAttributeListener) listeners[i]; + try { + listener.attributeRemoved(event); + } catch (Throwable t) { + // FIXME - should we do anything besides log these? + log(sm.getString("coyoteRequest.attributeEvent"), t); + } + } } @@ -1159,8 +1205,50 @@ return; } - attributes.put(name, value); + Object oldValue = null; + boolean replaced = false; + // Add or replace the specified attribute + synchronized (attributes) { + // Check for read only attribute + if (readOnlyAttributes.containsKey(name)) + return; + oldValue = attributes.get(name); + if (oldValue != null) + replaced = true; + attributes.put(name, value); + } + + // Notify interested application event listeners + Object listeners[] = context.getApplicationListeners(); + if ((listeners == null) || (listeners.length == 0)) + return; + ServletRequestAttributeEvent event = null; + if (replaced) + event = + new ServletRequestAttributeEvent(context.getServletContext(), + getRequest(), name, oldValue); + else + event = + new ServletRequestAttributeEvent(context.getServletContext(), + getRequest(), name, value); + + for (int i = 0; i < listeners.length; i++) { + if (!(listeners[i] instanceof ServletRequestAttributeListener)) + continue; + ServletRequestAttributeListener listener = + (ServletRequestAttributeListener) listeners[i]; + try { + if (replaced) { + listener.attributeReplaced(event); + } else { + listener.attributeAdded(event); + } + } catch (Throwable t) { + // FIXME - should we do anything besides log these? + log(sm.getString("coyoteRequest.attributeEvent"), t); + } + } } @@ -2101,5 +2189,41 @@ } + + /** + * Log a message on the Logger associated with our Container (if any). + * + * @param message Message to be logged + */ + private void log(String message) { + + Logger logger = connector.getContainer().getLogger(); + String localName = "CoyoteRequest"; + if (logger != null) + logger.log(localName + " " + message); + else + System.out.println(localName + " " + message); + + } + + + /** + * Log a message on the Logger associated with our Container (if any). + * + * @param message Message to be logged + * @param throwable Associated exception + */ + private void log(String message, Throwable throwable) { + + Logger logger = connector.getContainer().getLogger(); + String localName = "CoyoteRequest"; + if (logger != null) + logger.log(localName + " " + message, throwable); + else { + System.out.println(localName + " " + message); + throwable.printStackTrace(System.out); + } + + } } 1.2 +3 -0 jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat5/LocalStrings.properties Index: LocalStrings.properties =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat5/LocalStrings.properties,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- LocalStrings.properties 4 Aug 2002 19:39:49 -0000 1.1 +++ LocalStrings.properties 9 Aug 2002 02:10:10 -0000 1.2 @@ -36,3 +36,6 @@ coyoteRequest.getReader.ise=getInputStream() has already been called for this request coyoteRequest.sessionCreateCommitted=Cannot create a session after the response has been committed coyoteRequest.setAttribute.namenull=Cannot call setAttribute with a null name +coyoteRequest.listenerStart=Exception sending context initialized event to listener instance of class {0} +coyoteRequest.listenerStop=Exception sending context destroyed event to listener instance of class {0} +coyoteRequest.attributeEvent=Exception thrown by attributes event listener
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>