use of javax.management.MalformedObjectNameException in project tomcat by apache.
the class MemoryUserDatabaseMBean method findUser.
/**
* Return the MBean Name for the specified user name (if any);
* otherwise return <code>null</code>.
*
* @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;
}
}
use of javax.management.MalformedObjectNameException in project tomcat by apache.
the class StoreConfig method storeServer.
/**
* Store Server from Object Name (Catalina:type=Server).
*
* @param aServerName Server ObjectName
* @param backup <code>true</code> to backup existing configuration files
* before rewriting them
* @param externalAllowed <code>true</code> to allow saving webapp
* configuration for webapps that are not inside the host's app
* directory
* @throws MalformedObjectNameException Bad MBean name
*/
public synchronized void storeServer(String aServerName, boolean backup, boolean externalAllowed) throws MalformedObjectNameException {
if (aServerName == null || aServerName.length() == 0) {
if (log.isErrorEnabled())
log.error("Please, call with a correct server ObjectName!");
return;
}
MBeanServer mserver = MBeanUtils.createServer();
ObjectName objectName = new ObjectName(aServerName);
if (mserver.isRegistered(objectName)) {
try {
Server aServer = (Server) mserver.getAttribute(objectName, "managedResource");
StoreDescription desc = null;
desc = getRegistry().findDescription(StandardContext.class);
if (desc != null) {
boolean oldSeparate = desc.isStoreSeparate();
boolean oldBackup = desc.isBackup();
boolean oldExternalAllowed = desc.isExternalAllowed();
try {
desc.setStoreSeparate(true);
desc.setBackup(backup);
desc.setExternalAllowed(externalAllowed);
store(aServer);
} finally {
desc.setStoreSeparate(oldSeparate);
desc.setBackup(oldBackup);
desc.setExternalAllowed(oldExternalAllowed);
}
} else {
store(aServer);
}
} catch (Exception e) {
if (log.isInfoEnabled())
log.info("Object " + aServerName + " is no a Server instance or store exception", e);
}
} else if (log.isInfoEnabled())
log.info("Server " + aServerName + " not found!");
}
use of javax.management.MalformedObjectNameException in project tomcat by apache.
the class BaseGenericObjectPool method jmxRegister.
/**
* Registers the pool with the platform MBean server.
* The registered name will be
* <code>jmxNameBase + jmxNamePrefix + i</code> where i is the least
* integer greater than or equal to 1 such that the name is not already
* registered. Swallows MBeanRegistrationException, NotCompliantMBeanException
* returning null.
*
* @param config Pool configuration
* @param jmxNameBase default base JMX name for this pool
* @param jmxNamePrefix name prefix
* @return registered ObjectName, null if registration fails
*/
private ObjectName jmxRegister(final BaseObjectPoolConfig config, final String jmxNameBase, String jmxNamePrefix) {
ObjectName objectName = null;
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
int i = 1;
boolean registered = false;
String base = config.getJmxNameBase();
if (base == null) {
base = jmxNameBase;
}
while (!registered) {
try {
ObjectName objName;
// only one so the names are cleaner.
if (i == 1) {
objName = new ObjectName(base + jmxNamePrefix);
} else {
objName = new ObjectName(base + jmxNamePrefix + i);
}
mbs.registerMBean(this, objName);
objectName = objName;
registered = true;
} catch (final MalformedObjectNameException e) {
if (BaseObjectPoolConfig.DEFAULT_JMX_NAME_PREFIX.equals(jmxNamePrefix) && jmxNameBase.equals(base)) {
// Shouldn't happen. Skip registration if it does.
registered = true;
} else {
// Must be an invalid name. Use the defaults instead.
jmxNamePrefix = BaseObjectPoolConfig.DEFAULT_JMX_NAME_PREFIX;
base = jmxNameBase;
}
} catch (final InstanceAlreadyExistsException e) {
// Increment the index and try again
i++;
} catch (final MBeanRegistrationException e) {
// Shouldn't happen. Skip registration if it does.
registered = true;
} catch (final NotCompliantMBeanException e) {
// Shouldn't happen. Skip registration if it does.
registered = true;
}
}
return objectName;
}
use of javax.management.MalformedObjectNameException in project tomcat by apache.
the class BasicDataSource method jmxRegister.
private void jmxRegister() {
// Return immediately if this DataSource has already been registered
if (registeredJmxName != null) {
return;
}
// Return immediately if no JMX name has been specified
final String requestedName = getJmxName();
if (requestedName == null) {
return;
}
ObjectName oname;
try {
oname = new ObjectName(requestedName);
} catch (final MalformedObjectNameException e) {
log.warn("The requested JMX name [" + requestedName + "] was not valid and will be ignored.");
return;
}
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
try {
mbs.registerMBean(this, oname);
} catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
log.warn("Failed to complete JMX registration", e);
}
}
use of javax.management.MalformedObjectNameException in project tomcat by apache.
the class GroupMBean method getUsers.
/**
* @return the MBean Names of all users that are members of this group.
*/
public String[] getUsers() {
Group group = (Group) this.resource;
ArrayList<String> results = new ArrayList<>();
Iterator<User> users = group.getUsers();
while (users.hasNext()) {
User user = null;
try {
user = users.next();
ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), user);
results.add(oname.toString());
} catch (MalformedObjectNameException e) {
IllegalArgumentException iae = new IllegalArgumentException("Cannot create object name for user " + user);
iae.initCause(e);
throw iae;
}
}
return results.toArray(new String[results.size()]);
}
Aggregations