KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > repository > DataSourceParser


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 package com.sun.enterprise.repository;
24
25 import java.io.*;
26 import java.util.*;
27 import java.lang.reflect.*;
28 import com.sun.enterprise.util.LocalStringManagerImpl;
29 // IASRI 4660742 START
30
import java.util.logging.*;
31 import com.sun.logging.*;
32 // IASRI 4660742 END
33

34 /**
35  *********************************************************************
36  *********************************************************************
37  * NOTE : THIS CLASS IS NO LONGER USED. IT PARSES J2SDKEE 1.2.x
38  * STYLE PROPERTIES, WHICH ARE NOT SUPPORTED IN J2SDKEE 1.3.x.
39  * SEE com.sun.enterprise.repository.J2EEResourceCollection instead
40  *********************************************************************
41  *********************************************************************
42  *
43  * This class parses a set of properties value into a Hashtable of
44  * DataSource objects. The format of the properties is
45  *
46  * xadatasource.0.jndiname=<jndi name>
47  * xadatasource.0.classname=<class name>
48  * xadatasource.0.dbuser=<db user>
49  * xadatasource.0.dbpassword=<db password>
50  * xadatasource.0.prop.<prop1>=<value 1>
51  * xadatasource.0.prop.<prop2>=<value 2>
52  *
53  * xadatasource.1.jndiname=<jndi name>
54  * xadatasource.1.classname=<class name>
55  * xadatasource.1.prop.<prop1>=<value 1>
56  * xadatasource.1.prop.<prop2>=<value 2>
57  * xadatasource.1.dbuser=<db user>
58  * xadatasource.1.dbpassword=<db password>
59  *
60  * and so on
61  *
62  *
63  * For example,
64  *
65  * xadatasource.0.jndiname=bank/AccountDB
66  * xadatasource.0.classname=com.oracle.driver.XADataSource
67  * xadatasource.0.dbuser=sa
68  * xadatasource.0.dbpassword=sa
69  * xadatasource.0.prop.host=xyz
70  * xadatasource.0.prop.port=123
71  *
72  */

