KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > gjc > common > DataSourceObjectBuilder


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.common;
25
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Vector JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import com.sun.gjc.util.MethodExecutor;
32 import javax.resource.ResourceException JavaDoc;
33
34 import com.sun.logging.*;
35 import java.util.logging.Logger JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import com.sun.enterprise.util.i18n.StringManager;
38
39 /**
40  * Utility class, which would create necessary Datasource object according to the
41  * specification.
42  *
43  * @version 1.0, 02/07/23
44  * @author Binod P.G
45  * @see com.sun.gjc.common.DataSourceSpec
46  * @see com.sun.gjc.util.MethodExcecutor
47  */

48 public class DataSourceObjectBuilder implements java.io.Serializable JavaDoc{
49
50     private DataSourceSpec spec;
51     
52     private Hashtable JavaDoc driverProperties = null;
53     
54     private MethodExecutor executor = null;
55     
56     private static Logger JavaDoc _logger;
57     static {
58         _logger = LogDomains.getLogger( LogDomains.RSR_LOGGER );
59     }
60     private boolean debug = false;
61
62     private StringManager sm = StringManager.getManager(
63         DataSourceObjectBuilder.class );
64     /**
65      * Construct a DataSource Object from the spec.
66      *
67      * @param spec <code> DataSourceSpec </code> object.
68      */

69     public DataSourceObjectBuilder(DataSourceSpec spec) {
70         this.spec = spec;
71         executor = new MethodExecutor();
72     }
73     
74     /**
75      * Construct the DataSource Object from the spec.
76      *
77      * @return Object constructed using the DataSourceSpec.
78      * @throws <code>ResourceException</code> if the class is not found or some issue in executing
79      * some method.
80      */

81     public Object JavaDoc constructDataSourceObject() throws ResourceException JavaDoc{
82         driverProperties = parseDriverProperties(spec);
83         Object JavaDoc dataSourceObject = getDataSourceObject();
84         Method JavaDoc[] methods = dataSourceObject.getClass().getMethods();
85         for (int i=0; i < methods.length; i++) {
86             String JavaDoc methodName = methods[i].getName();
87         //Check for driver properties first since some jdbc properties
88
//may be supported in form of driver properties
89
if (driverProperties.containsKey(methodName.toUpperCase())){
90             Vector JavaDoc values = (Vector JavaDoc) driverProperties.get(methodName.toUpperCase());
91             executor.runMethod(methods[i],dataSourceObject, values);
92         } else if (methodName.equalsIgnoreCase("setUser")){
93             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.USERNAME),methods[i],dataSourceObject);
94             
95         } else if (methodName.equalsIgnoreCase("setPassword")){
96             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.PASSWORD),methods[i],dataSourceObject);
97             
98         } else if (methodName.equalsIgnoreCase("setLoginTimeOut")){
99             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.LOGINTIMEOUT),methods[i],dataSourceObject);
100             
101         } else if (methodName.equalsIgnoreCase("setLogWriter")){
102             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.LOGWRITER),methods[i],dataSourceObject);
103             
104         } else if (methodName.equalsIgnoreCase("setDatabaseName")){
105             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.DATABASENAME),methods[i],dataSourceObject);
106             
107         } else if (methodName.equalsIgnoreCase("setDataSourceName")){
108             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.DATASOURCENAME),methods[i],dataSourceObject);
109             
110         } else if (methodName.equalsIgnoreCase("setDescription")){
111             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.DESCRIPTION),methods[i],dataSourceObject);
112             
113         } else if (methodName.equalsIgnoreCase("setNetworkProtocol")){
114             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.NETWORKPROTOCOL),methods[i],dataSourceObject);
115             
116         } else if (methodName.equalsIgnoreCase("setPortNumber")){
117             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.PORTNUMBER),methods[i],dataSourceObject);
118             
119         } else if (methodName.equalsIgnoreCase("setRoleName")){
120             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.ROLENAME),methods[i],dataSourceObject);
121             
122         } else if (methodName.equalsIgnoreCase("setServerName")){
123             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.SERVERNAME),methods[i],dataSourceObject);
124             
125         } else if (methodName.equalsIgnoreCase("setMaxStatements")){
126             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.MAXSTATEMENTS),methods[i],dataSourceObject);
127             
128         } else if (methodName.equalsIgnoreCase("setInitialPoolSize")){
129             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.INITIALPOOLSIZE),methods[i],dataSourceObject);
130             
131         } else if (methodName.equalsIgnoreCase("setMinPoolSize")){
132             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.MINPOOLSIZE),methods[i],dataSourceObject);
133             
134         } else if (methodName.equalsIgnoreCase("setMaxPoolSize")){
135             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.MAXPOOLSIZE),methods[i],dataSourceObject);
136             
137         } else if (methodName.equalsIgnoreCase("setMaxIdleTime")){
138             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.MAXIDLETIME),methods[i],dataSourceObject);
139             
140         } else if (methodName.equalsIgnoreCase("setPropertyCycle")){
141             executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.PROPERTYCYCLE),methods[i],dataSourceObject);
142             
143         }
144         }
145         return dataSourceObject;
146     }
147     
148     /**
149      * Get the extra driver properties from the DataSourceSpec object and
150      * parse them to a set of methodName and parameters. Prepare a hashtable
151      * containing these details and return.
152      *
153      * @param spec <code> DataSourceSpec </code> object.
154      * @return Hashtable containing method names and parameters,
155      * @throws ResourceException If delimiter is not provided and property string
156      * is not null.
157      */

