com.englishtown.jmx.BeanManager Maven / Gradle / Ivy
/*
* The MIT License (MIT)
* Copyright © 2013 Englishtown
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.englishtown.jmx;
import javax.management.*;
import java.lang.management.ManagementFactory;
import java.util.Hashtable;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
*/
public enum BeanManager {
INSTANCE;
private static final String DOMAIN = "com.englishtown";
private MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
private ConcurrentHashMap registeredBeans = new ConcurrentHashMap<>();
private boolean enabled;
private BeanManager() {
enabled = ("true".equals(System.getProperty("englishtown.jmx")));
}
public synchronized BeanContainer registerBean(final Object bean, final Hashtable keys) throws MalformedObjectNameException, NotCompliantMBeanException, MBeanRegistrationException, InstanceAlreadyExistsException {
ObjectName objectName = new ObjectName(DOMAIN, keys);
if (!mBeanServer.isRegistered(objectName)) {
mBeanServer.registerMBean(bean, objectName);
}
BeanContainer beanContainer = new BeanContainer(bean, objectName);
BeanContainer registeredBeanContainer = registeredBeans.putIfAbsent(objectName, beanContainer);
return (registeredBeanContainer == null ? beanContainer : registeredBeanContainer);
}
public synchronized void unregisterBean(final ObjectName objectName) throws MBeanRegistrationException, InstanceNotFoundException {
if (mBeanServer.isRegistered(objectName)) {
mBeanServer.unregisterMBean(objectName);
}
registeredBeans.remove(objectName);
}
public void unregisterBean(final Hashtable keys) throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException {
ObjectName objectName = new ObjectName(DOMAIN, keys);
this.unregisterBean(objectName);
}
public synchronized void unregisterAllBeans() throws MBeanRegistrationException, InstanceNotFoundException {
Set objectNames = registeredBeans.keySet();
for (ObjectName objectName : objectNames) {
unregisterBean(objectName);
}
}
public boolean isRegistered(final ObjectName objectName) {
return mBeanServer.isRegistered(objectName);
}
public BeanContainer getBeanContainerForObjectName(final ObjectName objectName) {
return registeredBeans.get(objectName);
}
public boolean isEnabled() {
return enabled;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy