/* * JBoss, the OpenSource J2EE webOS * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.jboss.mx.metadata; import java.util.Map; import java.util.HashMap; import javax.management.MBeanInfo; import javax.management.NotCompliantMBeanException; /** * Abstract helper class for builder implementations. Includes accessors * for property values that can deal with either string values or equivalent * object types (such as string "true" or Boolean(true)). * * @see org.jboss.mx.metadata.MetaDataBuilder * * @author Juha Lindfors. * @version $Revision: 1.1 $ * */ public abstract class AbstractBuilder implements MetaDataBuilder { // Attributes ---------------------------------------------------- /** * Configuration properties. */ protected Map properties = new HashMap(); // Constructors -------------------------------------------------- /** * Default constructor. */ public AbstractBuilder() {} public AbstractBuilder(Map properties) { this.properties = properties; } // Public -------------------------------------------------------- /** * Returns true for Boolean(true) and strings "true" * and "T" (case insensitive). Returns false for * Boolean(false) and strings "false" and "F". * * @param key to lookup * * @return true or false * * @throws IllegalPropertyException if property value is not either * Boolean or String type or they key value is * null or a string contained an unknown value */ public boolean getBooleanProperty(String key) throws IllegalPropertyException { Object value = properties.get(key); if (value == null) throw new IllegalPropertyException("boolean property " + key + " does not exist"); if (value instanceof String) { String v = (String) value; if (v.equalsIgnoreCase("true")) return true; if (v.equalsIgnoreCase("false")) return false; if (v.equalsIgnoreCase("t")) return true; if (v.equalsIgnoreCase("f")) return false; throw new IllegalPropertyException("unknown string value '" + v + "' for boolean property"); } if (value instanceof Boolean) return ((Boolean)value).booleanValue(); throw new IllegalPropertyException("illegal property type: " + value.getClass().getName()); } /** * Returns a string property or null if key does not exist. */ public String getStringProperty(String key) { return (String)properties.get(key); } // Implements MetaDataBuilder interface -------------------------- /** * Sets a builder configuration property. */ public void setProperty(String key, Object value) { properties.put(key, value); } /** * Returns the value of a given configuration property. */ public Object getProperty(String key) { return properties.get(key); } public abstract MBeanInfo build() throws NotCompliantMBeanException; // Protected ----------------------------------------------------- /** * Sets a copy of a properties map to this builder instance. * * @param properties configuration properties */ protected void setProperties(Map properties) { this.properties = new HashMap(properties); } /** * Returns the property map of this builder instance. */ protected Map getProperties() { return properties; } }