/*
 * Copyright 2009 Kjetil Valstadsve
 *
 * Licensed 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 vanadis.jmx;

import vanadis.core.lang.Not;
import vanadis.core.system.VM;

import javax.management.*;

public final class Jmx {

    public static void registerJmx(ObjectName objectName, DynamicMBean dynamicMBean) {
        registerJmx(objectName, dynamicMBean, null);
    }

    public static void registerJmx(String name, DynamicMBean mbean) {
        verify(name, mbean);
        registerJmx(objectName(name), mbean, null);
    }

    public static <T> void registerJmx(String name, T mbean, Class<T> beanClass) {
        verify(name, mbean);
        registerJmx(objectName(name), mbean, beanClass);
    }

    public static <T> void registerJmx(ObjectName objectName, T mbean, Class<T> beanClass) {
        verify(objectName, mbean);
        try {
            Object mBean = mbean instanceof DynamicMBean ? mbean
                    : new StandardMBean(mbean, Not.nil(beanClass, "bean class"));
            VM.JMX.registerMBean(mBean, objectName);
        } catch (InstanceAlreadyExistsException e) {
            throw new JmxException
                    ("Failed to reregister " + describeArgs(objectName, mbean, beanClass), e);
        } catch (RuntimeOperationsException e) {
            throw new JmxException
                    ("Runtime operations exception " + describeArgs(objectName, mbean, beanClass), e);
        } catch (Exception e) {
            throw new JmxException
                    ("Failed to register " + describeArgs(objectName, mbean, beanClass), e);
        }
    }

    public static void unregisterJmx(String name) {
        unregisterJmx(objectName(name));
    }

    public static void unregisterJmx(ObjectName objectName) {
        try {
            VM.JMX.unregisterMBean(Not.nil(objectName, "object name"));
        } catch (InstanceNotFoundException e) {
            throw new JmxException("Registration not found: " + objectName, e);
        } catch (Exception e) {
            throw new JmxException("Failed to unregister " + objectName, e);
        }
    }

    private static <T> void verify(Object name, T mbean) {
        Not.nil(name, "name");
        Not.nil(mbean, "mBean");
    }

    private static <T> String describeArgs(Object name, T mbean, Class<T> beanClass) {
        return mbean + ":" + beanClass + " @ " + name;
    }

    private static ObjectName objectName(String name) {
        Not.nil(name, "name");
        try {
            return new ObjectName(name);
        } catch (Exception e) {
            throw new IllegalArgumentException("Failed to parse object name " + name, e);
        }
    }

    private Jmx() {}
}

@@ -743,36 +743,42 @@
   public static 
-void
+ObjectName
  registerJmx(Obj
@@ -826,32 +826,39 @@
 Bean) %7B%0A        
+return 
 registerJmx(obje
@@ -911,20 +911,26 @@
  static 
-void
+ObjectName
  registe
@@ -998,32 +998,39 @@
 mbean);%0A        
+return 
 registerJmx(obje
@@ -1078,36 +1078,42 @@
 blic static %3CT%3E 
-void
+ObjectName
  registerJmx(Str
@@ -1190,16 +1190,23 @@
         
+return 
 register
@@ -1275,20 +1275,26 @@
 tic %3CT%3E 
-void
+ObjectName
  registe
@@ -1597,24 +1597,55 @@
 bjectName);%0A
+            return objectName;%0A
         %7D ca
@@ -1728,36 +1728,32 @@
                 
-    
 (%22Failed to rere
@@ -1911,20 +1911,16 @@
         
-    
 (%22Runtim
@@ -2069,36 +2069,32 @@
                 
-  
-  
 (%22Failed to regi
@@ -2177,36 +2177,42 @@
   public static 
-void
+ObjectName
  unregisterJmx(S
@@ -2233,16 +2233,23 @@
         
+return 
 unregist
@@ -2298,20 +2298,26 @@
  static 
-void
+ObjectName
  unregis
@@ -2430,24 +2430,55 @@
 ct name%22));%0A
+            return objectName;%0A
         %7D ca

Return object names.