Permalink
Browse files

CAMEL-5975: Fixed camel-jmx to work in OSGi

git-svn-id: https://svn.apache.org/repos/asf/camel/trunk@1433891 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent dcc6061 commit 65e0b6566fec0fa8537d74c414af600e7f3532aa @davsclaus davsclaus committed Jan 16, 2013
@@ -22,7 +22,6 @@
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.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 @@
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 JMXConsumer(JMXEndpoint aEndpoint, Processor aProcessor) {
*/
@Override
protected void doStart() throws Exception {
+ ServiceHelper.startService(mFormatter);
+
// connect to the mbean server
if (mJmxEndpoint.isPlatformServer()) {
setServerConnection(ManagementFactory.getPlatformMBeanServer());
@@ -245,6 +247,8 @@ protected void doStop() throws Exception {
}
removeNotificationListeners();
+
+ ServiceHelper.stopService(mFormatter);
}
/**
@@ -255,6 +259,7 @@ protected void removeNotificationListeners() throws Exception {
getServerConnection().removeNotificationListener(mJmxEndpoint.getJMXObjectName(), this);
if (mConnectionNotificationListener != null) {
mConnector.removeConnectionNotificationListener(mConnectionNotificationListener);
+ mConnectionNotificationListener = null;
}
}
@@ -21,8 +21,6 @@
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 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 String format(Notification aNotification) throws NotificationFormatExcept
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,18 +154,35 @@ private DatatypeFactory getDatatypeFactory() throws DatatypeConfigurationExcepti
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) {
roles.add(on.toString());
}
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 {
+ }
+
}
@@ -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

0 comments on commit 65e0b65

Please sign in to comment.