KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > property > PropertyEditorManagerService


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.varia.property;
23
24 import java.beans.PropertyEditor JavaDoc;
25 import java.beans.PropertyEditorManager JavaDoc;
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Properties JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34
35 import javax.management.MBeanServer JavaDoc;
36 import javax.management.MalformedObjectNameException JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38
39 import org.jboss.system.ServiceMBeanSupport;
40
41 /**
42  * A service to access <tt>java.beans.PropertyEditorManager</tt>.
43  *
44  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
45  * @version <tt>$Revision: 39232 $</tt>
46  */

47 public class PropertyEditorManagerService extends ServiceMBeanSupport
48    implements PropertyEditorManagerServiceMBean
49 {
50    /**
51     * List of registered editors (java.lang.Class).
52     * These will be removed in destroyService().
53     * This is to prevent memory leakage of the class loader.
54     */

55    private List JavaDoc registeredEditors = Collections.synchronizedList(new ArrayList JavaDoc());
56
57    ///////////////////////////////////////////////////////////////////////////
58
// PropertyEditorManager Access //
59
///////////////////////////////////////////////////////////////////////////
60

61    /**
62     * Locate a value editor for a given target type.
63     *
64     * @jmx:managed-operation
65     *
66     * @param type The class of the object to be edited.
67     * @return An editor for the given type or null if none was found.
68     */

69    public PropertyEditor JavaDoc findEditor(final Class JavaDoc type)
70    {
71       return PropertyEditorManager.findEditor(type);
72    }
73
74    /**
75     * Locate a value editor for a given target type.
76     *
77     * @jmx:managed-operation
78     *
79     * @param typeName The class name of the object to be edited.
80     * @return An editor for the given type or null if none was found.
81     */

82    public PropertyEditor JavaDoc findEditor(final String JavaDoc typeName)
83       throws ClassNotFoundException JavaDoc
84    {
85       Class JavaDoc type = Class.forName(typeName);
86       
87       return PropertyEditorManager.findEditor(type);
88    }
89
90    /**
91     * Register an editor class to be used to editor values of a given target class.
92     *
93     * @jmx:managed-operation
94     *
95     * @param type The class of the objetcs to be edited.
96     * @param editorType The class of the editor.
97     */

98    public void registerEditor(final Class JavaDoc type, final Class JavaDoc editorType)
99    {
100       registeredEditors.add(type);
101       PropertyEditorManager.registerEditor(type, editorType);
102    }
103
104    /**
105     * Register an editor class to be used to editor values of a given target class.
106     *
107     * @jmx:managed-operation
108     *
109     * @param typeName The classname of the objetcs to be edited.
110     * @param editorTypeName The class of the editor.
111     */

112    public void registerEditor(final String JavaDoc typeName,
113                               final String JavaDoc editorTypeName)
114       throws ClassNotFoundException JavaDoc
115    {
116       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
117       Class JavaDoc type = cl.loadClass(typeName);
118       Class JavaDoc editorType = cl.loadClass(editorTypeName);
119       registerEditor(type, editorType);
120    }
121
122    /** Turn a string[] into an comma seperated list. */
123    private String JavaDoc makeString(final String JavaDoc[] array)
124    {
125       StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
126       
127       for (int i=0; i<array.length; i++) {
128          buff.append(array[i]);
129          if ((i + 1) < array.length) {
130             buff.append(",");
131          }
132       }
133
134       return buff.toString();
135    }
136
137    /** Turn an comma seperated list into a string[]. */
138    private String JavaDoc[] makeArray(final String JavaDoc listspec)
139    {
140       StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(listspec, ",");
141       List JavaDoc list = new ArrayList JavaDoc();
142       
143       while (stok.hasMoreTokens()) {
144          String JavaDoc url = stok.nextToken();
145          list.add(url);
146       }
147
148       return (String JavaDoc[])list.toArray(new String JavaDoc[list.size()]);
149    }
150
151    /**
152     * Gets the package names that will be searched for property editors.
153     *
154     * @jmx:managed-attribute
155     *
156     * @return The package names that will be searched for property editors.
157     */

158    public String JavaDoc getEditorSearchPath()
159    {
160       return makeString(PropertyEditorManager.getEditorSearchPath());
161    }
162
163    /**
164     * Sets the package names that will be searched for property editors.
165     *
166     * @jmx:managed-attribute
167     *
168     * @param path A comma sperated list of package names.
169     */

170    public void setEditorSearchPath(final String JavaDoc path)
171    {
172       PropertyEditorManager.setEditorSearchPath(makeArray(path));
173    }
174    
175
176    ///////////////////////////////////////////////////////////////////////////
177
// JMX & Configuration Helpers //
178
///////////////////////////////////////////////////////////////////////////
179

180    /**
181     * Load property editors based on the given properties string.
182     *
183     * @jmx:managed-attribute
184     *
185     * @param props, A string representation of a editor.class=editor.type
186     * Properties map for the editors to load.
187     */

188    public void setBootstrapEditors(final String JavaDoc propsString)
189       throws ClassNotFoundException JavaDoc, IOException JavaDoc
190    {
191       Properties JavaDoc props = new Properties JavaDoc();
192       ByteArrayInputStream JavaDoc stream = new ByteArrayInputStream JavaDoc(propsString.getBytes());
193       props.load(stream);
194       setEditors(props);
195    }
196
197    /**
198     * Set property editors based on the given properties map.
199     *
200     * @jmx:managed-attribute
201     *
202     * @param props Map of <em>type name</em> to </em>editor type name</em>.
203     */

204    public void setEditors(final Properties JavaDoc props) throws ClassNotFoundException JavaDoc
205    {
206       Iterator JavaDoc iter = props.keySet().iterator();
207       while (iter.hasNext()) {
208          String JavaDoc typeName = (String JavaDoc)iter.next();
209          String JavaDoc editorTypeName = props.getProperty(typeName);
210
211          registerEditor(typeName, editorTypeName);
212       }
213    }
214
215    /**
216     * Returns a list of registered editor classes.
217     * @jmx:managed-attribute
218     */

219    public Class JavaDoc[] getRegisteredEditors()
220    {
221       return (Class JavaDoc [])registeredEditors.toArray(new Class JavaDoc[registeredEditors.size()]);
222    }
223    
224    ///////////////////////////////////////////////////////////////////////////
225
// ServiceMBeanSupport Overrides //
226
///////////////////////////////////////////////////////////////////////////
227

228    protected ObjectName JavaDoc getObjectName(final MBeanServer JavaDoc server, final ObjectName JavaDoc name)
229       throws MalformedObjectNameException JavaDoc
230    {
231       return name == null ? OBJECT_NAME : name;
232    }
233    
234    /**
235     * Removes the list of registered editors.
236     */

237    protected void destroyService() throws Exception JavaDoc
238    {
239       Iterator JavaDoc iter = registeredEditors.iterator();
240       while (iter.hasNext())
241       {
242          Class JavaDoc type = (Class JavaDoc)iter.next();
243          PropertyEditorManager.registerEditor(type, null);
244          iter.remove();
245       }
246    }
247
248 }
249
Popular Tags