KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > metadata > AbstractBuilder


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mx.metadata;
23
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 import javax.management.MBeanInfo JavaDoc;
28 import javax.management.NotCompliantMBeanException JavaDoc;
29
30 /**
31  * Abstract helper class for builder implementations. Includes accessors
32  * for property values that can deal with either string values or equivalent
33  * object types (such as string <tt>"true"</tt> or <tt>Boolean(true)</tt>).
34  *
35  * @see org.jboss.mx.metadata.MetaDataBuilder
36  *
37  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
38  * @version $Revision: 37459 $
39  *
40  */

41 public abstract class AbstractBuilder
42    implements MetaDataBuilder
43 {
44
45    // Attributes ----------------------------------------------------
46

47    /**
48     * Configuration properties.
49     */

50    protected Map JavaDoc properties = new HashMap JavaDoc();
51
52    // Constructors --------------------------------------------------
53

54    /**
55     * Default constructor.
56     */

57    public AbstractBuilder() {}
58
59    public AbstractBuilder(Map JavaDoc properties)
60    {
61       this.properties = properties;
62    }
63
64    // Public --------------------------------------------------------
65

66    /**
67     * Returns true for <tt>Boolean(true)</tt> and strings <tt>"true"</tt>
68     * and <tt>"T"</tt> (case insensitive). Returns false for
69     * <tt>Boolean(false)</tt> and strings <tt>"false"</tt> and <tt>"F"</tt>.
70     *
71     * @param key to lookup
72     *
73     * @return true or false
74     *
75     * @throws IllegalPropertyException if property value is not either
76     * <tt>Boolean</tt> or <tt>String</tt> type or they key value is
77     * <tt>null</tt> or a string contained an unknown value
78     */

79    public boolean getBooleanProperty(String JavaDoc key) throws IllegalPropertyException
80    {
81       Object JavaDoc value = properties.get(key);
82
83       if (value == null)
84          throw new IllegalPropertyException("boolean property " + key + " does not exist");
85
86       if (value instanceof String JavaDoc)
87       {
88          String JavaDoc v = (String JavaDoc) value;
89          if (v.equalsIgnoreCase("true"))
90             return true;
91          if (v.equalsIgnoreCase("false"))
92             return false;
93          if (v.equalsIgnoreCase("t"))
94             return true;
95          if (v.equalsIgnoreCase("f"))
96             return false;
97
98          throw new IllegalPropertyException("unknown string value '" + v + "' for boolean property");
99       }
100       if (value instanceof Boolean JavaDoc)
101          return ((Boolean JavaDoc)value).booleanValue();
102
103       throw new IllegalPropertyException("illegal property type: " + value.getClass().getName());
104    }
105
106    /**
107     * Returns a string property or <tt>null</tt> if key does not exist.
108     */

109    public String JavaDoc getStringProperty(String JavaDoc key)
110    {
111       return (String JavaDoc)properties.get(key);
112    }
113
114
115    // Implements MetaDataBuilder interface --------------------------
116

117    /**
118     * Sets a builder configuration property.
119     */

120    public void setProperty(String JavaDoc key, Object JavaDoc value)
121    {
122       properties.put(key, value);
123    }
124
125    /**
126     * Returns the value of a given configuration property.
127     */

128    public Object JavaDoc getProperty(String JavaDoc key)
129    {
130       return properties.get(key);
131    }
132
133    public abstract MBeanInfo JavaDoc build() throws NotCompliantMBeanException JavaDoc;
134
135
136    // Protected -----------------------------------------------------
137
/**
138     * Sets a copy of a properties map to this builder instance.
139     *
140     * @param properties configuration properties
141     */

142    protected void setProperties(Map JavaDoc properties)
143    {
144       this.properties = new HashMap JavaDoc(properties);
145    }
146
147    /**
148     * Returns the property map of this builder instance.
149     */

150    protected Map JavaDoc getProperties()
151    {
152       return properties;
153    }
154
155
156 }
157
158
159
160
161
Popular Tags