Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* $Id: MBeanHelper.java,v 1.14 2014/05/03 20:02:39 oboehm Exp $
*
* Copyright (c) 2008 by Oliver Boehm
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 19.02.2009 by oliver ([email protected])
*/
package patterntesting.runtime.jmx;
import java.lang.management.ManagementFactory;
import javax.management.*;
import javax.management.openmbean.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.*;
/**
* This class simplifies the use of JMX and MBeans a little bit.
*
* @author oliver
* @since 19.02.2009
* @version $Revision: 1.14 $
*/
public class MBeanHelper {
private static final Logger log = LoggerFactory.getLogger(MBeanHelper.class);
private static MBeanServer server = ManagementFactory.getPlatformMBeanServer();
static {
registerMBean("patterntesting:type=runtime,name=Info", new Info());
}
/** Utility class - no need to instantiate it */
private MBeanHelper() {}
/**
* Gets the m bean name.
*
* @param mbean the mbean
*
* @return the name of the MBean
*/
public static String getMBeanName(final Object mbean) {
return getMBeanName(mbean.getClass());
}
/**
* Converts the class name into a MBean name. For a hierachical structure of
* the registered MBeans take a look at Java Management Extensions (JMX) - Best Practices.
*
* @param cl e.g. my.good.bye.World
* @return a valid MBean name e.g. "my:type=good,good=bye,name=World"
*/
public static String getMBeanName(final Class> cl) {
String packageName = cl.getPackage().getName();
String[] names = StringUtils.split(packageName, ".");
String domain = names[0];
String type = names[1];
StringBuffer mbeanName = new StringBuffer(domain + ":type=" + type);
for (int i = 2; i < names.length; i++) {
mbeanName.append("," + names[i-1] + "=" + names[i]);
}
return mbeanName + ",name=" + cl.getSimpleName();
}
/**
* Register m bean.
*
* @param mbean the mbean
*
* @throws JMException if registration fails
*/
public static void registerMBean(final Object mbean) throws JMException {
String mbeanName = getMBeanName(mbean);
registerMBean(mbeanName, mbean);
}
/**
* Register m bean.
*
* @param mbeanName the mbean name
* @param mbean the mbean
*/
public static synchronized void registerMBean(final String mbeanName, final Object mbean) {
try {
ObjectName name = new ObjectName(mbeanName);
registerMBean(name, mbean);
} catch (MalformedObjectNameException e) {
log.info("can't register <" + mbean + "> as MBean", e);
}
}
/**
* Register m bean.
*
* @param name the name
* @param mbean the mbean
*/
public static synchronized void registerMBean(final ObjectName name, final Object mbean) {
try {
log.trace("registering " + name + "...");
server.registerMBean(mbean, name);
log.debug(name + " successful registered as MBean");
} catch (Exception e) {
log.info("can't register <" + mbean + "> as MBean", e);
}
}
/**
* Unregister m bean.
*
* @param mbeanName the mbean name
*/
public static synchronized void unregisterMBean(final String mbeanName) {
try {
ObjectName name = new ObjectName(mbeanName);
server.unregisterMBean(name);
log.debug("MBean " + name + " successful unregistered");
} catch (Exception e) {
log.info("can't unregister " + mbeanName, e);
}
}
/**
* Checks if is registered.
*
* @param mbeanName the mbean name
*
* @return true, if is registered
*/
public static boolean isRegistered(final String mbeanName) {
try {
ObjectName name = new ObjectName(mbeanName);
ObjectInstance mbean = server.getObjectInstance(name);
return (mbean != null);
} catch (Exception e) {
log.trace(mbeanName + " not found (" + e + ")");
return false;
}
}
/**
* Checks if is registered.
*
* @param obj the obj
*
* @return true, if is registered
*/
public static boolean isRegistered(final Object obj) {
String mbeanName = getMBeanName(obj.getClass());
return isRegistered(mbeanName);
}
/**
* Gets the object instance.
*
* @param name the name
* @return the object instance
* @throws InstanceNotFoundException the instance not found exception
*/
public static ObjectInstance getObjectInstance(final String name) throws InstanceNotFoundException {
try {
ObjectName mbeanName = new ObjectName(name);
return getObjectInstance(mbeanName);
} catch (MalformedObjectNameException e) {
throw new IllegalArgumentException(name, e);
}
}
/**
* Gets the object instance.
*
* @param mbeanName the mbean name
* @return the object instance
* @throws InstanceNotFoundException the instance not found exception
*/
public static ObjectInstance getObjectInstance(final ObjectName mbeanName)
throws InstanceNotFoundException {
return server.getObjectInstance(mbeanName);
}
/**
* Gets the attribute.
*
* @param mbeanName the mbean name
* @param attributeName the attribute name
* @return the attribute
* @throws InstanceNotFoundException the instance not found exception
* @throws JMException the jM exception
*/
public static Object getAttribute(final String mbeanName, final String attributeName)
throws InstanceNotFoundException, JMException {
try {
ObjectName mbean = new ObjectName(mbeanName);
return getAttribute(mbean, attributeName);
} catch (MalformedObjectNameException e) {
throw new IllegalArgumentException(mbeanName, e);
}
}
/**
* Gets the attribute.
*
* @param mbean the mbean
* @param attributeName the attribute name
* @return the attribute
* @throws InstanceNotFoundException the instance not found exception
* @throws JMException the jM exception
*/
public static Object getAttribute(final ObjectName mbean, final String attributeName)
throws InstanceNotFoundException, JMException {
try {
return server.getAttribute(mbean, attributeName);
} catch (AttributeNotFoundException e) {
throw new IllegalArgumentException(attributeName, e);
}
}
/**
* Creates a {@link TabularDataSupport} object.
*
* @param rowType the row type
* @param itemNames the item names
* @return the tabular data support
* @throws OpenDataException the open data exception
*/
public static TabularDataSupport createTabularDataSupport(final CompositeType rowType, final String[] itemNames)
throws OpenDataException {
TabularType tabularType = new TabularType("propertyTabularType",
"properties tabular", rowType, itemNames);
TabularDataSupport data = new TabularDataSupport(tabularType);
return data;
}
}