KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > property > PropertyContainer


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.util.property;
23
24 import java.util.Properties JavaDoc;
25
26 /**
27  * Provides helper methods for working with instance or class properties.
28  *
29  * @version <tt>$Revision: 1958 $</tt>
30  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
31  */

32 public class PropertyContainer
33    extends PropertyMap
34 {
35    /** The group name for this container. */
36    protected String JavaDoc groupname = "<unknown>";
37
38    /**
39     * Initialize the container with a property group.
40     *
41     * @param group Properties.
42     */

43    public PropertyContainer(final Properties JavaDoc props) {
44       super(props);
45    }
46
47    /**
48     * Initialize the container with a property group of the given name.
49     *
50     * @param groupname Property group name.
51     */

52    public PropertyContainer(final String JavaDoc groupname) {
53       this(Property.getGroup(groupname));
54       this.groupname = groupname;
55    }
56
57    /**
58     * Initialize the container with a property group of the given class name.
59     *
60     * @param type The class whos name will be the property group name.
61     */

62    public PropertyContainer(final Class JavaDoc type) {
63       this(type.getName());
64    }
65    
66    /**
67     * Creates a {@link FieldBoundPropertyListener} for the field and
68     * property name and adds it the underlying property group.
69     *
70     * @param name The field name to bind values to.
71     * @param propertyName The property name to bind to.
72     *
73     * @throws IllegalArgumentException Field of property name is null or
74     * empty.
75     */

76    protected void bindField(final String JavaDoc name, final String JavaDoc propertyName) {
77       if (name == null || name.equals(""))
78          throw new IllegalArgumentException JavaDoc("name");
79       if (propertyName == null || propertyName.equals(""))
80          throw new IllegalArgumentException JavaDoc("propertyName");
81
82       addPropertyListener
83          (new FieldBoundPropertyListener(this, name, propertyName));
84    }
85
86    /**
87     * Creates a {@link FieldBoundPropertyListener} for the field and
88     * property name and adds it the underlying property group.
89     *
90     * @param name The field name and property to bind values to.
91     *
92     * @throws IllegalArgumentException Field of property name is null or
93     * empty.
94     */

95    protected void bindField(final String JavaDoc name) {
96       bindField(name, name);
97    }
98
99    /**
100     * Creates a {@link MethodBoundPropertyListener} for the method and
101     * property name and adds it the underlying property group.
102     *
103     * @param name The method name to bind values to.
104     * @param propertyName The property name to bind to.
105     *
106     * @throws IllegalArgumentException Method of property name is null or
107     * empty.
108     */

109    protected void bindMethod(final String JavaDoc name, final String JavaDoc propertyName) {
110       if (name == null || name.equals(""))
111          throw new IllegalArgumentException JavaDoc("name");
112       if (propertyName == null || propertyName.equals(""))
113          throw new IllegalArgumentException JavaDoc("propertyName");
114
115       addPropertyListener // opposite of field bound =(
116
(new MethodBoundPropertyListener(this, propertyName, name));
117    }
118
119    /**
120     * Creates a {@link MethodBoundPropertyListener} for the method and
121     * property name and adds it the underlying property group.
122     *
123     * @param name The method name and property to bind values to.
124     *
125     * @throws IllegalArgumentException Method of property name is null or
126     * empty.
127     */

128    protected void bindMethod(final String JavaDoc name) {
129       bindMethod(name, name);
130    }
131
132    private String JavaDoc makeName(final String JavaDoc name) {
133       return groupname + "." + name;
134    }
135
136    protected void throwException(final String JavaDoc name)
137       throws PropertyException
138    {
139       throw new PropertyException(makeName(name));
140    }
141
142    protected void throwException(final String JavaDoc name, final String JavaDoc msg)
143       throws PropertyException
144    {
145       throw new PropertyException(makeName(name) + ": " + msg);
146    }
147
148    protected void throwException(final String JavaDoc name, final String JavaDoc msg, final Throwable JavaDoc nested)
149       throws PropertyException
150    {
151       throw new PropertyException(makeName(name) + ": " + msg, nested);
152    }
153
154    protected void throwException(final String JavaDoc name, final Throwable JavaDoc nested)
155       throws PropertyException
156    {
157       throw new PropertyException(makeName(name), nested);
158    }
159 }
160
Popular Tags