use of javax.management.InstanceAlreadyExistsException in project felix by apache.
the class MX4JMBeanServer method register.
private void register(MBeanMetaData metadata, boolean privileged) throws InstanceAlreadyExistsException {
metadata.name = normalizeObjectName(metadata.name);
ObjectName objectName = metadata.name;
if (objectName == null || objectName.isPattern()) {
throw new RuntimeOperationsException(new IllegalArgumentException("ObjectName cannot be null or a pattern ObjectName"));
}
if (objectName.getDomain().equals("JMImplementation") && !privileged) {
throw new JMRuntimeException("Domain 'JMImplementation' is reserved for the JMX Agent");
}
MBeanRepository repository = getMBeanRepository();
synchronized (repository) {
if (repository.get(objectName) != null)
throw new InstanceAlreadyExistsException(objectName.toString());
repository.put(objectName, metadata);
}
addDomain(objectName.getDomain());
notify(objectName, MBeanServerNotification.REGISTRATION_NOTIFICATION);
}
use of javax.management.InstanceAlreadyExistsException in project felix by apache.
the class MX4JMBeanServer method registerImpl.
private void registerImpl(MBeanMetaData metadata, boolean privileged) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
introspector.introspect(metadata);
if (!introspector.isMBeanCompliant(metadata))
throw new NotCompliantMBeanException("MBean is not compliant");
MBeanServerInterceptor head = getHeadInterceptor();
try {
// With this call, the MBean implementor can replace the ObjectName with a subclass that is not secure, secure it again
head.registration(metadata, MBeanServerInterceptor.PRE_REGISTER);
metadata.name = secureObjectName(metadata.name);
metadata.instance = new ObjectInstance(metadata.name, metadata.info.getClassName());
register(metadata, privileged);
head.registration(metadata, MBeanServerInterceptor.POST_REGISTER_TRUE);
} catch (Throwable x) {
try {
head.registration(metadata, MBeanServerInterceptor.POST_REGISTER_FALSE);
} catch (MBeanRegistrationException ignored) {
/* Ignore this one to rethrow the other one */
}
if (x instanceof SecurityException) {
throw (SecurityException) x;
} else if (x instanceof InstanceAlreadyExistsException) {
throw (InstanceAlreadyExistsException) x;
} else if (x instanceof MBeanRegistrationException) {
throw (MBeanRegistrationException) x;
} else if (x instanceof RuntimeOperationsException) {
throw (RuntimeOperationsException) x;
} else if (x instanceof JMRuntimeException) {
throw (JMRuntimeException) x;
} else if (x instanceof Exception) {
throw new MBeanRegistrationException((Exception) x);
} else if (x instanceof Error) {
throw new MBeanRegistrationException(new RuntimeErrorException((Error) x));
} else {
throw new ImplementationException();
}
}
if (metadata.mbean instanceof ClassLoader && !(metadata.mbean instanceof PrivateClassLoader)) {
ClassLoader cl = (ClassLoader) metadata.mbean;
getModifiableClassLoaderRepository().addClassLoader(cl);
}
}
use of javax.management.InstanceAlreadyExistsException in project scylla-jmx by scylladb.
the class APIMBean method checkRegistration.
/**
* Helper method to add/remove dynamically created MBeans from a server
* instance.
*
* @param server
* The {@link MBeanServer} to check
* @param all
* All {@link ObjectName}s that should be bound
* @param predicate
* {@link QueryExp} predicate to filter relevant object names.
* @param generator
* {@link Function} to create a new MBean instance for a given
* {@link ObjectName}
*
* @return
* @throws MalformedObjectNameException
*/
public static boolean checkRegistration(MBeanServer server, Set<ObjectName> all, final Predicate<ObjectName> predicate, Function<ObjectName, Object> generator) throws MalformedObjectNameException {
Set<ObjectName> registered = queryNames(server, predicate);
for (ObjectName name : registered) {
if (!all.contains(name)) {
try {
server.unregisterMBean(name);
} catch (MBeanRegistrationException | InstanceNotFoundException e) {
}
}
}
int added = 0;
for (ObjectName name : all) {
if (!registered.contains(name)) {
try {
server.registerMBean(generator.apply(name), name);
added++;
} catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
}
}
}
return added > 0;
}
use of javax.management.InstanceAlreadyExistsException in project iaf by ibissource.
the class JmxMbeanHelper method registerMBean.
/**
* Registers an mBean at an MBeanServer. If there is already an mbean registered
* under the specified name, it is first de-registered.
*
* @param name the objectName
* @param mbean the modelMbean to register
* @throws ConfigurationException
*/
private static void registerMBean(ObjectName name, RequiredModelMBean mbean) throws ConfigurationException {
MBeanServer server = getMBeanServer();
if (server != null) {
LOG.debug("got an MBean server");
try {
if (server.isRegistered(name)) {
LOG.debug("unregistering [" + name.getCanonicalName() + "] as it already exists");
server.unregisterMBean(name);
}
server.registerMBean(mbean, name);
} catch (InstanceAlreadyExistsException iae) {
LOG.warn("Could not register mbean " + iae.getMessage());
} catch (Exception e) {
throw new ConfigurationException(e);
}
LOG.debug("MBean [" + name.getCanonicalName() + "] registered");
} else {
LOG.warn("No MBean server found");
}
}
use of javax.management.InstanceAlreadyExistsException in project jdk8u_jdk by JetBrains.
the class DefaultMBeanServerInterceptor method registerDynamicMBean.
private ObjectInstance registerDynamicMBean(String classname, DynamicMBean mbean, ObjectName name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
name = nonDefaultDomain(name);
if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
MBEANSERVER_LOGGER.logp(Level.FINER, DefaultMBeanServerInterceptor.class.getName(), "registerMBean", "ObjectName = " + name);
}
ObjectName logicalName = preRegister(mbean, server, name);
// preRegister returned successfully, so from this point on we
// must call postRegister(false) if there is any problem.
boolean registered = false;
boolean registerFailed = false;
ResourceContext context = null;
try {
if (mbean instanceof DynamicMBean2) {
try {
((DynamicMBean2) mbean).preRegister2(server, logicalName);
// until we succeed
registerFailed = true;
} catch (Exception e) {
if (e instanceof RuntimeException)
throw (RuntimeException) e;
if (e instanceof InstanceAlreadyExistsException)
throw (InstanceAlreadyExistsException) e;
throw new RuntimeException(e);
}
}
if (logicalName != name && logicalName != null) {
logicalName = ObjectName.getInstance(nonDefaultDomain(logicalName));
}
checkMBeanPermission(classname, null, logicalName, "registerMBean");
if (logicalName == null) {
final RuntimeException wrapped = new IllegalArgumentException("No object name specified");
throw new RuntimeOperationsException(wrapped, "Exception occurred trying to register the MBean");
}
final Object resource = getResource(mbean);
// Register the MBean with the repository.
// Returns the resource context that was used.
// The returned context does nothing for regular MBeans.
// For ClassLoader MBeans the context makes it possible to register these
// objects with the appropriate framework artifacts, such as
// the CLR, from within the repository lock.
// In case of success, we also need to call context.done() at the
// end of this method.
//
context = registerWithRepository(resource, mbean, logicalName);
registerFailed = false;
registered = true;
} finally {
try {
postRegister(logicalName, mbean, registered, registerFailed);
} finally {
if (registered && context != null)
context.done();
}
}
return new ObjectInstance(logicalName, classname);
}
Aggregations