KickJava   Java API By Example, From Geeks To Geeks.

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


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

24
25 package com.sun.enterprise.connectors.util;
26
27 import com.sun.enterprise.deployment.*;
28 import com.sun.enterprise.config.serverbeans.ElementProperty;
29 import com.sun.enterprise.connectors.*;
30 import com.sun.enterprise.util.*;
31 import com.sun.logging.LogDomains;
32 import java.util.logging.*;
33 import java.util.*;
34 import java.lang.*;
35 import java.lang.reflect.*;
36 import java.io.IOException JavaDoc;
37 import org.xml.sax.SAXParseException JavaDoc;
38 import javax.resource.spi.ResourceAdapter JavaDoc;
39 import javax.resource.spi.ResourceAdapterAssociation JavaDoc;
40
41
42 /**
43  * This is an util class containing methods for parsing connector
44  * configurations present in ra.xml.
45  *
46  * @author Srikanth P
47  *
48  */

49
50 public class ConnectorConfigParserUtils {
51
52     static Logger _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER);
53
54     /**
55      * Default constructor.
56      *
57      */

58
59     public ConnectorConfigParserUtils() {
60
61     }
62
63     /**
64      * Merges the properties obtained by introspecting the javabean and the
65      * properties present in ra.xml for the corresponding javabean.
66      *
67      * @param ddVals Properties obtained from ra.xml for the javabean
68      * @param introspectedVals Properties obtained by introspecting javabean
69      * @return Merged Properties present in ra.xml and introspected properties
70      * of javabean.
71      *
72      */

73
74     public Properties mergeProps(Set ddVals,
75                                  Properties introspectedVals)
76     {
77         Properties mergedVals = new Properties(introspectedVals);
78
79         if(ddVals != null) {
80             Object JavaDoc[] ddProps = ddVals.toArray();
81
82             String JavaDoc name = null;
83             String JavaDoc value = null;
84             for (int i = 0; i < ddProps.length; i++) {
85                 name = ((EnvironmentProperty)ddProps[i]).getName();
86                 value =((EnvironmentProperty)ddProps[i]).getValue();
87                 mergedVals.setProperty(name,value);
88             }
89         }
90
91         return mergedVals;
92     }
93
94     /**
95      * Merges the datatype of properties obtained by introspecting the
96      * javabean and the datatypes of properties present in ra.xml for
97      * the corresponding javabean. It is a Properties object consisting of
98      * property name and the property data type.
99      *
100      * @param ddVals Properties obtained from ra.xml for the javabean
101      * @param introspectedVals Properties obtained by
102      * introspecting javabean which consist of property name as key
103      * and datatype as the value.
104      * @return Merged Properties present in ra.xml and introspected properties
105      * of javabean. Properties consist of property name as the key
106      * and datatype as the value.
107      *
108      */

