package com.google.sitebricks.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.softee.management.exception.ManagementException;
import org.softee.management.helper.MBeanRegistration;
import javax.management.*;
import java.util.Random;

/**
 * @author jochen@pedesis.org (Jochen Bekmann)
 */
public class JmxUtil {
  private static final Logger log = LoggerFactory.getLogger(JmxUtil.class);
  private static final Random random = new Random();

  public static MBeanRegistration registerMBean(Object object, String packageName,
                                   String typeName, String uniqueName) {

    try {
      MBeanRegistration registration = new MBeanRegistration(object, new ObjectName(packageName + ":type=" +
          typeName + ", name=" + uniqueName + "[" + random.nextInt() + "]"));
      registration.register();
      return registration;
    } catch (ManagementException e) {
      log.error("Failed to register MBean.", e);
    } catch (MalformedObjectNameException e) {
      log.error("Failed to register MBean.", e);
    }
    return null;
  }

  public static void unregister(MBeanRegistration registration) {
    try {
      registration.unregister();
    } catch (ManagementException e) {
      log.error("Failed to unregister MBean.", e);
    }
  }

}

@@ -198,24 +198,25 @@
 gistration;%0A
+%0A
 import javax
@@ -230,16 +230,68 @@
 ment.*;%0A
+import java.text.DateFormat;%0Aimport java.util.Date;%0A
 import j
@@ -514,16 +514,117 @@
 andom();
+%0A  private static final DateFormat MBEAN_FRIENDLY = new java.text.SimpleDateFormat(%22MM-dd HH.mm.ss%22);
 %0A%0A  publ
@@ -700,16 +700,29 @@
 geName,%0A
+             
         
@@ -865,16 +865,26 @@
 (object,
+%0A         
  new Obj
@@ -916,16 +916,20 @@
 ype=%22 +%0A
+    
         
@@ -976,23 +976,40 @@
 %22 + 
-random.nextInt(
+MBEAN_FRIENDLY.format(new Date()
 ) + 
@@ -1433,37 +1433,37 @@
  e) %7B%0A      log.
-error
+debug
 (%22Failed to unre
@@ -1468,35 +1468,32 @@
 register MBean.%22
-, e
 );%0A    %7D%0A  %7D%0A%0A%7D%0A
@@ -1489,11 +1489,10 @@
   %7D%0A  %7D%0A
-%0A
 %7D%0A

change JmxUtil exception logging