* The naming resources for this web application.
*/
private NamingResources namingResources = null;
- private ObjectName onameNamingResources;
/**
* The message destinations for this web application.
// Process the property setting change
NamingResources oldNamingResources = this.namingResources;
this.namingResources = namingResources;
- namingResources.setContainer(this);
+ if (namingResources != null) {
+ namingResources.setContainer(this);
+ }
support.firePropertyChange("namingResources",
oldNamingResources, this.namingResources);
// If set from server.xml, getObjectKeyPropertiesNameOnly() will
// trigger an NPE. Initial registration takes place on INIT.
if (getState() != LifecycleState.NEW) {
- unregister(onameNamingResources);
- onameNamingResources = register(namingResources,
- "type=NamingResources," + getObjectKeyPropertiesNameOnly());
+ if (oldNamingResources != null) {
+ try {
+ oldNamingResources.destroy();
+ } catch (LifecycleException e) {
+ log.warn("standardContext.namingResource.destroy.fail", e);
+ }
+ }
+ if (namingResources != null) {
+ try {
+ namingResources.init();
+ } catch (LifecycleException e) {
+ log.warn("standardContext.namingResource.init.fail", e);
+ }
+ }
}
}
sequenceNumber.getAndIncrement());
broadcaster.sendNotification(notification);
- unregister(onameNamingResources);
+ if (namingResources != null) {
+ namingResources.destroy();
+ }
synchronized (instanceListenersLock) {
instanceListeners = new String[0];
// Register the naming resources
if (namingResources != null) {
- onameNamingResources = register(namingResources,
- "type=NamingResources," + getObjectNameKeyProperties());
+ namingResources.init();
}
// Send j2ee.object.created notification
import java.util.HashMap;
import java.util.Hashtable;
+import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Engine;
+import org.apache.catalina.LifecycleException;
+import org.apache.catalina.LifecycleState;
import org.apache.catalina.Server;
+import org.apache.catalina.mbeans.MBeanUtils;
+import org.apache.catalina.util.LifecycleMBeanBase;
+import org.apache.juli.logging.Log;
+import org.apache.juli.logging.LogFactory;
+import org.apache.tomcat.util.res.StringManager;
/**
* @version $Id$
*/
-public class NamingResources implements Serializable {
+public class NamingResources extends LifecycleMBeanBase implements Serializable {
private static final long serialVersionUID = 1L;
+
+ private static final Log log = LogFactory.getLog(NamingResources.class);
+
+ private static final StringManager sm =
+ StringManager.getManager(Constants.Package);
+
+ private volatile boolean resourceRequireExplicitRegistration = false;
// ----------------------------------------------------------- Constructors
}
support.firePropertyChange("environment", null, environment);
+ // Register with JMX
+ if (resourceRequireExplicitRegistration) {
+ try {
+ MBeanUtils.createMBean(environment);
+ } catch (Exception e) {
+ log.warn(sm.getString("namingResources.mbeanCreateFail",
+ environment.getName()), e);
+ }
+ }
}
// Container should be an instance of Server or Context. If it is anything
}
support.firePropertyChange("resource", null, resource);
+ // Register with JMX
+ if (resourceRequireExplicitRegistration) {
+ try {
+ MBeanUtils.createMBean(resource);
+ } catch (Exception e) {
+ log.warn(sm.getString("namingResources.mbeanCreateFail",
+ resource.getName()), e);
+ }
+ }
}
}
support.firePropertyChange("resourceLink", null, resourceLink);
+ // Register with JMX
+ if (resourceRequireExplicitRegistration) {
+ try {
+ MBeanUtils.createMBean(resourceLink);
+ } catch (Exception e) {
+ log.warn(sm.getString("namingResources.mbeanCreateFail",
+ resourceLink.getName()), e);
+ }
+ }
}
}
if (environment != null) {
support.firePropertyChange("environment", environment, null);
+ // De-register with JMX
+ if (resourceRequireExplicitRegistration) {
+ try {
+ MBeanUtils.destroyMBean(environment);
+ } catch (Exception e) {
+ log.warn(sm.getString("namingResources.mbeanDestroyFail",
+ environment.getName()), e);
+ }
+ }
environment.setNamingResources(null);
}
-
}
}
if (resource != null) {
support.firePropertyChange("resource", resource, null);
+ // De-register with JMX
+ if (resourceRequireExplicitRegistration) {
+ try {
+ MBeanUtils.destroyMBean(resource);
+ } catch (Exception e) {
+ log.warn(sm.getString("namingResources.mbeanDestroyFail",
+ resource.getName()), e);
+ }
+ }
resource.setNamingResources(null);
}
-
}
}
if (resourceLink != null) {
support.firePropertyChange("resourceLink", resourceLink, null);
+ // De-register with JMX
+ if (resourceRequireExplicitRegistration) {
+ try {
+ MBeanUtils.destroyMBean(resourceLink);
+ } catch (Exception e) {
+ log.warn(sm.getString("namingResources.mbeanDestroyFail",
+ resourceLink.getName()), e);
+ }
+ }
resourceLink.setNamingResources(null);
}
-
}
}
+ // ------------------------------------------------------- Lifecycle methods
+
+ @Override
+ protected void initInternal() throws LifecycleException {
+ super.initInternal();
+
+ // Set this before we register currently known naming resources to avoid
+ // timing issues. Duplication registration is not an issue.
+ resourceRequireExplicitRegistration = true;
+
+ for (ContextResource cr : resources.values()) {
+ try {
+ MBeanUtils.createMBean(cr);
+ } catch (Exception e) {
+ log.warn(sm.getString(
+ "namingResources.mbeanCreateFail", cr.getName()), e);
+ }
+ }
+
+ for (ContextEnvironment ce : envs.values()) {
+ try {
+ MBeanUtils.createMBean(ce);
+ } catch (Exception e) {
+ log.warn(sm.getString(
+ "namingResources.mbeanCreateFail", ce.getName()), e);
+ }
+ }
+
+ for (ContextResourceLink crl : resourceLinks.values()) {
+ try {
+ MBeanUtils.createMBean(crl);
+ } catch (Exception e) {
+ log.warn(sm.getString(
+ "namingResources.mbeanCreateFail", crl.getName()), e);
+ }
+ }
+ }
+
+
+ @Override
+ protected void startInternal() throws LifecycleException {
+ fireLifecycleEvent(CONFIGURE_START_EVENT, null);
+ setState(LifecycleState.STARTING);
+ }
+
+
+ @Override
+ protected void stopInternal() throws LifecycleException {
+ setState(LifecycleState.STOPPING);
+ fireLifecycleEvent(CONFIGURE_STOP_EVENT, null);
+ }
+
+
+ @Override
+ protected void destroyInternal() throws LifecycleException {
+
+ // Set this before we de-register currently known naming resources to
+ // avoid timing issues. Duplication de-registration is not an issue.
+ resourceRequireExplicitRegistration = false;
+
+ // Destroy in reverse order to create, although it should not matter
+ for (ContextResourceLink crl : resourceLinks.values()) {
+ try {
+ MBeanUtils.destroyMBean(crl);
+ } catch (Exception e) {
+ log.warn(sm.getString(
+ "namingResources.mbeanDestroyFail", crl.getName()), e);
+ }
+ }
+
+ for (ContextEnvironment ce : envs.values()) {
+ try {
+ MBeanUtils.destroyMBean(ce);
+ } catch (Exception e) {
+ log.warn(sm.getString(
+ "namingResources.mbeanDestroyFail", ce.getName()), e);
+ }
+ }
+
+ for (ContextResource cr : resources.values()) {
+ try {
+ MBeanUtils.destroyMBean(cr);
+ } catch (Exception e) {
+ log.warn(sm.getString(
+ "namingResources.mbeanDestroyFail", cr.getName()), e);
+ }
+ }
+
+ super.destroyInternal();
+ }
+
+
+ @Override
+ protected String getDomainInternal() {
+ // Use the same domain as our associated container if we have one
+ Object c = getContainer();
+
+ if (c instanceof LifecycleMBeanBase) {
+ return ((LifecycleMBeanBase) c).getDomain();
+ }
+
+ return null;
+ }
+
+
+ @Override
+ protected String getObjectNameKeyProperties() {
+ Object c = getContainer();
+ if (c instanceof Container) {
+ return "type=NamingResources" +
+ MBeanUtils.getContainerKeyProperties((Container) c);
+ }
+ // Server or just unknown
+ return "type=NamingResources";
+ }
}
*
* @exception Exception if an MBean cannot be created or registered
*/
- static DynamicMBean createMBean(ContextEnvironment environment)
+ public static DynamicMBean createMBean(ContextEnvironment environment)
throws Exception {
String mname = createManagedName(environment);
*
* @exception Exception if an MBean cannot be created or registered
*/
- static DynamicMBean createMBean(ContextResource resource)
+ public static DynamicMBean createMBean(ContextResource resource)
throws Exception {
String mname = createManagedName(resource);
*
* @exception Exception if an MBean cannot be created or registered
*/
- static DynamicMBean createMBean(ContextResourceLink resourceLink)
+ public static DynamicMBean createMBean(ContextResourceLink resourceLink)
throws Exception {
String mname = createManagedName(resourceLink);
if (registry == null) {
registry = Registry.getRegistry(null, null);
- ClassLoader cl=ServerLifecycleListener.class.getClassLoader();
+ ClassLoader cl = MBeanUtils.class.getClassLoader();
registry.loadDescriptors("org.apache.catalina.mbeans", cl);
registry.loadDescriptors("org.apache.catalina.authenticator", cl);
*
* @exception Exception if an MBean cannot be deregistered
*/
- static void destroyMBean(ContextEnvironment environment)
+ public static void destroyMBean(ContextEnvironment environment)
throws Exception {
String mname = createManagedName(environment);
*
* @exception Exception if an MBean cannot be deregistered
*/
- static void destroyMBean(ContextResource resource)
+ public static void destroyMBean(ContextResource resource)
throws Exception {
// If this is a user database resource need to destroy groups, roles,
*
* @exception Exception if an MBean cannot be deregistered
*/
- static void destroyMBean(ContextResourceLink resourceLink)
+ public static void destroyMBean(ContextResourceLink resourceLink)
throws Exception {
String mname = createManagedName(resourceLink);