001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.jems.as;
023:
024: import org.apache.log4j.Logger;
025: import org.jboss.mx.util.MBeanProxy;
026: import org.jboss.mx.util.MBeanProxyCreationException;
027: import org.jboss.mx.util.MBeanServerLocator;
028: import org.jboss.mx.util.ObjectNameConverter;
029:
030: import javax.management.InstanceNotFoundException;
031: import javax.management.ListenerNotFoundException;
032: import javax.management.MBeanRegistrationException;
033: import javax.management.MBeanServer;
034: import javax.management.MalformedObjectNameException;
035: import javax.management.NotificationFilter;
036: import javax.management.NotificationListener;
037: import javax.management.ObjectName;
038: import java.util.Comparator;
039: import java.util.Hashtable;
040: import java.util.Properties;
041:
042: /**
043: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
044: * @version $Revision: 8784 $
045: */
046: public final class JMX {
047:
048: private static final Logger log = Logger.getLogger(JMX.class);
049:
050: public static ObjectName extend(ObjectName name,
051: Properties keyProperties) {
052: try {
053: Hashtable table = name.getKeyPropertyList();
054: table.putAll(keyProperties);
055: return ObjectNameConverter.convert(name.getDomain(), table);
056: } catch (MalformedObjectNameException e) {
057: log.error("", e);
058: throw new RuntimeException();
059: }
060: }
061:
062: /** ObjectName comparator based on canonical name lexicography. */
063: public static Comparator OBJECT_NAME_COMPARATOR = new Comparator() {
064: public int compare(Object o1, Object o2) {
065: ObjectName n1 = (ObjectName) o1;
066: ObjectName n2 = (ObjectName) o2;
067: return n1.getCanonicalName().compareTo(
068: n2.getCanonicalName());
069: }
070: };
071:
072: public static void safeUnregister(MBeanServer server,
073: ObjectName name) {
074: if (server != null) {
075: if (name != null) {
076: try {
077: server.unregisterMBean(name);
078: } catch (InstanceNotFoundException e) {
079: log.error("MBean " + name + " nto here");
080: } catch (MBeanRegistrationException e) {
081: log
082: .error(
083: "MBean threw an exception during unregistration",
084: e.getTargetException());
085: }
086: } else {
087: log.error("Cannot unregister a null MBean");
088: }
089: } else {
090: log.error("Cannot unregister with a null MBeanServer");
091: }
092: }
093:
094: /**
095: * Retrieves the MBeanProxy associated with the given class and name from the specified MBeanServer.
096: *
097: * @param clazz the interface implemented by the MBean which is to be retrieved
098: * @param name the MBean's ObjectName
099: * @param server the MBeanServer from which to retrieve the MBeanProxy
100: * @return a MBeanProxy for the specified MBean if it exists
101: * @throws RuntimeException if the MBean couldn't be retrieved
102: */
103: public static Object getMBeanProxy(Class clazz, ObjectName name,
104: MBeanServer server) {
105: try {
106: return MBeanProxy.get(clazz, name, server);
107: } catch (MBeanProxyCreationException e) {
108: String message = "Couldn't retrieve '"
109: + name.getCanonicalName() + "' MBean with class "
110: + clazz.getName();
111: log.error(message, e);
112: throw new RuntimeException(message, e);
113: }
114: }
115:
116: /**
117: * Retrieves the MBeanProxy associated with the given class and name from the JBoss microkernel as returned by
118: * <code>MBeanServerLocator.locateJBoss()</code>.
119: *
120: * @param clazz the interface implemented by the MBean which is to be retrieved
121: * @param name a String representation of the MBean's ObjectName
122: * @return a MBeanProxy for the specified MBean if it exists
123: * @throws IllegalArgumentException if the given name is not a valid ObjectName
124: * @throws RuntimeException if the MBean couldn't be retrieved
125: * @see #getMBeanProxy(Class, javax.management.ObjectName, javax.management.MBeanServer)
126: * @since 2.4
127: */
128: public static Object getMBeanProxy(Class clazz, String name) {
129: ObjectName objecName;
130: try {
131: objecName = new ObjectName(name);
132: } catch (MalformedObjectNameException e) {
133: throw new IllegalArgumentException("'" + name
134: + "' is not a valid ObjectName");
135: }
136: MBeanServer server = MBeanServerLocator.locateJBoss();
137: return getMBeanProxy(clazz, objecName, server);
138: }
139:
140: public static boolean addNotificationListener(MBeanServer server,
141: ObjectName name, NotificationListener listener,
142: NotificationFilter filter, Object handback) {
143: try {
144: server.addNotificationListener(name, listener, filter,
145: handback);
146: return true;
147: } catch (InstanceNotFoundException e) {
148: return false;
149: }
150: }
151:
152: public static boolean removeNotificationListener(
153: MBeanServer server, ObjectName name,
154: NotificationListener listener) {
155: try {
156: server.removeNotificationListener(name, listener);
157: return true;
158: } catch (InstanceNotFoundException e) {
159: log.error("Cannot remove notification listener", e);
160: return false;
161: } catch (ListenerNotFoundException e) {
162: log.error("Cannot remove notification listener", e);
163: return false;
164: }
165: }
166: }
|