73 public class DataSourceParser {
74
75 // IASRI 4660742 START
76
private static Logger _logger=null;
77     static{
78        _logger=LogDomains.getLogger(LogDomains.ROOT_LOGGER);
79         }
80 // IASRI 4660742 END
81
static private final String JavaDoc DS = "xadatasource";
82     static private final String JavaDoc JNDINAME = "jndiname";
83     static private final String JavaDoc CLASSNAME = "classname";
84     static private final String JavaDoc PROP = "prop";
85     static private final String JavaDoc DBUSER = "dbuser";
86     static private final String JavaDoc DBPASSWORD = "dbpassword";
87
88     static private LocalStringManagerImpl localStrings =
89         new LocalStringManagerImpl(DataSourceParser.class);
90
91 /*********************************************************************
92  *********************************************************************
93  * NOTE : THIS CLASS IS NO LONGER USED. IT PARSES J2SDKEE 1.2.x
94  * STYLE PROPERTIES, WHICH ARE NOT SUPPORTED IN J2SDKEE 1.3.x.
95  * SEE com.sun.enterprise.repository.J2EEResourceCollection instead
96  *********************************************************************
97  *********************************************************************/

98
99     private DataSourceParser() {
100     }
101
102     /**
103      * Parse a property value
104      *
105      * @param prop The property value
106      *
107      * @return Vector DataSourceInfo objects
108      *
109      * @exception DataSourceParserException Thrown if there is an error
110      * in parsing
111      */

112     public Vector parseProperties(Properties props) {
113
114         Vector list = new Vector();
115         int count = 0;
116         while (true) {
117             String JavaDoc jndiName =
118                 props.getProperty(DS + "." + count + "." + JNDINAME);
119             String JavaDoc className =
120                 props.getProperty(DS + "." + count + "." + CLASSNAME);
121             String JavaDoc dbUser =
122                 props.getProperty(DS + "." + count + "." + DBUSER);
123             String JavaDoc dbPassword =
124                 props.getProperty(DS + "." + count + "." + DBPASSWORD);
125             if (jndiName == null || className == null) break;
126             try {
127                 DataSourceInfo dsi = new DataSourceInfo();
128                 dsi.jndiName = jndiName;
129                 dsi.dbUser = dbUser;
130                 dsi.dbPassword = dbPassword;
131                 Object JavaDoc obj = Class.forName(className).newInstance();
132                 dsi.dataSource = obj;
133                 list.addElement(dsi);
134             } catch (Exception JavaDoc ex) {
135                 Object JavaDoc[] args = {className};
136 //IASRI 4660742 System.err.println
137
//IASRI 4660742 (localStrings.getLocalString("enterprise.repository.init.class.err", "", args));
138
//IASRI 4660742 System.err.println("");
139
// START OF IASRI 4660742
140
_logger.log(Level.SEVERE,"enterprise.repository_init_class_err", args);
141 // END OF IASRI 4660742
142
}
143             count++;
144         }
145         
146         // set the properties
147
Enumeration e = props.keys();
148         while (e.hasMoreElements()) {
149             String JavaDoc key = (String JavaDoc) e.nextElement();
150             String JavaDoc val = props.getProperty(key);
151             if (key.startsWith(DS) && key.indexOf(PROP) != -1) {
152                 try {
153                     String JavaDoc s = key.substring(DS.length()+1,
154                                              key.indexOf(".",
155                                                          DS.length()+1));
156                     int idx = Integer.parseInt(s);
157                     String JavaDoc prop = key.substring(key.lastIndexOf(PROP + ".") +
158                                                 PROP.length() + 1);
159                     DataSourceInfo dsi = (DataSourceInfo) list.elementAt(idx);
160                     Object JavaDoc obj = dsi.dataSource;
161                     invokeSetMethod(obj, prop, val);
162                 } catch (Exception JavaDoc ex) {
163                     Object JavaDoc[] args = {key, val};
164 //IASRI 4660742 System.err.println
165
//IASRI 4660742 (localStrings.getLocalString("enterprise.repository.prop.err", "", args));
166
//IASRI 4660742 System.err.println("");
167
// START OF IASRI 4660742
168
_logger.log(Level.SEVERE,"enterprise.repository_prop_err", args);
169 // END OF IASRI 4660742
170
}
171             }
172         }
173         return list;
174     }
175
176     private void invokeSetMethod(Object JavaDoc obj, String JavaDoc prop, String JavaDoc value)
177         throws NoSuchMethodException JavaDoc, InvocationTargetException,
178         IllegalAccessException JavaDoc
179     {
180         Class JavaDoc cl = obj.getClass();
181         // change first letter to uppercase
182
String JavaDoc setMeth = "set" + prop.substring(0,1).toUpperCase() +
183             prop.substring(1);
184
185         // try string method
186
try {
187             Class JavaDoc[] cldef = {String JavaDoc.class};
188             Method meth = cl.getMethod(setMeth, cldef);
189             Object JavaDoc[] params = {value};
190             meth.invoke(obj, params);
191             return;
192         } catch (NoSuchMethodException JavaDoc ex) {
193             // try int method
194
Class JavaDoc[] cldef = {Integer.TYPE};
195             Method meth = cl.getMethod(setMeth, cldef);
196             Object JavaDoc[] params = {Integer.valueOf(value)};
197             meth.invoke(obj, params);
198             return;
199         }
200     }
201
202     static public class DataSourceInfo {
203         public String JavaDoc jndiName;
204         public String JavaDoc dbUser;
205         public String JavaDoc dbPassword;
206         public Object JavaDoc dataSource;
207         
208         public DataSourceInfo() {}
209     }
210 }
211
Popular Tags