servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject svn commit: r1374742 - in /servicemix/components/branches/components-2011.02.x: bindings/servicemix-http/ bindings/servicemix-http/src/main/java/org/apache/servicemix/http/ bindings/servicemix-http/src/test/java/org/apache/servicemix/http/ bindings/ser...
Date Sun, 19 Aug 2012 12:27:02 GMT
Author: gertv
Date: Sun Aug 19 12:27:01 2012
New Revision: 1374742

URL: http://svn.apache.org/viewvc?rev=1374742&view=rev
Log:
SMXCOMP-940: InstanceAlreadyExistsException in HTTP/JMS component bootstrap on WebSphere

Added:
    servicemix/components/branches/components-2011.02.x/bindings/servicemix-http/src/test/java/org/apache/servicemix/http/HttpBootstrapTest.java
    servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/management/
    servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/management/MBeanServerHelper.java
    servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/test/java/org/apache/servicemix/common/management/
    servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/test/java/org/apache/servicemix/common/management/MBeanServerHelperTest.java
Modified:
    servicemix/components/branches/components-2011.02.x/bindings/servicemix-http/pom.xml
    servicemix/components/branches/components-2011.02.x/bindings/servicemix-http/src/main/java/org/apache/servicemix/http/HttpBootstrap.java
    servicemix/components/branches/components-2011.02.x/bindings/servicemix-jms/pom.xml
    servicemix/components/branches/components-2011.02.x/bindings/servicemix-jms/src/main/java/org/apache/servicemix/jms/JmsBootstrap.java
    servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java

Modified: servicemix/components/branches/components-2011.02.x/bindings/servicemix-http/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/components/branches/components-2011.02.x/bindings/servicemix-http/pom.xml?rev=1374742&r1=1374741&r2=1374742&view=diff
==============================================================================
--- servicemix/components/branches/components-2011.02.x/bindings/servicemix-http/pom.xml (original)
+++ servicemix/components/branches/components-2011.02.x/bindings/servicemix-http/pom.xml Sun
Aug 19 12:27:01 2012
@@ -220,7 +220,11 @@
             <artifactId>jencks</artifactId>
             <scope>test</scope>
         </dependency>
-
+        <dependency>
+            <groupId>org.easymock</groupId>
+            <artifactId>easymock</artifactId>
+            <scope>test</scope>
+        </dependency>
 
         <!-- documentation -->
         <dependency>

Modified: servicemix/components/branches/components-2011.02.x/bindings/servicemix-http/src/main/java/org/apache/servicemix/http/HttpBootstrap.java
URL: http://svn.apache.org/viewvc/servicemix/components/branches/components-2011.02.x/bindings/servicemix-http/src/main/java/org/apache/servicemix/http/HttpBootstrap.java?rev=1374742&r1=1374741&r2=1374742&view=diff
==============================================================================
--- servicemix/components/branches/components-2011.02.x/bindings/servicemix-http/src/main/java/org/apache/servicemix/http/HttpBootstrap.java
(original)
+++ servicemix/components/branches/components-2011.02.x/bindings/servicemix-http/src/main/java/org/apache/servicemix/http/HttpBootstrap.java
Sun Aug 19 12:27:01 2012
@@ -16,6 +16,8 @@
  */
 package org.apache.servicemix.http;
 
+import org.apache.servicemix.common.management.MBeanServerHelper;
+
 import javax.jbi.JBIException;
 import javax.jbi.component.Bootstrap;
 import javax.jbi.component.InstallationContext;
@@ -63,15 +65,9 @@ public class HttpBootstrap implements Bo
         configuration.setRootDir(this.context.getInstallRoot());
         configuration.setComponentName(this.context.getComponentName());
         configuration.load();
-        mbeanName = this.context.getContext().getMBeanNames().createCustomComponentMBeanName("bootstrap");
-        MBeanServer server = this.context.getContext().getMBeanServer();
-        if (server == null) {
-            throw new JBIException("null mBeanServer");
-        }
-        if (server.isRegistered(this.mbeanName)) {
-            server.unregisterMBean(this.mbeanName);
-        }
-        server.registerMBean(configuration, this.mbeanName);
+
+        ObjectName name = this.context.getContext().getMBeanNames().createCustomComponentMBeanName("bootstrap");
+        this.mbeanName = MBeanServerHelper.register(getMBeanServer(), name, configuration);
     }
 
     /*
@@ -90,15 +86,7 @@ public class HttpBootstrap implements Bo
     }
 
     protected void doCleanUp() throws Exception {
-        if (this.mbeanName != null) {
-            MBeanServer server = this.context.getContext().getMBeanServer();
-            if (server == null) {
-                throw new JBIException("null mBeanServer");
-            }
-            if (server.isRegistered(this.mbeanName)) {
-                server.unregisterMBean(this.mbeanName);
-            }
-        }
+        MBeanServerHelper.unregister(getMBeanServer(), mbeanName);
     }
 
     /*
@@ -117,4 +105,10 @@ public class HttpBootstrap implements Bo
     public void onUninstall() throws JBIException {
     }
 
+    /*
+    * Get the MBeanServer for the installation context
+    */
+    private MBeanServer getMBeanServer() {
+        return this.context.getContext().getMBeanServer();
+    }
 }

