Search in sources :

Example 86 with ObjectInstance

use of javax.management.ObjectInstance in project camel by apache.

the class DefaultManagementAgent method registerMBeanWithServer.

private void registerMBeanWithServer(Object obj, ObjectName name, boolean forceRegistration) throws JMException {
    // have we already registered the bean, there can be shared instances in the camel routes
    boolean exists = isRegistered(name);
    if (exists) {
        if (forceRegistration) {
            LOG.info("ForceRegistration enabled, unregistering existing MBean with ObjectName: {}", name);
            server.unregisterMBean(name);
        } else {
            // okay ignore we do not want to force it and it could be a shared instance
            LOG.debug("MBean already registered with ObjectName: {}", name);
        }
    }
    // register bean if by force or not exists
    ObjectInstance instance = null;
    if (forceRegistration || !exists) {
        LOG.trace("Registering MBean with ObjectName: {}", name);
        instance = server.registerMBean(obj, name);
    }
    // need to use the name returned from the server as some JEE servers may modify the name
    if (instance != null) {
        ObjectName registeredName = instance.getObjectName();
        LOG.debug("Registered MBean with ObjectName: {}", registeredName);
        mbeansRegistered.put(name, registeredName);
    }
}
Also used : ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName)

Example 87 with ObjectInstance

use of javax.management.ObjectInstance in project camel by apache.

the class DefaultManagementAgentMockTest method testObjectNameModification.

@Test
public void testObjectNameModification() throws JMException {
    MBeanServer mbeanServer = createStrictMock(MBeanServer.class);
    ObjectInstance instance = createStrictMock(ObjectInstance.class);
    ManagementAgent agent = new DefaultManagementAgent();
    agent.setMBeanServer(mbeanServer);
    Object object = "object";
    ObjectName sourceObjectName = new ObjectName("domain", "key", "value");
    ObjectName registeredObjectName = new ObjectName("domain", "key", "otherValue");
    // Register MBean and return different ObjectName
    expect(mbeanServer.isRegistered(sourceObjectName)).andReturn(false);
    expect(mbeanServer.registerMBean(object, sourceObjectName)).andReturn(instance);
    expect(instance.getObjectName()).andReturn(registeredObjectName);
    expect(mbeanServer.isRegistered(registeredObjectName)).andReturn(true);
    replay(mbeanServer, instance);
    agent.register(object, sourceObjectName);
    assertTrue(agent.isRegistered(sourceObjectName));
    verify(mbeanServer, instance);
    reset(mbeanServer, instance);
    // ... and unregister it again
    expect(mbeanServer.isRegistered(registeredObjectName)).andReturn(true);
    mbeanServer.unregisterMBean(registeredObjectName);
    expect(mbeanServer.isRegistered(sourceObjectName)).andReturn(false);
    replay(mbeanServer);
    agent.unregister(sourceObjectName);
    assertFalse(agent.isRegistered(sourceObjectName));
    verify(mbeanServer);
}
Also used : ManagementAgent(org.apache.camel.spi.ManagementAgent) ObjectInstance(javax.management.ObjectInstance) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 88 with ObjectInstance

use of javax.management.ObjectInstance in project geode by apache.

the class MBeanServerConnectionRule method getProxyMBean.

/**
   * Retrieve a new proxy MBean
   *
   * @return A new proxy MBean of the same type with which the class was constructed
   */
public <T> T getProxyMBean(Class<T> proxyClass, String beanQueryName) throws MalformedObjectNameException, IOException {
    ObjectName name = null;
    QueryExp query = null;
    if (proxyClass != null) {
        query = Query.isInstanceOf(Query.value(proxyClass.getName()));
    }
    if (beanQueryName != null) {
        name = ObjectName.getInstance(beanQueryName);
    }
    Set<ObjectInstance> beans = con.queryMBeans(name, query);
    assertEquals("failed to find only one instance of type " + proxyClass.getName() + " with name " + beanQueryName, 1, beans.size());
    return JMX.newMXBeanProxy(con, ((ObjectInstance) beans.toArray()[0]).getObjectName(), proxyClass);
}
Also used : QueryExp(javax.management.QueryExp) ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName)

Example 89 with ObjectInstance

use of javax.management.ObjectInstance in project geode by apache.

the class ManagementAdapter method handleManagerStop.

/**
   * Handles all the clean up activities when a Manager is stopped It clears the distributed mbeans
   * and underlying data structures
   */
