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:46:21 UTC

svn commit: r1433891 - in /camel/trunk/components/camel-jmx/src/main: java/org/apache/camel/component/jmx/ resources/org/ resources/org/apache/ resources/org/apache/camel/ resources/org/apache/camel/component/ resources/org/apache/camel/component/jmx/ ...

Author: davsclaus
Date: Wed Jan 16 10:46:21 2013
New Revision: 1433891

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

Added:
    camel/trunk/components/camel-jmx/src/main/resources/org/
    camel/trunk/components/camel-jmx/src/main/resources/org/apache/
    camel/trunk/components/camel-jmx/src/main/resources/org/apache/camel/
    camel/trunk/components/camel-jmx/src/main/resources/org/apache/camel/component/
    camel/trunk/components/camel-jmx/src/main/resources/org/apache/camel/component/jmx/
    camel/trunk/components/camel-jmx/src/main/resources/org/apache/camel/component/jmx/jaxb/
    camel/trunk/components/camel-jmx/src/main/resources/org/apache/camel/component/jmx/jaxb/jaxb.index
Modified:
    camel/trunk/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumer.java
    camel/trunk/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/NotificationXmlFormatter.java

Modified: camel/trunk/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumer.java?rev=1433891&r1=1433890&r2=1433891&view=diff
==============================================================================
--- camel/trunk/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumer.java (original)
+++ camel/trunk/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumer.java Wed Jan 16 10:46:21 2013
@@ -22,7 +22,6 @@ import java.util.Collections;
 import java.util.Map;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
-
 import javax.management.MBeanServerConnection;
 import javax.management.Notification;
 import javax.management.NotificationFilter;
@@ -38,6 +37,7 @@ 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;
@@ -75,10 +75,10 @@ public class JMXConsumer extends Default
     private NotificationXmlFormatter mFormatter;
 
 
-    public JMXConsumer(JMXEndpoint aEndpoint, Processor aProcessor) {
-        super(aEndpoint, aProcessor);
-        this.mJmxEndpoint = aEndpoint;
-        mFormatter = new NotificationXmlFormatter();
+    public JMXConsumer(JMXEndpoint endpoint, Processor processor) {
+        super(endpoint, processor);
+        this.mJmxEndpoint = endpoint;
+        this.mFormatter = new NotificationXmlFormatter();
     }
 
     /**
@@ -87,6 +87,8 @@ public class JMXConsumer extends Default
      */
     @Override
     protected void doStart() throws Exception {
+        ServiceHelper.startService(mFormatter);
+
         // connect to the mbean server
         if (mJmxEndpoint.isPlatformServer()) {
             setServerConnection(ManagementFactory.getPlatformMBeanServer());
@@ -245,6 +247,8 @@ public class JMXConsumer extends Default
         }
 
         removeNotificationListeners();
+
+        ServiceHelper.stopService(mFormatter);
     }
     
     /**
@@ -255,6 +259,7 @@ public class JMXConsumer extends Default
         getServerConnection().removeNotificationListener(mJmxEndpoint.getJMXObjectName(), this);
         if (mConnectionNotificationListener != null) {
             mConnector.removeConnectionNotificationListener(mConnectionNotificationListener);
+            mConnectionNotificationListener = null;
         }    
     }
 

Modified: camel/trunk/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/NotificationXmlFormatter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/NotificationXmlFormatter.java?rev=1433891&r1=1433890&r2=1433891&view=diff
==============================================================================
--- camel/trunk/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/NotificationXmlFormatter.java (original)
+++ camel/trunk/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/NotificationXmlFormatter.java Wed Jan 16 10:46:21 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,21 +38,26 @@ 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.
  */
-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) {
@@ -125,12 +128,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);
@@ -153,13 +154,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) {
@@ -167,4 +161,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 {
+    }
+
 }

Added: camel/trunk/components/camel-jmx/src/main/resources/org/apache/camel/component/jmx/jaxb/jaxb.index
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jmx/src/main/resources/org/apache/camel/component/jmx/jaxb/jaxb.index?rev=1433891&view=auto
==============================================================================
--- camel/trunk/components/camel-jmx/src/main/resources/org/apache/camel/component/jmx/jaxb/jaxb.index (added)
+++ camel/trunk/components/camel-jmx/src/main/resources/org/apache/camel/component/jmx/jaxb/jaxb.index Wed Jan 16 10:46:21 2013
@@ -0,0 +1,24 @@
+## ------------------------------------------------------------------------
+## 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.
+## ------------------------------------------------------------------------
+AttributeChangeNotification
+JMXConnectionNotification
+MBeanServerNotification
+MonitorNotification
+NotificationEventType
+ObjectNamesType
+RelationNotification
+TimerNotification