KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > common > ParamInfo


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.enterprise.admin.common;
26
27 import java.util.HashMap JavaDoc;
28
29 /**
30     A class to create the signatures of various methods of known classes/interfaces
31     from object instances. This is handy when the methods that are called
32     involve introspection. The caller of various methods in this class just
33     provides the <code> instances </code> of various parameters of methods on
34     target classes/interfaces. The class in turn does basic introspection and
35     derives the <code> class </code> objects from instances and creates the
36     signature to relieve the callers of doing this task.
37     <p>
38     The most generic use of methods in this class requires callers to form
39     the arrays of objects before calling them. But convenience methods are
40     provided with 1, 2, 3 and 4 parameters (which covers 90 % of signatures).
41     This way, callers are not forced to create object arrays every time.
42     <p>
43     Note that this class only handles java.lang.Object and its subclasses. Not
44     designed for primitives. So methods of this class are not useful for
45     methods that contain parameters that contain both primitives and Objects.
46     <p>
47     An important thing to note is that since this class tries to get the class
48     from the passed <code> instance </code> of a class, it will always generate
49     exact class (and none of its super classes). Thus it is unlike (and
50     less powerful) than the static method call where one can call a method with
51     an Object parameter passing instances of *any* of its subclasses.
52     @author Senthil Chidambaram
53     @version 1.0
54 */

