1 25 26 package org.objectweb.easybeans.jmx; 27 28 import java.util.Hashtable ; 29 import java.util.Map ; 30 31 import javax.management.MalformedObjectNameException ; 32 import javax.management.ObjectName ; 33 34 import org.objectweb.easybeans.api.jmx.EZBManagementIdentifier; 35 import org.objectweb.easybeans.log.JLog; 36 import org.objectweb.easybeans.log.JLogFactory; 37 import org.objectweb.easybeans.util.loader.ClassUtils; 38 39 44 public final class MBeansHelper { 45 48 private static JLog logger = JLogFactory.getLog(MBeansHelper.class); 49 50 53 private static MBeansHelper instance = null; 54 55 59 private Map <Class , EZBManagementIdentifier> identifiers = null; 60 61 64 private boolean activate; 65 66 69 private MBeansHelper() { 70 identifiers = new Hashtable <Class , EZBManagementIdentifier>(); 71 } 72 73 76 public static MBeansHelper getInstance() { 77 if (instance == null) { 78 instance = new MBeansHelper(); 79 } 80 return instance; 81 } 82 83 88 public void activate(final boolean activate) { 89 this.activate = activate; 90 } 91 92 98 public <T> void registerMBean(final T instance) throws MBeansException { 99 100 if (activate) { 101 String on = getObjectName(instance); 102 103 try { 105 CommonsModelerHelper.registerModelerMBean(instance, on); 106 } catch (CommonsModelerException e) { 107 throw new MBeansException("Cannot register MBean", e); 108 } 109 } 110 } 111 112 118 public <T> void unregisterMBean(final T instance) throws MBeansException { 119 120 if (activate) { 121 String on = getObjectName(instance); 122 123 try { 125 CommonsModelerHelper.unregisterModelerMBean(new ObjectName (on)); 126 } catch (MalformedObjectNameException e) { 127 logger.warn("Cannot unregister MBean '" + on + "' : " + e.getMessage()); 128 } catch (NullPointerException e) { 129 logger.warn("Cannot unregister MBean '" + on + "' : " + e.getMessage()); 130 } 131 } 132 } 133 134 140 public <T> String getObjectName(final T instance) throws MBeansException { 141 142 EZBManagementIdentifier<T> identifier = getIdentifier(instance); 143 144 if (identifier != null) { 146 StringBuilder sb = new StringBuilder (); 147 148 sb.append(identifier.getDomain()); 149 sb.append(":"); 150 sb.append(identifier.getTypeProperty()); 151 String additionnal = identifier.getAdditionnalProperties(instance); 152 if (additionnal != null && (!"".equals(additionnal))) { 153 sb.append(","); 154 sb.append(additionnal); 155 } 156 sb.append(","); 157 sb.append("name="); 158 sb.append(identifier.getNamePropertyValue(instance)); 159 return sb.toString(); 160 } 161 162 return null; 163 164 } 165 166 172 @SuppressWarnings ("unchecked") 173 private <T> EZBManagementIdentifier<T> getIdentifier(final T instance) throws MBeansException { 174 175 Class clz = instance.getClass(); 177 if (identifiers.containsKey(clz)) { 178 return identifiers.get(clz); 179 } 180 181 String mbeanClassname = instance.getClass().getName(); 183 String mbeanPackage = mbeanClassname.substring(0, mbeanClassname 184 .lastIndexOf(".") + 1); 185 186 String identifierClassname = mbeanPackage + "management." + clz.getSimpleName() + "Identifier"; 190 191 try { 193 Class <? extends EZBManagementIdentifier> cls = ClassUtils.forName( 194 identifierClassname).asSubclass(EZBManagementIdentifier.class); 195 EZBManagementIdentifier<T> id = cls.newInstance(); 196 197 identifiers.put(clz, id); 199 200 return id; 201 } catch (ClassNotFoundException e) { 202 throw new MBeansException("Identifier Class not found", e); 203 } catch (InstantiationException e) { 204 throw new MBeansException("Identifier Class not instantiated", e); 205 } catch (IllegalAccessException e) { 206 throw new MBeansException("Identifier Class not instantiated", e); 207 } 208 } 209 210 } 211 | Popular Tags |