Author: markt
Date: Tue Oct  7 12:36:17 2014
New Revision: 1629871

URL: http://svn.apache.org/r1629871
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=43682
Add support for referring to the current context, host and service name in per 
Context logging.properties files

Added:
    tomcat/trunk/java/org/apache/juli/WebappProperties.java   (with props)
Modified:
    tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoaderBase.java
    tomcat/trunk/java/org/apache/juli/ClassLoaderLogManager.java
    tomcat/trunk/webapps/docs/changelog.xml
    tomcat/trunk/webapps/docs/logging.xml

Modified: 
tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoaderBase.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoaderBase.java?rev=1629871&r1=1629870&r2=1629871&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoaderBase.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoaderBase.java Tue 
Oct  7 12:36:17 2014
@@ -65,6 +65,7 @@ import java.util.jar.Manifest;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.apache.catalina.Container;
 import org.apache.catalina.Globals;
 import org.apache.catalina.Lifecycle;
 import org.apache.catalina.LifecycleException;
@@ -73,6 +74,7 @@ import org.apache.catalina.LifecycleStat
 import org.apache.catalina.WebResource;
 import org.apache.catalina.WebResourceRoot;
 import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
+import org.apache.juli.WebappProperties;
 import org.apache.tomcat.InstrumentableClassLoader;
 import org.apache.tomcat.util.ExceptionUtils;
 import org.apache.tomcat.util.IntrospectionUtils;
@@ -123,7 +125,7 @@ import org.apache.tomcat.util.res.String
  * @author Craig R. McClanahan
  */
 public abstract class WebappClassLoaderBase extends URLClassLoader
-        implements Lifecycle, InstrumentableClassLoader {
+        implements Lifecycle, InstrumentableClassLoader, WebappProperties {
 
     private static final org.apache.juli.logging.Log log =
         org.apache.juli.logging.LogFactory.getLog(WebappClassLoaderBase.class);
@@ -2785,4 +2787,37 @@ public abstract class WebappClassLoaderB
         super.addURL(url);
         hasExternalRepositories = true;
     }
+
+
+    @Override
+    public String getWebappName() {
+        return getContextName();
+    }
+
+
+    @Override
+    public String getHostName() {
+        if (resources != null) {
+            Container host = resources.getContext().getParent();
+            if (host != null) {
+                return host.getName();
+            }
+        }
+        return null;
+    }
+
+
+    @Override
+    public String getServiceName() {
+        if (resources != null) {
+            Container host = resources.getContext().getParent();
+            if (host != null) {
+                Container engine = host.getParent();
+                if (engine != null) {
+                    return engine.getName();
+                }
+            }
+        }
+        return null;
+    }
 }

Modified: tomcat/trunk/java/org/apache/juli/ClassLoaderLogManager.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/juli/ClassLoaderLogManager.java?rev=1629871&r1=1629870&r2=1629871&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/juli/ClassLoaderLogManager.java (original)
+++ tomcat/trunk/java/org/apache/juli/ClassLoaderLogManager.java Tue Oct  7 
12:36:17 2014
@@ -14,7 +14,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.juli;
 
 import java.io.File;
@@ -618,8 +617,11 @@ public class ClassLoaderLogManager exten
                     break;
                 }
                 String propName = str.substring(pos_start + 2, pos_end);
-                String replacement = propName.length() > 0 ? System
-                        .getProperty(propName) : null;
+
+                String replacement = replaceWebApplicationProperties(propName);
+                if (replacement == null) {
+                    replacement = propName.length() > 0 ? 
System.getProperty(propName) : null;
+                }
                 if (replacement != null) {
                     builder.append(replacement);
                 } else {
@@ -633,6 +635,26 @@ public class ClassLoaderLogManager exten
         return result;
     }
 