public void handleManagerStop() throws ManagementException {
    if (!isServiceInitialised("handleManagerStop")) {
        return;
    }
    MBeanJMXAdapter jmxAdapter = service.getJMXAdapter();
    Map<ObjectName, Object> registeredMBeans = jmxAdapter.getLocalGemFireMBean();
    ObjectName aggregatemMBeanPattern;
    try {
        aggregatemMBeanPattern = new ObjectName(ManagementConstants.AGGREGATE_MBEAN_PATTERN);
    } catch (MalformedObjectNameException | NullPointerException e1) {
        throw new ManagementException(e1);
    }
    MemberMXBean localMember = service.getMemberMXBean();
    ObjectName memberObjectName = MBeanJMXAdapter.getMemberMBeanName(InternalDistributedSystem.getConnectedInstance().getDistributedMember());
    FederationComponent removedComp = service.getLocalManager().getFedComponents().get(memberObjectName);
    service.afterRemoveProxy(memberObjectName, MemberMXBean.class, localMember, removedComp);
    for (ObjectName objectName : registeredMBeans.keySet()) {
        if (objectName.equals(memberObjectName)) {
            continue;
        }
        if (aggregatemMBeanPattern.apply(objectName)) {
            continue;
        }
        Object object = registeredMBeans.get(objectName);
        ObjectInstance instance;
        try {
            instance = mbeanServer.getObjectInstance(objectName);
            String className = instance.getClassName();
            Class cls = ClassLoadUtil.classFromName(className);
            Type[] intfTyps = cls.getGenericInterfaces();
            FederationComponent oldObj = service.getLocalManager().getFedComponents().get(objectName);
            for (Type intfTyp1 : intfTyps) {
                Class intfTyp = (Class) intfTyp1;
                service.afterRemoveProxy(objectName, intfTyp, object, oldObj);
            }
        } catch (InstanceNotFoundException | ClassNotFoundException e) {
            logger.warn("Failed to invoke aggregator for {} with exception {}", objectName, e.getMessage(), e);
        }
    }
    service.removeProxyListener(this.aggregator);
    this.aggregator = null;
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName) MemberMXBean(org.apache.geode.management.MemberMXBean) ManagementException(org.apache.geode.management.ManagementException) Type(java.lang.reflect.Type) JMXNotificationType(org.apache.geode.management.JMXNotificationType) MBeanJMXAdapter(org.apache.geode.management.internal.MBeanJMXAdapter) FederationComponent(org.apache.geode.management.internal.FederationComponent)

Example 90 with ObjectInstance

use of javax.management.ObjectInstance in project jdk8u_jdk by JetBrains.

the class ScanManager method addDirectoryScanner.

// Creates and registers a new directory scanner.
// Called by applyConfiguration.
// throws IllegalStateException if state is not STOPPED or COMPLETED
// (you cannot change the config while scanning is scheduled or running).
//
private DirectoryScannerMXBean addDirectoryScanner(DirectoryScannerConfig bean) throws JMException {
    try {
        final DirectoryScannerMXBean scanner;
        final ObjectName scanName;
        synchronized (this) {
            if (state != STOPPED && state != COMPLETED)
                throw new IllegalStateException(state.toString());
            scanner = createDirectoryScanner(bean);
            scanName = makeDirectoryScannerName(bean.getName());
        }
        LOG.fine("server: " + mbeanServer);
        LOG.fine("scanner: " + scanner);
        LOG.fine("scanName: " + scanName);
        final ObjectInstance moi = mbeanServer.registerMBean(scanner, scanName);
        final ObjectName moiName = moi.getObjectName();
        final DirectoryScannerMXBean proxy = JMX.newMXBeanProxy(mbeanServer, moiName, DirectoryScannerMXBean.class, true);
        scanmap.put(moiName, proxy);
        return proxy;
    } catch (RuntimeException x) {
        final String msg = "Operation failed: " + x;
        if (LOG.isLoggable(Level.FINEST))
            LOG.log(Level.FINEST, msg, x);
        else
            LOG.fine(msg);
        throw x;
    } catch (JMException x) {
        final String msg = "Operation failed: " + x;
        if (LOG.isLoggable(Level.FINEST))
            LOG.log(Level.FINEST, msg, x);
        else
            LOG.fine(msg);
        throw x;
    }
}
Also used : ObjectInstance(javax.management.ObjectInstance) JMException(javax.management.JMException) ObjectName(javax.management.ObjectName)

Aggregations

ObjectInstance (javax.management.ObjectInstance)172 ObjectName (javax.management.ObjectName)102 Test (org.junit.Test)38 MBeanServer (javax.management.MBeanServer)36 InstanceNotFoundException (javax.management.InstanceNotFoundException)28 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)16 MBeanInfo (javax.management.MBeanInfo)16 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)15 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)15 HashMap (java.util.HashMap)13 Attribute (javax.management.Attribute)13 HashSet (java.util.HashSet)12 JmxResultProcessor (com.googlecode.jmxtrans.model.JmxResultProcessor)11 Result (com.googlecode.jmxtrans.model.Result)11 Set (java.util.Set)11 ReflectionException (javax.management.ReflectionException)11 MalformedObjectNameException (javax.management.MalformedObjectNameException)10 Test (org.junit.jupiter.api.Test)10 MBeanServerConnection (javax.management.MBeanServerConnection)9