/* * 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.util.ArrayList; import java.util.Iterator; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; import org.apache.catalina.Group; import org.apache.catalina.Role; import org.apache.catalina.User; import org.apache.catalina.UserDatabase; import org.apache.tomcat.util.modeler.BaseModelMBean; import org.apache.tomcat.util.modeler.ManagedBean; import org.apache.tomcat.util.modeler.Registry; /** *
A ModelMBean implementation for the
* org.apache.catalina.users.MemoryUserDatabase component.
ManagedBean information describing this MBean.
*/
protected final ManagedBean managed = registry.findManagedBean("MemoryUserDatabase");
/**
* The ManagedBean information describing Group MBeans.
*/
protected final ManagedBean managedGroup = registry.findManagedBean("Group");
/**
* The ManagedBean information describing Group MBeans.
*/
protected final ManagedBean managedRole = registry.findManagedBean("Role");
/**
* The ManagedBean information describing User MBeans.
*/
protected final ManagedBean managedUser = registry.findManagedBean("User");
// ------------------------------------------------------------- Attributes
/**
* @return the MBean Names of all groups defined in this database.
*/
public String[] getGroups() {
UserDatabase database = (UserDatabase) this.resource;
ArrayListnull.
*
* @param groupname Group name to look up
* @return the group object name
*/
public String findGroup(String groupname) {
UserDatabase database = (UserDatabase) this.resource;
Group group = database.findGroup(groupname);
if (group == null) {
return null;
}
try {
ObjectName oname = MBeanUtils.createObjectName(managedGroup.getDomain(), group);
return oname.toString();
} catch (MalformedObjectNameException e) {
IllegalArgumentException iae = new IllegalArgumentException(
"Cannot create object name for group [" + groupname + "]");
iae.initCause(e);
throw iae;
}
}
/**
* Return the MBean Name for the specified role name (if any);
* otherwise return null.
*
* @param rolename Role name to look up
* @return the role object name
*/
public String findRole(String rolename) {
UserDatabase database = (UserDatabase) this.resource;
Role role = database.findRole(rolename);
if (role == null) {
return null;
}
try {
ObjectName oname = MBeanUtils.createObjectName(managedRole.getDomain(), role);
return oname.toString();
} catch (MalformedObjectNameException e) {
IllegalArgumentException iae = new IllegalArgumentException(
"Cannot create object name for role [" + rolename + "]");
iae.initCause(e);
throw iae;
}
}
/**
* Return the MBean Name for the specified user name (if any);
* otherwise return null.
*
* @param username User name to look up
* @return the user object name
*/
public String findUser(String username) {
UserDatabase database = (UserDatabase) this.resource;
User user = database.findUser(username);
if (user == null) {
return null;
}
try {
ObjectName oname = MBeanUtils.createObjectName(managedUser.getDomain(), user);
return oname.toString();
} catch (MalformedObjectNameException e) {
IllegalArgumentException iae = new IllegalArgumentException(
"Cannot create object name for user [" + username + "]");
iae.initCause(e);
throw iae;
}
}
/**
* Remove an existing group and destroy the corresponding MBean.
*
* @param groupname Group name to remove
*/
public void removeGroup(String groupname) {
UserDatabase database = (UserDatabase) this.resource;
Group group = database.findGroup(groupname);
if (group == null) {
return;
}
try {
MBeanUtils.destroyMBean(group);
database.removeGroup(group);
} catch (Exception e) {
IllegalArgumentException iae = new IllegalArgumentException(
"Exception destroying group [" + groupname + "] MBean");
iae.initCause(e);
throw iae;
}
}
/**
* Remove an existing role and destroy the corresponding MBean.
*
* @param rolename Role name to remove
*/
public void removeRole(String rolename) {
UserDatabase database = (UserDatabase) this.resource;
Role role = database.findRole(rolename);
if (role == null) {
return;
}
try {
MBeanUtils.destroyMBean(role);
database.removeRole(role);
} catch (Exception e) {
IllegalArgumentException iae = new IllegalArgumentException(
"Exception destroying role [" + rolename + "] MBean");
iae.initCause(e);
throw iae;
}
}
/**
* Remove an existing user and destroy the corresponding MBean.
*
* @param username User name to remove
*/
public void removeUser(String username) {
UserDatabase database = (UserDatabase) this.resource;
User user = database.findUser(username);
if (user == null) {
return;
}
try {
MBeanUtils.destroyMBean(user);
database.removeUser(user);
} catch (Exception e) {
IllegalArgumentException iae = new IllegalArgumentException(
"Exception destroying user [" + username + "] MBean");
iae.initCause(e);
throw iae;
}
}
}