+
+    private String replaceWebApplicationProperties(String propName) {
+        ClassLoader cl = Thread.currentThread().getContextClassLoader();
+        if (cl instanceof WebappProperties) {
+            WebappProperties wProps = (WebappProperties) cl;
+            if ("classloader.webappName".equals(propName)) {
+                return wProps.getWebappName();
+            } else if ("classloader.hostName".equals(propName)) {
+                return wProps.getHostName();
+            } else if ("classloader.serviceName".equals(propName)) {
+                return wProps.getServiceName();
+            } else {
+                return null;
+            }
+        } else {
+            return null;
+        }
+    }
+
+
     // ---------------------------------------------------- LogNode Inner Class
 
 

Added: tomcat/trunk/java/org/apache/juli/WebappProperties.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/juli/WebappProperties.java?rev=1629871&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/juli/WebappProperties.java (added)
+++ tomcat/trunk/java/org/apache/juli/WebappProperties.java Tue Oct  7 12:36:17 
2014
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.juli;
+
+/**
+ * An interface intended for use by class loaders associated with a web
+ * application that enables them to provide additional information to JULI 
about
+ * the web application with which they are associated. For any web application
+ * the combination of {@link #getWebappName()}, {@link #getHostName()} and
+ * {@link #getServiceName()} must be unique.
+ */
+public interface WebappProperties {
+
+    /**
+     * Returns a name for the web application or null if none is available.
+     */
+    String getWebappName();
+
+    /**
+     * Returns a name for the Host where the web application is deployed or 
null
+     * if none is available.
+     */
+    String getHostName();
+
+    /**
+     * Returns a name for the service where the Host is deployed or null if 
none
+     * is available.
+     */
+    String getServiceName();
+}

Propchange: tomcat/trunk/java/org/apache/juli/WebappProperties.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1629871&r1=1629870&r2=1629871&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/trunk/webapps/docs/changelog.xml Tue Oct  7 12:36:17 2014
@@ -51,6 +51,13 @@
         <bug>43548</bug>: Add an XML schema for the tomcat-users.xml file.
         (markt)
       </add>
+      <add>
+        <bug>43682</bug>: Add support for referring to the current context, 
host
+        and service name in per Context logging.properties files by using the
+        properties <code>${classloader.webappName}</code>,
+        <code>${classloader.hostName}</code> and
+        <code>${classloader.serviceName}</code>. (markt)
+      </add>
       <fix>
         <bug>55984</bug>: Using the allow separators in version 0 cookies 
option
         with the legacy cookie processor should only apply to version 0 
cookies.

Modified: tomcat/trunk/webapps/docs/logging.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/logging.xml?rev=1629871&r1=1629870&r2=1629871&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/logging.xml (original)
+++ tomcat/trunk/webapps/docs/logging.xml Tue Oct  7 12:36:17 2014
@@ -274,6 +274,14 @@ java.util.logging.ConsoleHandler.level=A
       prefix.</li>
       <li>System property replacement is performed for property values which
       contain ${systemPropertyName}.</li>
+      <li>If using a class loader that implements the
+      <code>org.apache.juli.WebappProperties</code> interface (Tomcat&apos;s
+      web application class loader does) then property replacement is also
+      performed for <code>${classloader.webappName}</code>,
+      <code>${classloader.hostName}</code> and
+      <code>${classloader.serviceName}</code> which are replaced with the
+      web application name, the host name and the service name respectively.
+      </li>
       <li>By default, loggers will not delegate to their parent if they have
       associated handlers. This may be changed per logger using the
       <code>loggerName.useParentHandlers</code> property, which accepts a
@@ -358,7 +366,7 @@ org.apache.catalina.core.ContainerBase.[
 
 org.apache.juli.FileHandler.level = FINE
 org.apache.juli.FileHandler.directory = ${catalina.base}/logs
-org.apache.juli.FileHandler.prefix = servlet-examples.
+org.apache.juli.FileHandler.prefix = ${classloader.webappName}.
 
 java.util.logging.ConsoleHandler.level = FINE
 java.util.logging.ConsoleHandler.formatter = 
java.util.logging.SimpleFormatter]]></source>



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to