From 1c1af3d67082821ec9ec4eb39aa51bcd58977e22 Mon Sep 17 00:00:00 2001 From: markt Date: Wed, 24 Nov 2010 17:28:55 +0000 Subject: [PATCH] Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=50201 Track changes that require an update to the default access log. It isn't perfect but should cover normal usage git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1038711 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/catalina/core/StandardEngine.java | 79 ++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/java/org/apache/catalina/core/StandardEngine.java b/java/org/apache/catalina/core/StandardEngine.java index bbc1fc3ca..727f63f47 100644 --- a/java/org/apache/catalina/core/StandardEngine.java +++ b/java/org/apache/catalina/core/StandardEngine.java @@ -16,15 +16,22 @@ */ package org.apache.catalina.core; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import java.util.Locale; import org.apache.catalina.AccessLog; import org.apache.catalina.Container; +import org.apache.catalina.ContainerEvent; +import org.apache.catalina.ContainerListener; import org.apache.catalina.Context; import org.apache.catalina.Engine; import org.apache.catalina.Globals; import org.apache.catalina.Host; +import org.apache.catalina.Lifecycle; +import org.apache.catalina.LifecycleEvent; import org.apache.catalina.LifecycleException; +import org.apache.catalina.LifecycleListener; import org.apache.catalina.Realm; import org.apache.catalina.Service; import org.apache.catalina.connector.Request; @@ -321,12 +328,24 @@ public class StandardEngine extends ContainerBase implements Engine { if (host != null && host.getState().isAvailable()) { defaultAccessLog = host.getAccessLog(); - if (defaultAccessLog == null) { + if (defaultAccessLog != null) { + AccessLogListener l = new AccessLogListener(this); + host.addPropertyChangeListener(l); + host.addContainerListener(l); + host.addLifecycleListener(l); + } else { // Try the ROOT context of default host Context context = (Context) host.findChild(""); if (context != null && context.getState().isAvailable()) { defaultAccessLog = context.getAccessLog(); + + if (defaultAccessLog != null) { + AccessLogListener l = + new AccessLogListener(this); + context.addPropertyChangeListener(l); + context.addLifecycleListener(l); + } } } } @@ -369,4 +388,62 @@ public class StandardEngine extends ContainerBase implements Engine { // NOOP } } + + protected static final class AccessLogListener + implements PropertyChangeListener, LifecycleListener, + ContainerListener { + + private StandardEngine engine; + private volatile boolean disabled = false; + + public AccessLogListener(StandardEngine engine) { + this.engine = engine; + } + + @Override + public void lifecycleEvent(LifecycleEvent event) { + if (disabled) return; + + String type = event.getType(); + if (Lifecycle.AFTER_START_EVENT.equals(type) || + Lifecycle.BEFORE_STOP_EVENT.equals(type)) { + // Container is being started/stopped + // Force re-calculation but do not disable listener since it + // might be re-used + engine.defaultAccessLog = null; + } else if (Lifecycle.BEFORE_DESTROY_EVENT.endsWith(type)) { + // Container is being removed + // Force re-calculation and disable listener since it won't + // be re-used + engine.defaultAccessLog = null; + disabled = true; + } + } + + @Override + public void propertyChange(PropertyChangeEvent evt) { + if (disabled) return; + if ("defaultHost".equals(evt.getPropertyName())) { + // Force re-calculation and disable listener since it won't + // be re-used + engine.defaultAccessLog = null; + disabled = true; + } + } + + @Override + public void containerEvent(ContainerEvent event) { + // Only useful for hosts + if (disabled) return; + + if (Container.ADD_CHILD_EVENT.equals(event.getType())) { + Context context = (Context) event.getData(); + if ("".equals(context.getPath())) { + // New ROOT context in default host + // Force recalculation of default access log + engine.defaultAccessLog = null; + } + } + } + } } -- 2.11.0