Author: andy
Date: Thu Jun 27 20:02:55 2013
New Revision: 1497546

URL: http://svn.apache.org/r1497546
Log:
Unregistering JMX beans.  Refactoring.

Modified:
    jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/mgt/ARQMgt.java

Modified: 
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/mgt/ARQMgt.java
URL: 
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/mgt/ARQMgt.java?rev=1497546&r1=1497545&r2=1497546&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/mgt/ARQMgt.java 
(original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/mgt/ARQMgt.java 
Thu Jun 27 20:02:55 2013
@@ -16,7 +16,7 @@
  * limitations under the License.
  */
 
-package com.hp.hpl.jena.sparql.mgt;
+package com.hp.hpl.jena.sparql.mgt ;
 
 import java.lang.management.ManagementFactory ;
 import java.util.HashMap ;
@@ -38,33 +38,32 @@ import com.hp.hpl.jena.query.ARQ ;
 import com.hp.hpl.jena.sparql.ARQException ;
 import com.hp.hpl.jena.sparql.engine.QueryEngineBase ;
 
-public class ARQMgt
-{
+public class ARQMgt {
     // In some environments, JMX does not exist.
-    static private Logger log = LoggerFactory.getLogger(ARQMgt.class) ;
-    private static boolean initialized = false ;
-    private static boolean noJMX = false ;
-    private static Map<ObjectName, Object> mgtObjects = new 
HashMap<ObjectName, Object>() ;
-    private static MBeanServer mbs = null ;  
-    
-    public static synchronized void init()
-    {
-        if ( initialized )
+    static private Logger                  log         = 
LoggerFactory.getLogger(ARQMgt.class) ;
+    private static boolean                 initialized = false ;
+    private static boolean                 noJMX       = false ;
+    private static Map<ObjectName, Object> mgtObjects  = new 
HashMap<ObjectName, Object>() ;
+    private static MBeanServer             mbs         = null ;
+
+    public static synchronized void init() {
+        if (initialized)
             return ;
         initialized = true ;
 
         try {
 
-            mbs = ManagementFactory.getPlatformMBeanServer();
+            mbs = ManagementFactory.getPlatformMBeanServer() ;
 
             String NS = ARQ.PATH ;
 
             ContextMBean cxtBean = new ContextMBean(ARQ.getContext()) ;
             QueryEngineInfo qeInfo = QueryEngineBase.queryEngineInfo ;
 
-            // Done in ARQ initialization -- 
register(NS+".system:type=SystemInfo", ARQ.systemInfo) ;
-            register(NS+".system:type=Context", cxtBean) ;
-            register(NS+".system:type=Engine", qeInfo) ;
+            // Done in ARQ initialization --
+            // register(NS+".system:type=SystemInfo", ARQ.systemInfo) ;
+            register(NS + ".system:type=Context", cxtBean) ;
+            register(NS + ".system:type=Engine", qeInfo) ;
 
         } catch (Throwable ex) {
             Log.warn(ARQMgt.class, "Failed to initialize JMX", ex) ;
@@ -72,47 +71,67 @@ public class ARQMgt
             mbs = null ;
         }
     }
-    
-    public static void register(String name, Object bean)
-    {
+
+    public static void register(String name, Object bean) {
         init() ;
-        
-        if ( noJMX )
+        if (noJMX)
             return ;
-        
-        ObjectName objName = null ;
-        try
-        { objName = new ObjectName(name) ; }
-        catch (MalformedObjectNameException ex)
-        {  throw new ARQException("Failed to create name '"+name+"': 
"+ex.getMessage(), ex) ; }
-        
+        ObjectName objName = objectName(name) ;
+        register(objName, bean) ;
+    }
+
+    public static void unregister(String name) {
+        init() ;
+        if (noJMX)
+            return ;
+        ObjectName objName = objectName(name) ;
+        unregister(objName) ;
+    }
+
+    private static void register(ObjectName objName, Object bean) {
         try {
             // Unregister to avoid classloader problems.
             // A previous load of this class will have registered something
             // with the object name. Remove it - copes with reloading.
             // (Does not cope with multiple loads running in parallel.)
-            if ( mbs.isRegistered(objName) )
-            {
-                try { mbs.unregisterMBean(objName); }
-                catch (InstanceNotFoundException ex) {}
+            if (mbs.isRegistered(objName)) {
+                try {
+                    mbs.unregisterMBean(objName) ;
+                } catch (InstanceNotFoundException ex) {}
             }
-            log.debug("Register MBean: "+objName) ;
-            mbs.registerMBean(bean, objName);
+            log.debug("Register MBean: " + objName) ;
+            mbs.registerMBean(bean, objName) ;
             // remember ...
             mgtObjects.put(objName, bean) ;
+        } catch (NotCompliantMBeanException ex) {
+            log.warn("Failed to register (NotCompliantMBeanException)'" + 
objName.getCanonicalName() + "': " + ex.getMessage()) ;
+            throw new ARQException("Failed to register '" + 
objName.getCanonicalName() + "': " + ex.getMessage(), ex) ;
+        } catch (InstanceAlreadyExistsException ex) {
+            log.warn("Failed to register (InstanceAlreadyExistsException)'" + 
objName.getCanonicalName() + "': " + ex.getMessage()) ;
+            throw new ARQException("Failed to register '" + 
objName.getCanonicalName() + "': " + ex.getMessage(), ex) ;
+        } catch (MBeanRegistrationException ex) {
+            log.warn("Failed to register (MBeanRegistrationException)'" + 
objName.getCanonicalName() + "': " + ex.getMessage()) ;
+            throw new ARQException("Failed to register '" + 
objName.getCanonicalName() + "': " + ex.getMessage(), ex) ;
+        }
+    }
+
+    private static void unregister(ObjectName objName) {
+        log.debug("Unregister MBean: " + objName) ;
+        if ( ! mgtObjects.containsKey(objName) )
+            log.warn("MBean not already registered: " + objName) ;
+        try {
+            mbs.unregisterMBean(objName) ;
+        } catch (InstanceNotFoundException ex) {
+            ex.printStackTrace() ;
+        } catch (MBeanRegistrationException ex) {
+            ex.printStackTrace() ;
+        }
+    }
 
-        } catch (NotCompliantMBeanException ex)
-        {
-            log.warn("Failed to register 
(NotCompliantMBeanException)'"+objName.getCanonicalName()+"': 
"+ex.getMessage()) ;
-            throw new ARQException("Failed to register 
'"+objName.getCanonicalName()+"': "+ex.getMessage(), ex) ;
-        } catch (InstanceAlreadyExistsException ex)
-        {
-            log.warn("Failed to register 
(InstanceAlreadyExistsException)'"+objName.getCanonicalName()+"': 
"+ex.getMessage()) ;
-            throw new ARQException("Failed to register 
'"+objName.getCanonicalName()+"': "+ex.getMessage(), ex) ;
-        } catch (MBeanRegistrationException ex)
-        {
-            log.warn("Failed to register 
(MBeanRegistrationException)'"+objName.getCanonicalName()+"': 
"+ex.getMessage()) ;
-            throw new ARQException("Failed to register 
'"+objName.getCanonicalName()+"': "+ex.getMessage(), ex) ;
+    private static ObjectName objectName(String name) {
+        try { return new ObjectName(name) ; }
+        catch (MalformedObjectNameException ex) {
+            throw new ARQException("Failed to create name '" + name + "': " + 
ex.getMessage(), ex) ;
         }
     }
 }


Reply via email to