jdillon 2003/08/30 09:32:38
Added: modules/common/src/java/org/apache/geronimo/common/jmx
AbstractMBeanProxyHandler.java
JMXExceptionDecoder.java MBeanProxyFactory.java
MBeanServerLocator.java ObjectNameFactory.java
Log:
o Moved MBeanProxyFactory & friends from core
o Added JMX helper utilities
Revision Changes Path
1.1
incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/jmx/AbstractMBeanProxyHandler.java
Index: AbstractMBeanProxyHandler.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.geronimo.common.jmx;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.HashMap;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.Attribute;
import javax.management.MBeanException;
import javax.management.ReflectionException;
import javax.management.RuntimeOperationsException;
import javax.management.RuntimeMBeanException;
import javax.management.RuntimeErrorException;
import org.apache.geronimo.common.NullArgumentException;
/**
* This class handles invocations for MBean proxies.
*
* Normally only the getObjectName method is necessary.
*
* @version $Revision: 1.1 $ $Date: 2003/08/30 16:32:38 $
*/
public abstract class AbstractMBeanProxyHandler
implements InvocationHandler, MBeanProxyFactory.MBeanProxy
{
protected final MBeanServer server;
protected final Map operationMap;
public AbstractMBeanProxyHandler(final Class iface, final MBeanServer
server)
{
if (iface == null) {
throw new NullArgumentException("iface");
}
if (!iface.isInterface()) {
throw new IllegalArgumentException("Not an interface: " + iface);
}
if (server == null) {
throw new NullArgumentException("server");
}
this.server = server;
Method[] methods = iface.getMethods();
operationMap = new HashMap(methods.length);
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
Class returnType = method.getReturnType();
String methodName = method.getName();
Class[] paramTypes = method.getParameterTypes();
// skip non-operation methods
if (methodName.startsWith("set") && returnType == Void.TYPE &&
paramTypes.length == 1) {
continue;
} else if (methodName.startsWith("get") && paramTypes.length ==
0) {
continue;
} else if (methodName.startsWith("is") && returnType ==
Boolean.TYPE && paramTypes.length == 0) {
continue;
}
String[] paramTypeNames = new String[paramTypes.length];
for (int j = 0; j < paramTypes.length; j++) {
paramTypeNames[j] = paramTypes[j].getName();
}
operationMap.put(method, paramTypeNames);
}
}
public Object invoke(final Object proxy, final Method method, final
Object[] args)
throws Throwable
{
// if the method belongs to MBeanProxy, then invoke locally
Class type = method.getDeclaringClass();
if (type == MBeanProxyFactory.MBeanProxy.class) {
return method.invoke(this, args);
}
try {
String methodName = method.getName();
// quick check if this is an operation
String[] params = (String[]) operationMap.get(method);
if (params != null) {
return invokeOperation(methodName, args, params);
}
if (methodName.startsWith("set")) {
setAttribute(new Attribute(methodName.substring(3), args[0]));
return null;
}
else if (methodName.startsWith("get")) {
return getAttribute(methodName.substring(3));
}
else if (methodName.startsWith("is")) {
return getAttribute(methodName.substring(2));
}
}
catch (Throwable t) {
Class[] declaredEx = method.getExceptionTypes();
Throwable tt = t;
while (true) {
for (int i = 0; i < declaredEx.length; i++) {
type = declaredEx[i];
if (type.isInstance(tt)) {
throw tt;
}
}
throw JMXExceptionDecoder.decode(tt);
}
}
throw new AssertionError("Method did not match during invoke");
}
public abstract ObjectName getObjectName();
public ObjectName getMBeanProxyObjectName()
{
return getObjectName();
}
public MBeanServer getMBeanProxyMBeanServer()
{
return server;
}
protected void setAttribute(Attribute attribute) throws Throwable
{
server.setAttribute(getObjectName(), attribute);
}
protected Object getAttribute(String attribute) throws Throwable
{
return server.getAttribute(this.getObjectName(), attribute);
}
protected Object invokeOperation(final String method,
final Object[] args,
final String[] params)
throws Throwable
{
return server.invoke(getObjectName(), method, args, params);
}
}
1.1
incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/jmx/JMXExceptionDecoder.java
Index: JMXExceptionDecoder.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.geronimo.common.jmx;
import javax.management.MBeanException;
import javax.management.ReflectionException;
import javax.management.RuntimeErrorException;
import javax.management.RuntimeMBeanException;
import javax.management.RuntimeOperationsException;
/**
* A simple helper to rethrow and/or decode those pesky
* JMX exceptions.
*
* @version $Revision: 1.1 $ $Date: 2003/08/30 16:32:38 $
*/
public class JMXExceptionDecoder
{
/**
* Attempt to decode the given Throwable. If it
* is a container JMX exception, then the target
* is returned. Otherwise the argument is returned.
*/
public static Throwable decode(final Throwable t)
{
if (t instanceof MBeanException) {
return ((MBeanException)t).getTargetException();
}
if (t instanceof ReflectionException) {
return ((ReflectionException)t).getTargetException();
}
if (t instanceof RuntimeOperationsException) {
return ((RuntimeOperationsException)t).getTargetException();
}
if (t instanceof RuntimeMBeanException) {
return ((RuntimeMBeanException)t).getTargetException();
}
if (t instanceof RuntimeErrorException) {
return ((RuntimeErrorException)t).getTargetError();
}
// can't decode
return t;
}
/**
* Decode and rethrow the given Throwable. If it
* is a container JMX exception, then the target
* is thrown. Otherwise the argument is thrown.
*/
public static void rethrow(final Exception e)
throws Exception
{
if (e instanceof MBeanException) {
throw ((MBeanException)e).getTargetException();
}
if (e instanceof ReflectionException) {
throw ((ReflectionException)e).getTargetException();
}
if (e instanceof RuntimeOperationsException) {
throw ((RuntimeOperationsException)e).getTargetException();
}
if (e instanceof RuntimeMBeanException) {
throw ((RuntimeMBeanException)e).getTargetException();
}
if (e instanceof RuntimeErrorException) {
throw ((RuntimeErrorException)e).getTargetError();
}
// can't decode
throw e;
}
}
1.1
incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/jmx/MBeanProxyFactory.java
Index: MBeanProxyFactory.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.geronimo.common.jmx;
import java.lang.reflect.Proxy;
import javax.management.MBeanServer;
import javax.management.ObjectName;
/**
* Creates a dynamic proxy to an MBean by ObjectName.
*
* The interface type and object existance is not enforced during
construction.
* Instead, if a method is invoked on the proxy and there is no object
registered
* with the assigned name, an InvocationTargetException is thrown, which
contains
* an InstanceNotFoundException. If an interface method that is not
implemented by
* the MBean is invoked, an InvocationTargetException is thrown, which
contains an
* NoSuchMethodException.
*
* @version $Revision: 1.1 $ $Date: 2003/08/30 16:32:38 $
*/
public final class MBeanProxyFactory
{
/** Disallow instantation. */
private MBeanProxyFactory() {}
/**
* Creates an MBean proxy using the specified interface to the objectName.
*
* @param iface The interface to implement for this proxy
* @param server The MBeanServer in which the object is registered
* @param objectName The objectName of the MBean to proxy
* @return The new MBean proxy, which implemnts the
specified interface.
*/
public static Object getProxy(final Class iface,
final MBeanServer server,
final ObjectName objectName)
{
assert iface != null;
assert iface.isInterface();
assert server != null;
ClassLoader cl = iface.getClassLoader();
LocalHandler handler = new LocalHandler(iface, server, objectName);
return Proxy.newProxyInstance(
cl, new Class[] { iface, MBeanProxyInstance.class }, handler
);
}
public static interface MBeanProxy
{
/**
* Return the ObjectName for this proxy.
*
* @return The ObjectName for this proxy.
*/
ObjectName getMBeanProxyObjectName();
/**
* Return the MBeanServer for this proxy.
*
* @return The ObjectName for this proxy.
*/
MBeanServer getMBeanProxyMBeanServer();
}
private static class LocalHandler
extends AbstractMBeanProxyHandler
{
private ObjectName objectName;
public LocalHandler(final Class iface,
final MBeanServer server,
final ObjectName objectName)
{
super(iface, server);
this.objectName = objectName;
}
public ObjectName getObjectName() {
return objectName;
}
}
}
1.1
incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/jmx/MBeanServerLocator.java
Index: MBeanServerLocator.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.geronimo.common.jmx;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
/**
* A helper class to locate a MBeanServer.
*
* @version $Revision: 1.1 $ $Date: 2003/08/30 16:32:38 $
*/
public class MBeanServerLocator
{
public static MBeanServer locate(final String agentID) {
MBeanServer server = (MBeanServer)
MBeanServerFactory.findMBeanServer(agentID).iterator().next();
return server;
}
public static MBeanServer locate() {
return locate(null);
}
}
1.1
incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/jmx/ObjectNameFactory.java
Index: ObjectNameFactory.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.geronimo.common.jmx;
import java.util.Hashtable;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
/**
* A simple factory for creating safe object names. This factory
* will <b>not</b> throw MalformedObjectNameException. Any such
* exceptions will be translated into Errors.
*
* <p>
* This should only be used where it is not possible to catch a
* MalformedObjectNameException, such as when defining a static final in an
* interface.
*
* @version $Revision: 1.1 $ $Date: 2003/08/30 16:32:38 $
*/
public class ObjectNameFactory
{
public static ObjectName create(String name) {
try {
return new ObjectName(name);
}
catch (MalformedObjectNameException e) {
throw new Error("Invalid ObjectName: " + name + "; " + e);
}
}
public static ObjectName create(String domain, String key, String value) {
try {
return new ObjectName(domain, key, value);
}
catch (MalformedObjectNameException e) {
throw new Error("Invalid ObjectName: " + domain + "," + key + ","
+ value + "; " + e);
}
}
public static ObjectName create(String domain, Hashtable table) {
try {
return new ObjectName(domain, table);
}
catch (MalformedObjectNameException e) {
throw new Error("Invalid ObjectName: " + domain + "," + table +
"; " + e);
}
}
}