camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1327402 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/management/DefaultManagementAgent.java test/java/org/apache/camel/management/DefaultManagementAgentMockTest.java
Date Wed, 18 Apr 2012 07:40:45 GMT
Author: davsclaus
Date: Wed Apr 18 07:40:44 2012
New Revision: 1327402

URL: http://svn.apache.org/viewvc?rev=1327402&view=rev
Log:
CAMEL-5187: Improved Camel JMX working with WebSphere. Thanks to Christian Ohr for the patch.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/management/DefaultManagementAgentMockTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementAgent.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementAgent.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementAgent.java?rev=1327402&r1=1327401&r2=1327402&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementAgent.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementAgent.java
Wed Apr 18 07:40:44 2012
@@ -22,9 +22,9 @@ import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.rmi.RemoteException;
 import java.rmi.registry.LocateRegistry;
-import java.util.HashSet;
+import java.util.HashMap;
 import java.util.List;
-import java.util.Set;
+import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import javax.management.JMException;
 import javax.management.MBeanServer;
@@ -60,7 +60,8 @@ public class DefaultManagementAgent exte
     private CamelContext camelContext;
     private ExecutorService executorService;
     private MBeanServer server;
-    private final Set<ObjectName> mbeansRegistered = new HashSet<ObjectName>();
+    // need a name -> actual name mapping as some servers changes the names (suc as WebSphere)
+    private final Map<ObjectName, ObjectName> mbeansRegistered = new HashMap<ObjectName,
ObjectName>();
     private JMXConnectorServer cs;
 
     private Integer registryPort;
@@ -240,15 +241,17 @@ public class DefaultManagementAgent exte
     }
 
     public void unregister(ObjectName name) throws JMException {
-        if (server.isRegistered(name)) {
-            server.unregisterMBean(name);
+        if (isRegistered(name)) {
+            server.unregisterMBean(mbeansRegistered.get(name));
             LOG.debug("Unregistered MBean with ObjectName: {}", name);
         }
         mbeansRegistered.remove(name);
     }
 
     public boolean isRegistered(ObjectName name) {
-        return server.isRegistered(name);
+        return (mbeansRegistered.containsKey(name) 
+                && server.isRegistered(mbeansRegistered.get(name))) 
+                || server.isRegistered(name);
     }
 
     protected void doStart() throws Exception {
@@ -280,11 +283,10 @@ public class DefaultManagementAgent exte
         }
 
         // Using the array to hold the busMBeans to avoid the CurrentModificationException
-        ObjectName[] mBeans = mbeansRegistered.toArray(new ObjectName[mbeansRegistered.size()]);
+        ObjectName[] mBeans = mbeansRegistered.keySet().toArray(new ObjectName[mbeansRegistered.size()]);
         int caught = 0;
         for (ObjectName name : mBeans) {
             try {
-                mbeansRegistered.remove(name);
                 unregister(name);
             } catch (Exception e) {
                 LOG.info("Exception unregistering MBean with name " + name, e);
@@ -302,7 +304,7 @@ public class DefaultManagementAgent exte
         throws JMException {
 
         // have we already registered the bean, there can be shared instances in the camel
routes
-        boolean exists = server.isRegistered(name);
+        boolean exists = isRegistered(name);
         if (exists) {
             if (forceRegistration) {
                 LOG.info("ForceRegistration enabled, unregistering existing MBean with ObjectName:
{}", name);
@@ -324,7 +326,7 @@ public class DefaultManagementAgent exte
         if (instance != null) {
             ObjectName registeredName = instance.getObjectName();
             LOG.debug("Registered MBean with ObjectName: {}", registeredName);
-            mbeansRegistered.add(registeredName);
+            mbeansRegistered.put(name, registeredName);
         }
     }
 

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/management/DefaultManagementAgentMockTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/DefaultManagementAgentMockTest.java?rev=1327402&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/management/DefaultManagementAgentMockTest.java
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/DefaultManagementAgentMockTest.java
Wed Apr 18 07:40:44 2012
@@ -0,0 +1,79 @@
+/**
+ * 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.camel.management;
+
+import javax.management.JMException;
+import javax.management.MBeanServer;
+import javax.management.ObjectInstance;
+import javax.management.ObjectName;
+
+import org.apache.camel.spi.ManagementAgent;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.createStrictMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.reset;
+import static org.easymock.EasyMock.verify;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests proper behavior of DefaultManagementAgent when
+ * {@link MBeanServer#registerMBean(Object, ObjectName)} returns an
+ * {@link ObjectInstance} with a different ObjectName
+ */
+public class DefaultManagementAgentMockTest {
+
+    @Test
+    public void testObjectNameModification() throws JMException {
+        MBeanServer mbeanServer = createStrictMock(MBeanServer.class);
+        ObjectInstance instance = createStrictMock(ObjectInstance.class);
+
+        ManagementAgent agent = new DefaultManagementAgent();
+        agent.setMBeanServer(mbeanServer);
+
+        Object object = "object";
+        ObjectName sourceObjectName = new ObjectName("domain", "key", "value");
+        ObjectName registeredObjectName = new ObjectName("domain", "key", "otherValue");
+
+        // Register MBean and return different ObjectName
+        expect(mbeanServer.isRegistered(sourceObjectName)).andReturn(false);
+        expect(mbeanServer.registerMBean(object, sourceObjectName)).andReturn(instance);
+        expect(instance.getObjectName()).andReturn(registeredObjectName);
+        expect(mbeanServer.isRegistered(registeredObjectName)).andReturn(true);
+        replay(mbeanServer, instance);
+
+        agent.register(object, sourceObjectName);
+
+        assertTrue(agent.isRegistered(sourceObjectName));
+        verify(mbeanServer, instance);
+        reset(mbeanServer, instance);
+
+        // ... and unregister it again
+        expect(mbeanServer.isRegistered(registeredObjectName)).andReturn(true);
+        mbeanServer.unregisterMBean(registeredObjectName);
+        expect(mbeanServer.isRegistered(sourceObjectName)).andReturn(false);
+        replay(mbeanServer);
+
+        agent.unregister(sourceObjectName);
+
+        assertFalse(agent.isRegistered(sourceObjectName));
+        verify(mbeanServer);
+    }
+
+}



Mime
View raw message