KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jdbc > Utility


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: Utility.java,v 1.6 2005/04/28 08:43:24 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.jdbc;
26
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import javax.resource.ResourceException JavaDoc;
34 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
35 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
36 import javax.resource.spi.SecurityException JavaDoc;
37 import javax.resource.spi.security.PasswordCredential JavaDoc;
38 import javax.security.auth.Subject JavaDoc;
39
40 import org.objectweb.util.monolog.api.BasicLevel;
41 import org.objectweb.util.monolog.api.Logger;
42
43 /**
44  * <p>This class contains the support methods for the Resource Adapter.
45  * @author Eric Hardesty
46  */

47 public class Utility {
48
49     static synchronized Object JavaDoc getDataSource(ManagedConnectionFactoryImpl mcf, PasswordCredential JavaDoc pc, Logger trace)
50             throws ResourceException JavaDoc {
51
52         MCFData prop = mcf.mcfData;
53
54         if (prop.getMCFData(MCFData.DSCLASS) == null) {
55             ResourceException JavaDoc re = new ResourceException JavaDoc("A DataSource (dsClass) value must be specified");
56             trace.log(BasicLevel.INFO, re.getMessage());
57         }
58         String JavaDoc clsName = prop.getMCFData(MCFData.DSCLASS);
59         Class JavaDoc dsClass = null;
60         Object JavaDoc dsObj = null;
61         try {
62             dsClass = Class.forName(clsName, true, Thread.currentThread().getContextClassLoader());
63             dsObj = dsClass.newInstance();
64             if (trace.isLoggable(BasicLevel.DEBUG)) {
65                 trace.log(BasicLevel.DEBUG, "dsClass(" + clsName + ") is " + dsObj);
66             }
67         } catch (ClassNotFoundException JavaDoc cnfe) {
68             throw new ResourceException JavaDoc("Class Name not found:" + clsName);
69         } catch (Exception JavaDoc ex) {
70             throw new ResourceException JavaDoc("Error in class: " + clsName + " " + ex.getMessage());
71         }
72
73         Object JavaDoc[] param = new Object JavaDoc[1];
74         String JavaDoc methodName = null;
75         String JavaDoc paramVal = null;
76         Method JavaDoc meth = null;
77         for (Enumeration JavaDoc e = prop.getProperties(); e.hasMoreElements();) {
78             int offset = Integer.parseInt((String JavaDoc) e.nextElement());
79             methodName = MCFData.dsMethodNames[offset];
80             if (!(methodName.equals("setDSClass") || methodName.equals("setDbSpecificMethods") || offset > MCFData.JONASOFFSET)) {
81                 try {
82                     /* Determine if a String method exist */
83                     paramVal = prop.getProperty("" + offset);
84                     if (trace.isLoggable(BasicLevel.DEBUG)) {
85                         trace.log(BasicLevel.DEBUG, "calling method " + methodName + " with String " + paramVal);
86                     }
87                     meth = dsClass.getMethod(methodName, new Class JavaDoc[] {String JavaDoc.class});
88                     param[0] = paramVal;
89                     meth.invoke(dsObj, param);
90                 } catch (NoSuchMethodException JavaDoc ns) {
91                     try {
92                         /* Valid case, determine if an Integer method exist */
93                         if (trace.isLoggable(BasicLevel.DEBUG)) {
94                             trace.log(BasicLevel.DEBUG, "calling method " + methodName + " with int " + paramVal);
95                         }
96                         meth = dsClass.getMethod(methodName, new Class JavaDoc[] {int.class});
97                         param[0] = new Integer JavaDoc(paramVal);
98                         meth.invoke(dsObj, param);
99                     } catch (NoSuchMethodException JavaDoc nsme) {
100                         // Valid case no String or Integer method continue thru
101
// remainder of properties
102
;
103                     } catch (NumberFormatException JavaDoc nfm) {
104                         // Valid case cannot convert to an Integer
105
;
106                     } catch (Exception JavaDoc ex0) {
107                         ex0.printStackTrace();
108                         throw new ResourceException JavaDoc("Error on method: " + methodName + " " + ex0.getMessage());
109                     }
110                 } catch (IllegalArgumentException JavaDoc iae) {
111                     ;
112                 } catch (Exception JavaDoc ex) {
113                     ex.printStackTrace();
114                     throw new ResourceException JavaDoc("Error on method: " + methodName + " " + ex.getMessage());
115                 }
116             }
117             if (methodName.equals("setDbSpecificMethods")) {
118                 Vector JavaDoc meths = new Vector JavaDoc();
119                 Vector JavaDoc methParams = new Vector JavaDoc();
120                 Vector JavaDoc methTypes = new Vector JavaDoc();
121                 try {
122                     Utility.parseValues(prop.getProperty("" + offset), meths, methParams, methTypes, trace);
123                 } catch (Exception JavaDoc ex1) {
124                     throw new ResourceException JavaDoc("Error parsing dbSpecificMethods: " + ex1.getMessage());
125                 }
126                 if (meths != null && meths.size() > 0) {
127                     for (int i = 0; i < meths.size(); i++) {
128                         try {
129                             /* Determine if a String method exist */
130                             methodName = (String JavaDoc) meths.elementAt(i);
131                             Class JavaDoc toPass = null;
132                             String JavaDoc curMethType = (String JavaDoc) methTypes.elementAt(i);
133                             if (curMethType.equalsIgnoreCase("String")) {
134                                 toPass = String JavaDoc.class;
135                                 param[0] = (String JavaDoc) methParams.elementAt(i);
136                             } else if (curMethType.equalsIgnoreCase("Integer")) {
137                                 toPass = Integer JavaDoc.class;
138                                 param[0] = Integer.valueOf((String JavaDoc) methParams.elementAt(i));
139                             } else if (curMethType.equalsIgnoreCase("int")) {
140                                 toPass = int.class;
141                                 param[0] = Integer.valueOf((String JavaDoc) methParams.elementAt(i));
142                             } else if (curMethType.equals("Float")) {
143                                 toPass = Float JavaDoc.class;
144                                 param[0] = Float.valueOf((String JavaDoc) methParams.elementAt(i));
145                             } else if (curMethType.equals("float")) {
146                                 toPass = float.class;
147                                 param[0] = Float.valueOf((String JavaDoc) methParams.elementAt(i));
148                             } else if (curMethType.equals("Boolean")) {
149                                 toPass = Boolean JavaDoc.class;
150                                 param[0] = Boolean.valueOf((String JavaDoc) methParams.elementAt(i));
151                             } else if (curMethType.equals("boolean")) {
152                                 toPass = boolean.class;
153                                 param[0] = Boolean.valueOf((String JavaDoc) methParams.elementAt(i));
154                             } else if (curMethType.equalsIgnoreCase("Character")) {
155                                 toPass = Character JavaDoc.class;
156                                 param[0] = new Character JavaDoc(((String JavaDoc) methParams.elementAt(i)).charAt(0));
157                             } else if (curMethType.equalsIgnoreCase("char")) {
158                                 toPass = char.class;
159                                 param[0] = new Character JavaDoc(((String JavaDoc) methParams.elementAt(i)).charAt(0));
160                             } else if (curMethType.equals("Double")) {
161                                 toPass = Double JavaDoc.class;
162                                 param[0] = Double.valueOf((String JavaDoc) methParams.elementAt(i));
163                             } else if (curMethType.equals("double")) {
164                                 toPass = double.class;
165                                 param[0] = Double.valueOf((String JavaDoc) methParams.elementAt(i));
166                             } else if (curMethType.equals("Byte")) {
167                                 toPass = Byte JavaDoc.class;
168                                 param[0] = Byte.valueOf((String JavaDoc) methParams.elementAt(i));
169                             } else if (curMethType.equals("byte")) {
170                                 toPass = byte.class;
171                                 param[0] = Byte.valueOf((String JavaDoc) methParams.elementAt(i));
172                             } else if (curMethType.equals("Short")) {
173                                 toPass = Short JavaDoc.class;
174                                 param[0] = Short.valueOf((String JavaDoc) methParams.elementAt(i));
175                             } else if (curMethType.equals("short")) {
176                                 toPass = short.class;
177                                 param[0] = Short.valueOf((String JavaDoc) methParams.elementAt(i));
178                             } else if (curMethType.equals("Long")) {
179                                 toPass = Long JavaDoc.class;
180                                 param[0] = Long.valueOf((String JavaDoc) methParams.elementAt(i));
181                             } else if (curMethType.equals("long")) {
182                                 toPass = long.class;
183                                 param[0] = Long.valueOf((String JavaDoc) methParams.elementAt(i));
184                             }
185                             if (trace.isLoggable(BasicLevel.DEBUG)) {
186                                 trace.log(BasicLevel.DEBUG, "calling method " + methodName + " with " + param[0]);
187                             }
188                             meth = dsClass.getMethod(methodName, new Class JavaDoc[] {toPass});
189                             meth.invoke(dsObj, param);
190                         } catch (NoSuchMethodException JavaDoc ns) {
191                             throw new ResourceException JavaDoc("No such method: " + methodName + " " + ns.getMessage());
192                         } catch (Exception JavaDoc ex) {
193                             ex.printStackTrace();
194                             throw new ResourceException JavaDoc("Error on method: " + methodName + " " + ex.getMessage());
195                         }
196                     }
197                 }
198             }
199         }
200         if (pc != null) {
201             try {
202                 param[0] = pc.getUserName();
203                 meth = dsClass.getMethod("setUserName", new Class JavaDoc[] {Class.forName("String")});
204                 meth.invoke(dsObj, param);
205
206                 param[0] = new String JavaDoc(pc.getPassword());
207                 meth = dsClass.getMethod("setPassword", new Class JavaDoc[] {Class.forName("String")});
208                 meth.invoke(dsObj, param);
209             } catch (Exception JavaDoc ex) {
210                 throw new ResourceException JavaDoc("Error on method: " + methodName + " " + ex.getMessage());
211             }
212         }
213
214         if (trace.isLoggable(BasicLevel.DEBUG)) {
215             try {
216                 meth = dsClass.getMethod("getURL", (Class JavaDoc[]) null);
217                 trace.log(BasicLevel.DEBUG, "URL is " + meth.invoke(dsObj, (Object JavaDoc[]) null));
218             } catch (Exception JavaDoc e) {
219             }
220         }
221         return dsObj;
222
223     }
224
225     private static void parseValues(String JavaDoc val, Vector JavaDoc vMeth, Vector JavaDoc vValues, Vector JavaDoc vTypes, Logger trace)
226             throws Exception JavaDoc {
227         if (trace.isLoggable(BasicLevel.DEBUG)) {
228             trace.log(BasicLevel.DEBUG, "");
229         }
230         char delim = ':';
231         boolean done = false;
232         String JavaDoc methName = null;
233         int offset = 0;
234         boolean parsed = false;
235         String JavaDoc parseVal = val.trim();
236         String JavaDoc typeVal = "";
237         String JavaDoc valVal = "";
238
239         if (parseVal.length() == 0) {
240             return;
241         }
242         if (parseVal.startsWith(":")) {
243             delim = parseVal.charAt(1);
244             parseVal = parseVal.substring(2);
245         }
246         while (!parsed) {
247             offset = parseVal.indexOf('=');
248             if (offset < 0) {
249                 throw new Exception JavaDoc("Invalid value specified for dbSpecificMethods");
250             }
251             methName = parseVal.substring(0, offset);
252             vMeth.add(methName);
253             parseVal = parseVal.substring(offset + 1);
254             if (parseVal.charAt(0) == delim) {
255                 valVal = "";
256             } else {
257                 offset = parseVal.indexOf(delim);
258                 if (offset < 0) {
259                     valVal = parseVal;
260                     offset = valVal.length() - 1;
261                 } else {
262                     valVal = parseVal.substring(0, offset);
263                 }
264             }
265             vValues.add(valVal);
266             if (offset < 0) {
267                 parsed = true;
268             } else {
269                 parseVal = parseVal.substring(offset + 1);
270                 if (parseVal.length() == 0) {
271                     parsed = true;
272                 }
273             }
274             if (parseVal.startsWith("" + delim)) {
275                 parseVal = parseVal.substring(1);
276                 offset = parseVal.indexOf(delim);
277                 if (offset < 0) {
278                     typeVal = parseVal;
279                 } else {
280                     typeVal = parseVal.substring(0, offset);
281                 }
282                 vTypes.add(typeVal);
283                 if (offset < 0) {
284                     parsed = true;
285                 } else {
286                     parseVal = parseVal.substring(offset + 1);
287                     if (parseVal.length() == 0) {
288                         parsed = true;
289                     }
290                 }
291             } else {
292                 vTypes.add("String");
293             }
294             if (trace.isLoggable(BasicLevel.DEBUG)) {
295                 trace.log(BasicLevel.DEBUG, "Parsed: method(" + methName + ") value(" + valVal + ") type(" + typeVal + ")");
296             }
297         }
298     }
299
300     /**
301      * Returns the Password credential
302      * @param mcf ManagedConnectionFactory currently being used
303      * @param subject Subject associated with this call
304      * @param info ConnectionRequestInfo
305      * @return PasswordCredential for this user
306      */

307     static synchronized PasswordCredential JavaDoc getPasswordCredential(ManagedConnectionFactory JavaDoc mcf, Subject JavaDoc subject,
308             ConnectionRequestInfo JavaDoc info, java.io.PrintWriter JavaDoc out) throws ResourceException JavaDoc {
309
310         if (subject == null) {
311             if (info == null) {
312                 return null;
313             }
314             ConnectionRequestInfoImpl crii = (ConnectionRequestInfoImpl) info;
315             PasswordCredential JavaDoc pc = new PasswordCredential JavaDoc(crii.user, crii.password.toCharArray());
316             pc.setManagedConnectionFactory(mcf);
317             return pc;
318         }
319         Set JavaDoc cred = subject.getPrivateCredentials(PasswordCredential JavaDoc.class);
320         PasswordCredential JavaDoc pc = null;
321         for (Iterator JavaDoc iter = cred.iterator(); iter.hasNext();) {
322             PasswordCredential JavaDoc tmpPc = (PasswordCredential JavaDoc) iter.next();
323             if (tmpPc.getManagedConnectionFactory().equals(mcf)) {
324                 pc = tmpPc;
325                 break;
326             }
327         }
328         if (pc == null) {
329             SecurityException JavaDoc se = new SecurityException JavaDoc("No PasswordCredential found");
330             out.println("" + se);
331             throw se;
332         }
333         return pc;
334     }
335
336 }
Popular Tags