KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > connectors > util > SetMethodAction


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.enterprise.connectors.util;
25
26 import com.sun.enterprise.deployment.EnvironmentProperty;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34 import com.sun.logging.LogDomains;
35 import java.security.PrivilegedAction JavaDoc;
36 import java.security.PrivilegedActionException JavaDoc;
37 import java.security.PrivilegedExceptionAction JavaDoc;
38 import com.sun.enterprise.connectors.ConnectorRuntimeException;
39
40 /**
41  * Executes setter methods on java beans.
42  *
43  * @author Qingqing Ouyang, Binod P.G, Sivakumar Thyagarajan
44  */

45 public class SetMethodAction implements PrivilegedExceptionAction JavaDoc {
46
47     private Object JavaDoc bean;
48     private Set JavaDoc props;
49     private Method JavaDoc[] methods;
50
51     private static Logger JavaDoc logger =
52     LogDomains.getLogger(LogDomains.RSR_LOGGER);
53
54     /**
55      * Accepts java bean object and properties to be set.
56      */

57     public SetMethodAction(Object JavaDoc bean, Set JavaDoc props) {
58             this.bean = bean;
59             this.props = props;
60         }
61
62     /**
63      * Executes the setter methods in the java bean.
64      */

65     public Object JavaDoc run() throws Exception JavaDoc {
66         Iterator JavaDoc it = props.iterator();
67         methods = bean.getClass().getMethods();
68         while (it.hasNext()) {
69             EnvironmentProperty prop = (EnvironmentProperty) it.next();
70             String JavaDoc propName = prop.getName();
71             Class JavaDoc type = getTypeOf(prop);
72             //If there were no getter, use the EnvironmentProperty's
73
//property type
74
if ( type == null ) {
75                 type = Class.forName(prop.getType());
76             }
77             
78         if (prop.getResolvedValue() != null &&
79         prop.getResolvedValue().trim().length() != 0) {
80                 Method JavaDoc meth = null;
81                 try {
82                     meth = getMutatorMethod(propName, type);
83                     if (meth != null) {
84                         logger.log(Level.FINER, "Invoking" + meth + " on "
85                                    + bean.getClass().getName() + "with " +
86                                    "value [" + prop.getResolvedValueObject().getClass()
87                                    + " , " + getFilteredPropValue(prop) +" ] ");
88                         meth.invoke(bean,new Object JavaDoc[] {prop.getResolvedValueObject()});
89                     } else {
90                         //log WARNING, deployment can continue.
91
logger.log(Level.WARNING, "rardeployment.no_setter_method",
92                                         new Object JavaDoc[]{prop.getName(), bean.getClass().getName()});
93                     }
94                 } catch (IllegalArgumentException JavaDoc ia) {
95                     logger.log(Level.FINE, "IllegalException while trying to set " +
96                               prop.getName() + " and value "+ getFilteredPropValue(prop),
97                               ia + " on an instance of " + bean.getClass() +
98                               " -- trying again with the type from bean");
99                     boolean prevBoundsChecking = EnvironmentProperty.isBoundsChecking();
100                     try {
101                         EnvironmentProperty.setBoundsChecking(false);
102                         prop.setType(type.getName());
103                         logger.log(Level.FINE, "2nd try :: Invoking" + meth + " on "
104                                         + bean.getClass().getName() + "with value ["
105                                         + prop.getResolvedValueObject().getClass()
106                                         + " , " + getFilteredPropValue(prop) +" ] ");
107                         meth.invoke(bean,new Object JavaDoc[] {prop.getResolvedValueObject()});
108                     } catch (Exception JavaDoc e) {
109                         handleException(e, prop, bean);
110                     } finally {
111                         //restore boundsChecking
112
EnvironmentProperty.setBoundsChecking(prevBoundsChecking);
113                     }
114                 } catch (Exception JavaDoc ex) {
115                     handleException(ex, prop, bean);
116                 }
117         }
118         }
119         return null;
120     }
121
122     private void handleException(Exception JavaDoc ex, EnvironmentProperty prop, Object JavaDoc bean) throws ConnectorRuntimeException {
123         logger.log(Level.WARNING, "rardeployment.exception_on_invoke_setter",
124             new Object JavaDoc[]{prop.getName(), getFilteredPropValue(prop),
125             ex.getMessage()});
126         logger.log(Level.FINE, "Exception while trying to set " +
127             prop.getName() + " and value "+ getFilteredPropValue(prop),
128             ex + " on an instance of " + bean.getClass());
129         throw (ConnectorRuntimeException)
130         (new ConnectorRuntimeException(ex.getMessage()).initCause(ex));
131     }
132
133     private static String JavaDoc getFilteredPropValue(EnvironmentProperty prop){
134         if (prop == null)
135             return "null";
136         
137     String JavaDoc propname = prop.getName();
138     if (propname.toLowerCase().contains("password"))
139          return "********";
140         
141         return (prop.getResolvedValue());
142     }
143
144
145     /**
146      * Retrieves the appropriate setter method in the resurce adapter java bean
147      * class
148      */

149     private Method JavaDoc getMutatorMethod(String JavaDoc propertyName, Class JavaDoc type){
150         String JavaDoc setterMethodName = "set" + getCamelCasedPropertyName(propertyName);
151         Method JavaDoc m = null;
152
153         //Get all setter methods for property
154
Method JavaDoc[] setterMethods = findMethod(setterMethodName);
155         
156         if (setterMethods.length == 1) {
157             //Only one setter method for this property
158
m = (Method JavaDoc)setterMethods[0];
159         } else {
160             //When more than one setter for the property, do type
161
//checking to determine property
162
//This check is very important, because the resource
163
//adapter java-bean's methods might be overridden and calling
164
//set over the wrong method will result in an exception
165
for (int i =0; i < setterMethods.length; i++) {
166                 Class JavaDoc[] paramTypes = setterMethods[i].getParameterTypes();
167                 if(paramTypes.length > 0) {
168                     if (paramTypes[0].equals(type) && paramTypes.length == 1){
169                         logger.log(Level.FINER, "Method " + methods[i] +
170                                         "matches with the right arg type");
171                         m = setterMethods[i];
172                     }
173                 }
174            }
175         }
176         
177         if ( m != null ) {
178             return m;
179         } else {
180             logger.log(Level.WARNING, "no.such.method",
181                   new Object JavaDoc[] {setterMethodName, bean.getClass().getName()});
182             return null;
183         }
184     }
185
186     /**
187      * Use a property's accessor method in the resource adapter
188      * javabean to get the Type of the property
189      *
190      * This helps in ensuring that the type as coded in the java-bean
191      * is used while setting values on a java-bean instance,
192      * rather than on the values specified in ra.xml
193      */

194     private Class JavaDoc getTypeOf(EnvironmentProperty prop) {
195         String JavaDoc name = prop.getName();
196         Method JavaDoc accessorMeth = getAccessorMethod(name);
197         if (accessorMeth != null ) {
198             return accessorMeth.getReturnType();
199         }
200         //not having a getter is not a WARNING.
201
logger.log(Level.FINE, "method.name.nogetterforproperty",
202                 new Object JavaDoc[] {prop.getName(), bean.getClass()});
203         return null;
204     }
205     
206     /**
207      * Gets the accessor method for a property
208      */

209     private Method JavaDoc getAccessorMethod(String JavaDoc propertyName){
210         String JavaDoc getterName = "get" + getCamelCasedPropertyName(propertyName);
211         Method JavaDoc[] getterMethods = findMethod(getterName);
212         if (getterMethods.length > 0) {
213             return getterMethods[0];
214         } else {
215             return null;
216         }
217     }
218     
219     /**
220      * Finds methods in the resource adapter java bean class with the same name
221      * RA developers could inadvertently not camelCase getters and/or setters
222      * and this implementation of findMethod returns both camelCased and non-Camel
223      * cased methods.
224      */

225     private Method JavaDoc[] findMethod(String JavaDoc methodName) {
226         ArrayList JavaDoc matchedMethods = new ArrayList JavaDoc();
227         
228         //check for CamelCased Method(s)
229
for (int i = 0; i < this.methods.length; i++) {
230             if (methods[i].getName().equals(methodName)){
231                 matchedMethods.add(methods[i]);
232             }
233         }
234         
235         //check for nonCamelCased Method(s)
236
for (int i = 0; i < this.methods.length; i++) {
237             if (methods[i].getName().equalsIgnoreCase(methodName)){
238                 matchedMethods.add(methods[i]);
239             }
240         }
241         return (Method JavaDoc[])matchedMethods.toArray(new Method JavaDoc[]{});
242     }
243     
244     /**
245      * Returns camel-cased version of a propertyName. Used to construct
246      * correct accessor and mutator method names for a give property.
247      */

248     private String JavaDoc getCamelCasedPropertyName(String JavaDoc propertyName){
249         return propertyName.substring(0,1).toUpperCase() +
250         propertyName.substring(1);
251     }
252     
253 }
254
Popular Tags