158     private Hashtable JavaDoc parseDriverProperties(DataSourceSpec spec) throws ResourceException JavaDoc{
159         String JavaDoc delim = spec.getDetail(DataSourceSpec.DELIMITER);
160
161         String JavaDoc prop = spec.getDetail(DataSourceSpec.DRIVERPROPERTIES);
162     if ( prop == null || prop.trim().equals("")) {
163         return new Hashtable JavaDoc();
164     } else if (delim == null || delim.equals("")) {
165         String JavaDoc msg = sm.getString("dsob.delim_not_specified");
166         throw new ResourceException JavaDoc (msg);
167     }
168     
169     Hashtable JavaDoc properties = new Hashtable JavaDoc();
170     delim = delim.trim();
171     String JavaDoc sep = delim+delim;
172     int sepLen = sep.length();
173     String JavaDoc cache = prop;
174     Vector JavaDoc methods = new Vector JavaDoc();
175     
176     while (cache.indexOf(sep) != -1) {
177         int index = cache.indexOf(sep);
178         String JavaDoc name = cache.substring(0,index);
179         if (name.trim() != "") {
180             methods.add(name);
181             cache = cache.substring(index+sepLen);
182         }
183     }
184     
185         Enumeration JavaDoc allMethods = methods.elements();
186         while (allMethods.hasMoreElements()) {
187             String JavaDoc oneMethod = (String JavaDoc) allMethods.nextElement();
188             if (!oneMethod.trim().equals("")) {
189                 String JavaDoc methodName = null;
190                 Vector JavaDoc parms = new Vector JavaDoc();
191                 StringTokenizer JavaDoc methodDetails = new StringTokenizer JavaDoc(oneMethod,delim);
192         for (int i=0; methodDetails.hasMoreTokens();i++ ) {
193             String JavaDoc token = (String JavaDoc) methodDetails.nextToken();
194             if (i==0) {
195                 methodName = token.toUpperCase();
196             } else {
197                 parms.add(token);
198             }
199         }
200         properties.put(methodName,parms);
201             }
202         }
203         return properties;
204     }
205     
206     /**
207      * Creates a Datasource object according to the spec.
208      *
209      * @return Initial DataSource Object instance.
210      * @throws <code>ResourceException</code> If class name is wrong or classpath is not set
211      * properly.
212      */

213     private Object JavaDoc getDataSourceObject() throws ResourceException JavaDoc{
214         String JavaDoc className = spec.getDetail(DataSourceSpec.CLASSNAME);
215         try {
216             Class JavaDoc dataSourceClass = Thread.currentThread().getContextClassLoader().loadClass(className);
217             Object JavaDoc dataSourceObject = dataSourceClass.newInstance();
218             return dataSourceObject;
219         } catch(ClassNotFoundException JavaDoc cfne){
220         _logger.log(Level.SEVERE, "jdbc.exc_cnfe_ds", cfne);
221         String JavaDoc msg = sm.getString( "dsob.class_not_found", className);
222             throw new ResourceException JavaDoc(msg);
223         } catch(InstantiationException JavaDoc ce) {
224         _logger.log(Level.SEVERE, "jdbc.exc_inst", className);
225         String JavaDoc msg = sm.getString( "dsob.error_instantiating", className );
226             throw new ResourceException JavaDoc(msg);
227         } catch(IllegalAccessException JavaDoc ce) {
228         _logger.log(Level.SEVERE, "jdbc.exc_acc_inst", className);
229         String JavaDoc msg = sm.getString( "dsob.access_error", className );
230             throw new ResourceException JavaDoc(msg);
231         }
232     }
233   
234 }
235
Popular Tags