FELIX-1848 - Include instance name in the JMX ObjectName for AdminService and FeaturesService MBeans


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@834657 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/admin/management/pom.xml b/karaf/admin/management/pom.xml
index d227ede..ca83d14 100644
--- a/karaf/admin/management/pom.xml
+++ b/karaf/admin/management/pom.xml
@@ -54,6 +54,13 @@
             <artifactId>org.apache.felix.karaf.admin.core</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.easymock</groupId>
+            <artifactId>easymockclassextension</artifactId>
+            <version>${easymock.version}</version>
+            <scope>test</scope>
+        </dependency>
+
     </dependencies>
 
     <build>
diff --git a/karaf/admin/management/src/main/java/org/apache/felix/karaf/admin/management/internal/MBeanRegistrer.java b/karaf/admin/management/src/main/java/org/apache/felix/karaf/admin/management/internal/MBeanRegistrer.java
index 0479b77..ea08e05 100644
--- a/karaf/admin/management/src/main/java/org/apache/felix/karaf/admin/management/internal/MBeanRegistrer.java
+++ b/karaf/admin/management/src/main/java/org/apache/felix/karaf/admin/management/internal/MBeanRegistrer.java
@@ -52,7 +52,8 @@
     protected void registerMBeans() throws JMException {
         if (mbeanServer != null && mbeans != null) {
             for (Map.Entry<Object, String> entry : mbeans.entrySet()) {
-                mbeanServer.registerMBean(entry.getKey(), new ObjectName(entry.getValue()));
+                String value = parseProperty(entry.getValue());
+                mbeanServer.registerMBean(entry.getKey(), new ObjectName(value));
             }
         }
     }
@@ -60,8 +61,20 @@
     protected void unregisterMBeans() throws JMException {
         if (mbeanServer != null && mbeans != null) {
             for (Map.Entry<Object, String> entry : mbeans.entrySet()) {
-                mbeanServer.unregisterMBean(new ObjectName(entry.getValue()));
+                String value = parseProperty(entry.getValue());
+                mbeanServer.unregisterMBean(new ObjectName(value));
             }
         }
     }
-}
\ No newline at end of file
+
+    protected String parseProperty(String raw) {
+        if (raw.indexOf("${") > -1 && raw.indexOf("}", raw.indexOf("${")) > -1) {
+            String var = raw.substring(raw.indexOf("${") + 2, raw.indexOf("}"));
+            String val = System.getProperty(var);
+            if (val != null) {
+                raw = raw.replace("${" + var + "}", val);
+            }
+        }
+        return raw;
+    }
+}
diff --git a/karaf/admin/management/src/main/resources/OSGI-INF/blueprint/admin-management.xml b/karaf/admin/management/src/main/resources/OSGI-INF/blueprint/admin-management.xml
index 3e4a253..8a630aa 100644
--- a/karaf/admin/management/src/main/resources/OSGI-INF/blueprint/admin-management.xml
+++ b/karaf/admin/management/src/main/resources/OSGI-INF/blueprint/admin-management.xml
@@ -39,7 +39,7 @@
                             <argument value="org.apache.felix.karaf.admin.management.AdminServiceMBean"/>
                         </bean>
                     </key>
-                    <value>org.apache.felix.karaf:service=admin</value>
+                    <value>org.apache.felix.karaf:type=admin,name=${karaf.name}</value>
                 </entry>
             </map>
         </property>
