cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1081445 - in /cxf/trunk/rt/management/src: main/java/org/apache/cxf/management/jmx/InstrumentationManagerImpl.java test/java/org/apache/cxf/management/jmx/BusRegistrationTest.java test/resources/no-connector-spring.xml
Date Mon, 14 Mar 2011 16:24:44 GMT
Author: dkulp
Date: Mon Mar 14 16:24:44 2011
New Revision: 1081445

URL: http://svn.apache.org/viewvc?rev=1081445&view=rev
Log:
[CXF-3396] Make sure the standalone MBeanServer lookups are done
properly to prevent multiple MBeanServers from being created when they
shouldn't be.

Patch from Aki Yoshida applied.

Added:
    cxf/trunk/rt/management/src/test/java/org/apache/cxf/management/jmx/BusRegistrationTest.java
  (with props)
    cxf/trunk/rt/management/src/test/resources/no-connector-spring.xml   (with props)
Modified:
    cxf/trunk/rt/management/src/main/java/org/apache/cxf/management/jmx/InstrumentationManagerImpl.java

Modified: cxf/trunk/rt/management/src/main/java/org/apache/cxf/management/jmx/InstrumentationManagerImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/management/src/main/java/org/apache/cxf/management/jmx/InstrumentationManagerImpl.java?rev=1081445&r1=1081444&r2=1081445&view=diff
==============================================================================
--- cxf/trunk/rt/management/src/main/java/org/apache/cxf/management/jmx/InstrumentationManagerImpl.java
(original)
+++ cxf/trunk/rt/management/src/main/java/org/apache/cxf/management/jmx/InstrumentationManagerImpl.java
Mon Mar 14 16:24:44 2011
@@ -21,8 +21,10 @@ package org.apache.cxf.management.jmx;
 
 import java.io.IOException;
 import java.lang.management.ManagementFactory;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -32,6 +34,7 @@ import javax.annotation.Resource;
 import javax.management.InstanceAlreadyExistsException;
 import javax.management.JMException;
 import javax.management.MBeanServer;
+import javax.management.MBeanServerDelegate;
 import javax.management.MBeanServerFactory;
 
 import javax.management.NotCompliantMBeanException;
