diff options
author | Ted Ross <tross@apache.org> | 2009-07-17 19:05:49 +0000 |
---|---|---|
committer | Ted Ross <tross@apache.org> | 2009-07-17 19:05:49 +0000 |
commit | 6fa55d7260e4a08343a93f2360408f6ee0b41f74 (patch) | |
tree | fdd897357ce36c6e82cdd127b6dfdd07bc394c73 | |
parent | 96b013f6670b450311f2cd669bab5ed5a3f07d32 (diff) | |
download | qpid-python-6fa55d7260e4a08343a93f2360408f6ee0b41f74.tar.gz |
QPID-1938 - Make the Java Agent more friendly for inclusion by JBoss Deployer
Patch from Bryan Kearney
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@795198 13f79535-47bb-0310-9956-ffa450edef68
14 files changed, 182 insertions, 70 deletions
diff --git a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/Agent.java b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/Agent.java index 823bda7dca..40797d51f9 100644 --- a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/Agent.java +++ b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/Agent.java @@ -54,33 +54,34 @@ import org.apache.qpid.transport.codec.BBEncoder; import org.apache.qpid.transport.codec.Decoder; /** - * The main class for interacting with the QMF bus. Objects which are - * to be managed can be registered with the agent, as can classes - * to be exposed via the schema. + * The main class for interacting with the QMF bus. Objects which are to be + * managed can be registered with the agent, as can classes to be exposed via + * the schema. */ public class Agent implements MessageListener { // The following are settings to configure the Agent - private AMQConnection connection; - private boolean sessionTransacted = false; - private int acknowledgeMode = Session.AUTO_ACKNOWLEDGE; - private String label; - private UUID systemId; + protected AMQConnection connection; + protected boolean sessionTransacted = false; + protected int acknowledgeMode = Session.AUTO_ACKNOWLEDGE; + protected String label; + protected UUID systemId; // this list holds the objects until the agent is started - private List managedObjects = new ArrayList(); - private List registeredClasses = new ArrayList(); + protected List managedObjects = new ArrayList(); + protected List registeredClasses = new ArrayList(); // The following instance variables are not // able to be set by the end user. - private Session session; - private MessageProducer prod; - private MessageConsumer cons; - private Queue reply; - private BindingContext bctx = new BindingContext(); - private Map<Long, ManagedObject> objects = new Hashtable<Long, ManagedObject>(); - private long bbank; - private long abank; - private static Log log = LogFactory.getLog(Agent.class); - private volatile boolean inside = false; + protected Session session; + protected MessageProducer prod; + protected MessageConsumer cons; + protected Queue reply; + protected BindingContext bctx = new BindingContext(); + protected Map<Long, ManagedObject> objects = new Hashtable<Long, ManagedObject>(); + protected long bbank; + protected long abank; + protected static Log log = LogFactory.getLog(Agent.class); + protected volatile boolean inside = false; + protected ClassLoader classLoader = null; public Agent() { @@ -97,7 +98,6 @@ public class Agent implements MessageListener systemId.toString())); } - public void register(ManagedObject managedObject) { Class managedClass = managedObject.getObjectClass(); @@ -123,8 +123,26 @@ public class Agent implements MessageListener } /** - * Starts up the agent. Many bean containers may call this by - * default which aids in deployment + * Stops the agents connection to the bus + */ + public void stop() + { + try + { + cons.close(); + prod.close(); + connection.stop(); + connection.close(); + session.close(); + } catch (JMSException e) + { + log.error("Exception:", e); + } + } + + /** + * Starts up the agent. Many bean containers may call this by default which + * aids in deployment */ public void start() { @@ -134,7 +152,14 @@ public class Agent implements MessageListener { try { - Class cls = Class.forName(clsName.toString()); + Class cls = null; + if (String.class.isAssignableFrom(clsName.getClass())) + { + cls = getClass(clsName.toString()); + } else + { + cls = (Class) clsName; + } this.registerClass(cls); } catch (Exception e) { @@ -149,7 +174,11 @@ public class Agent implements MessageListener { session = connection.createSession(sessionTransacted, acknowledgeMode); - reply = session.createQueue(String.format("direct://amq.direct//%s-%s?exclusive='True'&autodelete='True'",label,systemId)); + reply = session + .createQueue(String + .format( + "direct://amq.direct//%s-%s?exclusive='True'&autodelete='True'", + label, systemId)); cons = session.createConsumer(reply); cons.setMessageListener(this); prod = session.createProducer(null); @@ -360,10 +389,11 @@ public class Agent implements MessageListener } catch (BindingException ex) { log - .error(String - .format( - "An exception occured invoking method %s. Stack trace sent to console.", - method.getName())); + .error( + String + .format( + "An exception occured invoking method %s. Stack trace sent to console.", + method.getName()), ex); StringWriter str = new StringWriter(); PrintWriter writer = new PrintWriter(str); ex.printStackTrace(writer); @@ -492,7 +522,7 @@ public class Agent implements MessageListener } } - private void attachRequest(String label, UUID systemId) + protected void attachRequest(String label, UUID systemId) { BBEncoder enc = init('A'); enc.writeStr8(label); @@ -502,14 +532,14 @@ public class Agent implements MessageListener send(enc); } - private void packageIndication(String pkg) + protected void packageIndication(String pkg) { BBEncoder enc = init('p'); enc.writeStr8(pkg); send(enc); } - private void classIndication(ClassBinding cb) + protected void classIndication(ClassBinding cb) { BBEncoder enc = init('q'); enc.writeUint8(cb.getKind()); @@ -519,14 +549,14 @@ public class Agent implements MessageListener send(enc); } - private void schemaResponse(long seq, ClassBinding cb) + protected void schemaResponse(long seq, ClassBinding cb) { BBEncoder enc = init('s', seq); cb.encode(enc); send(enc); } - private void content(char c, long seq, Destination dest, ManagedObject mo) + protected void content(char c, long seq, Destination dest, ManagedObject mo) { BBEncoder enc = init(c, seq); ClassBinding cb = getClassBinding(mo); @@ -555,7 +585,7 @@ public class Agent implements MessageListener } } - private void complete(long seq, Destination dest) + protected void complete(long seq, Destination dest) { BBEncoder enc = init('z', seq); enc.writeUint32(0); @@ -563,13 +593,13 @@ public class Agent implements MessageListener send(dest, enc); } - private void methodResponse(long seq, Destination dest, int status, + protected void methodResponse(long seq, Destination dest, int status, String text) { methodResponse(seq, dest, status, text, null, null); } - private void methodResponse(long seq, Destination dest, int status, + protected void methodResponse(long seq, Destination dest, int status, String text, MethodBinding method, Object[] result) { BBEncoder enc = init('m', seq); @@ -586,6 +616,24 @@ public class Agent implements MessageListener send(dest, enc); } + protected Class getClass(String className) + { + try + { + if (classLoader != null) + { + return classLoader.loadClass(className); + } else + { + return Class.forName(className); + } + } catch (ClassNotFoundException e) + { + throw new AgentException(String.format( + "No class named %s was found", className), e); + } + } + public String getLabel() { return label; diff --git a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/AgentException.java b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/AgentException.java index ed189d2bc0..410cad2d8c 100644 --- a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/AgentException.java +++ b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/AgentException.java @@ -22,10 +22,12 @@ package org.apache.qpid.agent; /** * AgentException - * + * */ public class AgentException extends RuntimeException { + private static final long serialVersionUID = 1L; + public AgentException(String msg) { super(msg); diff --git a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/ManagedEJB.java b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/ManagedEJB.java index 4c5bc594d3..60efb1e284 100644 --- a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/ManagedEJB.java +++ b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/ManagedEJB.java @@ -23,30 +23,42 @@ package org.apache.qpid.agent; import javax.naming.InitialContext; import javax.naming.NamingException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.qpid.agent.binding.BindingUtils; import org.apache.qpid.agent.binding.MethodBinding; import org.apache.qpid.agent.binding.PropertyBinding; /** - * Wrapper classe for adding EJBS which are to be - * managed by the QMF Agent. The jndi location and the - * public interface to exposed are used to generate the schema. + * Wrapper classe for adding EJBS which are to be managed by the QMF Agent. The + * jndi location and the public interface to exposed are used to generate the + * schema. */ public class ManagedEJB extends ManagedObjectBase { + private Log log = LogFactory.getLog(ManagedEJB.class); protected String className; protected String jndiLocation; + protected ClassLoader classLoader; protected Object getEJB() { + ClassLoader previousCL = Thread.currentThread().getContextClassLoader(); try { + if (classLoader != null) + { + Thread.currentThread().setContextClassLoader(classLoader); + } InitialContext ctx = new InitialContext(); return ctx.lookup(jndiLocation); } catch (NamingException e) { throw new AgentException("Error looking up EJB at " + jndiLocation, e); + } finally + { + Thread.currentThread().setContextClassLoader(previousCL); } } @@ -67,7 +79,14 @@ public class ManagedEJB extends ManagedObjectBase { try { - return Class.forName(className); + if (classLoader != null) + { + log.debug("Using the classloader"); + return classLoader.loadClass(className); + } else + { + return Class.forName(className); + } } catch (ClassNotFoundException e) { throw new AgentException(String.format( @@ -106,4 +125,14 @@ public class ManagedEJB extends ManagedObjectBase { this.jndiLocation = jndiLocation; } + + public ClassLoader getClassLoader() + { + return classLoader; + } + + public void setClassLoader(ClassLoader cloader) + { + this.classLoader = cloader; + } } diff --git a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/ManagedPOJO.java b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/ManagedPOJO.java index 061334b252..94d9b9d0a8 100644 --- a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/ManagedPOJO.java +++ b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/ManagedPOJO.java @@ -27,8 +27,7 @@ import org.apache.qpid.agent.binding.MethodBinding; import org.apache.qpid.agent.binding.PropertyBinding; /** - * Wrapper classe for adding POJOS which are to be - * managed by the QMF Agent. + * Wrapper classe for adding POJOS which are to be managed by the QMF Agent. */ public class ManagedPOJO extends ManagedObjectBase implements ManagedObject { diff --git a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFAgent.java b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFAgent.java new file mode 100644 index 0000000000..b9fcf40fc1 --- /dev/null +++ b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFAgent.java @@ -0,0 +1,34 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.agent.annotations; + +import java.lang.annotation.Target; +import java.lang.annotation.Retention; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target(ElementType.FIELD) +@Retention(RUNTIME) +@Documented +public @interface QMFAgent +{ +} diff --git a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFEvent.java b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFEvent.java index 319c471445..314f2a7e68 100644 --- a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFEvent.java +++ b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFEvent.java @@ -28,9 +28,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.Target; /** - * Tells the QMF Agent that this object will be passed as a - * QMF event. This will cause only properties to be sent across - * the wire. + * Tells the QMF Agent that this object will be passed as a QMF event. This will + * cause only properties to be sent across the wire. */ @Target(TYPE) @Retention(RUNTIME) diff --git a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFHide.java b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFHide.java index f3824ff228..db6cb0412f 100644 --- a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFHide.java +++ b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFHide.java @@ -28,8 +28,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.Target; /** - * Causes the property which is annotated to not be added to the - * QMF schema when it is built. + * Causes the property which is annotated to not be added to the QMF schema when + * it is built. */ @Target(ElementType.METHOD) @Retention(RUNTIME) diff --git a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFObject.java b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFObject.java index d737dbb852..7d1713cdbe 100644 --- a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFObject.java +++ b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFObject.java @@ -28,9 +28,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.Target; /** - * Tells the QMF Agent that this object will be a managed - * object. This will allow users to query for it, as well as - * invoke methods on it. + * Tells the QMF Agent that this object will be a managed object. This will + * allow users to query for it, as well as invoke methods on it. */ @Target(TYPE) @Retention(RUNTIME) diff --git a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFSeeAlso.java b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFSeeAlso.java index c14ea11ca4..bb9b87805d 100644 --- a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFSeeAlso.java +++ b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFSeeAlso.java @@ -28,8 +28,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.Target; /** - * Augements the schema generation process to look for known - * subclasses of a type. Modeled after the JAXB @XMLSeeAlso. + * Augements the schema generation process to look for known subclasses of a + * type. Modeled after the JAXB @XMLSeeAlso. */ @Target(TYPE) @Retention(RUNTIME) diff --git a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFType.java b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFType.java index 5d43276a55..8da1e15124 100644 --- a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFType.java +++ b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/annotations/QMFType.java @@ -28,9 +28,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.Target; /** - * Tells the QMF Agent that this object will be a type only - * This will cause only properties to be sent across - * the wire. + * Tells the QMF Agent that this object will be a type only This will cause only + * properties to be sent across the wire. */ @Target(TYPE) @Retention(RUNTIME) diff --git a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/BindingContext.java b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/BindingContext.java index aa84b9dd54..835fed0ce1 100644 --- a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/BindingContext.java +++ b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/BindingContext.java @@ -33,8 +33,8 @@ import org.apache.qpid.agent.annotations.QMFSeeAlso; import org.apache.qpid.agent.annotations.QMFType; /** - * Contains the mappings from java classes to QMF schema and back. - * There is one context per agent, and it contains all the metadata. + * Contains the mappings from java classes to QMF schema and back. There is one + * context per agent, and it contains all the metadata. */ public class BindingContext { diff --git a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/EnumBinding.java b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/EnumBinding.java index d2010dfed1..dcb619b736 100644 --- a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/EnumBinding.java +++ b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/EnumBinding.java @@ -65,17 +65,20 @@ public class EnumBinding extends ClassBinding { // FIXME This only works with POJOs Object instance = null; - String value = null ; + String value = null; try { value = dec.readStr16(); - if ((value != null ) && (!value.isEmpty())) { - instance = Enum.valueOf((Class<Enum>) this.getJavaClass(), value); - } + if ((value != null) && (!value.isEmpty())) + { + instance = Enum.valueOf((Class<Enum>) this.getJavaClass(), + value); + } } catch (Exception e) { - log.error(String.format("Could not create an enum of type %s with value %s", - this.javaClass.getName(),value )); + log.error(String.format( + "Could not create an enum of type %s with value %s", + this.javaClass.getName(), value)); throw new BindingException(e); } return instance; diff --git a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/ListBinding.java b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/ListBinding.java index 0de8a07107..a3658a812c 100644 --- a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/ListBinding.java +++ b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/ListBinding.java @@ -49,7 +49,8 @@ public class ListBinding implements TypeBinding { List list = (List) value; BBEncoder newEncoder = new BBEncoder(10); - if (list != null) { + if (list != null) + { newEncoder.writeUint32(list.size()); for (Object obj : list) { @@ -57,9 +58,9 @@ public class ListBinding implements TypeBinding newEncoder.writeUint8(type.getCode()); type.encode(newEncoder, obj); } - } - else { - newEncoder.writeUint32(0) ; + } else + { + newEncoder.writeUint32(0); } enc.writeVbin32(newEncoder.buffer().array()); } diff --git a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/PropertyBinding.java b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/PropertyBinding.java index 22ad8cd8e4..604d66c818 100644 --- a/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/PropertyBinding.java +++ b/qpid/java/management/agent/src/main/java/org/apache/qpid/agent/binding/PropertyBinding.java @@ -27,7 +27,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.qpid.transport.codec.Encoder; - /** * Metadata for mapping a java property (getter/setter) to a QMF schema */ |