55
56 public class ParamInfo
57 {
58
59     protected String JavaDoc mOperationName = null;
60     protected String JavaDoc[] mSignature = null;
61     protected Object JavaDoc[] mParams = null;
62     protected boolean mForcePrimitives = false;
63
64     private static final HashMap JavaDoc mPrimitives = createPrimitivesMap();
65     private static final HashMap JavaDoc mPrimitiveClasses = createPrimitiveClassesMap();
66
67     /** Creates a lookup table to get primitive class names corresponding to their
68      * wrapper objects.
69      * @return HashMap Returns the HashMap of the Class Primitive type key/value pairs.
70      */

71
72     // create a lookup table to get primitive class names corresponding to their wrapper objects
73

74     private static HashMap JavaDoc createPrimitivesMap()
75     {
76         HashMap JavaDoc primitives = new HashMap JavaDoc();
77         primitives.put(Integer JavaDoc.class, "int");
78         primitives.put(Boolean JavaDoc.class, "boolean");
79         primitives.put(Float JavaDoc.class, "float");
80         primitives.put(Double JavaDoc.class, "double");
81         primitives.put(Byte JavaDoc.class, "byte");
82         primitives.put(Character JavaDoc.class, "char");
83         primitives.put(Short JavaDoc.class, "short");
84         primitives.put(Long JavaDoc.class, "long");
85
86         return primitives;
87     }
88     
89     private static HashMap JavaDoc createPrimitiveClassesMap()
90     {
91         HashMap JavaDoc primitiveClassMap = new HashMap JavaDoc();
92         primitiveClassMap.put("int", Integer.TYPE);
93         primitiveClassMap.put("boolean", Boolean.TYPE);
94         primitiveClassMap.put("float", Float.TYPE);
95         primitiveClassMap.put("double", Double.TYPE);
96         primitiveClassMap.put("byte", Byte.TYPE);
97         primitiveClassMap.put("char", Character.TYPE);
98         primitiveClassMap.put("short", Short.TYPE);
99         primitiveClassMap.put("long", Long.TYPE);
100
101         return primitiveClassMap;
102     }
103
104     public static Class JavaDoc getPrimitiveClass(String JavaDoc type)
105     {
106         return ( (Class JavaDoc) mPrimitiveClasses.get(type) );
107     }
108
109     /**
110             Forces the primitive type conversion to false.
111             So that certain classes are not covnerted.
112     */

113
114     public void initCoercionOptions()
115     {
116         mForcePrimitives = false;
117     }
118
119     /**
120     * Constructor takes the operationName, and an array of Object Parameters.
121     * This constructor calls the paramstoClassNames to set the signature
122     * of the params array object.
123     */

124
125     public ParamInfo( String JavaDoc operationName, Object JavaDoc[] params )
126     {
127         mOperationName = operationName;
128         mParams = params;
129
130         initCoercionOptions();
131         mSignature = paramsToClassNames( params );
132     }
133
134     public ParamInfo(String JavaDoc operationName)
135     {
136         this(operationName, new Object JavaDoc[0]);
137     }
138
139
140     /**
141     * Construct a ParamInfo object for an operation with a single parameter
142     * This constructor calls the ParamInfo array object constructor to
143     * set the signature for the param object.
144     * @param operationName name of the operation
145     * @param param Parameter for the operation
146     */

147
148     public ParamInfo( String JavaDoc operationName, Object JavaDoc param )
149     {
150         this(operationName, new Object JavaDoc[]{param} );
151     }
152
153
154     /**
155     * Construct a ParamInfo object for an operation with two parameters.
156     * Then it calls the ParamInfo array object constructor to set the
157     * signature for the parameters.
158     *
159     * @param operationName name of the operation
160     * @param param1 Parameter for the operation
161     * @param param2 Second parameter for the operation
162     */

163
164     public ParamInfo( String JavaDoc operationName, Object JavaDoc param1, Object JavaDoc param2 )
165     {
166         this(operationName, new Object JavaDoc[]{param1, param2});
167     }
168
169
170     /**
171     * Construct a ParamInfo object for an operation with three parameters.
172     * Then it calls the ParamInfo array object constructor to set the
173     * signature for the parameters.
174     *
175     * @param operationName name of the operation
176     * @param param1 Parameter for the operation
177     * @param param2 Second parameter for the operation
178     * @param param3 Third parameter for the operation
179     */

180
181
182     public ParamInfo( String JavaDoc operationName, Object JavaDoc param1,
183         Object JavaDoc param2, Object JavaDoc param3 )
184     {
185         this(operationName, new Object JavaDoc[]{param1, param2, param3});
186     }
187
188
189     /**
190     * Construct a ParamInfo object for an operation with four parameters.
191     * Then it calls the ParamInfo array object constructor to set the
192     * signature for the parameters.
193     *
194     * @param operationName name of the operation
195     * @param param1 Parameter for the operation
196     * @param param2 Second parameter for the operation
197     * @param param3 Third parameter for the operation
198     * @param param4 Fourth parameter
199     */

200
201     public ParamInfo( String JavaDoc operationName,
202                         Object JavaDoc param1,
203                         Object JavaDoc param2,
204                         Object JavaDoc param3,
205                         Object JavaDoc param4 )
206     {
207         this(operationName, new Object JavaDoc[]{param1, param2, param3, param4});
208     }
209
210
211     /**
212     * Method returns the operationName.
213     * @return operationName.
214     */

215
216     public String JavaDoc getOperationName()
217     {
218         return mOperationName;
219     }
220
221
222     /**
223     * Method returns an array of params Object.
224     * @return Object array of params.
225     */

226
227     public Object JavaDoc[] getParams()
228     {
229         return mParams;
230     }
231
232
233     /**
234     * Method returns the String array of signatures.
235     * @return String array of signatures.
236     */

237
238     public String JavaDoc[] getSignature()
239     {
240         return mSignature;
241     }
242
243
244     /**
245     * Sets the classname(signature) for the parametes in the Object array.
246     * @return String array of signatures
247     */

248
249     public String JavaDoc[] paramsToClassNames(Object JavaDoc[] params)
250     {
251         String JavaDoc[] signature = new String JavaDoc[params.length];
252
253         for(int ctr = 0; ctr < params.length; ctr++)
254         {
255             Object JavaDoc primitive = null;
256
257             if(mForcePrimitives) // see if this object's class represents a primitive
258
{
259                 primitive = mPrimitives.get(params[ctr].getClass());
260             }
261             /* Certain Operations on the MBean take primitive types like "int", instead
262             * of the Integer object. We can't pass primitive types across the wire.
263             * So, we're setting the signature to int, and passing Integer parameter.
264             * We do this for all primitive types if mForcePrimitives is true.
265             */

266             if (primitive != null)
267             {
268                 signature[ctr] = (String JavaDoc) primitive;
269             }
270             else
271             {
272                 signature[ctr] = params[ctr].getClass().getName();
273             }
274         }
275         return signature;
276     }
277 }
Popular Tags