@@ -60,6 +63,8 @@ public class InstrumentationManagerImpl 
     implements InstrumentationManager, BusLifeCycleListener {
     private static final Logger LOG = LogUtils.getL7dLogger(InstrumentationManagerImpl.class);
 
+    private static Map<String, String>mbeanServerIDMap = new HashMap<String, String>();
+
     private Bus bus;
     private MBServerConnectorFactory mcf;    
     private MBeanServer mbs;
@@ -119,10 +124,20 @@ public class InstrumentationManagerImpl 
                 if (usePlatformMBeanServer) {
                     mbs = ManagementFactory.getPlatformMBeanServer();
                 } else {
-                    List<MBeanServer> servers = CastUtils
-                        .cast(MBeanServerFactory.findMBeanServer(mbeanServerName));
-                    if (servers.size() <= 1) {
+                    String mbeanServerID = mbeanServerIDMap.get(mbeanServerName);
+                    List<MBeanServer> servers = null;
+                    if (mbeanServerID != null) {
+                        servers = CastUtils.cast(MBeanServerFactory.findMBeanServer(mbeanServerID));
+                    }
+                    if (servers == null || servers.size() == 0) {
                         mbs = MBeanServerFactory.createMBeanServer(mbeanServerName);
+                        try {
+                            mbeanServerID = (String) mbs.getAttribute(MBeanServerDelegate.DELEGATE_NAME,
+                                                                     "MBeanServerId");
+                            mbeanServerIDMap.put(mbeanServerName, mbeanServerID);
+                        } catch (JMException e) {
+                            // ignore
+                        }
                     } else {
                         mbs = (MBeanServer)servers.get(0);
                     }

Added: cxf/trunk/rt/management/src/test/java/org/apache/cxf/management/jmx/BusRegistrationTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/management/src/test/java/org/apache/cxf/management/jmx/BusRegistrationTest.java?rev=1081445&view=auto
==============================================================================
--- cxf/trunk/rt/management/src/test/java/org/apache/cxf/management/jmx/BusRegistrationTest.java
(added)
+++ cxf/trunk/rt/management/src/test/java/org/apache/cxf/management/jmx/BusRegistrationTest.java
Mon Mar 14 16:24:44 2011
@@ -0,0 +1,126 @@
+/**
+ * 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.cxf.management.jmx;
+
+import java.util.Set;
+
+import javax.management.JMException;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.bus.spring.SpringBusFactory;
+import org.apache.cxf.management.InstrumentationManager;
+import org.apache.cxf.management.ManagementConstants;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * 
+ */
+public class BusRegistrationTest extends Assert {
+    private Bus serverBus;
+    private Bus clientBus;
+    private InstrumentationManager serverIM;
+    private InstrumentationManager clientIM;
+    private boolean ready;
+    private boolean running;
+    
+    @Before
+    public void setUp() throws Exception {
+    }
+    
+    @After
+    public void tearDown() throws Exception {
+        if (clientBus != null) {
+            clientBus.shutdown(true);
+        }
+        if (serverBus != null) {
+            serverBus.shutdown(true);
+        }
+    }
+
+    @Test
+    public void testRegisterMultipleBuses() throws Exception {
+        final SpringBusFactory factory = new SpringBusFactory();
+
+        serverBus =  factory.createBus("managed-spring.xml");
+        serverIM = serverBus.getExtension(InstrumentationManager.class);
+        assertTrue("Instrumentation Manager should not be null", serverIM != null);
+        Thread t = new Thread(new Runnable() {
+            @Override
+            public void run() {
+                clientBus = factory.createBus("no-connector-spring.xml");
+                clientIM = clientBus.getExtension(InstrumentationManager.class);
+                assertTrue("Instrumentation Manager should not be null", clientIM != null);
+                ready = true;
+                running = true;
+                while (running) {
+                    try {
+                        Thread.sleep(1000);
+                    } catch (InterruptedException e) {
+                        // ignore and leave
+                        running = false;
+                    }
+                }
+            }
+        });
+        
+        t.start();
+        while (!ready) {
+            Thread.sleep(1000);
+        }
+        
+        try {
+            MBeanServer mbs = serverIM.getMBeanServer();
+            assertNotNull("MBeanServer should be available.", mbs);
+            MBeanServer mbs2 = clientIM.getMBeanServer();
+            assertNotNull("MBeanServer should be available.", mbs2);
+            
+            // check if these servers refer to the same server
+            assertEquals("There should be one MBeanServer", mbs, mbs2);
+            
+            // check both server and client bus can be found from this server
+            Set s;
+            ObjectName serverName = getObjectName(serverBus);
+            s = mbs.queryNames(serverName, null);
+            assertTrue("sever-side bus should be found", s.size() == 1);
+
+            ObjectName clientName = getObjectName(clientBus);
+            s = mbs.queryNames(clientName, null);
+            assertTrue("client-side bus should be found", s.size() == 1);
+        } finally {
+            running = false;            
+        }
+    }
+    
+    private static ObjectName getObjectName(Bus bus) throws JMException {
+        String busId = bus.getId();
+        StringBuilder buffer = new StringBuilder(ManagementConstants.DEFAULT_DOMAIN_NAME
+ ":");
+        buffer.append(ManagementConstants.BUS_ID_PROP + "=" +  busId + ",");
+        buffer.append(ManagementConstants.TYPE_PROP + "=Bus");
+        
+        return new ObjectName(buffer.toString());
+    }
+
+}

Propchange: cxf/trunk/rt/management/src/test/java/org/apache/cxf/management/jmx/BusRegistrationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/management/src/test/java/org/apache/cxf/management/jmx/BusRegistrationTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/management/src/test/resources/no-connector-spring.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/management/src/test/resources/no-connector-spring.xml?rev=1081445&view=auto
==============================================================================
--- cxf/trunk/rt/management/src/test/resources/no-connector-spring.xml (added)
+++ cxf/trunk/rt/management/src/test/resources/no-connector-spring.xml Mon Mar 14 16:24:44
2011
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:im="http://cxf.apache.org/management"
+       xsi:schemaLocation="
+http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
+  
+    <bean id="org.apache.cxf.management.InstrumentationManager" class="org.apache.cxf.management.jmx.InstrumentationManagerImpl">
+        <property name="bus" ref="cxf" />
+        <property name="enabled" value="true"/>
+        <property name="createMBServerConnectorFactory" value="false" />
+                        
+    </bean> 
+ 
+</beans>
\ No newline at end of file

Propchange: cxf/trunk/rt/management/src/test/resources/no-connector-spring.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/management/src/test/resources/no-connector-spring.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/trunk/rt/management/src/test/resources/no-connector-spring.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml



Mime
View raw message