KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import com.sun.logging.LogDomains;
35 import java.util.logging.*;
36
37 /**
38  * A simple class to get the properties of a ConnectionDefinition class , that
39  * could be overridden by the administrator during deployment.
40  *
41  * @author Sivakumar Thyagarjan
42  */

43 public class ConnectionDefinitionUtils {
44
45     static Logger _logger= LogDomains.getLogger(LogDomains.RSR_LOGGER);
46
47     /**
48      * Gets the properties of the Java bean connection definition class that
49      * have setter methods defined
50      *
51      * @param connectionDefinitionClassName
52      * The Connection Definition Java bean class for which
53      * overrideable properties are required.
54      * @return A Set of properties that have a setter method defined in the
55      * Connection Definition class
56      */

57     public static Set JavaDoc getConnectionDefinitionProperties(String JavaDoc connectionDefinitionClassName) {
58         HashSet JavaDoc propertySet= new HashSet JavaDoc();
59         try {
60             Method JavaDoc[] methods=
61                 Thread.currentThread().getContextClassLoader().loadClass(connectionDefinitionClassName).getMethods();
62             for (int i= 0; i < methods.length; i++) {
63                 //Method starts with "set" and has only one parameter and has
64
// a
65
// valid argument
66
if (isValidSetterMethod(methods[i])) {
67                     String JavaDoc name= methods[i].getName();
68                     String JavaDoc propertyName=
69                         name.substring(
70                             (name.indexOf("set") + "set".length()),
71                             name.length());
72                     propertySet.add(propertyName);
73                 }
74             }
75         } catch (SecurityException JavaDoc e) {
76             handleException(e);
77         } catch (ClassNotFoundException JavaDoc e) {
78             handleException(e);
79         }
80         return propertySet;
81     }
82
83     private static boolean isValidSetterMethod(Method JavaDoc method) {
84         return (
85             (method.getName().startsWith("set"))
86                 && (method.getParameterTypes().length == 1)
87                 && (isValidArgumentType(method)));
88     }
89
90     private static boolean isValidArgumentType(Method JavaDoc method) {
91         Class JavaDoc[] parameters= method.getParameterTypes();
92         boolean isValid= true;
93         for (int i= 0; i < parameters.length; i++) {
94             Class JavaDoc param= parameters[i];
95             if (!(param.isPrimitive() || param.equals(String JavaDoc.class)))
96                 return false;
97         }
98         return isValid;
99     }
100
101     /**
102      * Gets the properties of the Java bean connection definition class that
103      * have setter methods defined and the default values as provided by the
104      * Connection Definition java bean developer.
105      *
106      * @param connectionDefinitionClassName
107      * The Connection Definition Java bean class for which
108      * overrideable properties are required.
109      * @return Map [property, defaultValue]
110      */

111     public static Map JavaDoc getConnectionDefinitionPropertiesAndDefaults(String JavaDoc connectionDefinitionClassName) {
112         Set JavaDoc s= getConnectionDefinitionProperties(connectionDefinitionClassName);
113         HashMap JavaDoc hm= new HashMap JavaDoc();
114         Class JavaDoc connectionDefinitionClass;
115         try {
116             connectionDefinitionClass=
117                 Thread.currentThread().getContextClassLoader().loadClass(connectionDefinitionClassName);
118             Object JavaDoc obj= connectionDefinitionClass.newInstance();
119             for (Iterator JavaDoc iter= s.iterator(); iter.hasNext();) {
120                 String JavaDoc property= (String JavaDoc) iter.next();
121                 Object JavaDoc defaultVal= null;
122                 try {
123                     Method JavaDoc m=
124                         connectionDefinitionClass.getMethod(
125                             "get" + property,
126                             new Class JavaDoc[] {});
127                     defaultVal= m.invoke(obj, new Object JavaDoc[] {});
128                     //ignore these exceptions. Some drivers have a setter but
129
// no getters for properties [example the password property
130
// in the OracleDataSource
131
} catch (NoSuchMethodException JavaDoc e) {
132                     //ignore
133
} catch (IllegalArgumentException JavaDoc e) {
134                     //ignore
135
} catch (InvocationTargetException JavaDoc e) {
136                     //ignore
137
}
138                 //If the property does not have a corresponding getter method,
139
// a null is placed as the default value.
140
hm.put(property, defaultVal);
141             }
142         } catch (ClassNotFoundException JavaDoc e) {
143             handleException(e);
144         } catch (InstantiationException JavaDoc e) {
145             handleException(e);
146         } catch (IllegalAccessException JavaDoc e) {
147             handleException(e);
148         } catch (SecurityException JavaDoc e) {
149             handleException(e);
150         }
151         return hm;
152     }
153
154     private static void handleException(Exception JavaDoc ex) {
155         ex.printStackTrace();
156         _logger.log(
157             Level.SEVERE,
158             "Exception while trying to find properties ",
159             ex.getMessage());
160     }
161
162     //test code
163
public static void main(String JavaDoc[] args) {
164
165         //oracle.jdbc.xa.client.OracleXADataSource
166
//com.pointbase.jdbc.jdbcDataSource
167
Map JavaDoc m=
168             ConnectionDefinitionUtils
169                 .getConnectionDefinitionPropertiesAndDefaults(
170                 "sun.jdbc.odbc.ee.DataSource");
171
172         for (Iterator JavaDoc iter= m.keySet().iterator(); iter.hasNext();) {
173             String JavaDoc element= (String JavaDoc) iter.next();
174             System.out.println(element + " : " + m.get(element));
175         }
176     }
177 }
178
Popular Tags