import org.apache.catalina.User;
import org.apache.catalina.UserDatabase;
import org.apache.catalina.Valve;
+import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.deploy.ContextEnvironment;
import org.apache.catalina.deploy.ContextResource;
* Determine the name of the domain to register MBeans for from a given
* Container.
*
- * @param container
+ * @param container
* @return
*/
public static String getDomain(Container container) {
return domain;
}
+
+ /**
+ * Calculate the key properties string to be added to an object's
+ * {@link ObjectName} to indicate that it is associated with that container.
+ *
+ * @param container The container the object is associated with
+ * @return A string suitable for appending to the ObjectName
+ */
+ public static String getContainerKeyProperties(Container container) {
+
+ Container c = container;
+ StringBuilder keyProperties = new StringBuilder();
+ int unknown = 0;
+
+ // Work up container hierarchy, add a component to the name for
+ // each container
+ while (!(c instanceof Engine)) {
+ if (c instanceof Wrapper) {
+ keyProperties.append(",servlet=");
+ keyProperties.append(c.getName());
+ } else if (c instanceof Context) {
+ String path = ((Context)c).getPath();
+ if (path.length() < 1) {
+ path = "/";
+ }
+ keyProperties.append(",path=");
+ keyProperties.append(path);
+ } else if (c instanceof Host) {
+ keyProperties.append(",host=");
+ keyProperties.append(c.getName());
+ } else {
+ // Should never happen...
+ keyProperties.append(",unknown");
+ keyProperties.append(unknown++);
+ keyProperties.append('=');
+ keyProperties.append(c.getName());
+ }
+ c = c.getParent();
+ }
+
+ return keyProperties.toString();
+ }
}
import java.security.cert.X509Certificate;
import java.util.ArrayList;
-import javax.management.Attribute;
-import javax.management.MBeanRegistration;
-import javax.management.MBeanServer;
-import javax.management.ObjectName;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.Container;
import org.apache.catalina.Service;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
-import org.apache.catalina.core.ContainerBase;
import org.apache.catalina.deploy.LoginConfig;
import org.apache.catalina.deploy.SecurityConstraint;
import org.apache.catalina.deploy.SecurityCollection;
+import org.apache.catalina.mbeans.MBeanUtils;
import org.apache.catalina.util.HexUtils;
import org.apache.catalina.util.LifecycleBase;
+import org.apache.catalina.util.LifecycleMBeanBase;
import org.apache.catalina.util.MD5Encoder;
import org.apache.tomcat.util.res.StringManager;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
-import org.apache.tomcat.util.modeler.Registry;
/**
* Simple implementation of <b>Realm</b> that reads an XML file to configure
* @version $Id$
*/
-public abstract class RealmBase extends LifecycleBase
- implements Realm, MBeanRegistration {
+public abstract class RealmBase extends LifecycleMBeanBase implements Realm {
private static final Log log = LogFactory.getLog(RealmBase.class);
}
+ @Override
+ protected void initInternal() throws LifecycleException {
+
+ super.initInternal();
+
+ // We want logger as soon as possible
+ if (container != null) {
+ this.containerLog = container.getLogger();
+ }
+ }
+
/**
* Prepare for the beginning of active use of the public methods of this
* component and implement the requirements of
}
- @Override
- protected void destroyInternal() {
-
- // unregister this realm
- if ( oname!=null ) {
- try {
- Registry.getRegistry(null, null).unregisterComponent(oname);
- if(log.isDebugEnabled())
- log.debug( "unregistering realm " + oname );
- } catch( Exception ex ) {
- log.error( "Can't unregister realm " + oname, ex);
- }
- }
-
- }
-
// ------------------------------------------------------ Protected Methods
// -------------------- JMX and Registration --------------------
- protected String type;
- protected String domain;
- protected String host;
- protected String path;
- protected String realmPath = "/realm0";
- protected ObjectName oname;
- protected ObjectName controller;
- protected MBeanServer mserver;
- public ObjectName getController() {
- return controller;
- }
-
- public void setController(ObjectName controller) {
- this.controller = controller;
- }
+ @Override
+ public String getObjectNameKeyProperties() {
+
+ StringBuilder keyProperties = new StringBuilder("type=Realm");
+ keyProperties.append(getRealmSuffix());
+ keyProperties.append(MBeanUtils.getContainerKeyProperties(container));
- public ObjectName getObjectName() {
- return oname;
+ return keyProperties.toString();
}
- public String getDomain() {
- return domain;
+ @Override
+ public String getDomainInternal() {
+ return MBeanUtils.getDomain(container);
}
- public String getType() {
- return type;
- }
+ protected String realmPath = "/realm0";
public String getRealmPath() {
return realmPath;
realmPath = theRealmPath;
}
- public ObjectName preRegister(MBeanServer server,
- ObjectName name) throws Exception {
- oname=name;
- mserver=server;
- domain=name.getDomain();
-
- type=name.getKeyProperty("type");
- host=name.getKeyProperty("host");
- path=name.getKeyProperty("path");
-
- return name;
- }
-
- public void postRegister(Boolean registrationDone) {
- // NOOP in base class
- }
-
- public void preDeregister() throws Exception {
- // NOOP in base class
- }
-
- public void postDeregister() {
- // NOOP in base class
- }
-
- @Override
- protected void initInternal() {
-
- // We want logger as soon as possible
- if (container != null) {
- this.containerLog = container.getLogger();
- }
-
- if( container== null ) {
- ObjectName parent=null;
- // Register with the parent
- try {
- if( host == null ) {
- // global
- parent=new ObjectName(domain +":type=Engine");
- } else if( path==null ) {
- parent=new ObjectName(domain +
- ":type=Host,host=" + host);
- } else {
- parent=new ObjectName(domain +":j2eeType=WebModule,name=//" +
- host + path);
- }
- if( mserver.isRegistered(parent )) {
- if(log.isDebugEnabled())
- log.debug("Register with " + parent);
- mserver.setAttribute(parent, new Attribute("realm", this));
- }
- } catch (Exception e) {
- log.error("Parent not available yet: " + parent);
- }
- }
-
- if( oname==null ) {
- // register
- try {
- ContainerBase cb=(ContainerBase)container;
- oname=new ObjectName(cb.getDomain()+":type=Realm" +
- getRealmSuffix() + cb.getContainerSuffix());
- Registry.getRegistry(null, null).registerComponent(this, oname, null );
- if(log.isDebugEnabled())
- log.debug("Register Realm "+oname);
- } catch (Throwable e) {
- log.error( "Can't register " + oname, e);
- }
- }
-
- }
-
-
protected String getRealmSuffix() {
return ",realmPath=" + getRealmPath();
}
import org.apache.catalina.Contained;
import org.apache.catalina.Container;
-import org.apache.catalina.Context;
-import org.apache.catalina.Engine;
-import org.apache.catalina.Host;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.Valve;
-import org.apache.catalina.Wrapper;
import org.apache.catalina.comet.CometEvent;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
StringBuilder name = new StringBuilder("type=Valve");
Container container = getContainer();
- int unknown = 0;
-
- // Work up container hierarchy, add a component to the name for
- // each container
- while (!(container instanceof Engine)) {
- if (container instanceof Wrapper) {
- name.append(",servlet=");
- name.append(container.getName());
- } else if (container instanceof Context) {
- String path = ((Context)container).getPath();
- if (path.length() < 1) {
- path = "/";
- }
- name.append(",path=");
- name.append(path);
- } else if (container instanceof Host) {
- name.append(",host=");
- name.append(container.getName());
- } else {
- // Should never happen...
- name.append(",unknown");
- name.append(unknown++);
- name.append('=');
- name.append(container.getName());
- }
- container = container.getParent();
- }
+ name.append(MBeanUtils.getContainerKeyProperties(container));
+
int seq = 0;
for (Valve valve : container.getPipeline().getValves()) {
// Skip null valves