Author: chetanm
Date: Wed Nov 18 10:42:43 2015
New Revision: 1714964

URL: http://svn.apache.org/viewvc?rev=1714964&view=rev
Log:
OAK-3477 - Make JMX Bean names predictable so they can be used in 
configurations.

-- type and name would only be quoted if required
-- dropped the id counter

Added:
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/spi/whiteboard/WhiteboardUtilsTest.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/whiteboard/WhiteboardUtils.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java?rev=1714964&r1=1714963&r2=1714964&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java 
(original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java 
Wed Nov 18 10:42:43 2015
@@ -243,7 +243,7 @@ public class Oak {
     private Whiteboard whiteboard = new DefaultWhiteboard() {
         @Override
         public <T> Registration register(
-                Class<T> type, T service, Map<?, ?> properties) {
+                final Class<T> type, T service, Map<?, ?> properties) {
             final Registration registration =
                     super.register(type, service, properties);
 
@@ -279,7 +279,8 @@ public class Oak {
                     }
                     mbeanServer.registerMBean(service, objectName);
                 } catch (JMException e) {
-                    // ignore
+                    LOG.warn("Unexpected exception while registering MBean of 
type [{}] " +
+                            "against name [{}]", type, objectName, e);
                 }
             }
 
@@ -295,7 +296,8 @@ public class Oak {
                         try {
                             mbeanServer.unregisterMBean(on);
                         } catch (JMException e) {
-                            // ignore
+                            LOG.warn("Unexpected exception while unregistering 
MBean of type {} " +
+                                    "against name {} ", type, on, e);
                         }
                     }
                     try {

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/whiteboard/WhiteboardUtils.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/whiteboard/WhiteboardUtils.java?rev=1714964&r1=1714963&r2=1714964&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/whiteboard/WhiteboardUtils.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/whiteboard/WhiteboardUtils.java
 Wed Nov 18 10:42:43 2015
@@ -23,7 +23,6 @@ import java.util.Collections;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
-import java.util.concurrent.atomic.AtomicLong;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
@@ -40,8 +39,6 @@ import com.google.common.collect.Iterabl
 
 public class WhiteboardUtils {
 
-    private static final AtomicLong COUNTER = new AtomicLong();
-
     public static Registration scheduleWithFixedDelay(
             Whiteboard whiteboard, Runnable runnable, long delayInSeconds) {
         return scheduleWithFixedDelay(whiteboard, runnable, delayInSeconds, 
false);
@@ -72,9 +69,8 @@ public class WhiteboardUtils {
         try {
 
             Hashtable<String, String> table = new Hashtable<String, 
String>(attrs);
-            table.put("type", ObjectName.quote(type));
-            table.put("name", ObjectName.quote(name));
-            table.put("id", String.valueOf(COUNTER.incrementAndGet()));
+            table.put("type", quoteIfRequired(type));
+            table.put("name", quoteIfRequired(name));
             return whiteboard.register(iface, bean, ImmutableMap.of(
                     "jmx.objectname",
                     new ObjectName("org.apache.jackrabbit.oak", table)));
@@ -168,5 +164,12 @@ public class WhiteboardUtils {
 
     }
 
+    static String quoteIfRequired(String text) {
+        String quoted = ObjectName.quote(text);
+        if (quoted.substring(1, quoted.length() - 1).equals(text)) {
+            return text;
+        }
+        return quoted;
+    }
 
 }

Added: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/spi/whiteboard/WhiteboardUtilsTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/spi/whiteboard/WhiteboardUtilsTest.java?rev=1714964&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/spi/whiteboard/WhiteboardUtilsTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/spi/whiteboard/WhiteboardUtilsTest.java
 Wed Nov 18 10:42:43 2015
@@ -0,0 +1,94 @@
+/*
+ * 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.jackrabbit.oak.spi.whiteboard;
+
+import java.lang.management.ManagementFactory;
+import java.util.List;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import com.google.common.collect.Lists;
+import org.apache.jackrabbit.oak.Oak;
+import org.junit.After;
+import org.junit.Test;
+
+import static junit.framework.TestCase.assertEquals;
+import static junit.framework.TestCase.assertTrue;
+import static org.junit.Assert.assertNotNull;
+
+public class WhiteboardUtilsTest {
+    private List<Registration> regs = Lists.newArrayList();
+
+    @After
+    public void unregisterRegs(){
+        new CompositeRegistration(regs).unregister();
+    }
+
+    @Test
+    public void jmxBeanRegistration() throws Exception{
+        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
+        Oak oak = new Oak().with(server);
+        Whiteboard wb = oak.getWhiteboard();
+        Hello hello = new Hello();
+        regs.add(WhiteboardUtils.registerMBean(wb, HelloMBean.class, hello, 
"test", "hello"));
+        assertNotNull(server.getObjectInstance(new 
ObjectName("org.apache.jackrabbit.oak:type=test,name=hello")));
+    }
+
+    @Test
+    public void jmxBeanRegistrationDuplicate() throws Exception{
+        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
+        Oak oak = new Oak().with(server);
+        Whiteboard wb = oak.getWhiteboard();
+        Hello hello = new Hello();
+        regs.add(WhiteboardUtils.registerMBean(wb, HelloMBean.class, hello, 
"test", "hello"));
+
+        //Second one would trigger a warning log but no affect on caller
+        regs.add(WhiteboardUtils.registerMBean(wb, HelloMBean.class, hello, 
"test", "hello"));
+        assertNotNull(server.getObjectInstance(new 
ObjectName("org.apache.jackrabbit.oak:type=test,name=hello")));
+    }
+
+    @Test
+    public void quotation() throws Exception{
+        assertEquals("text", WhiteboardUtils.quoteIfRequired("text"));
+        assertEquals("", WhiteboardUtils.quoteIfRequired(""));
+        
assertTrue(WhiteboardUtils.quoteIfRequired("text*with?chars").startsWith("\""));
+    }
+
+    private interface HelloMBean {
+        boolean isRunning();
+        int getCount();
+    }
+
+    private static class Hello implements HelloMBean {
+        int count;
+        boolean running;
+
+        @Override
+        public boolean isRunning() {
+            return running;
+        }
+
+        @Override
+        public int getCount() {
+            return count;
+        }
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/spi/whiteboard/WhiteboardUtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to