Added: servicemix/components/branches/components-2011.02.x/bindings/servicemix-http/src/test/java/org/apache/servicemix/http/HttpBootstrapTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/branches/components-2011.02.x/bindings/servicemix-http/src/test/java/org/apache/servicemix/http/HttpBootstrapTest.java?rev=1374742&view=auto
==============================================================================
--- servicemix/components/branches/components-2011.02.x/bindings/servicemix-http/src/test/java/org/apache/servicemix/http/HttpBootstrapTest.java
(added)
+++ servicemix/components/branches/components-2011.02.x/bindings/servicemix-http/src/test/java/org/apache/servicemix/http/HttpBootstrapTest.java
Sun Aug 19 12:27:01 2012
@@ -0,0 +1,109 @@
+/*
+ * 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.servicemix.http;
+
+import junit.framework.TestCase;
+import org.w3c.dom.DocumentFragment;
+
+import javax.jbi.JBIException;
+import javax.jbi.component.ComponentContext;
+import javax.jbi.component.InstallationContext;
+import javax.jbi.management.MBeanNames;
+import javax.management.*;
+import java.util.List;
+
+import static org.easymock.EasyMock.*;
+
+/**
+ * Test to ensure HttpBootstrap MBean registration works correct on containers that alter
the MBean ObjectName
+ * (e.g. IBM WebSphere)
+ */
+public class HttpBootstrapTest extends TestCase {
+
+    private HttpBootstrap bootstrap;
+    private ObjectName originalName;
+    private ObjectName registeredName;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        bootstrap = new HttpBootstrap();
+        originalName = new ObjectName("test:Type=bootstrap");
+        registeredName = new ObjectName("test:Type=bootstrap,process=process1,node=node1,cell=cell1");
+    }
+
+    public void testInitMBeanNameChanging() throws JBIException {
+        bootstrap.init(new MockInstallationContext());
+        assertEquals("HttpBootstrap should return the actual object name (including container-specific
extra elements)",
+                     registeredName, bootstrap.getExtensionMBeanName());
+    }
+
+    private final class MockInstallationContext implements InstallationContext {
+
+        public String getComponentClassName() {
+            return null;  // just a mock object - no need to implement this
+        }
+
+        public List getClassPathElements() {
+            return null;  // just a mock object - no need to implement this
+        }
+
+        public String getComponentName() {
+            return null;  // just a mock object - no need to implement this
+        }
+
+        public ComponentContext getContext() {
+            ComponentContext context = createMock(ComponentContext.class);
+
+            // Set up the mock MBeanNames to return the original ObjectName
+            MBeanNames names = createMock(MBeanNames.class);
+            expect(context.getMBeanNames()).andReturn(names);
+            expect(names.createCustomComponentMBeanName("bootstrap")).andReturn(originalName);
+
+            // Set up the mock MBeanServer to return the altered ObjectName after registration
+            MBeanServer server = createMock(MBeanServer.class);
+            expect(context.getMBeanServer()).andReturn(server);
+            try {
+                expect(server.isRegistered(originalName)).andReturn(false);
+                expect(server.registerMBean(isA(HttpConfiguration.class), eq(originalName))).andReturn(new
ObjectInstance(registeredName, null));
+            } catch (Exception e) {
+                fail("Exception occured while setting up the test: " + e.getMessage());
+            }
+
+            replay(context, names, server);
+
+            return context;
+        }
+
+        public String getInstallRoot() {
+            return null;  // just a mock object - no need to implement this
+        }
+
+        public DocumentFragment getInstallationDescriptorExtension() {
+            return null;  // just a mock object - no need to implement this
+        }
+
+        public boolean isInstall() {
+            return false;  // just a mock object - no need to implement this
+        }
+
+        public void setClassPathElements(List list) {
+            // just a mock object - no need to implement this
+        }
+    }
+}

