Author: markt Date: Sun May 2 17:36:38 2010 New Revision: 940273 URL: http://svn.apache.org/viewvc?rev=940273&view=rev Log: Add new interface to StandardServer to enable MBean (de)registration on init/destroy
Added: tomcat/trunk/test/org/apache/catalina/mbeans/ tomcat/trunk/test/org/apache/catalina/mbeans/RegistrationTest.java (with props) Modified: tomcat/trunk/java/org/apache/catalina/Globals.java tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties tomcat/trunk/java/org/apache/catalina/core/StandardServer.java Modified: tomcat/trunk/java/org/apache/catalina/Globals.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/Globals.java?rev=940273&r1=940272&r2=940273&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/Globals.java (original) +++ tomcat/trunk/java/org/apache/catalina/Globals.java Sun May 2 17:36:38 2010 @@ -334,4 +334,9 @@ public final class Globals { public static final String ASYNC_SUPPORTED_ATTR = "org.apache.catalina.ASYNC_SUPPORTED"; + + /** + * Default domain for MBeans if none can be determined + */ + public static final String DEFAULT_MBEAN_DOMAIN = "Catalina"; } Modified: tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties?rev=940273&r1=940272&r2=940273&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties Sun May 2 17:36:38 2010 @@ -188,6 +188,7 @@ standardHost.warRequired=URL to web appl standardHost.warURL=Invalid URL for web application archive: {0} standardHost.validationEnabled=XML validation enabled standardHost.validationDisabled=XML validation disabled +standardServer.onameFail=MBean name specified for Server [{0}] is not valid standardServer.shutdownViaPort=A valid shutdown command was received via the shutdown port. Stopping the Server instance. standardService.connector.failed=Failed to start connector [{0}] standardService.initialize.failed=Service initializing at {0} failed Modified: tomcat/trunk/java/org/apache/catalina/core/StandardServer.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardServer.java?rev=940273&r1=940272&r2=940273&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/StandardServer.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/StandardServer.java Sun May 2 17:36:38 2010 @@ -29,12 +29,15 @@ import java.net.Socket; import java.security.AccessControlException; import java.util.Random; -import javax.management.MBeanRegistration; import javax.management.MBeanServer; +import javax.management.MalformedObjectNameException; import javax.management.ObjectName; +import org.apache.catalina.Container; import org.apache.catalina.Context; +import org.apache.catalina.Globals; import org.apache.catalina.LifecycleException; +import org.apache.catalina.LifecycleMBeanRegistration; import org.apache.catalina.LifecycleState; import org.apache.catalina.Server; import org.apache.catalina.Service; @@ -47,6 +50,8 @@ import org.apache.juli.logging.LogFactor import org.apache.tomcat.util.buf.StringCache; import org.apache.tomcat.util.modeler.Registry; +import com.sun.xml.internal.ws.api.pipe.Engine; + /** @@ -57,7 +62,7 @@ import org.apache.tomcat.util.modeler.Re * @version $Id$ */ public final class StandardServer extends LifecycleBase - implements Server, MBeanRegistration { + implements Server, LifecycleMBeanRegistration { private static final Log log = LogFactory.getLog(StandardServer.class); @@ -682,17 +687,10 @@ public final class StandardServer extend @Override protected void initInternal() throws LifecycleException { - if( oname==null ) { - try { - oname=new ObjectName( "Catalina:type=Server"); - Registry.getRegistry(null, null) - .registerComponent(this, oname, null ); - } catch (Exception e) { - log.error("Error registering ",e); - } - } - // Register global String cache + // Note although the cache is global, if there are multiple Servers + // present in the JVM (may happen when embedding) then the same cache + // will be registered under multiple names try { ObjectName oname2 = new ObjectName(oname.getDomain() + ":type=StringCache"); @@ -708,39 +706,82 @@ public final class StandardServer extend } } + @Override protected void destroyInternal() { // NOOP } - protected String type; - protected String domain; - protected String suffix; - protected ObjectName oname; + protected volatile String domain; + protected volatile ObjectName oname; protected MBeanServer mserver; + /** + * Obtain the MBean domain for this server. The domain is obtained using + * the following search order: + * <ol> + * <li>Name of first {...@link Engine}.</li> + * <li>Name of first {...@link Service}.</li> + * <li>Global default defined by {...@link Globals#DEFAULT_MBEAN_DOMAIN}</li> + * </ol> + */ + public String getDomain() { + if (domain == null) { + Service[] services = findServices(); + if (services.length > 0) { + Service service = services[0]; + if (service != null) { + Container container = service.getContainer(); + if (container != null) { + domain = container.getName(); + } else { + domain = service.getName(); + } + } + } + if (domain == null) { + domain = Globals.DEFAULT_MBEAN_DOMAIN; + } + } + return domain; + } + + public ObjectName getObjectName() { + if (oname == null) { + StringBuilder name = new StringBuilder(getDomain()); + name.append(":type=Server"); + + try { + oname = new ObjectName(name.toString()); + } catch (MalformedObjectNameException e) { + log.warn(sm.getString("standardServer.onameFail", name), e); + } catch (NullPointerException e) { + // Never going to happen + } + } + return oname; } - public String getDomain() { - return domain; - } public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception { - oname=name; - mserver=server; - domain=name.getDomain(); + oname = name; + mserver = server; + domain = name.getDomain(); return name; } public void postRegister(Boolean registrationDone) { + // NOOP } public void preDeregister() throws Exception { + // NOOP } public void postDeregister() { + // NOOP } } Added: tomcat/trunk/test/org/apache/catalina/mbeans/RegistrationTest.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/mbeans/RegistrationTest.java?rev=940273&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/mbeans/RegistrationTest.java (added) +++ tomcat/trunk/test/org/apache/catalina/mbeans/RegistrationTest.java Sun May 2 17:36:38 2010 @@ -0,0 +1,85 @@ +/* + * 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.mbeans; + +import java.io.File; +import java.util.Set; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.apache.catalina.core.StandardHost; +import org.apache.catalina.startup.Tomcat; +import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.tomcat.util.modeler.Registry; + +/** + * General tests around the process of registration and de-registration that + * don't necessarily apply to one specific Tomcat class. + * + */ +public class RegistrationTest extends TomcatBaseTest { + + /** + * Test verifying that Tomcat correctly de-registers the MBeans it has + * registered. + * @author Marc Guillemot + */ + public void testMBeanDeregistration() throws Exception { + final MBeanServer mbeanServer = Registry.getRegistry(null, null).getMBeanServer(); + Set<ObjectName> onames = mbeanServer.queryNames(new ObjectName("Catalina:*"), null); + assertEquals("Remaining: " + onames, 0, onames.size()); + + final Tomcat tomcat = getTomcatInstance(); + // need to register a ServerLifecycleListener otherwise only a few MBeans are registered + tomcat.getServer().addLifecycleListener(new ServerLifecycleListener()); + final File contextDir = new File("output/webappFoo"); + contextDir.mkdir(); + tomcat.addContext("/foo", contextDir.getAbsolutePath()); + tomcat.start(); + + // Verify there are no Catalina MBeans + onames = mbeanServer.queryNames(new ObjectName("Catalina:*"), null); + assertEquals("Found: " + onames, 0, onames.size()); + + // Verify there are some Tomcat MBeans + onames = mbeanServer.queryNames(new ObjectName("Tomcat:*"), null); + assertTrue("No Tomcat MBeans", onames.size() > 0); + + tomcat.stop(); + + // Verify there are no Tomcat MBeans + onames = mbeanServer.queryNames(new ObjectName("Catalina:*"), null); + assertEquals("Remaining: " + onames, 0, onames.size()); + + // add a new host + StandardHost host = new StandardHost(); + host.setName("otherhost"); + tomcat.getEngine().addChild(host); + + final File contextDir2 = new File("output/webappFoo2"); + contextDir2.mkdir(); + tomcat.addContext(host, "/foo2", contextDir2.getAbsolutePath()); + + tomcat.start(); + tomcat.stop(); + + onames = mbeanServer.queryNames(new ObjectName("Catalina:*"), null); + assertEquals("Remaining: " + onames, 0, onames.size()); + } + +} Propchange: tomcat/trunk/test/org/apache/catalina/mbeans/RegistrationTest.java ------------------------------------------------------------------------------ svn:eol-style = native --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org