Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 351   Methods: 14
NCLOC: 257   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ConfigurationDynamicMBean.java 0% 0% 0% 0%
coverage
 1    package org.jconfig.jmx;
 2   
 3    import java.lang.reflect.Constructor;
 4    import java.util.ArrayList;
 5    import java.util.Hashtable;
 6   
 7    import javax.management.Attribute;
 8    import javax.management.AttributeNotFoundException;
 9    import javax.management.InvalidAttributeValueException;
 10    import javax.management.MBeanAttributeInfo;
 11    import javax.management.MBeanConstructorInfo;
 12    import javax.management.MBeanException;
 13    import javax.management.MBeanInfo;
 14    import javax.management.MBeanNotificationInfo;
 15    import javax.management.MBeanOperationInfo;
 16    import javax.management.MBeanParameterInfo;
 17    import javax.management.ObjectName;
 18    import javax.management.ReflectionException;
 19    import javax.management.RuntimeOperationsException;
 20   
 21    import org.jconfig.Configuration;
 22    import org.jconfig.ConfigurationManager;
 23    import org.jconfig.ConfigurationManagerException;
 24   
 25    /**
 26    * This MBean manages each Configuration that the ConfiguratorManager holds.<br><br>
 27    * Atributes: <br>
 28    * <ul><li>Its own name</li>
 29    * <li>One attribute for each category</li></ul>
 30    * <br>
 31    * Operations: <br>
 32    * <ul><li>Reload: It reloads the configuration using ConfigurationManager.reload()</li>
 33    * <li>Save: Saves que configuration using ConfigurationMananager.save()</li></ul>
 34    * <br>
 35    * <br>
 36    * Notes: <br>
 37    * ConfigurationMbeans could be registered as FileChangeListeners
 38    * If the configuration file changes, the method reloadConfiguration should be executed
 39    * so as to sincronize the MBean with its managed configuration
 40    *
 41    *@author Eduardo Macarron emacarron@euskalnet.net
 42    */
 43    public class ConfigurationDynamicMBean extends AbstractDynamicMBean {
 44   
 45    private MBeanConstructorInfo[] dConstructors = new MBeanConstructorInfo[1];
 46   
 47    // can reload, and save a configuration
 48    private MBeanOperationInfo[] dOperations = new MBeanOperationInfo[3];
 49   
 50    private ArrayList alBasicAttributes = new ArrayList();
 51    private ArrayList alMbeanAttributes = new ArrayList();
 52   
 53    private String dClassName = this.getClass().getName();
 54   
 55    private String dDescription =
 56    "This MBean acts as a management facade for a org.jconfig.Configuration instance.";
 57   
 58    private ObjectName objectName;
 59   
 60   
 61    // Configuration name
 62    // Cannot hold a Configuration instance because it changes on reloads
 63    private String configurationName;
 64   
 65  0 public ConfigurationDynamicMBean(ObjectName objectName,String configurationName) {
 66    // constructor, wraps configurator and builds MBean infor
 67  0 this.configurationName = configurationName;
 68  0 this.objectName = objectName;
 69  0 buildDynamicMBeanInfo();
 70    }
 71   
 72  0 private void buildDynamicMBeanInfo() {
 73   
 74  0 Constructor[] constructors = this.getClass().getConstructors();
 75  0 dConstructors[0] =
 76    new MBeanConstructorInfo(
 77    "ConfigurationDynamicMBean(): Constructs a ConfigurationDynamicMBean instance",
 78    constructors[0]);
 79   
 80  0 alBasicAttributes.add(
 81    new MBeanAttributeInfo(
 82    "name",
 83    "java.lang.String",
 84    "The name of this Configuration.",
 85    true,
 86    false,
 87    false));
 88   
 89    // has no parameters
 90  0 MBeanParameterInfo[] params = new MBeanParameterInfo[0];
 91  0 MBeanParameterInfo[] params2 = new MBeanParameterInfo[1];
 92   
 93  0 params2[0] = new MBeanParameterInfo(
 94    "name",
 95    "java.lang.String",
 96    "The name");
 97   
 98  0 dOperations[0] =
 99    new MBeanOperationInfo(
 100    "reloadConfiguration",
 101    "reloadConfiguration(): reloads configuration",
 102    params,
 103    "void",
 104    MBeanOperationInfo.ACTION);
 105   
 106  0 dOperations[1] =
 107    new MBeanOperationInfo(
 108    "saveConfiguration",
 109    "saveConfiguration(): saves configuration",
 110    params,
 111    "void",
 112    MBeanOperationInfo.ACTION);
 113   
 114  0 dOperations[2] =
 115    new MBeanOperationInfo(
 116    "addCategory",
 117    "addCategory(String name): create new category",
 118    params2,
 119    "void",
 120    MBeanOperationInfo.ACTION);
 121    }
 122   
 123  0 public MBeanInfo getMBeanInfo() {
 124   
 125  0 System.out.println("getMBeanInfo called.");
 126   
 127  0 MBeanAttributeInfo[] attribs =
 128    new MBeanAttributeInfo[alBasicAttributes.size()
 129    + alMbeanAttributes.size()];
 130  0 ArrayList al = new ArrayList();
 131  0 al.addAll(alBasicAttributes);
 132  0 al.addAll(alMbeanAttributes);
 133  0 al.toArray(attribs);
 134  0 al = null;
 135   
 136  0 MBeanInfo mb =
 137    new MBeanInfo(
 138    dClassName,
 139    dDescription,
 140    attribs,
 141    dConstructors,
 142    dOperations,
 143    new MBeanNotificationInfo[0]);
 144   
 145  0 System.out.println("getMBeanInfo exit.");
 146  0 return mb;
 147    }
 148   
 149  0 public Object invoke(
 150    String operationName,
 151    Object params[],
 152    String signature[])
 153    throws MBeanException, ReflectionException {
 154   
 155  0 if (operationName.equals("reloadConfiguration")) {
 156  0 reloadConfiguration();
 157  0 } else if (operationName.equals("saveConfiguration")) {
 158  0 saveConfiguration();
 159    } else {
 160  0 throw new ReflectionException(
 161    new NoSuchMethodException(operationName),
 162    "Cannot find the operation "
 163    + operationName
 164    + " in "
 165    + dClassName);
 166    }
 167   
 168  0 return null;
 169    }
 170   
 171  0 public void addCategory(String name) throws MBeanException {
 172   
 173    }
 174   
 175  0 public void reloadConfiguration() throws MBeanException {
 176   
 177  0 System.out.println("reloadConfiguration called");
 178   
 179  0 try {
 180    // deregister categories
 181  0 categoryMBeanDeregistration();
 182    // reload configuration
 183  0 ConfigurationManager.getInstance().reload(configurationName);
 184    // register categories again
 185  0 categoryMBeanRegistration();
 186   
 187    } catch (ConfigurationManagerException cme) {
 188  0 throw new MBeanException(cme);
 189    }
 190    }
 191   
 192  0 public void saveConfiguration() throws MBeanException {
 193   
 194  0 System.out.println("saveConfiguration called");
 195   
 196  0 try {
 197  0 ConfigurationManager.getInstance().save(configurationName);
 198    } catch (ConfigurationManagerException cme) {
 199  0 throw new MBeanException(cme);
 200    }
 201    }
 202   
 203  0 public Object getAttribute(String attributeName)
 204    throws AttributeNotFoundException, MBeanException, ReflectionException {
 205   
 206    // Check attributeName is not null to avoid NullPointerException later on
 207  0 if (attributeName == null) {
 208  0 throw new RuntimeOperationsException(
 209    new IllegalArgumentException("Attribute name cannot be null"),
 210    "Cannot invoke a getter of "
 211    + dClassName
 212    + " with null attribute name");
 213    }
 214   
 215    // Check for a recognized attributeName and call the corresponding getter
 216  0 if (attributeName.equals("name")) {
 217  0 return configurationName;
 218  0 } else if (attributeName.startsWith("category=")) {
 219  0 Hashtable keys = new Hashtable();
 220  0 keys.put("configuration", configurationName);
 221  0 keys.put(
 222    "category",
 223    attributeName.substring(attributeName.indexOf("=") + 1));
 224  0 try {
 225  0 return new ObjectName(objectName.getDomain(), keys);
 226    } catch (Exception e) {
 227  0 System.out.println(
 228    "Could not create ObjectName "
 229    + objectName.getDomain()
 230    + ":"
 231    + keys);
 232    }
 233    }
 234   
 235    //If attributeName has not been recognized throw an AttributeNotFoundException
 236  0 throw (
 237    new AttributeNotFoundException(
 238    "Cannot find "
 239    + attributeName
 240    + " attribute in "
 241    + dClassName));
 242    }
 243   
 244  0 public void setAttribute(Attribute attribute)
 245    throws
 246    AttributeNotFoundException,
 247    InvalidAttributeValueException,
 248    MBeanException,
 249    ReflectionException {
 250   
 251    // Check attribute is not null to avoid NullPointerException later on
 252  0 if (attribute == null) {
 253  0 throw new RuntimeOperationsException(
 254    new IllegalArgumentException("Attribute cannot be null"),
 255    "Cannot invoke a setter of "
 256    + dClassName
 257    + " with null attribute");
 258    }
 259   
 260  0 String name = attribute.getName();
 261  0 Object value = attribute.getValue();
 262   
 263  0 if (name == null) {
 264  0 throw new RuntimeOperationsException(
 265    new IllegalArgumentException("Attribute name cannot be null"),
 266    "Cannot invoke the setter of "
 267    + dClassName
 268    + " with null attribute name");
 269    }
 270    }
 271   
 272  0 void categoryMBeanRegistration() {
 273   
 274    // registers all categories
 275  0 Configuration configuration =
 276    ConfigurationManager.getConfiguration(configurationName);
 277  0 String[] categoryNames = configuration.getCategoryNames();
 278  0 for (int i = 0; i < categoryNames.length; i++) {
 279  0 registerCategoryMBean(categoryNames[i]);
 280    }
 281    }
 282   
 283  0 void registerCategoryMBean(String categoryName) {
 284   
 285  0 System.out.println("Adding CategoryMBean for category named " + categoryName);
 286  0 ObjectName categoryObjectName = null;
 287  0 try {
 288  0 Hashtable keys = new Hashtable();
 289  0 keys.put("configuration", configurationName);
 290  0 keys.put("category", categoryName);
 291  0 categoryObjectName = new ObjectName(objectName.getDomain(), keys);
 292  0 CategoryDynamicMBean categoryMBean =
 293    new CategoryDynamicMBean(
 294    categoryObjectName,
 295    configurationName,
 296    categoryName);
 297  0 server.registerMBean(categoryMBean, categoryObjectName);
 298   
 299  0 alMbeanAttributes.add(
 300    new MBeanAttributeInfo(
 301    "category=" + categoryName,
 302    "javax.management.ObjectName",
 303    "The " + categoryName + " category.",
 304    true,
 305    true,
 306    false));
 307   
 308    } catch (Exception e) {
 309  0 System.out.println(
 310    "Could not add CategoryMBean for [" + categoryName + "]."+e);
 311    }
 312    }
 313   
 314  0 public void postRegister(java.lang.Boolean registrationDone) {
 315   
 316  0 System.out.println("postRegister called");
 317   
 318    // registers an MBean for every category found in this configuration
 319  0 categoryMBeanRegistration();
 320    }
 321   
 322  0 void categoryMBeanDeregistration() {
 323    // deregisters all categories
 324  0 Configuration configuration =
 325    ConfigurationManager.getConfiguration(configurationName);
 326  0 String[] categoryNames = configuration.getCategoryNames();
 327  0 for (int i = 0; i < categoryNames.length; i++) {
 328  0 unregisterCategoryMBean(categoryNames[i]);
 329    }
 330   
 331    // categories are removed
 332  0 alMbeanAttributes.clear();
 333    }
 334   
 335  0 void unregisterCategoryMBean(String categoryName) {
 336   
 337  0 System.out.println("Removing CategoryMBean for category named " + categoryName);
 338  0 ObjectName categoryObjectName = null;
 339  0 try {
 340  0 Hashtable keys = new Hashtable();
 341  0 keys.put("configuration", configurationName);
 342  0 keys.put("category", categoryName);
 343  0 categoryObjectName = new ObjectName(objectName.getDomain(), keys);
 344  0 server.unregisterMBean(categoryObjectName);
 345    } catch (Exception e) {
 346  0 System.out.println(
 347    "Could not add CategoryMBean for [" + categoryName + "]."+e);
 348    }
 349    }
 350   
 351    }