Modified: servicemix/components/branches/components-2011.02.x/bindings/servicemix-jms/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/components/branches/components-2011.02.x/bindings/servicemix-jms/pom.xml?rev=1374742&r1=1374741&r2=1374742&view=diff
==============================================================================
--- servicemix/components/branches/components-2011.02.x/bindings/servicemix-jms/pom.xml (original)
+++ servicemix/components/branches/components-2011.02.x/bindings/servicemix-jms/pom.xml Sun
Aug 19 12:27:01 2012
@@ -62,6 +62,10 @@
     <dependencies>
         <dependency>
             <groupId>org.apache.servicemix</groupId>
+            <artifactId>servicemix-common</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.servicemix</groupId>
             <artifactId>servicemix-shared</artifactId>
         </dependency>
 

Modified: servicemix/components/branches/components-2011.02.x/bindings/servicemix-jms/src/main/java/org/apache/servicemix/jms/JmsBootstrap.java
URL: http://svn.apache.org/viewvc/servicemix/components/branches/components-2011.02.x/bindings/servicemix-jms/src/main/java/org/apache/servicemix/jms/JmsBootstrap.java?rev=1374742&r1=1374741&r2=1374742&view=diff
==============================================================================
--- servicemix/components/branches/components-2011.02.x/bindings/servicemix-jms/src/main/java/org/apache/servicemix/jms/JmsBootstrap.java
(original)
+++ servicemix/components/branches/components-2011.02.x/bindings/servicemix-jms/src/main/java/org/apache/servicemix/jms/JmsBootstrap.java
Sun Aug 19 12:27:01 2012
@@ -16,6 +16,8 @@
  */
 package org.apache.servicemix.jms;
 
+import org.apache.servicemix.common.management.MBeanServerHelper;
+
 import javax.jbi.JBIException;
 import javax.jbi.component.Bootstrap;
 import javax.jbi.component.InstallationContext;
@@ -66,19 +68,12 @@ public class JmsBootstrap implements Boo
         configuration.setComponentName(this.context.getComponentName());
         configuration.load();
         Object mbean = getExtensionMBean();
+
         if (mbean != null) {
-            this.mbeanName = createExtensionMBeanName();
-            MBeanServer server = this.context.getContext().getMBeanServer();
-            if (server == null) {
-                throw new JBIException("null mBeanServer");
-            }
-            if (server.isRegistered(this.mbeanName)) {
-                server.unregisterMBean(this.mbeanName);
-            }
-            server.registerMBean(mbean, this.mbeanName);
+            this.mbeanName = MBeanServerHelper.register(getMBeanServer(), createExtensionMBeanName(),
mbean);
         }
     }
-    
+
     /* (non-Javadoc)
      * @see javax.jbi.component.Bootstrap#cleanUp()
      */
@@ -93,15 +88,7 @@ public class JmsBootstrap implements Boo
     }
 
     protected void doCleanUp() throws Exception {
-        if (this.mbeanName != null) {
-            MBeanServer server = this.context.getContext().getMBeanServer();
-            if (server == null) {
-                throw new JBIException("null mBeanServer");
-            }
-            if (server.isRegistered(this.mbeanName)) {
-                server.unregisterMBean(this.mbeanName);
-            }
-        }
+        MBeanServerHelper.unregister(getMBeanServer(), mbeanName);
     }
 
     /* (non-Javadoc)
@@ -135,5 +122,11 @@ public class JmsBootstrap implements Boo
 
     protected void doOnUninstall() throws Exception {
     }
-    
+
+    /*
+     * Get the MBeanServer for the installation context
+     */
+    private MBeanServer getMBeanServer() {
+        return this.context.getContext().getMBeanServer();
+    }
 }

Modified: servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java
URL: http://svn.apache.org/viewvc/servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java?rev=1374742&r1=1374741&r2=1374742&view=diff
==============================================================================
--- servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java
(original)
+++ servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java
Sun Aug 19 12:27:01 2012
@@ -33,6 +33,7 @@ import javax.jbi.messaging.MessageExchan
 import javax.jbi.messaging.MessagingException;
 import javax.jbi.servicedesc.ServiceEndpoint;
 import javax.management.MBeanServer;
+import javax.management.ObjectInstance;
 import javax.management.ObjectName;
 import javax.transaction.Status;
 import javax.transaction.SystemException;
@@ -40,6 +41,7 @@ import javax.transaction.Transaction;
 import javax.transaction.TransactionManager;
 import javax.xml.namespace.QName;
 
