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);
}
}
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);
}
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);
}
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;
}
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;
}
}
Aggregations