Search in sources :

Example 76 with InstanceAlreadyExistsException

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);
}
Also used : InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) JMRuntimeException(javax.management.JMRuntimeException) ObjectName(javax.management.ObjectName) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 77 with InstanceAlreadyExistsException

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);
    }
}
Also used : RuntimeErrorException(javax.management.RuntimeErrorException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) ImplementationException(org.apache.felix.mosgi.jmx.agent.mx4j.ImplementationException) ObjectInstance(javax.management.ObjectInstance) PrivateClassLoader(javax.management.loading.PrivateClassLoader) IntrospectionException(javax.management.IntrospectionException) OperationsException(javax.management.OperationsException) BadBinaryOpValueExpException(javax.management.BadBinaryOpValueExpException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) BadStringOperationException(javax.management.BadStringOperationException) ReflectionException(javax.management.ReflectionException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) RuntimeErrorException(javax.management.RuntimeErrorException) MalformedObjectNameException(javax.management.MalformedObjectNameException) ListenerNotFoundException(javax.management.ListenerNotFoundException) RuntimeOperationsException(javax.management.RuntimeOperationsException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) JMRuntimeException(javax.management.JMRuntimeException) PrivilegedActionException(java.security.PrivilegedActionException) BadAttributeValueExpException(javax.management.BadAttributeValueExpException) IOException(java.io.IOException) ImplementationException(org.apache.felix.mosgi.jmx.agent.mx4j.ImplementationException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) InvalidApplicationException(javax.management.InvalidApplicationException) MBeanServerInterceptor(org.apache.felix.mosgi.jmx.agent.mx4j.server.interceptor.MBeanServerInterceptor) InvokerMBeanServerInterceptor(org.apache.felix.mosgi.jmx.agent.mx4j.server.interceptor.InvokerMBeanServerInterceptor) PrivateClassLoader(javax.management.loading.PrivateClassLoader) JMRuntimeException(javax.management.JMRuntimeException) MBeanRegistrationException(javax.management.MBeanRegistrationException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 78 with InstanceAlreadyExistsException

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;
}
Also used : NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InstanceNotFoundException(javax.management.InstanceNotFoundException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) MBeanRegistrationException(javax.management.MBeanRegistrationException) ObjectName(javax.management.ObjectName)

Example 79 with InstanceAlreadyExistsException

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");
    }
}
Also used : InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) MalformedObjectNameException(javax.management.MalformedObjectNameException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanServer(javax.management.MBeanServer)

Example 80 with InstanceAlreadyExistsException

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);
}
Also used : JMRuntimeException(javax.management.JMRuntimeException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) ObjectInstance(javax.management.ObjectInstance) NamedObject(com.sun.jmx.mbeanserver.NamedObject) IntrospectionException(javax.management.IntrospectionException) OperationsException(javax.management.OperationsException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) ReflectionException(javax.management.ReflectionException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) RuntimeMBeanException(javax.management.RuntimeMBeanException) RuntimeErrorException(javax.management.RuntimeErrorException) ListenerNotFoundException(javax.management.ListenerNotFoundException) RuntimeOperationsException(javax.management.RuntimeOperationsException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) JMRuntimeException(javax.management.JMRuntimeException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ObjectName(javax.management.ObjectName) DynamicMBean2(com.sun.jmx.mbeanserver.DynamicMBean2) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Aggregations

InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)116 ObjectName (javax.management.ObjectName)87 MBeanRegistrationException (javax.management.MBeanRegistrationException)73 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)70 MalformedObjectNameException (javax.management.MalformedObjectNameException)64 MBeanServer (javax.management.MBeanServer)48 InstanceNotFoundException (javax.management.InstanceNotFoundException)37 IOException (java.io.IOException)13 ObjectInstance (javax.management.ObjectInstance)12 Test (org.junit.Test)12 StandardMBean (javax.management.StandardMBean)9 MBeanException (javax.management.MBeanException)7 ReflectionException (javax.management.ReflectionException)7 HashMap (java.util.HashMap)5 JMRuntimeException (javax.management.JMRuntimeException)4 RuntimeOperationsException (javax.management.RuntimeOperationsException)4 MalformedURLException (java.net.MalformedURLException)3 AttributeNotFoundException (javax.management.AttributeNotFoundException)3 DynamicMBean (javax.management.DynamicMBean)3 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)3