You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/01/16 11:54:20 UTC

svn commit: r1433894 - in /camel/branches/camel-2.9.x: ./ components/camel-jmx/src/main/java/org/apache/camel/component/jmx/ components/camel-jmx/src/main/resources/org/

Author: davsclaus
Date: Wed Jan 16 10:54:19 2013
New Revision: 1433894

URL: http://svn.apache.org/viewvc?rev=1433894&view=rev
Log:
CAMEL-5975: Fixed camel-jmx to work in OSGi

Added:
    camel/branches/camel-2.9.x/components/camel-jmx/src/main/resources/org/
      - copied from r1433892, camel/branches/camel-2.10.x/components/camel-jmx/src/main/resources/org/
Modified:
    camel/branches/camel-2.9.x/   (props changed)
    camel/branches/camel-2.9.x/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumer.java
    camel/branches/camel-2.9.x/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/NotificationXmlFormatter.java

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
  Merged /camel/trunk:r1433891
  Merged /camel/branches/camel-2.10.x:r1433892

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-2.9.x/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumer.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumer.java?rev=1433894&r1=1433893&r2=1433894&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumer.java (original)
+++ camel/branches/camel-2.9.x/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumer.java Wed Jan 16 10:54:19 2013
@@ -19,7 +19,6 @@ package org.apache.camel.component.jmx;
 import java.lang.management.ManagementFactory;
 import java.util.Collections;
 import java.util.Map;
-
 import javax.management.MBeanServerConnection;
 import javax.management.Notification;
 import javax.management.NotificationFilter;
@@ -34,6 +33,10 @@ import org.apache.camel.ExchangePattern;
 import org.apache.camel.Message;
 import org.apache.camel.Processor;
 import org.apache.camel.impl.DefaultConsumer;
+import org.apache.camel.util.ServiceHelper;
+import org.apache.camel.util.URISupport;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Consumer will add itself as a NotificationListener on the object
@@ -51,9 +54,9 @@ public class JMXConsumer extends Default
      */
     private NotificationXmlFormatter mFormatter;
 
-    public JMXConsumer(JMXEndpoint aEndpoint, Processor aProcessor) {
-        super(aEndpoint, aProcessor);
-        mFormatter = new NotificationXmlFormatter();
+    public JMXConsumer(JMXEndpoint endpoint, Processor processor) {
+        super(endpoint, processor);
+        this.mFormatter = new NotificationXmlFormatter();
     }
 
     /**
@@ -66,6 +69,8 @@ public class JMXConsumer extends Default
 
         JMXEndpoint ep = (JMXEndpoint) getEndpoint();
 
+        ServiceHelper.startService(mFormatter);
+
         // connect to the mbean server
         if (ep.isPlatformServer()) {
             setServerConnection(ManagementFactory.getPlatformMBeanServer());
@@ -100,8 +105,10 @@ public class JMXConsumer extends Default
     protected void doStop() throws Exception {
         super.doStop();
         removeNotificationListener();
-    }
 
+        ServiceHelper.stopService(mFormatter);
+    }
+    
     /**
      * Removes the consumer as a listener from the bean. 
      */

Modified: camel/branches/camel-2.9.x/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/NotificationXmlFormatter.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/NotificationXmlFormatter.java?rev=1433894&r1=1433893&r2=1433894&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/NotificationXmlFormatter.java (original)
+++ camel/branches/camel-2.9.x/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/NotificationXmlFormatter.java Wed Jan 16 10:54:19 2013
@@ -21,8 +21,6 @@ import java.util.ArrayList;
 import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.List;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
 import javax.management.AttributeChangeNotification;
 import javax.management.MBeanServerNotification;
 import javax.management.Notification;
@@ -40,23 +38,28 @@ import javax.xml.datatype.DatatypeFactor
 import org.apache.camel.component.jmx.jaxb.NotificationEventType;
 import org.apache.camel.component.jmx.jaxb.ObjectFactory;
 import org.apache.camel.component.jmx.jaxb.ObjectNamesType;
+import org.apache.camel.support.ServiceSupport;
+import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Converts the Notification into an XML stream.
  *
  * @author markford
  */
-public class NotificationXmlFormatter {
+public class NotificationXmlFormatter extends ServiceSupport {
 
+    private static final transient Logger LOG = LoggerFactory.getLogger(NotificationXmlFormatter.class);
+
+    private final ObjectFactory mObjectFactory = new ObjectFactory();
+    private JAXBContext jaxbContext;
     private DatatypeFactory mDatatypeFactory;
-    private Marshaller mMarshaller;
-    private Lock mMarshallerLock = new ReentrantLock(false);
-    private ObjectFactory mObjectFactory = new ObjectFactory();
 
     public String format(Notification aNotification) throws NotificationFormatException {
+        ObjectHelper.notNull(jaxbContext, "jaxbContext");
 
-        NotificationEventType jaxb = null;
-
+        NotificationEventType jaxb;
         boolean wrap = false;
 
         if (aNotification instanceof AttributeChangeNotification) {
@@ -127,12 +130,10 @@ public class NotificationXmlFormatter {
 
             StringWriter sw = new StringWriter();
 
-            try {
-                mMarshallerLock.lock();
-                getMarshaller(mObjectFactory.getClass().getPackage().getName()).marshal(bean, sw);
-            } finally {
-                mMarshallerLock.unlock();
-            }
+            // must create a new marshaller as its not thread safe
+            Marshaller marshaller = jaxbContext.createMarshaller();
+            marshaller.marshal(bean, sw);
+
             return sw.toString();
         } catch (JAXBException e) {
             throw new NotificationFormatException(e);
@@ -155,13 +156,6 @@ public class NotificationXmlFormatter {
         return mDatatypeFactory;
     }
 
-    private Marshaller getMarshaller(String aPackageName) throws JAXBException {
-        if (mMarshaller == null) {
-            mMarshaller = JAXBContext.newInstance(aPackageName).createMarshaller();
-        }
-        return mMarshaller;
-    }
-
     private List<String> toStringList(List<ObjectName> objectNames) {
         List<String> roles = new ArrayList<String>(objectNames.size());
         for (ObjectName on : objectNames) {
@@ -169,4 +163,28 @@ public class NotificationXmlFormatter {
         }
         return roles;
     }
+
+    /**
+      * Strategy to create JAXB context
+      */
+    protected JAXBContext createContext(String contextPath) throws JAXBException {
+        ClassLoader cl = NotificationXmlFormatter.class.getClassLoader();
+        try {
+            LOG.info("Creating JAXBContext with contextPath: " + contextPath + " and classloader: " + cl);
+            return JAXBContext.newInstance(contextPath, cl);
+        } catch (Exception e) {
+            LOG.info("Creating JAXBContext with contextPath: " + contextPath);
+            return JAXBContext.newInstance(contextPath);
+        }
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        jaxbContext = createContext("org.apache.camel.component.jmx.jaxb");
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+    }
+
 }