KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > gjc > util > MethodExecutor


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 package com.sun.gjc.util;
25
26 import java.lang.reflect.Method JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.util.Vector JavaDoc;
29 import javax.resource.ResourceException JavaDoc;
30
31 import com.sun.logging.*;
32 import java.util.logging.Logger JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import com.sun.gjc.common.DataSourceObjectBuilder;
35 import com.sun.enterprise.util.i18n.StringManager;
36
37 /**
38  * Execute the methods based on the parameters.
39  *
40  * @version 1.0, 02/07/23
41  * @author Binod P.G
42  */

43 public class MethodExecutor implements java.io.Serializable JavaDoc{
44
45     private static Logger JavaDoc _logger;
46     static {
47         _logger = LogDomains.getLogger( LogDomains.RSR_LOGGER );
48     }
49     private boolean debug = false;
50
51     private StringManager sm = StringManager.getManager(
52         DataSourceObjectBuilder.class );
53     /**
54      * Exceute a simple set Method.
55      *
56      * @param value Value to be set.
57      * @param method <code>Method</code> object.
58      * @param obj Object on which the method to be executed.
59      * @throws <code>ResourceException</code>, in case of the mismatch of parameter values or
60      * a security violation.
61      */

62     public void runJavaBeanMethod(String JavaDoc value, Method JavaDoc method, Object JavaDoc obj) throws ResourceException JavaDoc{
63         if (value==null || value.trim().equals("")) {
64             return;
65         }
66         try {
67             Class JavaDoc[] parameters = method.getParameterTypes();
68             if ( parameters.length == 1) {
69                 Object JavaDoc[] values = new Object JavaDoc[1];
70                 values[0] = convertType(parameters[0], value);
71                 method.invoke(obj, values);
72             }
73         } catch (IllegalAccessException JavaDoc iae) {
74         _logger.log(Level.SEVERE, "jdbc.exc_jb_val", iae);
75         String JavaDoc msg = sm.getString("me.access_denied", method.getName());
76             throw new ResourceException JavaDoc(msg);
77         } catch (IllegalArgumentException JavaDoc ie) {
78         _logger.log(Level.SEVERE, "jdbc.exc_jb_val", ie);
79         String JavaDoc msg = sm.getString("me.illegal_args", method.getName());
80             throw new ResourceException JavaDoc(msg);
81         } catch (InvocationTargetException JavaDoc ite) {
82         _logger.log(Level.SEVERE, "jdbc.exc_jb_val", ite);
83         String JavaDoc msg = sm.getString("me.access_denied", method.getName());
84             throw new ResourceException JavaDoc(msg);
85         }
86     }
87     
88     /**
89      * Executes the method.
90      *
91      * @param method <code>Method</code> object.
92      * @param obj Object on which the method to be executed.
93      * @param values Parameter values for executing the method.
94      * @throws <code>ResourceException</code>, in case of the mismatch of parameter values or
95      * a security violation.
96      */

97     public void runMethod(Method JavaDoc method, Object JavaDoc obj, Vector JavaDoc values) throws ResourceException JavaDoc{
98         try {
99         Class JavaDoc[] parameters = method.getParameterTypes();
100         if (values.size() != parameters.length) {
101             return;
102         }
103             Object JavaDoc[] actualValues = new Object JavaDoc[parameters.length];
104             for (int i =0; i<parameters.length ; i++) {
105                 String JavaDoc val = (String JavaDoc) values.get(i);
106                 if (val.trim().equals("NULL")) {
107                     actualValues[i] = null;
108                 } else {
109                     actualValues[i] = convertType(parameters[i], val);
110                 }
111             }
112             method.invoke(obj, actualValues);
113         }catch (IllegalAccessException JavaDoc iae) {
114         _logger.log(Level.SEVERE, "jdbc.exc_jb_val", iae);
115         String JavaDoc msg = sm.getString("me.access_denied", method.getName());
116             throw new ResourceException JavaDoc(msg);
117         } catch (IllegalArgumentException JavaDoc ie) {
118         _logger.log(Level.SEVERE, "jdbc.exc_jb_val", ie);
119         String JavaDoc msg = sm.getString("me.illegal_args", method.getName());
120             throw new ResourceException JavaDoc(msg);
121         } catch (InvocationTargetException JavaDoc ite) {
122         _logger.log(Level.SEVERE, "jdbc.exc_jb_val", ite);
123         String JavaDoc msg = sm.getString("me.access_denied", method.getName());
124             throw new ResourceException JavaDoc(msg);
125         }
126     }
127     
128     /**
129      * Converts the type from String to the Class type.
130      *
131      * @param type Class name to which the conversion is required.
132      * @param parameter String value to be converted.
133      * @return Converted value.
134      * @throws <code>ResourceException</code>, in case of the mismatch of parameter values or
135      * a security violation.
136      */

137     private Object JavaDoc convertType(Class JavaDoc type, String JavaDoc parameter) throws ResourceException JavaDoc{
138         try {
139             String JavaDoc typeName = type.getName();
140             if ( typeName.equals("java.lang.String") || typeName.equals("java.lang.Object")) {
141                 return parameter;
142             }
143         
144             if (typeName.equals("int") || typeName.equals("java.lang.Integer")) {
145                 return new Integer JavaDoc(parameter);
146             }
147             
148             if (typeName.equals("short") || typeName.equals("java.lang.Short")) {
149                 return new Short JavaDoc(parameter);
150             }
151             
152             if (typeName.equals("byte") || typeName.equals("java.lang.Byte")) {
153                 return new Byte JavaDoc(parameter);
154             }
155             
156             if (typeName.equals("long") || typeName.equals("java.lang.Long")) {
157                 return new Long JavaDoc(parameter);
158             }
159             
160             if (typeName.equals("float") || typeName.equals("java.lang.Float")) {
161                 return new Float JavaDoc(parameter);
162             }
163             
164             if (typeName.equals("double") || typeName.equals("java.lang.Double")) {
165                 return new Double JavaDoc(parameter);
166             }
167             
168             if (typeName.equals("java.math.BigDecimal")) {
169                 return new java.math.BigDecimal JavaDoc(parameter);
170             }
171             
172             if (typeName.equals("java.math.BigInteger")) {
173                 return new java.math.BigInteger JavaDoc(parameter);
174             }
175             
176             if (typeName.equals("boolean") || typeName.equals("java.lang.Boolean")) {
177                 return new Boolean JavaDoc(parameter);
178             }
179
180             return parameter;
181         } catch (NumberFormatException JavaDoc nfe) {
182         _logger.log(Level.SEVERE, "jdbc.exc_nfe", parameter);
183         String JavaDoc msg = sm.getString( "me.invalid_param", parameter );
184             throw new ResourceException JavaDoc(msg);
185         }
186     }
187     
188 }
189
190
Popular Tags