+import org.apache.servicemix.common.management.MBeanServerHelper;
 import org.apache.servicemix.executors.Executor;
 import org.apache.servicemix.executors.ExecutorFactory;
 import org.apache.servicemix.executors.impl.ExecutorFactoryImpl;
@@ -214,20 +216,13 @@ public class AsyncBaseLifeCycle implemen
 
     protected void doInit() throws Exception {
         // Register extension mbean
+        MBeanServer server = this.context.getMBeanServer();
         Object mbean = getExtensionMBean();
-        if (mbean != null) {
-            MBeanServer server = this.context.getMBeanServer();
-            if (server == null) {
-                // TODO: log a warning ?
-                // throw new JBIException("null mBeanServer");
-            } else {
-                this.mbeanName = createExtensionMBeanName();
-                if (server.isRegistered(this.mbeanName)) {
-                    server.unregisterMBean(this.mbeanName);
-                }
-                server.registerMBean(mbean, this.mbeanName);
-            }
+
+        if (server != null && mbean != null) {
+            MBeanServerHelper.register(server, createExtensionMBeanName(), mbean);
         }
+
         // Obtain or create the work manager
         // When using the WorkManager from ServiceMix,
         // some class loader problems can appear when

Added: servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/management/MBeanServerHelper.java
URL: http://svn.apache.org/viewvc/servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/management/MBeanServerHelper.java?rev=1374742&view=auto
==============================================================================
--- servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/management/MBeanServerHelper.java
(added)
+++ servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/management/MBeanServerHelper.java
Sun Aug 19 12:27:01 2012
@@ -0,0 +1,111 @@
+/*
+ * 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.servicemix.common.management;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.jbi.JBIException;
+import javax.management.*;
+
+/**
+ * Helper methods to register and unregister objects with a JMX MBeanServer
+ */
+public class MBeanServerHelper {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(MBeanServerHelper.class);
+
+    /**
+     * Registers an object with the MBean server using the specified object name.  If another
object is already registered,
+     * this method will first unregister it.  The method also returns the final object name
after registration, because
+     * some MBean servers will alter the name during the registration process
+     *
+     * @param server the MBean server
+     * @param name the object name
+     * @param object the object to be registered
+     * @return the final object name after registration
+     * @throws JBIException if the MBean server is null or if a problem occurs during MBean
registration
+     */
+    public static ObjectName register(MBeanServer server, ObjectName name, Object object)
throws JBIException {
+        if (server == null) {
+            throw new JBIException("MBeanServer is null when registering MBean " + name);
+        }
+
+        try {
+            // unregister a previously existing MBean first
+            doUnregister(server, name);
+
+            LOGGER.debug("Registering MBean {}", name);
+            ObjectInstance instance = doRegister(server, name, object);
+
+            ObjectName result = instance.getObjectName();
+            LOGGER.debug("Successfully registered MBean {}", result);
+
+            return result;
+        } catch (JMException e) {
+            throw new JBIException("Exception occured while registering JMX MBean " + name,
e);
+        }
+    }
+
+    private static ObjectInstance doRegister(MBeanServer server, ObjectName name, Object
object) throws JMException, JBIException {
+        try {
+            return server.registerMBean(object, name);
+        } catch (InstanceAlreadyExistsException e) {
+            ObjectName existing = new ObjectName(e.getMessage());
+
+            if (existing.equals(name)) {
+                throw e;
+            } else {
+                // if the server has another instance registered under a different name,
+                // let's try unregistering that alternative name and retry registration afterwards
+                LOGGER.debug("Existing MBean {} matches {} - unregistering it before continuing",
existing, name);
+                doUnregister(server, existing);
+
+                return doRegister(server, name, object);
+            }
+        }
+    }
+
+    /**
+     * Unregister the object with the specified name from the MBean server
+     *
+     * @param server the mbean server
+     * @param name the object name
+     *
+     * @throws JBIException if the MBean server is null or if there is a problem unregistering
the MBean
+     */
+    public static void unregister(MBeanServer server, ObjectName name) throws JBIException
{
+        if (name != null) {
+            if (server == null) {
+                throw new JBIException("MBeanServer is null when registering MBean " + name);
+            }
+
+            try {
+                doUnregister(server, name);
+            } catch (JMException e) {
+                throw new JBIException("Unable to unregister object with name " + name, e);
+            }
+        }
+    }
+
+    private static void doUnregister(MBeanServer server, ObjectName name) throws JMException
{
+        if (server.isRegistered(name)) {
+            LOGGER.debug("Unregistering MBean {}", name);
+            server.unregisterMBean(name);
+        }
+    }
+}

Added: servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/test/java/org/apache/servicemix/common/management/MBeanServerHelperTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/test/java/org/apache/servicemix/common/management/MBeanServerHelperTest.java?rev=1374742&view=auto
==============================================================================
--- servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/test/java/org/apache/servicemix/common/management/MBeanServerHelperTest.java
(added)
+++ servicemix/components/branches/components-2011.02.x/shared-libraries/servicemix-common/src/test/java/org/apache/servicemix/common/management/MBeanServerHelperTest.java
Sun Aug 19 12:27:01 2012
@@ -0,0 +1,135 @@
+/*
+ * 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.servicemix.common.management;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.jbi.JBIException;
+import javax.management.*;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+/**
+ * Test cases for {@link MBeanServerHelper}
+ */
+public class MBeanServerHelperTest {
+
+    private MBeanServer server = MBeanServerFactory.createMBeanServer();
+    private ObjectName defaultObjectName;
+    private ObjectName alteredObjectName;
+
+    @Before
+    public void setUp() throws Exception {
+        defaultObjectName = new ObjectName("test:Type=bean");
+        alteredObjectName = new ObjectName("test:Type=bean,node=node1,process=process1,cell=cell1");
+    }
+
+    @Test(expected = JBIException.class)
+    public void testRegisterNullServerThrowsJBIException() throws NotCompliantMBeanException,
MalformedObjectNameException, JBIException {
+        SampleMBean mbean = new SampleMBeanImpl();
+        Object object = new StandardMBean(mbean, SampleMBean.class);
+
+        MBeanServerHelper.register(null, defaultObjectName, object);
+    }
+
+    @Test(expected = JBIException.class)
+    public void testUnregisterNullServerThrowsJBIException() throws Exception {
+        MBeanServerHelper.unregister(null, defaultObjectName);
+    }
+
+    public void testUnregisterNullNameWithoutException() throws Exception {
+        MBeanServerHelper.unregister(server, null);
+    }
+
+    @Test
+    public void testRegisterWithDefaultName() throws Exception {
+        SampleMBean mbean = new SampleMBeanImpl();
+        Object object = new StandardMBean(mbean, SampleMBean.class);
+
+        assertEquals(defaultObjectName, MBeanServerHelper.register(server, defaultObjectName,
object));
+
+        // let's try a subsequent registration as well - this should not be throwing an exception
+        assertEquals(defaultObjectName, MBeanServerHelper.register(server, defaultObjectName,
object));
+
+        MBeanServerHelper.unregister(server, defaultObjectName);
+        assertFalse(server.isRegistered(defaultObjectName));
+    }
+
+    @Test
+    public void testRegisterWithContainerDefinedName() throws Exception {
+        SampleMBean mbean = new SampleMBeanImpl();
+        Object object = new RenamingStandardMBean(mbean, SampleMBean.class);
+
+        assertEquals(alteredObjectName, MBeanServerHelper.register(server, defaultObjectName,
object));
+
+        // let's try a subsequent registration as well - this should not be throwing an exception
+        assertEquals(alteredObjectName, MBeanServerHelper.register(server, defaultObjectName,
object));
+
+        MBeanServerHelper.unregister(server, alteredObjectName);
+        assertFalse(server.isRegistered(alteredObjectName));
+    }
+
+    /*
+    * MBean interface definition used for testing
+    */
+    public static interface SampleMBean {
+
+        void doSomething();
+
+    }
+
+    /*
+     * MBean implementation
+     */
+    public static final class SampleMBeanImpl implements SampleMBean {
+
+        public void doSomething() {
+            // graciously do nothing here
+        }
+    }
+
+    /*
+    * {@link StandardMBean} implementation that will provide an alternate MBean name upon
registration
+    * (similar to what e.g. WebSphere does when it appends cell/node/process information)
+    */
+    public final class RenamingStandardMBean extends StandardMBean implements MBeanRegistration
{
+
+
+        public <T> RenamingStandardMBean(T object, Class<T> type) throws NotCompliantMBeanException
{
+            super(object, type);
+        }
+
+
+        public ObjectName preRegister(MBeanServer mBeanServer, ObjectName objectName) throws
Exception {
+            return alteredObjectName;
+        }
+
+        public void postRegister(Boolean aBoolean) {
+            // graciously do nothing here
+        }
+
+        public void preDeregister() throws Exception {
+            // graciously do nothing here
+        }
+
+        public void postDeregister() {
+            // graciously do nothing here
+        }
+    }
+}



Mime
View raw message