Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 211   Methods: 7
NCLOC: 150   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CategoryDynamicMBean.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.Enumeration;
 6    import java.util.Properties;
 7   
 8    import javax.management.Attribute;
 9    import javax.management.AttributeNotFoundException;
 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.ObjectName;
 18    import javax.management.ReflectionException;
 19    import javax.management.RuntimeOperationsException;
 20   
 21    import org.jconfig.Category;
 22    import org.jconfig.Configuration;
 23    import org.jconfig.ConfigurationManager;
 24   
 25    /**
 26    * This MBean represents a Category contained in a Configuration <br><br>
 27    *
 28    * Attributes:
 29    * <ul><li>Its own name</li>
 30    * <li>One attribute for each property the category contains</li></ul>
 31    * <br>
 32    * Operations: None
 33    *
 34    *@author Eduardo Macarron emacarron@euskalnet.net
 35    */
 36    public class CategoryDynamicMBean extends AbstractDynamicMBean {
 37   
 38    private MBeanConstructorInfo[] dConstructors = new MBeanConstructorInfo[1];
 39   
 40    private ArrayList alAttributes = new ArrayList();
 41   
 42    private String dClassName = this.getClass().getName();
 43   
 44    private MBeanOperationInfo[] dOperations = new MBeanOperationInfo[0];
 45    private String dDescription =
 46    "This MBean acts as a management facade for categories.";
 47   
 48    private ObjectName objectName;
 49   
 50    // dont keep an instance, it changes when reloading
 51    private String configurationName;
 52    private String categoryName;
 53   
 54  0 public CategoryDynamicMBean(
 55    ObjectName objectName,
 56    String configurationName,
 57    String categoryName) {
 58   
 59  0 this.configurationName = configurationName;
 60  0 this.categoryName = categoryName;
 61  0 this.objectName = objectName;
 62  0 buildDynamicMBeanInfo();
 63    }
 64   
 65  0 private void buildDynamicMBeanInfo() {
 66   
 67  0 Constructor[] constructors = this.getClass().getConstructors();
 68  0 dConstructors[0] =
 69    new MBeanConstructorInfo(
 70    "CategoryDynamicMBean(): Constructs a CategoryDynamicMBean instance",
 71    constructors[0]);
 72   
 73    // categoryName
 74  0 alAttributes.add(
 75    new MBeanAttributeInfo(
 76    "name",
 77    "java.lang.String",
 78    "The name of this Category.",
 79    true,
 80    false,
 81    false));
 82   
 83    // now we have to crate one attribute per property found
 84  0 Configuration configuration =
 85    ConfigurationManager.getConfiguration(configurationName);
 86  0 Category category = configuration.getCategory(categoryName);
 87  0 Properties properties = category.getProperties();
 88  0 Enumeration propertiesEnum = properties.keys();
 89   
 90  0 while (propertiesEnum.hasMoreElements()) {
 91   
 92  0 String propertyName = (String) propertiesEnum.nextElement();
 93   
 94  0 alAttributes
 95    .add(new MBeanAttributeInfo(
 96    propertyName,
 97    "java.lang.String",
 98    "The name of this Property.",
 99    true,
 100    true,
 101    //clickable
 102    false));
 103    }
 104    }
 105   
 106  0 public MBeanInfo getMBeanInfo() {
 107   
 108  0 MBeanAttributeInfo[] attribs =
 109    new MBeanAttributeInfo[alAttributes.size()];
 110  0 alAttributes.toArray(attribs);
 111   
 112  0 MBeanInfo mb =
 113    new MBeanInfo(
 114    dClassName,
 115    dDescription,
 116    attribs,
 117    dConstructors,
 118    dOperations,
 119    new MBeanNotificationInfo[0]);
 120   
 121  0 return mb;
 122    }
 123   
 124  0 public Object invoke(
 125    String operationName,
 126    Object params[],
 127    String signature[])
 128    throws MBeanException, ReflectionException {
 129   
 130    // there are no operations on categories
 131   
 132  0 return null;
 133    }
 134   
 135  0 public Object getAttribute(String attributeName)
 136    throws AttributeNotFoundException, MBeanException, ReflectionException {
 137   
 138    // Check attributeName is not null to avoid NullPointerException later on
 139  0 if (attributeName == null) {
 140  0 throw new RuntimeOperationsException(
 141    new IllegalArgumentException("Attribute name cannot be null"),
 142    "Cannot invoke a getter of "
 143    + dClassName
 144    + " with null attribute name");
 145    }
 146   
 147  0 Object value = null;
 148   
 149    // Check for a recognized attributeName and call the corresponding getter
 150  0 if (attributeName.equals("name")) {
 151  0 value = categoryName;
 152    } else {
 153  0 Configuration configuration =
 154    ConfigurationManager.getConfiguration(configurationName);
 155  0 Category category = configuration.getCategory(categoryName);
 156   
 157  0 if (category.getProperty(attributeName) != null) {
 158  0 value = category.getProperty(attributeName);
 159    }
 160    }
 161   
 162    // there is no way to know if attribute exists on Configuration....
 163  0 if (false)
 164  0 throw (
 165    new AttributeNotFoundException(
 166    "Cannot find "
 167    + attributeName
 168    + " attribute in "
 169    + dClassName));
 170   
 171  0 return value;
 172    }
 173   
 174  0 public void setAttribute(Attribute attribute)
 175    throws
 176    AttributeNotFoundException,
 177    InvalidAttributeValueException,
 178    MBeanException,
 179    ReflectionException {
 180   
 181    // Check attribute is not null to avoid NullPointerException later on
 182  0 if (attribute == null) {
 183  0 throw new RuntimeOperationsException(
 184    new IllegalArgumentException("Attribute cannot be null"),
 185    "Cannot invoke a setter of "
 186    + dClassName
 187    + " with null attribute");
 188    }
 189   
 190  0 String attributeName = attribute.getName();
 191  0 Object value = attribute.getValue();
 192   
 193  0 if (attributeName == null) {
 194  0 throw new RuntimeOperationsException(
 195    new IllegalArgumentException("Attribute name cannot be null"),
 196    "Cannot invoke the setter of "
 197    + dClassName
 198    + " with null attribute name");
 199    }
 200   
 201   
 202  0 Configuration configuration =
 203    ConfigurationManager.getConfiguration(configurationName);
 204  0 Category category = configuration.getCategory(categoryName);
 205  0 category.setProperty(attributeName, (String) value);
 206    }
 207   
 208  0 public void postRegister(java.lang.Boolean registrationDone) {
 209    // there are no MBeans to create
 210    }
 211    }