109
110     public Properties mergePropsReturnTypes(Set ddVals,
111                                  Properties introspectedVals)
112     {
113         Properties mergedVals = new Properties(introspectedVals);
114
115         if(ddVals != null) {
116             Object JavaDoc[] ddProps = ddVals.toArray();
117
118             String JavaDoc name = null;
119             String JavaDoc value = null;
120             for (int i = 0; i < ddProps.length; i++) {
121                 name = ((EnvironmentProperty)ddProps[i]).getName();
122                 value = ((EnvironmentProperty)ddProps[i]).getType();
123                 mergedVals.setProperty(name,value);
124             }
125         }
126
127         return mergedVals;
128     }
129     
130     public Properties introspectJavaBean(String JavaDoc className, Set ddPropsSet)
131                             throws ConnectorRuntimeException {
132         return introspectJavaBean(className, ddPropsSet, false, null);
133     }
134     
135     public Properties introspectJavaBean(String JavaDoc className, Set ddPropsSet,
136                     boolean associateResourceAdapter, String JavaDoc resourceAdapterName)
137                        throws ConnectorRuntimeException {
138         Class JavaDoc loadedClass = loadClass(className);
139         Object JavaDoc loadedInstance = instantiate(loadedClass);
140         try {
141             if (associateResourceAdapter) {
142                 ActiveResourceAdapter activeRA = ConnectorRegistry.getInstance().
143                                   getActiveResourceAdapter(resourceAdapterName);
144                 if (activeRA == null) {
145                     //Check and Load RAR
146
(new ConnectorServiceImpl()).loadDeferredResourceAdapter(
147                                                       resourceAdapterName);
148                     activeRA = ConnectorRegistry.getInstance().
149                                   getActiveResourceAdapter(resourceAdapterName);
150                 }
151                 
152                 //Associate RAR
153
if (activeRA instanceof ActiveInboundResourceAdapter) {
154                     ResourceAdapter JavaDoc raInstance =
155                         ((ActiveInboundResourceAdapter)(activeRA)).getResourceAdapter();
156                     if (loadedInstance instanceof ResourceAdapterAssociation JavaDoc) {
157                         ((ResourceAdapterAssociation JavaDoc)loadedInstance).
158                                              setResourceAdapter(raInstance);
159                     }
160                 }
161             }
162         } catch (Exception JavaDoc e) {
163             _logger.log(Level.WARNING,
164                             "rardeployment.error_associating_ra",e);
165             _logger.log(Level.FINE,
166                             "Exception while associating the resource adapter" +
167                             "to the JavaBean",e);
168         }
169         return introspectJavaBean(loadedInstance, ddPropsSet);
170     }
171     
172     
173
174     /**
175      * Introspects the javabean and returns only the introspected properties
176      * not present in the configuration in ra.xml for the corresponding
177      * javabean. If no definite value is obtained while introspection of
178      * a method empty string is taken as the value.
179      *
180      * @param className Name of the class to be introspected.
181      * @param ddProps Set of Properties present in configuration in ra.xml for
182      * the corresponding javabean.
183      * @return Introspected properties not present in the configuration in
184      * ra.xml for the corresponding javabean.
185      * @throws ConnectorRuntimeException if the Class could not be loaded
186      * or instantiated.
187      */

188
189     public Properties introspectJavaBean(
190         Object JavaDoc javaBeanInstance ,Set ddPropsSet) throws ConnectorRuntimeException
191     {
192         Class JavaDoc loadedClass = javaBeanInstance.getClass();
193
194         Method[] methods = loadedClass.getMethods();
195         if(methods == null) {
196             return null;
197         }
198         Properties props = new Properties();
199         String JavaDoc name = null;
200         String JavaDoc value = null;
201         Object JavaDoc[] ddProps = null;
202         if(ddPropsSet != null) {
203             ddProps = ddPropsSet.toArray();
204         }
205
206         for(int i=0; i<methods.length;++i) {
207             _logger.fine("Method -> " + methods[i].getName() + ":" + methods[i].getReturnType());
208             if(isProperty(methods[i]) && !presentInDDProps(methods[i],ddProps)
209                                       && isValid(methods[i], loadedClass)) {
210                 name = getPropName(methods[i]);
211                 value = getPropValue(methods[i], loadedClass, javaBeanInstance);
212                 props.setProperty(name,value);
213             }
214         }
215         return props;
216     }
217
218     /**
219      * Introspects the javabean and returns only the introspected properties
220      * and their datatypes not present in the configuration in ra.xml for
221      * the corresponding javabean.
222      *
223      * @param className Name of the class to be introspected.
224      * @param ddProps Set of Properties present in configuration in ra.xml for
225      * the corresponding javabean.
226      * @return Introspected properties and their datatype not present in the
227      * configuration in ra.xml for the corresponding javabean. The
228      * properties consist of property name as the key and datatype as
229      * the value
230      * @throws ConnectorRuntimeException if the Class could not be loaded
231      * or instantiated.
232      */

