Author: remm Date: Thu Nov 8 10:10:00 2018 New Revision: 1846112 URL: http://svn.apache.org/viewvc?rev=1846112&view=rev Log: Add a few additional utility methods to reduce code duplication: get the default config path of a container, unregister bean based on properties and get the Service.
Modified: tomcat/trunk/java/org/apache/catalina/Container.java tomcat/trunk/java/org/apache/catalina/util/LifecycleMBeanBase.java Modified: tomcat/trunk/java/org/apache/catalina/Container.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/Container.java?rev=1846112&r1=1846111&r2=1846112&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/Container.java (original) +++ tomcat/trunk/java/org/apache/catalina/Container.java Thu Nov 8 10:10:00 2018 @@ -297,6 +297,56 @@ public interface Container extends Lifec public void setRealm(Realm realm); + /** + * Find the configuration path where a configuration resource + * is located. + * @param container The container + * @param resourceName The resource file name + * @return the configuration path + */ + public static String getConfigPath(Container container, String resourceName) { + StringBuffer result = new StringBuffer(); + Container host = null; + Container engine = null; + while (container != null) { + if (container instanceof Host) { + host = container; + } else if (container instanceof Engine) { + engine = container; + } + container = container.getParent(); + } + if (host != null && ((Host) host).getXmlBase() != null) { + result.append(((Host) host).getXmlBase()).append('/'); + } else { + if (engine != null) { + result.append(engine.getName()).append('/'); + } + if (host != null) { + result.append(host.getName()).append('/'); + } + } + result.append(resourceName); + return result.toString(); + } + + + /** + * Return the Service to which this container belongs. + * @param container The container to start from + * @return the Service, or null if not found + */ + public static Service getService(Container container) { + while (container != null && !(container instanceof Engine)) { + container = container.getParent(); + } + if (container == null) { + return null; + } + return ((Engine) container).getService(); + } + + // --------------------------------------------------------- Public Methods Modified: tomcat/trunk/java/org/apache/catalina/util/LifecycleMBeanBase.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/util/LifecycleMBeanBase.java?rev=1846112&r1=1846111&r2=1846112&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/util/LifecycleMBeanBase.java (original) +++ tomcat/trunk/java/org/apache/catalina/util/LifecycleMBeanBase.java Thu Nov 8 10:10:00 2018 @@ -178,6 +178,36 @@ public abstract class LifecycleMBeanBase * Note: This method should only be used once {@link #initInternal()} has * been called and before {@link #destroyInternal()} has been called. * + * @param objectNameKeyProperties The key properties component of the + * object name to use to unregister the + * object + */ + protected final void unregister(String objectNameKeyProperties) { + // Construct an object name with the right domain + StringBuilder name = new StringBuilder(getDomain()); + name.append(':'); + name.append(objectNameKeyProperties); + + ObjectName on = null; + + try { + on = new ObjectName(name.toString()); + Registry.getRegistry(null, null).unregisterComponent(on); + } catch (MalformedObjectNameException e) { + log.warn(sm.getString("lifecycleMBeanBase.unregisterFail", name), e); + } catch (Exception e) { + log.warn(sm.getString("lifecycleMBeanBase.unregisterFail", name), e); + } + } + + + /** + * Utility method to enable sub-classes to easily unregister additional + * components that don't implement {@link JmxEnabled} with an MBean server. + * <br> + * Note: This method should only be used once {@link #initInternal()} has + * been called and before {@link #destroyInternal()} has been called. + * * @param on The name of the component to unregister */ protected final void unregister(ObjectName on) { --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org