KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.Constructor JavaDoc;
27
28 import org.jboss.system.ConfigurationException;
29 import org.jboss.util.Classes;
30 import org.jboss.util.StringPropertyReplacer;
31
32 /**
33  * ServiceConstructorMetaData.
34  *
35  * This class is based on the old ConstructorInfo from ServiceCreator
36  *
37  * @author <a HREF="mailto:marc.fleury@jboss.org">Marc Fleury</a>
38  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
39  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
40  * @version $Revision: 1.1 $
41  */

42 public class ServiceConstructorMetaData
43 {
44    /** An empty parameters */
45    public static final Object JavaDoc[] EMPTY_PARAMETERS = {};
46
47    /** An empty parameters list. */
48    public static final String JavaDoc[] EMPTY_PARAMS = {};
49
50    /** An signature list. */
51    public static final String JavaDoc[] EMPTY_SIGNATURE = {};
52
53    /** The constructor signature. */
54    private String JavaDoc[] signature = EMPTY_SIGNATURE;
55
56    /** The constructor parameters. */
57    private String JavaDoc[] params = EMPTY_PARAMS;
58
59    /** The real parameters */
60    private Object JavaDoc[] parameters;
61    
62    /**
63     * Get the params.
64     *
65     * @return the params.
66     */

67    public String JavaDoc[] getParams()
68    {
69       return params;
70    }
71
72    /**
73     * Set the params.
74     *
75     * @param params the params.
76     */

77    public void setParams(String JavaDoc[] params)
78    {
79       if (params == null)
80          throw new IllegalArgumentException JavaDoc("Null params");
81       this.params = params;
82    }
83
84    /**
85     * Get the signature.
86     *
87     * @return the signature.
88     * @throws ConfigurationException if there is a problem with the signature
89     */

90    public String JavaDoc[] getSignature() throws ConfigurationException
91    {
92       for (String JavaDoc string : signature)
93       {
94          if (string == null || string.trim().length() == 0)
95             throw new ConfigurationException("Missing or empty 'type' attribute in constructor arg");
96       }
97       return signature;
98    }
99
100    /**
101     * Set the signature.
102     *
103     * @param signature the signature.
104     */

105    public void setSignature(String JavaDoc[] signature)
106    {
107       if (signature == null)
108          throw new IllegalArgumentException JavaDoc("Null signature");
109       this.signature = signature;
110    }
111    
112    /**
113     * Get the parameters
114     *
115     * @param cl the class loader
116     * @return the parameters
117     * @throws Exception for any error
118     */

119    public Object JavaDoc[] getParameters(ClassLoader JavaDoc cl) throws Exception JavaDoc
120    {
121       if (parameters != null)
122          return parameters;
123       
124       if (params.length == 0)
125          return EMPTY_PARAMETERS;
126
127       String JavaDoc[] signature = getSignature();
128       
129       Object JavaDoc[] result = new Object JavaDoc[params.length];
130       for (int i = 0; i < result.length; ++i)
131       {
132          if (params[i] == null)
133             throw new ConfigurationException("Missing 'value' attribute in constructor arg");
134          
135          String JavaDoc value = StringPropertyReplacer.replaceProperties(params[i]);
136          Object JavaDoc realValue = value;
137
138          if (signature[i] != null)
139          {
140             // See if it is a primitive type first
141
Class JavaDoc typeClass = Classes.getPrimitiveTypeForName(signature[i]);
142             if (typeClass == null)
143                typeClass = cl.loadClass(signature[i]);
144
145             // Convert the string to the real value
146
PropertyEditor JavaDoc editor = PropertyEditorManager.findEditor(typeClass);
147             if (editor == null)
148             {
149                try
150                {
151                   // See if there is a ctor(String) for the type
152
Class JavaDoc[] sig = {String JavaDoc.class};
153                   Constructor JavaDoc ctor = typeClass.getConstructor(sig);
154                   Object JavaDoc[] args = {value};
155                   realValue = ctor.newInstance(args);
156                }
157                catch (Exception JavaDoc e)
158                {
159                   throw new ConfigurationException("No property editor for type: " + typeClass);
160                }
161             }
162             else
163             {
164                editor.setAsText(value);
165                realValue = editor.getValue();
166             }
167          }
168          result[i] = realValue;
169       }
170       return result;
171    }
172
173    /**
174     * Set the parameters.
175     *
176     * @param parameters the parameters.
177     */

178    public void setParameters(Object JavaDoc[] parameters)
179    {
180       this.parameters = parameters;
181    }
182 }
183
Popular Tags