233
234     public Properties introspectJavaBeanReturnTypes(
235         String JavaDoc className,Set ddPropsSet) throws ConnectorRuntimeException
236     {
237
238         Class JavaDoc loadedClass = loadClass(className);
239         Object JavaDoc loadedInstance = instantiate(loadedClass);
240         Method[] methods = loadedClass.getMethods();
241         if(methods == null) {
242             return null;
243         }
244         Properties props = new Properties();
245         String JavaDoc name = null;
246         String JavaDoc value = null;
247         Object JavaDoc[] ddProps = null;
248         if(ddPropsSet != null) {
249             ddProps = ddPropsSet.toArray();
250         }
251
252         for(int i=0; i<methods.length;++i) {
253             if(isProperty(methods[i])&&!presentInDDProps(methods[i],ddProps)) {
254                 name = getPropName(methods[i]);
255                 value = getPropType(methods[i]);
256                 if(value != null) {
257                     props.setProperty(name,value);
258                 }
259             }
260         }
261         return props;
262     }
263     /**
264      * Checks whether the property pertaining to the method is already presenti
265      * in the array of Properties passed as second argument.
266      * The properties already present in ra.xml for the corresponding
267      * javabean is passed as the second argument.
268      */

269
270     private boolean presentInDDProps(Method method,Object JavaDoc[] ddProps) {
271
272         String JavaDoc name = null;
273         String JavaDoc ddPropName = null;
274         int length = "set".length();
275         if(method != null) {
276             name = method.getName().substring(length);
277         }
278         for(int i=0; name != null && ddProps != null && i<ddProps.length;++i) {
279             ddPropName = ((EnvironmentProperty)ddProps[i]).getName();
280             if(name.equalsIgnoreCase(ddPropName) == true) {
281                 return true;
282             }
283         }
284         return false;
285     }
286
287     /**
288      * Checks whether the property is valid or not.
289      */

290     private boolean isValid(Method setMethod, Class JavaDoc loadedClass) {
291         Method getMethod = correspondingGetMethod( setMethod, loadedClass);
292         if (getMethod != null) {
293             return RARUtils.isValidRABeanConfigProperty(getMethod.getReturnType());
294         } else {
295             return false;
296         }
297     }
298
299     /**
300      * Checks whether the method pertains to a valid javabean property.
301      * i.e it check whether the method starts with "set" and it has only
302      * one parameter. It more than one parameter is present it is taken as
303      * not a property
304      *
305      */

306
307     private boolean isProperty(Method method) {
308
309         if(method == null) {
310             return false;
311         }
312         String JavaDoc methodName = method.getName();
313         Class JavaDoc[] parameterTypes = method.getParameterTypes();
314         if(methodName.startsWith("set") &&
315            parameterTypes != null && parameterTypes.length == 1) {
316             return true;
317         } else {
318             return false;
319         }
320     }
321
322     /**
323      * Gets the property name of the method passed. It strips the first three
324      * charaters (size of "set") of the method name and converts the first
325      * character (for the string after stripping) to upper case and returns
326      * that string.
327      *
328      */

329      
330     private String JavaDoc getPropName(Method method) {
331
332         if(method == null) {
333             return null;
334         }
335         String JavaDoc methodName = method.getName();
336         int length = "set".length();
337         String JavaDoc retValue =
338             methodName.substring(length,length+1).toUpperCase() +
339             methodName.substring(length+1);
340         return retValue;
341     }
342
343     /**
344      * Returns the getXXX() or isXXX() for the setXXX method passed.
345      * XXX is the javabean property.
346      * Check is made if there are no parameters for the getXXX() and isXXX()
347      * methods. If there is any parameter, null is returned.
348      */

349
350     private Method correspondingGetMethod(Method setMethod,
351                                           Class JavaDoc loadedClass) {
352
353         Method[] allMethods = loadedClass.getMethods();
354         if(allMethods == null) {
355             return null;
356         }
357         int length = "set".length();
358         String JavaDoc methodName = setMethod.getName();
359         Class JavaDoc[] parameterTypes = null;
360         String JavaDoc[] possibleGetMethodNames = new String JavaDoc[2];
361         possibleGetMethodNames[0] = "is"+methodName.substring(length);
362         possibleGetMethodNames[1] = "get"+methodName.substring(length);
363
364         for(int i = 0;i < allMethods.length;++i) {
365             if(allMethods[i].getName().equals(possibleGetMethodNames[0]) ||
366                allMethods[i].getName().equals(possibleGetMethodNames[1])) {
367                 parameterTypes = allMethods[i].getParameterTypes();
368                 if(parameterTypes != null && parameterTypes.length == 0) {
369                     return allMethods[i];
370                 }
371             }
372         }
373         return null;
374     }
375
376     /**
377      * Invokes the method passed and returns the value obtained. If method
378      * invocation fails empty string is returned. If the return type is not
379      * of Wrapper class of the primitive types, empty string is returned.
380      */

