KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > system > metadata > ServiceTextValueMetaData


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.system.metadata;
23
24 import java.beans.PropertyEditor JavaDoc;
25 import java.beans.PropertyEditorManager JavaDoc;
26
27 import javax.management.MBeanAttributeInfo JavaDoc;
28
29 import org.jboss.deployment.DeploymentException;
30 import org.jboss.util.Classes;
31 import org.jboss.util.propertyeditor.PropertyEditors;
32
33 /**
34  * ServiceTextValueMetaData.
35  *
36  * This class is based on the old ServiceConfigurator
37  *
38  * @author <a HREF="mailto:marc@jboss.org">Marc Fleury</a>
39  * @author <a HREF="mailto:hiram@jboss.org">Hiram Chirino</a>
40  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
41  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
42  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
43  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
44  * @version $Revision: 1.1 $
45  */

46 public class ServiceTextValueMetaData extends AbstractMetaDataVisitorNode implements ServiceValueMetaData
47 {
48    static
49    {
50       try
51       {
52          PropertyEditors.init();
53       }
54       catch (Exception JavaDoc ignored)
55       {
56       }
57    }
58    
59    /** The text */
60    private String JavaDoc text;
61
62    /**
63     * Create a new ServiceTextValueMetaData.
64     *
65     * @param text the text
66     */

67    public ServiceTextValueMetaData(String JavaDoc text)
68    {
69       setText(text);
70    }
71
72    /**
73     * Get the text.
74     *
75     * @return the text.
76     */

77    public String JavaDoc getText()
78    {
79       return text;
80    }
81
82    /**
83     * Set the text.
84     *
85     * @param text the text.
86     */

87    public void setText(String JavaDoc text)
88    {
89       if (text == null)
90          throw new IllegalArgumentException JavaDoc("Null text");
91       this.text = text;
92    }
93
94    public Object JavaDoc getValue(ServiceValueContext valueContext) throws Exception JavaDoc
95    {
96       MBeanAttributeInfo JavaDoc attributeInfo = valueContext.getAttributeInfo();
97       ClassLoader JavaDoc cl = valueContext.getClassloader();
98
99       String JavaDoc typeName = attributeInfo.getType();
100       if (typeName == null)
101          throw new DeploymentException("AttributeInfo for " + attributeInfo.getName() + " has no type");
102
103       // see if it is a primitive type first
104
Class JavaDoc typeClass = Classes.getPrimitiveTypeForName(typeName);
105       if (typeClass == null)
106       {
107          // nope try look up
108
try
109          {
110             typeClass = cl.loadClass(typeName);
111          }
112          catch (ClassNotFoundException JavaDoc e)
113          {
114             throw new DeploymentException("Class not found for attribute: " + attributeInfo.getName(), e);
115          }
116       }
117
118       PropertyEditor JavaDoc editor = PropertyEditorManager.findEditor(typeClass);
119       if (editor == null)
120          throw new DeploymentException("No property editor for attribute: " + attributeInfo.getName() + "; type=" + typeClass.getName());
121
122       // JBAS-1709, temporarily switch the TCL so that property
123
// editors have access to the actual deployment ClassLoader.
124
ClassLoader JavaDoc tcl = Thread.currentThread().getContextClassLoader();
125       Thread.currentThread().setContextClassLoader(cl);
126       try
127       {
128          editor.setAsText(text);
129          return editor.getValue();
130       }
131       finally
132       {
133          Thread.currentThread().setContextClassLoader(tcl);
134       }
135    }
136 }
137
Popular Tags