KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > LegacyConfigConverter


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

35
36 public class LegacyConfigConverter {
37
38     // START OF IASRI 4660742
39
static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
40     // END OF IASRI 4660742
41

42     private static final String JavaDoc OLD_JDBC_DRIVER_KEY = "jdbc.drivers";
43     private static final String JavaDoc OLD_JDBC_DATASOURCES_KEY = "jdbc.datasources";
44     private static final String JavaDoc OLD_JDBC_XA_DATASOURCES_KEY =
45         "jdbc20.datasources";
46     private static final String JavaDoc OLD_JDBC_DELIMITER = "|";
47     private static final String JavaDoc KEY_DELIMITER = ".";
48     private static final String JavaDoc OLD_XA_DATASOURCE_PREFIX = "xadatasource";
49     private static final String JavaDoc OLD_XA_JNDI_NAME = "jndiname";
50     private static final String JavaDoc OLD_XA_CLASSNAME = "classname";
51     private static final String JavaDoc OLD_XA_DB_USER = "dbuser";
52     private static final String JavaDoc OLD_XA_DB_PASSWORD = "dbpassword";
53     private static final String JavaDoc OLD_PROP_INDICATOR = "prop";
54
55
56     // START OF IASRI 4679641
57
// private static final boolean debug = true;
58
private static final boolean debug = com.sun.enterprise.util.logging.Debug.enabled;
59     // END OF IASRI 4679641
60

61     public static void main(String JavaDoc args[]) {
62         if( args.length == 2 ) {
63             String JavaDoc defaultPropsFilename = args[0];
64             String JavaDoc destinationPropsFilename = args[1];
65             LegacyConfigConverter conv = new LegacyConfigConverter();
66               /** IASRI 4660742
67             System.out.println("Reading old jdbc properties from " +
68                                defaultPropsFilename);
69             System.out.println("Destination file is " +
70                                destinationPropsFilename);
71               **/

72               // START OF IASRI 4660742
73
if (_logger.isLoggable(Level.FINE)) {
74                   _logger.log(Level.FINE,"Reading old jdbc properties from "
75                               + defaultPropsFilename);
76                   _logger.log(Level.FINE,"Destination file is "
77                               + destinationPropsFilename);
78             }
79               // END OF IASRI 4660742
80

81             conv.convert(defaultPropsFilename,
82                          destinationPropsFilename);
83             
84         } else {
85             System.out.println("Usage : <old_props_file> <new_props_file>");
86             System.out.println("For example : " + "/a/b/default.properties "
87                                + "/a/b/resource.properties");
88         }
89     }
90
91     private J2EEResourceFactory resourceFactory_;
92
93     public LegacyConfigConverter() {
94     }
95
96     public void convert(String JavaDoc oldFile, String JavaDoc newFile) {
97
98         InputStream inputStream = null;
99
100         try {
101             File defaultPropsFile = new File(oldFile);
102             FileInputStream fis = new FileInputStream(defaultPropsFile);
103             inputStream = new BufferedInputStream(fis);
104             Properties defaultProps = new Properties();
105             defaultProps.load(inputStream);
106             
107             resourceFactory_ = ServerConfiguration.getJ2EEResourceFactory();
108
109             J2EEResourceCollection resources =
110                 resourceFactory_.loadResourceCollection(newFile);
111
112             // add jdbc stuff from default.properties
113
convertJdbcDrivers(defaultProps, resources);
114             convertJdbcResources(defaultProps, resources);
115             convertJdbcXAResources(defaultProps, resources);
116
117             resourceFactory_.storeResourceCollection(resources, newFile);
118
119               /** IASRI 4660742
120             System.out.println("Conversion complete. " + oldFile + " has not been changed. Please remove any obsolete jdbc properties if it will be used as a default.properties file in j2ee");
121               **/

122               // START OF IASRI 4660742
123
if (_logger.isLoggable(Level.FINE)) {
124                _logger.log(Level.FINE,"Conversion complete. " + oldFile + " has not been changed. Please remove any obsolete jdbc properties if it will be used as a default.properties file in j2ee");
125              }
126               // END OF IASRI 4660742
127

128         } catch(Exception JavaDoc e) {
129             /** IASRI 4660742
130              e.printStackTrace();
131             **/

132               // START OF IASRI 4660742
133
_logger.log(Level.SEVERE,"enterprise_util.excep_legacyconfigconverter_convert",e);
134               // END OF IASRI 4660742
135
if( inputStream != null ) {
136                 try {
137                     inputStream.close();
138                 } catch(Exception JavaDoc ce) {}
139             }
140         }
141     }
142
143     private void convertJdbcDrivers(Properties defaultProps,
144                                      J2EEResourceCollection resources)
145         throws Exception JavaDoc {
146
147         String JavaDoc drivers = defaultProps.getProperty(OLD_JDBC_DRIVER_KEY);
148
149         if( drivers == null ) {
150             /** IASRI 4660742
151             System.out.println("No Jdbc Drivers...");
152             **/

153               // START OF IASRI 4660742
154
_logger.log(Level.FINE,"No Jdbc Drivers...");
155               // END OF IASRI 4660742
156
return;
157         }
158
159         StringTokenizer tokenizer =
160             new StringTokenizer(drivers, OLD_JDBC_DELIMITER);
161
162         while(tokenizer.hasMoreTokens()) {
163             String JavaDoc nextDriverClass = tokenizer.nextToken();
164             J2EEResource driver =
165                 resourceFactory_.createResource(J2EEResource.JDBC_DRIVER,
166                                                 nextDriverClass);
167             resources.addResource(driver);
168             if( debug ) {
169                 /** IASRI 4660742
170                 System.out.println("Adding " + driver);
171                 **/

172                     // START OF IASRI 4660742
173
if (_logger.isLoggable(Level.FINE))
174                         _logger.log(Level.FINE,"Adding " + driver);
175                     // END OF IASRI 4660742
176
}
177         }
178     }
179
180     private void convertJdbcResources(Properties defaultProps,
181                                       J2EEResourceCollection resources)
182         throws Exception JavaDoc {
183
184         String JavaDoc jdbcResources = defaultProps.getProperty
185             (OLD_JDBC_DATASOURCES_KEY);
186
187         if( jdbcResources == null ) {
188            /** IASRI 4660742
189            System.out.println("No Jdbc Resources...");
190            **/

191              // START OF IASRI 4660742
192
_logger.log(Level.FINE,"No Jdbc Resources...");
193              // END OF IASRI 4660742
194
return;
195         }
196
197         StringTokenizer tokenizer =
198             new StringTokenizer(jdbcResources, OLD_JDBC_DELIMITER);
199
200         while(tokenizer.hasMoreTokens()) {
201             String JavaDoc jndiName = tokenizer.nextToken();
202             String JavaDoc url = tokenizer.nextToken();
203
204             JdbcResource jdbcResource = (JdbcResource)
205                 resourceFactory_.createResource(J2EEResource.JDBC_RESOURCE,
206                                                 jndiName);
207             //jdbcResource.setUrl(url);
208
resources.addResource(jdbcResource);
209             if( debug ) {
210                 /** IASRI 4660742
211                 System.out.println("Adding " + jdbcResource);
212                 **/

213                   // START OF IASRI 4660742
214
if (_logger.isLoggable(Level.FINE))
215                       _logger.log(Level.FINE,"Adding " + jdbcResource);
216                   // END OF IASRI 4660742
217
}
218         }
219     }
220
221     private void convertJdbcXAResources(Properties defaultProps,
222                                         J2EEResourceCollection resources)
223         throws Exception JavaDoc {
224
225         String JavaDoc xaResources = defaultProps.getProperty
226             (OLD_JDBC_XA_DATASOURCES_KEY);
227
228         if( xaResources == null ) {
229            /** IASRI 4660742
230            System.out.println("No XA Jdbc Resources...");
231            **/

232              // START OF IASRI 4660742
233
_logger.log(Level.FINE,"No XA Jdbc Resources...");
234              // END OF IASRI 4660742
235
return;
236         }
237
238         StringTokenizer tokenizer =
239             new StringTokenizer(xaResources, OLD_JDBC_DELIMITER);
240
241         int xaDriverCount = 0;
242         Hashtable xaDriverResources = new Hashtable();
243         
244         while(tokenizer.hasMoreTokens()) {
245             String JavaDoc name = tokenizer.nextToken();
246
247             String JavaDoc xaJndiName = tokenizer.nextToken();
248
249             JdbcXAResource jdbcXAResource = (JdbcXAResource)
250                 resourceFactory_.createResource(J2EEResource.JDBC_XA_RESOURCE,
251                                                 name);
252         
253             xaDriverResources.put(xaJndiName, jdbcXAResource);
254             resources.addResource(jdbcXAResource);
255
256             xaDriverCount++;
257         }
258
259         for(int driverIndex = 0; driverIndex < xaDriverCount; driverIndex++) {
260             String JavaDoc propPrefix = OLD_XA_DATASOURCE_PREFIX + KEY_DELIMITER +
261                 driverIndex + KEY_DELIMITER;
262             String JavaDoc xaJndiName = defaultProps.getProperty
263                 (propPrefix + OLD_XA_JNDI_NAME);
264             
265             JdbcXAResource jdbcXAResource = (JdbcXAResource)
266                 xaDriverResources.get(xaJndiName);
267             
268             String JavaDoc className = defaultProps.getProperty
269                 (propPrefix + OLD_XA_CLASSNAME);
270             String JavaDoc dbuser = defaultProps.getProperty
271                 (propPrefix + OLD_XA_DB_USER);
272             String JavaDoc dbPassword = defaultProps.getProperty
273                 (propPrefix + OLD_XA_DB_PASSWORD);
274
275             jdbcXAResource.setClassname(className);
276             //jdbcXAResource.setDbuser(dbuser);
277
//jdbcXAResource.setDbpassword(dbPassword);
278

279             setJdbcXAProps(defaultProps, jdbcXAResource, driverIndex);
280
281             if( debug ) {
282                 /** IASRI 4660742
283                 System.out.println("Adding " + jdbcXAResource);
284                 **/

285                   // START OF IASRI 4660742
286
if (_logger.isLoggable(Level.FINE))
287                       _logger.log(Level.FINE,"Adding " + jdbcXAResource);
288                   // END OF IASRI 4660742
289
}
290         }
291     }
292
293     private void setJdbcXAProps(Properties defaultProps,
294                                 JdbcXAResource resource,
295                                 int index) {
296         Set propSet = defaultProps.entrySet();
297         String JavaDoc keyPrefix = OLD_XA_DATASOURCE_PREFIX + KEY_DELIMITER +
298             index + KEY_DELIMITER + OLD_PROP_INDICATOR + KEY_DELIMITER;
299
300         for(Iterator iter = propSet.iterator(); iter.hasNext(); ) {
301             Map.Entry next = (Map.Entry) iter.next();
302             String JavaDoc key = (String JavaDoc) next.getKey();
303             String JavaDoc value = (String JavaDoc) next.getValue();
304             
305             if( key.startsWith(keyPrefix) ) {
306                 int propNameIndex = key.lastIndexOf(KEY_DELIMITER);
307                 String JavaDoc propName = key.substring(propNameIndex + 1);
308                 ResourceProperty rs =
309                     new ResourcePropertyImpl(propName, value);
310                 resource.addProperty(rs);
311             }
312         }
313     }
314
315 }
316
Popular Tags