381
382     private String JavaDoc getPropValue(Method method,
383                    Class JavaDoc loadedClass, Object JavaDoc loadedInstance) {
384
385         Object JavaDoc retValue = null;
386         Method getMethod = correspondingGetMethod(method, loadedClass);
387
388         if(getMethod != null) {
389             try {
390                 retValue = getMethod.invoke(loadedInstance, (java.lang.Object JavaDoc[])null);
391             } catch (IllegalAccessException JavaDoc ie) {
392                 _logger.log(Level.FINE,
393                      "rardeployment.illegalaccess_error",loadedClass.getName());
394             } catch (InvocationTargetException ie) {
395                 _logger.log(Level.FINE,
396                      "Failed to invoke the method",loadedClass.getName());
397             }
398         }
399         return convertToString(retValue);
400     }
401
402     private String JavaDoc getPropType(Method method) {
403
404         Class JavaDoc[] parameterTypeClass = method.getParameterTypes();
405         if(parameterTypeClass.length != 1) {
406             return null;
407         }
408         if(parameterTypeClass[0].isPrimitive() ||
409                   parameterTypeClass[0].getName().equals("java.lang.String")) {
410             return parameterTypeClass[0].getName();
411         } else {
412             return null;
413         }
414     }
415
416     /**
417      * Converts the object to String if it belongs to Wrapper class of primitive
418      * type or a string itself. For all other types empty String is returned.
419      */

420
421     private String JavaDoc convertToString(Object JavaDoc obj) {
422         if(obj == null) {
423             return new String JavaDoc();
424         }
425
426         if(obj instanceof String JavaDoc) {
427             return (String JavaDoc)obj;
428         }else if( obj instanceof Integer JavaDoc ||
429               obj instanceof Float JavaDoc ||
430               obj instanceof Long JavaDoc ||
431               obj instanceof Double JavaDoc ||
432               obj instanceof Character JavaDoc ||
433               obj instanceof Boolean JavaDoc ||
434               obj instanceof Byte JavaDoc ||
435               obj instanceof Short JavaDoc ) {
436             return String.valueOf(obj);
437         } else {
438             return new String JavaDoc();
439         }
440     }
441   
442
443     /**
444      * Loads and instantiates the class
445      * Throws ConnectorRuntimeException if loading or instantiation fails.
446      */

447
448     private Class JavaDoc loadClass(String JavaDoc className)
449                    throws ConnectorRuntimeException
450     {
451         ClassLoader JavaDoc classLoader = ConnectorClassLoader.getInstance();
452         try {
453             return classLoader.loadClass(className);
454         } catch(ClassNotFoundException JavaDoc ce) {
455             _logger.log(Level.FINE,
456                  "rardeployment.class_not_found",className);
457             throw new ConnectorRuntimeException(
458                     "ClassNot Found : " + className);
459         }
460     }
461     /**
462      * Instantiates the class
463      */

464
465     private Object JavaDoc instantiate(Class JavaDoc loadedClass)
466                    throws ConnectorRuntimeException
467     {
468         try {
469             return loadedClass.newInstance();
470         } catch(InstantiationException JavaDoc ie) {
471             _logger.log(Level.FINE,
472                  "rardeployment.class_instantiation_error",loadedClass.getName());
473             throw new ConnectorRuntimeException(
474                      "Could not instantiate class : " + loadedClass.getName());
475         } catch (IllegalAccessException JavaDoc ie) {
476             _logger.log(Level.FINE,
477                  "rardeployment.illegalaccess_error",loadedClass.getName());
478             throw new ConnectorRuntimeException(
479                        "Couldnot access class : "+loadedClass.getName());
480         }
481     }
482 }
483
Popular Tags