/* * 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;
/** * Public utility methods in support of the server side MBeans implementation. * * @author Craig R. McClanahan * @author Amy Roh
*/ publicclass MBeanUtils {
protectedstaticfinal StringManager sm = StringManager.getManager(MBeanUtils.class);
/** * The set of exceptions to the normal rules used by <code>createManagedBean()</code>. The first element of each * pair is a class name, and the second element is the managed bean name.
*/ privatestaticfinal String exceptions[][] = { { "org.apache.catalina.users.MemoryGroup", "Group" },
{ "org.apache.catalina.users.MemoryRole", "Role" }, { "org.apache.catalina.users.MemoryUser", "User" },
{ "org.apache.catalina.users.GenericGroup", "Group" }, { "org.apache.catalina.users.GenericRole", "Role" },
{ "org.apache.catalina.users.GenericUser", "User" } };
/** * The configuration information registry for our managed beans.
*/ privatestatic Registry registry = createRegistry();
/** * The <code>MBeanServer</code> for this application.
*/ privatestatic MBeanServer mserver = createServer();
/** * Create and return the name of the <code>ManagedBean</code> that corresponds to this Catalina component. * * @param component The component for which to create a name
*/ static String createManagedName(Object component) {
// Deal with exceptions to the standard rule
String className = component.getClass().getName(); for (String[] exception : exceptions) { if (className.equals(exception[0])) { return exception[1];
}
}
// Perform the standard transformation int period = className.lastIndexOf('.'); if (period >= 0) {
className = className.substring(period + 1);
} return className;
}
/** * Create, register, and return an MBean for this <code>ContextEnvironment</code> object. * * @param environment The ContextEnvironment to be managed * * @return a new MBean * * @exception Exception if an MBean cannot be created or registered
*/ publicstatic DynamicMBean createMBean(ContextEnvironment environment) throws Exception {
/** * Create, register, and return an MBean for this <code>ContextResource</code> object. * * @param resource The ContextResource to be managed * * @return a new MBean * * @exception Exception if an MBean cannot be created or registered
*/ publicstatic DynamicMBean createMBean(ContextResource resource) throws Exception {
/** * Create, register, and return an MBean for this <code>ContextResourceLink</code> object. * * @param resourceLink The ContextResourceLink to be managed * * @return a new MBean * * @exception Exception if an MBean cannot be created or registered
*/ publicstatic DynamicMBean createMBean(ContextResourceLink resourceLink) throws Exception {
/** * Create, register, and return an MBean for this <code>Group</code> object. * * @param group The Group to be managed * * @return a new MBean * * @exception Exception if an MBean cannot be created or registered
*/ static DynamicMBean createMBean(Group group) throws Exception {
/** * Create, register, and return an MBean for this <code>Role</code> object. * * @param role The Role to be managed * * @return a new MBean * * @exception Exception if an MBean cannot be created or registered
*/ static DynamicMBean createMBean(Role role) throws Exception {
/** * Create, register, and return an MBean for this <code>User</code> object. * * @param user The User to be managed * * @return a new MBean * * @exception Exception if an MBean cannot be created or registered
*/ static DynamicMBean createMBean(User user) throws Exception {
/** * Create, register, and return an MBean for this <code>UserDatabase</code> object. * * @param userDatabase The UserDatabase to be managed * * @return a new MBean * * @exception Exception if an MBean cannot be created or registered
*/ static DynamicMBean createMBean(UserDatabase userDatabase) throws Exception {
if (userDatabase.isSparse()) { // Register a sparse database bean as well
ManagedBean managed = registry.findManagedBean("SparseUserDatabase"); if (managed == null) {
Exception e = new Exception(sm.getString("mBeanUtils.noManagedBean", "SparseUserDatabase")); thrownew MBeanException(e);
}
String domain = managed.getDomain(); if (domain == null) {
domain = mserver.getDefaultDomain();
}
DynamicMBean mbean = managed.createMBean(userDatabase);
ObjectName oname = createObjectName(domain, userDatabase); if (mserver.isRegistered(oname)) {
mserver.unregisterMBean(oname);
}
mserver.registerMBean(mbean, oname);
}
/** * Create an <code>ObjectName</code> for this <code>Service</code> object. * * @param domain Domain in which this name is to be created * @param environment The ContextEnvironment to be named * * @return a new object name * * @exception MalformedObjectNameException if a name cannot be created
*/ publicstatic ObjectName createObjectName(String domain, ContextEnvironment environment) throws MalformedObjectNameException {
ObjectName name = null;
Object container = environment.getNamingResources().getContainer(); if (container instanceof Server) {
name = new ObjectName(domain + ":type=Environment" + ",resourcetype=Global,name=" + environment.getName());
} elseif (container instanceof Context) {
Context context = ((Context) container);
ContextName cn = new ContextName(context.getName(), false);
Container host = context.getParent();
name = new ObjectName(domain + ":type=Environment" + ",resourcetype=Context,host=" + host.getName() + ",context=" + cn.getDisplayName() + ",name=" + environment.getName());
} return name;
}
/** * Create an <code>ObjectName</code> for this <code>ContextResource</code> object. * * @param domain Domain in which this name is to be created * @param resource The ContextResource to be named * * @return a new object name * * @exception MalformedObjectNameException if a name cannot be created
*/ publicstatic ObjectName createObjectName(String domain, ContextResource resource) throws MalformedObjectNameException {
/** * Create an <code>ObjectName</code> for this <code>ContextResourceLink</code> object. * * @param domain Domain in which this name is to be created * @param resourceLink The ContextResourceLink to be named * * @return a new object name * * @exception MalformedObjectNameException if a name cannot be created
*/ publicstatic ObjectName createObjectName(String domain, ContextResourceLink resourceLink) throws MalformedObjectNameException {
ObjectName name = null;
String quotedResourceLinkName = ObjectName.quote(resourceLink.getName());
Object container = resourceLink.getNamingResources().getContainer(); if (container instanceof Server) {
name = new ObjectName(
domain + ":type=ResourceLink" + ",resourcetype=Global" + ",name=" + quotedResourceLinkName);
} elseif (container instanceof Context) {
Context context = ((Context) container);
ContextName cn = new ContextName(context.getName(), false);
Container host = context.getParent();
name = new ObjectName(domain + ":type=ResourceLink" + ",resourcetype=Context,host=" + host.getName() + ",context=" + cn.getDisplayName() + ",name=" + quotedResourceLinkName);
}
return name;
}
/** * Create an <code>ObjectName</code> for this <code>Group</code> object. * * @param domain Domain in which this name is to be created * @param group The Group to be named * * @return a new object name * * @exception MalformedObjectNameException if a name cannot be created
*/ static ObjectName createObjectName(String domain, Group group) throws MalformedObjectNameException {
ObjectName name = null;
name = new ObjectName(domain + ":type=Group,groupname=" + ObjectName.quote(group.getGroupname()) + ",database=" + group.getUserDatabase().getId()); return name;
}
/** * Create an <code>ObjectName</code> for this <code>Loader</code> object. * * @param domain Domain in which this name is to be created * @param loader The Loader to be named * * @return a new object name * * @exception MalformedObjectNameException if a name cannot be created
*/ static ObjectName createObjectName(String domain, Loader loader) throws MalformedObjectNameException {
ObjectName name = null;
Context context = loader.getContext();
ContextName cn = new ContextName(context.getName(), false);
Container host = context.getParent();
name = new ObjectName(domain + ":type=Loader,host=" + host.getName() + ",context=" + cn.getDisplayName());
return name;
}
/** * Create an <code>ObjectName</code> for this <code>Role</code> object. * * @param domain Domain in which this name is to be created * @param role The Role to be named * * @return a new object name * * @exception MalformedObjectNameException if a name cannot be created
*/ static ObjectName createObjectName(String domain, Role role) throws MalformedObjectNameException {
ObjectName name = new ObjectName(domain + ":type=Role,rolename=" + ObjectName.quote(role.getRolename()) + ",database=" + role.getUserDatabase().getId()); return name;
}
/** * Create an <code>ObjectName</code> for this <code>User</code> object. * * @param domain Domain in which this name is to be created * @param user The User to be named * * @return a new object name * * @exception MalformedObjectNameException if a name cannot be created
*/ static ObjectName createObjectName(String domain, User user) throws MalformedObjectNameException {
ObjectName name = new ObjectName(domain + ":type=User,username=" + ObjectName.quote(user.getUsername()) + ",database=" + user.getUserDatabase().getId()); return name;
}
/** * Create an <code>ObjectName</code> for this <code>UserDatabase</code> object. * * @param domain Domain in which this name is to be created * @param userDatabase The UserDatabase to be named * * @return a new object name * * @exception MalformedObjectNameException if a name cannot be created
*/ static ObjectName createObjectName(String domain, UserDatabase userDatabase) throws MalformedObjectNameException {
ObjectName name = null;
name = new ObjectName(domain + ":type=UserDatabase,database=" + userDatabase.getId()); return name;
}
/** * Create and configure (if necessary) and return the registry of managed object descriptions. * * @return the singleton registry
*/ publicstaticsynchronized Registry createRegistry() { if (registry == null) {
registry = Registry.getRegistry(null, null);
ClassLoader cl = MBeanUtils.class.getClassLoader();
/** * Create and configure (if necessary) and return the <code>MBeanServer</code> with which we will be registering our * <code>DynamicMBean</code> implementations. * * @return the singleton MBean server
*/ publicstaticsynchronized MBeanServer createServer() { if (mserver == null) {
mserver = Registry.getRegistry(null, null).getMBeanServer();
} return mserver;
}
/** * Deregister the MBean for this <code>ContextEnvironment</code> object. * * @param environment The ContextEnvironment to be managed * * @exception Exception if an MBean cannot be deregistered
*/ publicstaticvoid destroyMBean(ContextEnvironment environment) throws Exception {
/** * Deregister the MBean for this <code>ContextResource</code> object. * * @param resource The ContextResource to be managed * * @exception Exception if an MBean cannot be deregistered
*/ publicstaticvoid destroyMBean(ContextResource resource) throws Exception {
// If this is a user database resource need to destroy groups, roles, // users and UserDatabase mbean if ("org.apache.catalina.UserDatabase".equals(resource.getType())) {
destroyMBeanUserDatabase(resource.getName());
}
/** * Deregister the MBean for this <code>ContextResourceLink</code> object. * * @param resourceLink The ContextResourceLink to be managed * * @exception Exception if an MBean cannot be deregistered
*/ publicstaticvoid destroyMBean(ContextResourceLink resourceLink) throws Exception {
/** * Deregister the MBean for this <code>Group</code> object. * * @param group The Group to be managed * * @exception Exception if an MBean cannot be deregistered
*/ staticvoid destroyMBean(Group group) throws Exception {
/** * Deregister the MBean for this <code>Role</code> object. * * @param role The Role to be managed * * @exception Exception if an MBean cannot be deregistered
*/ staticvoid destroyMBean(Role role) throws Exception {
/** * Deregister the MBean for this <code>User</code> object. * * @param user The User to be managed * * @exception Exception if an MBean cannot be deregistered
*/ staticvoid destroyMBean(User user) throws Exception {
/** * Deregister the MBean for the <code>UserDatabase</code> object with this name. * * @param userDatabase The UserDatabase to be managed * * @exception Exception if an MBean cannot be deregistered
*/ staticvoid destroyMBeanUserDatabase(String userDatabase) throws Exception {
// Groups
query = new ObjectName("Users:type=Group,database=" + userDatabase + ",*");
results = mserver.queryNames(query, null); for (ObjectName result : results) {
mserver.unregisterMBean(result);
}
// Roles
query = new ObjectName("Users:type=Role,database=" + userDatabase + ",*");
results = mserver.queryNames(query, null); for (ObjectName result : results) {
mserver.unregisterMBean(result);
}
// Users
query = new ObjectName("Users:type=User,database=" + userDatabase + ",*");
results = mserver.queryNames(query, null); for (ObjectName result : results) {
mserver.unregisterMBean(result);
}
// The database itself
ObjectName db = new ObjectName("Users:type=UserDatabase,database=" + userDatabase); if (mserver.isRegistered(db)) {
mserver.unregisterMBean(db);
}
db = new ObjectName("Catalina:type=UserDatabase,database=" + userDatabase); if (mserver.isRegistered(db)) {
mserver.unregisterMBean(db);
}
}
}
¤ Dauer der Verarbeitung: 0.5 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.