diff --git a/karaf/admin/management/src/test/java/org/apache/felix/karaf/admin/management/internal/MBeanRegistererTest.java b/karaf/admin/management/src/test/java/org/apache/felix/karaf/admin/management/internal/MBeanRegistererTest.java
new file mode 100644
index 0000000..15e9cdb
--- /dev/null
+++ b/karaf/admin/management/src/test/java/org/apache/felix/karaf/admin/management/internal/MBeanRegistererTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.felix.karaf.admin.management.internal;
+
+import java.util.Map;
+import java.util.HashMap;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.felix.karaf.admin.management.AdminServiceMBean;
+
+import static org.easymock.classextension.EasyMock.*;
+import org.easymock.classextension.IMocksControl;
+
+import junit.framework.TestCase;
+import org.junit.Assert;
+
+public class MBeanRegistererTest extends TestCase {
+    public void testRegistration() throws Exception {
+        IMocksControl control = createNiceControl();
+        MBeanServer mbeanServer = control.createMock(MBeanServer.class);
+        String rawName = "org.apache.felix.karaf:type=admin,name=${karaf.name}";
+        AdminServiceMBean mbean = control.createMock(AdminServiceMBean.class);
+        ObjectName name = new ObjectName("org.apache.felix.karaf:type=admin,name=foobar");
+        expect(mbeanServer.registerMBean(isA(AdminServiceMBean.class), eq(name))).andReturn(null);
+        mbeanServer.unregisterMBean(eq(name));
+        expectLastCall();
+        control.replay();
+
+        String old = System.getProperty("karaf.name");
+        System.setProperty("karaf.name", "foobar");
+        MBeanRegistrer registerer = new MBeanRegistrer();
+        Map<Object, String> mbeans = new HashMap<Object, String>();
+        mbeans.put(mbean, rawName);
+        registerer.setMbeans(mbeans);
+        registerer.registerMBeanServer(mbeanServer);
+        registerer.unregisterMBeanServer(mbeanServer);
+
+        restoreProperties(old);
+        control.verify();
+    }
+
+    private void restoreProperties(String old) {
+        if (old != null) {
+           System.setProperty("karaf.name", old);
+        } else { 
+           System.getProperties().remove("karaf.name");
+        }
+    }
+}
diff --git a/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/internal/MBeanRegistrer.java b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/internal/MBeanRegistrer.java
index 004ad00..1dbac81 100644
--- a/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/internal/MBeanRegistrer.java
+++ b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/internal/MBeanRegistrer.java
@@ -57,7 +57,8 @@
     protected void registerMBeans() throws JMException {
         if (mbeanServer != null && mbeans != null) {
             for (Map.Entry<Object, String> entry : mbeans.entrySet()) {
-                mbeanServer.registerMBean(entry.getKey(), new ObjectName(entry.getValue()));
+                String value = parseProperty(entry.getValue());
+                mbeanServer.registerMBean(entry.getKey(), new ObjectName(value));
             }
         }
     }
@@ -65,8 +66,20 @@
     protected void unregisterMBeans() throws JMException {
         if (mbeanServer != null && mbeans != null) {
             for (Map.Entry<Object, String> entry : mbeans.entrySet()) {
-                mbeanServer.unregisterMBean(new ObjectName(entry.getValue()));
+                String value = parseProperty(entry.getValue());
+                mbeanServer.unregisterMBean(new ObjectName(value));
             }
         }
     }
+
+    protected String parseProperty(String raw) {
+        if (raw.indexOf("${") > -1 && raw.indexOf("}", raw.indexOf("${")) > -1) {
+            String var = raw.substring(raw.indexOf("${") + 2, raw.indexOf("}"));
+            String val = System.getProperty(var);
+            if (val != null) {
+                raw = raw.replace("${" + var + "}", val);
+            }
+        }
+        return raw;
+    }
 }
diff --git a/karaf/features/management/src/main/resources/OSGI-INF/blueprint/features-management.xml b/karaf/features/management/src/main/resources/OSGI-INF/blueprint/features-management.xml
index 98f1041..42da1d4 100644
--- a/karaf/features/management/src/main/resources/OSGI-INF/blueprint/features-management.xml
+++ b/karaf/features/management/src/main/resources/OSGI-INF/blueprint/features-management.xml
@@ -41,7 +41,7 @@
                             <argument value="org.apache.felix.karaf.features.management.FeaturesServiceMBean"/>
                         </bean>
                     </key>
-                    <value>org.apache.felix.karaf:service=features</value>
+                    <value>org.apache.felix.karaf:type=features,name=${karaf.name}</value>
                 </entry>
             </map>
         </property>