Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 290   Methods: 12
NCLOC: 192   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ConfigurationManagerDynamicMBean.java 0% 0% 0% 0%
coverage
 1    package org.jconfig.jmx;
 2   
 3    //import org.apache.log4j.Logger;
 4    import java.lang.reflect.Constructor;
 5    import java.util.ArrayList;
 6   
 7    import javax.management.Attribute;
 8    import javax.management.AttributeNotFoundException;
 9    import javax.management.DynamicMBean;
 10    import javax.management.InvalidAttributeValueException;
 11    import javax.management.MBeanAttributeInfo;
 12    import javax.management.MBeanConstructorInfo;
 13    import javax.management.MBeanException;
 14    import javax.management.MBeanInfo;
 15    import javax.management.MBeanNotificationInfo;
 16    import javax.management.MBeanOperationInfo;
 17    import javax.management.MBeanParameterInfo;
 18    import javax.management.NotificationBroadcasterSupport;
 19    import javax.management.ObjectName;
 20    import javax.management.ReflectionException;
 21    import javax.management.RuntimeOperationsException;
 22   
 23    import org.jconfig.ConfigurationManager;
 24   
 25    /**
 26    * It is the root MBean of jConfig JMX <br>
 27    * It creates one ConfigurationDynamicMBean per Configuration it founds <br><br>
 28    *
 29    * Has only one operation named "addConfiguration". This operation loads a
 30    * configuration on memory calling ConfigurationManager.getConfiguration(name)
 31    * and creates an associated ConfigurationDynamicMBean.
 32    *
 33    *@author Eduardo Macarron emacarron@euskalnet.net
 34    */
 35   
 36    public class ConfigurationManagerDynamicMBean extends AbstractDynamicMBean implements DynamicMBean {
 37   
 38    private MBeanConstructorInfo[] dConstructors = new MBeanConstructorInfo[1];
 39    // only one operation, loadConfiguration
 40    private MBeanOperationInfo[] dOperations = new MBeanOperationInfo[1];
 41   
 42    private NotificationBroadcasterSupport nbs =
 43    new NotificationBroadcasterSupport();
 44   
 45    private ArrayList alAttributes = new ArrayList();
 46   
 47    private String dClassName = this.getClass().getName();
 48    private String dDescription =
 49    "This MBean acts as a management facade for ConfigurationManager";
 50   
 51    private ObjectName objectName;
 52    /*
 53    private static Logger log =
 54    Logger.getLogger(ConfigurationManagerDynamicMBean.class);
 55    */
 56  0 public ConfigurationManagerDynamicMBean() {
 57  0 try {
 58  0 objectName = new ObjectName("user:configuration=DynamicConfiguration");
 59  0 buildDynamicMBeanInfo();
 60    }
 61    catch (Exception e) {
 62  0 e.printStackTrace();
 63    }
 64    }
 65    /*
 66    public ConfigurationManagerDynamicMBean(ObjectName objectName) {
 67    buildDynamicMBeanInfo();
 68    this.objectName = objectName;
 69    }
 70    */
 71   
 72  0 private void buildDynamicMBeanInfo() {
 73  0 System.out.println("----------------------------------------------------- buildDynamicMBeanInfo()");
 74  0 Constructor[] constructors = this.getClass().getConstructors();
 75  0 dConstructors[0] =
 76    new MBeanConstructorInfo(
 77    "HierarchyDynamicMBean(): Constructs a ConfigurationManagerDynamicMBean instance",
 78    constructors[0]);
 79    /*
 80    dConstructors[1] =
 81    new MBeanConstructorInfo(
 82    "HierarchyDynamicMBean(): Constructs a ConfigurationManagerDynamicMBean instance",
 83    constructors[1]);
 84    */
 85    // parameter information
 86    // this MBean is able to create new configurations
 87   
 88  0 MBeanParameterInfo[] params = new MBeanParameterInfo[1];
 89  0 params[0] =
 90    new MBeanParameterInfo(
 91    "name",
 92    "java.lang.String",
 93    "Create a Configuration");
 94   
 95  0 dOperations[0] =
 96    new MBeanOperationInfo(
 97    "loadConfiguration",
 98    "loadConfiguration(): loads a Configuration",
 99    params,
 100    "javax.management.ObjectName",
 101    MBeanOperationInfo.ACTION);
 102  0 String[] names = ConfigurationManager.getInstance().getConfigurationNames();
 103  0 System.out.println("----------------------------------------------------- buildDynamicMBeanInfo loading all configs");
 104  0 for ( int i = 0; i < names.length;i++) {
 105  0 System.out.println("----------------------------------------------------- buildDynamicMBeanInfo config:"+names[i]);
 106  0 loadConfiguration(names[i]);
 107    }
 108    }
 109   
 110  0 public void setName(String name) {
 111  0 loadConfiguration(name);
 112    }
 113   
 114  0 public String getName() {
 115  0 return "my name";
 116    }
 117    // method that adds a ConfigurationMBean
 118  0 public ObjectName loadConfiguration(String name) {
 119   
 120  0 System.out.println("loadConfiguration called:"+name);
 121   
 122    // test if MBean has been already loaded
 123    // it can also be tested asking directly to MBeanServer
 124  0 for (int i = 0; i < alAttributes.size(); i++) {
 125  0 MBeanAttributeInfo mai = (MBeanAttributeInfo) alAttributes.get(i);
 126  0 if (mai.getName().equalsIgnoreCase("configuration=" + name))
 127  0 return null; // shoud throw an exception??
 128    }
 129   
 130  0 return registerConfigurationMBean(name);
 131    }
 132   
 133  0 ObjectName registerConfigurationMBean(String name) {
 134   
 135  0 ObjectName configurationObjectName = null;
 136  0 try {
 137  0 configurationObjectName =
 138    new ObjectName(objectName.getDomain(), "configuration", name);
 139  0 ConfigurationDynamicMBean configurationMBean =
 140    new ConfigurationDynamicMBean(configurationObjectName, name);
 141  0 server.registerMBean(configurationMBean, configurationObjectName);
 142   
 143    // tenemos un atributo nuevo
 144  0 alAttributes
 145    .add(new MBeanAttributeInfo(
 146    "configuration=" + name,
 147    "javax.management.ObjectName",
 148    "The " + name + " configuration.",
 149    true,
 150    true,
 151    // this makes the object clickable
 152    false));
 153   
 154    } catch (Exception e) {
 155    //log.error(
 156    // "Could not add configurationMBean for [" + name + "].",
 157    // e);
 158    }
 159  0 return objectName;
 160    }
 161    /*
 162    protected Logger getLogger() {
 163    return log;
 164    }
 165    */
 166  0 public MBeanInfo getMBeanInfo() {
 167   
 168    //log.debug("getMBeanInfo called.");
 169   
 170  0 MBeanAttributeInfo[] attribs =
 171    new MBeanAttributeInfo[alAttributes.size()];
 172  0 alAttributes.toArray(attribs);
 173   
 174  0 return new MBeanInfo(
 175    dClassName,
 176    dDescription,
 177    attribs,
 178    dConstructors,
 179    dOperations,
 180    new MBeanNotificationInfo[0]);
 181    }
 182   
 183  0 public MBeanNotificationInfo[] getNotificationInfo() {
 184  0 return nbs.getNotificationInfo();
 185    }
 186   
 187  0 public Object invoke(
 188    String operationName,
 189    Object params[],
 190    String signature[])
 191    throws MBeanException, ReflectionException {
 192   
 193  0 if (operationName == null) {
 194  0 throw new RuntimeOperationsException(
 195    new IllegalArgumentException("Operation name cannot be null"),
 196    "Cannot invoke a null operation in " + dClassName);
 197    }
 198    // Check for a recognized operation name and call the corresponding operation
 199  0 if (operationName.equals("loadConfiguration")) {
 200  0 return loadConfiguration((String) params[0]);
 201    } else {
 202  0 throw new ReflectionException(
 203    new NoSuchMethodException(operationName),
 204    "Cannot find the operation "
 205    + operationName
 206    + " in "
 207    + dClassName);
 208    }
 209    }
 210   
 211  0 public Object getAttribute(String attributeName)
 212    throws AttributeNotFoundException, MBeanException, ReflectionException {
 213   
 214    // Check attributeName is not null to avoid NullPointerException later on
 215  0 if (attributeName == null) {
 216  0 throw new RuntimeOperationsException(
 217    new IllegalArgumentException("Attribute name cannot be null"),
 218    "Cannot invoke a getter of "
 219    + dClassName
 220    + " with null attribute name");
 221    }
 222   
 223    //log.debug("Called getAttribute with [" + attributeName + "].");
 224   
 225    // Check for a recognized attributeName and call the corresponding getter
 226  0 if (attributeName.startsWith("configuration")) {
 227  0 try {
 228  0 return new ObjectName(
 229    objectName.getDomain() + ":" + attributeName);
 230    } catch (Exception e) {
 231  0 e.printStackTrace();
 232    /*
 233    log.error(
 234    "Could not create ObjectName "
 235    + objectName.getDomain()
 236    + ":"
 237    + attributeName);
 238    */
 239    }
 240    }
 241   
 242    // If attributeName has not been recognized throw an AttributeNotFoundException
 243  0 throw (
 244    new AttributeNotFoundException(
 245    "Cannot find "
 246    + attributeName
 247    + " attribute in "
 248    + dClassName));
 249    }
 250   
 251  0 public void postRegister(java.lang.Boolean registrationDone) {
 252    // called when registration is done
 253    // here we have to get every Configuration object and add it to Va
 254   
 255    //log.debug("postRegister is called.");
 256   
 257  0 ConfigurationManager configMngr = ConfigurationManager.getInstance();
 258  0 String[] configNames = configMngr.getConfigurationNames();
 259  0 for (int i = 0; i < configNames.length; i++) {
 260  0 registerConfigurationMBean(configNames[i]);
 261    }
 262    }
 263   
 264  0 public void setAttribute(Attribute attribute)
 265    throws
 266    AttributeNotFoundException,
 267    InvalidAttributeValueException,
 268    MBeanException,
 269    ReflectionException {
 270   
 271    // Check attribute is not null to avoid NullPointerException later on
 272  0 if (attribute == null) {
 273  0 throw new RuntimeOperationsException(
 274    new IllegalArgumentException("Attribute cannot be null"),
 275    "Cannot invoke a setter of "
 276    + dClassName
 277    + " with null attribute");
 278    }
 279  0 String name = attribute.getName();
 280  0 Object value = attribute.getValue();
 281  0 System.out.println("-------------------------------------------set:"+name+" value:"+value);
 282  0 if (name == null) {
 283  0 throw new RuntimeOperationsException(
 284    new IllegalArgumentException("Attribute name cannot be null"),
 285    "Cannot invoke the setter of "
 286    + dClassName
 287    + " with null attribute name");
 288    }
 289    }
 290    }