Author: markt Date: Thu Nov 22 14:58:51 2012 New Revision: 1412575 URL: http://svn.apache.org/viewvc?rev=1412575&view=rev Log: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=54170 Ensure correct registration of Filters and Servlets in the JMX registry if the Filter or Servlet name includes a character that must be quoted if used in an ObjectName value.
Added: tomcat/trunk/java/org/apache/tomcat/util/modeler/Util.java (with props) tomcat/trunk/test/org/apache/catalina/core/TestApplicationFilterConfig.java (with props) Modified: tomcat/trunk/TOMCAT-NEXT.txt tomcat/trunk/java/org/apache/catalina/core/ApplicationFilterConfig.java tomcat/trunk/java/org/apache/catalina/core/StandardWrapper.java Modified: tomcat/trunk/TOMCAT-NEXT.txt URL: http://svn.apache.org/viewvc/tomcat/trunk/TOMCAT-NEXT.txt?rev=1412575&r1=1412574&r2=1412575&view=diff ============================================================================== --- tomcat/trunk/TOMCAT-NEXT.txt (original) +++ tomcat/trunk/TOMCAT-NEXT.txt Thu Nov 22 14:58:51 2012 @@ -210,7 +210,7 @@ but possibly 7.1.x). 13. Fix all FindBugs warnings - Complete for javax.* - - Complete for o.a.[catalina|coyote|el].* + - Complete for o.a.[catalina|coyote|el|juli|naming].* - Remaining code in progress 14. Review date formatting with a view to reducing duplication. Modified: tomcat/trunk/java/org/apache/catalina/core/ApplicationFilterConfig.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/ApplicationFilterConfig.java?rev=1412575&r1=1412574&r2=1412575&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/ApplicationFilterConfig.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/ApplicationFilterConfig.java Thu Nov 22 14:58:51 2012 @@ -43,6 +43,7 @@ import org.apache.tomcat.InstanceManager import org.apache.tomcat.util.ExceptionUtils; import org.apache.tomcat.util.log.SystemLogHandler; import org.apache.tomcat.util.modeler.Registry; +import org.apache.tomcat.util.modeler.Util; import org.apache.tomcat.util.res.StringManager; @@ -416,16 +417,19 @@ public final class ApplicationFilterConf String webMod = "//" + hostName + parentName; String onameStr = null; + String filterName = filterDef.getFilterName(); + if (Util.objectNameValueNeedsQuote(filterName)) { + filterName = ObjectName.quote(filterName); + } if (context instanceof StandardContext) { StandardContext standardContext = (StandardContext) context; - onameStr = domain + ":j2eeType=Filter,name=" + - filterDef.getFilterName() + ",WebModule=" + webMod + - ",J2EEApplication=" + + onameStr = domain + ":j2eeType=Filter,name=" + filterName + + ",WebModule=" + webMod + ",J2EEApplication=" + standardContext.getJ2EEApplication() + ",J2EEServer=" + standardContext.getJ2EEServer(); } else { - onameStr = domain + ":j2eeType=Filter,name=" + - filterDef.getFilterName() + ",WebModule=" + webMod; + onameStr = domain + ":j2eeType=Filter,name=" + filterName + + ",WebModule=" + webMod; } try { oname = new ObjectName(onameStr); Modified: tomcat/trunk/java/org/apache/catalina/core/StandardWrapper.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardWrapper.java?rev=1412575&r1=1412574&r2=1412575&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/StandardWrapper.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/StandardWrapper.java Thu Nov 22 14:58:51 2012 @@ -66,6 +66,7 @@ import org.apache.tomcat.PeriodicEventLi import org.apache.tomcat.util.ExceptionUtils; import org.apache.tomcat.util.log.SystemLogHandler; import org.apache.tomcat.util.modeler.Registry; +import org.apache.tomcat.util.modeler.Util; /** * Standard implementation of the <b>Wrapper</b> interface that represents @@ -1775,7 +1776,11 @@ public class StandardWrapper extends Con StringBuilder keyProperties = new StringBuilder("j2eeType=Servlet,name="); - keyProperties.append(getName()); + String name = getName(); + if (Util.objectNameValueNeedsQuote(name)) { + name = ObjectName.quote(name); + } + keyProperties.append(name); keyProperties.append(getWebModuleKeyProperties()); Added: tomcat/trunk/java/org/apache/tomcat/util/modeler/Util.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/modeler/Util.java?rev=1412575&view=auto ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/modeler/Util.java (added) +++ tomcat/trunk/java/org/apache/tomcat/util/modeler/Util.java Thu Nov 22 14:58:51 2012 @@ -0,0 +1,35 @@ +/* + * 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.tomcat.util.modeler; + +public class Util { + + private Util() { + // Utility class. Hide default constructor. + } + + public static boolean objectNameValueNeedsQuote(String input) { + char[] chars = input.toCharArray(); + for (int i = 0; i < input.length(); i++) { + char ch = chars[i]; + if (ch == ',' || ch == '=' || ch == ':' || ch == '*' || ch == '?') { + return true; + } + } + return false; + } +} Propchange: tomcat/trunk/java/org/apache/tomcat/util/modeler/Util.java ------------------------------------------------------------------------------ svn:eol-style = native Added: tomcat/trunk/test/org/apache/catalina/core/TestApplicationFilterConfig.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TestApplicationFilterConfig.java?rev=1412575&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/core/TestApplicationFilterConfig.java (added) +++ tomcat/trunk/test/org/apache/catalina/core/TestApplicationFilterConfig.java Thu Nov 22 14:58:51 2012 @@ -0,0 +1,69 @@ +/* + * 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.catalina.core; + +import java.util.Set; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.catalina.Context; +import org.apache.catalina.deploy.FilterDef; +import org.apache.catalina.filters.AddDefaultCharsetFilter; +import org.apache.catalina.startup.Tomcat; +import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.tomcat.util.modeler.Registry; + +public class TestApplicationFilterConfig extends TomcatBaseTest { + + @Test + public void testBug54170() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + // Must have a real docBase - just use temp + Context ctx = + tomcat.addContext("", System.getProperty("java.io.tmpdir")); + + Tomcat.addServlet(ctx, "HelloWorld", new HelloWorldServlet()); + ctx.addServletMapping("/", "HelloWorld"); + + // Add a filter with a name that should be escaped if used in a JMX + // object name + FilterDef filterDef = new FilterDef(); + filterDef.setFilterClass(AddDefaultCharsetFilter.class.getName()); + filterDef.setFilterName("bug54170*"); + ctx.addFilterDef(filterDef); + + tomcat.start(); + + final MBeanServer mbeanServer = + Registry.getRegistry(null, null).getMBeanServer(); + + // There should be one Servlet MBean registered + Set<ObjectName> servlets = mbeanServer.queryNames( + new ObjectName("Tomcat:j2eeType=Servlet,*"), null); + Assert.assertEquals(1, servlets.size()); + + // There should be one Filter MBean registered + Set<ObjectName> filters = mbeanServer.queryNames( + new ObjectName("Tomcat:j2eeType=Filter,*"), null); + Assert.assertEquals(1, filters.size()); + } +} Propchange: tomcat/trunk/test/org/apache/catalina/core/TestApplicationFilterConfig.java ------------------------------------------------------------------------------ svn:eol-style = native --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org