KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > converter > bea > WebLogic81DatabaseConverter


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.converter.bea;
18
19 import java.io.Reader JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31 import org.apache.geronimo.converter.DOMUtils;
32 import org.apache.geronimo.converter.DatabaseConversionStatus;
33 import org.apache.geronimo.converter.JDBCPool;
34 import org.apache.geronimo.converter.XADatabasePool;
35 import org.apache.geronimo.converter.AbstractDatabasePool;
36 import org.apache.geronimo.kernel.util.XmlUtil;
37 import org.w3c.dom.Document JavaDoc;
38 import org.w3c.dom.Element JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41 import org.xml.sax.InputSource JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43
44 /**
45  * Converts database pools from WebLogic 8.1 to Geronimo
46  *
47  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
48  */

49 public class WebLogic81DatabaseConverter extends DOMUtils {
50     public static DatabaseConversionStatus convert(String JavaDoc libDir, String JavaDoc domainDir) throws IOException JavaDoc {
51         Weblogic81Utils utils = new Weblogic81Utils(libDir, domainDir);
52         String JavaDoc config = utils.getConfigXML();
53         return convert(new StringReader JavaDoc(config));
54     }
55
56     public static DatabaseConversionStatus convert(Reader JavaDoc configXml) throws IOException JavaDoc {
57         List JavaDoc status = new ArrayList JavaDoc();
58         List JavaDoc noTx = new ArrayList JavaDoc();
59         List JavaDoc local = new ArrayList JavaDoc();
60         List JavaDoc xa = new ArrayList JavaDoc();
61
62         DocumentBuilderFactory JavaDoc factory = XmlUtil.newDocumentBuilderFactory();
63         factory.setValidating(false);
64         try {
65             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
66             Document JavaDoc doc = builder.parse(new InputSource JavaDoc(configXml));
67             configXml.close();
68             parseDocument(doc, status, local, xa);
69         } catch (ParserConfigurationException JavaDoc e) {
70             throw (IOException JavaDoc)new IOException JavaDoc().initCause(e);
71         } catch (SAXException JavaDoc e) {
72             throw (IOException JavaDoc)new IOException JavaDoc().initCause(e);
73         }
74
75         DatabaseConversionStatus result = new DatabaseConversionStatus();
76         result.setMessages((String JavaDoc[]) status.toArray(new String JavaDoc[status.size()]));
77         result.setNoTXPools((JDBCPool[]) noTx.toArray(new JDBCPool[noTx.size()]));
78         result.setJdbcPools((JDBCPool[]) local.toArray(new JDBCPool[noTx.size()]));
79         result.setXaPools((XADatabasePool[]) xa.toArray(new XADatabasePool[xa.size()]));
80         return result;
81     }
82
83     private static void parseDocument(Document JavaDoc doc, List JavaDoc status, List JavaDoc local, List JavaDoc xa) {
84         Element JavaDoc domain = doc.getDocumentElement();
85         if(!domain.getNodeName().equalsIgnoreCase("Domain")) {
86             status.add("ERROR: Unrecognized file beginning with "+domain.getNodeName()+" element. Expected a WebLogic config.xml file.");
87             return;
88         }
89         NodeList JavaDoc list = domain.getChildNodes();
90         Map JavaDoc pools = new HashMap JavaDoc();
91         for(int i=0; i<list.getLength(); i++) {
92             Node JavaDoc node = list.item(i);
93             if(node.getNodeType() == Node.ELEMENT_NODE) {
94                 String JavaDoc name = node.getNodeName();
95                 if(name.equalsIgnoreCase("JDBCConnectionPool")) {
96                     ConnectionPool pool = getConnectionPool((Element JavaDoc)node, status);
97                     pools.put(pool.getName(), pool);
98                 } else if(name.equalsIgnoreCase("JDBCDataSource")) {
99                     DataSource ds = getDataSource((Element JavaDoc)node, false);
100                     ConnectionPool pool = (ConnectionPool) pools.get(ds.getPoolName());
101                     if(pool != null) {
102                         pool.getDataSources().add(ds);
103                     } else {
104                         status.add("ERROR: Can't find pool for data source '"+ds.getName()+"' ("+ds.getPoolName()+")");
105                     }
106                 } else if(name.equalsIgnoreCase("JDBCTxDataSource")) {
107                     DataSource ds = getDataSource((Element JavaDoc)node, true);
108                     ConnectionPool pool = (ConnectionPool) pools.get(ds.getPoolName());
109                     if(pool != null) {
110                         pool.getDataSources().add(ds);
111                     } else {
112                         status.add("ERROR: Can't find pool for data source '"+ds.getName()+"' ("+ds.getPoolName()+")");
113                     }
114                 } else {
115                     status.add("Skipping element '"+name+"'");
116                 }
117             }
118         }
119         if(pools.size() > 0) {
120             for (Iterator JavaDoc it = pools.values().iterator(); it.hasNext();) {
121                 ConnectionPool pool = (ConnectionPool) it.next();
122                 if(pool.getPassword() != null && pool.getPassword().startsWith("{")) {
123                     status.add("NOTE: When importing from WebLogic, typically database passwords cannot be recovered, and will need to be re-entered.");
124                     break;
125                 }
126             }
127         }
128         processPools((ConnectionPool[]) pools.values().toArray(new ConnectionPool[0]),
129                 status, local, xa);
130     }
131
132     private static void processPools(ConnectionPool[] pools, List JavaDoc status, List JavaDoc local, List JavaDoc xa) {
133         for (int i = 0; i < pools.length; i++) {
134             ConnectionPool pool = pools[i];
135             boolean isXA;
136             if(pool.hasEmulate()) {
137                 isXA = false;
138             } else if(pool.hasNonTX()) {
139                 isXA = false;
140             } else if(pool.hasXADriverName()) {
141                 isXA = true;
142             } else {
143                 isXA = false;
144                 status.add("Can't tell whether pool '"+pool.getName()+"' is an XA driver or not; will create local transaction pools in Geronimo.");
145             }
146             if(pool.getDataSources().size() == 0) {
147                 status.add("Pool '"+pool.getName()+"' has no associated data sources. Creating a default pool for it.");
148                 if(isXA) {
149                     xa.add(createXAPool(pool, pool.getName(), null));
150                 } else {
151                     local.add(createJDBCPool(pool, pool.getName(), null));
152                 }
153             } else {
154                 for (int j = 0; j < pool.getDataSources().size(); j++) {
155                     DataSource ds = (DataSource) pool.getDataSources().get(j);
156                     if(isXA) {
157                         xa.add(createXAPool(pool, ds.getName(), ds.getJndiName()));
158                     } else {
159                         local.add(createJDBCPool(pool, ds.getName(), ds.getJndiName()));
160                     }
161                 }
162             }
163         }
164     }
165
166     private static void populatePool(ConnectionPool pool, AbstractDatabasePool target) {
167         if(pool.getReserveTimeoutSecs() != null) {
168             target.setBlockingTimeoutMillis(new Integer JavaDoc(pool.getReserveTimeoutSecs().intValue()*1000));
169         }
170         if(pool.getIdleTimeoutSecs() != null) {
171             target.setIdleTimeoutMillis(new Integer JavaDoc(pool.getIdleTimeoutSecs().intValue()*1000));
172         }
173         target.setMaxSize(pool.getMax());
174         target.setMinSize(pool.getMin());
175         target.setNewConnectionSQL(pool.getInitSQL());
176         target.setStatementCacheSize(pool.getCacheSize());
177         target.setTestConnectionSQL(pool.getTestTable() == null ? null : "SELECT * FROM "+pool.getTestTable()+" WHERE 0=1");
178         if(pool.getDriverName().toLowerCase().indexOf("oracle") > -1) target.setVendor(JDBCPool.VENDOR_ORACLE);
179         if(pool.getDriverName().toLowerCase().indexOf("mysql") > -1) target.setVendor(JDBCPool.VENDOR_MYSQL);
180         if(pool.getDriverName().toLowerCase().indexOf("sybase") > -1) target.setVendor(JDBCPool.VENDOR_SYBASE);
181         if(pool.getDriverName().toLowerCase().indexOf("informix") > -1) target.setVendor(JDBCPool.VENDOR_INFORMIX);
182     }
183
184     private static JDBCPool createJDBCPool(ConnectionPool pool, String JavaDoc name, String JavaDoc jndiName) {
185         JDBCPool result = new JDBCPool();
186         result.setName(name);
187         result.setJndiName(jndiName);
188         populatePool(pool, result);
189         result.setConnectionProperties(pool.getProperties());
190         result.setDriverClass(pool.getDriverName());
191         result.setJdbcURL(pool.getUrl());
192         // Don't bother putting encrypted passwords into the pool
193
if(pool.getPassword() != null && !pool.getPassword().startsWith("{")) {
194             result.setPassword(pool.getPassword());
195         }
196         result.setUsername(pool.getUsername());
197         return result;
198     }
199
200     private static XADatabasePool createXAPool(ConnectionPool pool, String JavaDoc name, String JavaDoc jndiName) {
201         XADatabasePool result = new XADatabasePool();
202         result.setName(name);
203         result.setJndiName(jndiName);
204         populatePool(pool, result);
205         result.setXaDataSourceClass(pool.getDriverName());
206         result.setProperties(pool.getProperties());
207         return result;
208     }
209
210     private static DataSource getDataSource(Element JavaDoc root, boolean tx) {
211         DataSource ds = new DataSource();
212         ds.setDeclaredAsTX(tx);
213         ds.setEmulate(getBoolean(root.getAttribute("EnableTwoPhaseCommit"), false));
214         ds.setName(root.getAttribute("Name"));
215         ds.setJndiName(root.getAttribute("JNDIName"));
216         ds.setPoolName(root.getAttribute("PoolName"));
217         return ds;
218     }
219
220     private static boolean getBoolean(String JavaDoc value, boolean defaultResult) {
221         if(value == null) {
222             return defaultResult;
223         }
224         return new Boolean JavaDoc(value).booleanValue();
225     }
226
227     private static ConnectionPool getConnectionPool(Element JavaDoc root, List JavaDoc status) {
228         ConnectionPool pool = new ConnectionPool();
229         pool.setName(root.getAttribute("Name"));
230         pool.setDriverName(root.getAttribute("DriverName"));
231         pool.setUrl(root.getAttribute("URL"));
232         pool.setMin(getInteger(root.getAttribute("InitialCapacity")));
233         pool.setMax(getInteger(root.getAttribute("MaxCapacity")));
234         readProperties(pool.getProperties(), root.getAttribute("Properties"), status);
235         pool.setUsername(pool.getProperties().getProperty("user"));
236         pool.getProperties().remove("user");
237         if(root.hasAttribute("Password")) {
238             pool.setPassword(root.getAttribute("Password"));
239         } else if(root.hasAttribute("PasswordEncrypted")) {
240             pool.setPassword(root.getAttribute("PasswordEncrypted"));
241         }
242         pool.setReserveTimeoutSecs(getInteger(root.getAttribute("ConnectionReserveTimeoutSeconds")));
243         pool.setIdleTimeoutSecs(getInteger(root.getAttribute("InactiveConnectionTimeoutSeconds")));
244         pool.setCacheSize(getInteger(root.getAttribute("StatementCacheSize")));
245         pool.setInitSQL(root.getAttribute("InitSQL"));
246         pool.setTestTable(root.getAttribute("TestTableName"));
247         return pool;
248     }
249
250     private static void readProperties(Properties JavaDoc props, String JavaDoc value, List JavaDoc status) {
251         if(value == null) {
252             return;
253         }
254         value = value.trim();
255         if(value.equals("")) {
256             return;
257         }
258         int last = -1;
259         int pos = value.indexOf(';');
260         while(pos > -1) {
261             String JavaDoc s = value.substring(last+1, pos);
262             int eq = s.indexOf('=');
263             if(eq > -1) {
264                 props.setProperty(s.substring(0, eq), s.substring(eq+1));
265             } else {
266                 status.add("WARN: Unable to read property '"+s+"'");
267             }
268             last = pos;
269             pos = value.indexOf(';', pos+1);
270         }
271         String JavaDoc s = value.substring(last+1);
272         int eq = s.indexOf('=');
273         if(eq > -1) {
274             props.setProperty(s.substring(0, eq), s.substring(eq+1));
275         } else {
276             status.add("WARN: Unable to read property '"+s+"'");
277         }
278     }
279
280     private static Integer JavaDoc getInteger(String JavaDoc value) {
281         if(value == null) {
282             return null;
283         }
284         value = value.trim();
285         if(value.equals("")) {
286             return null;
287         }
288         return new Integer JavaDoc(value);
289     }
290
291     public static class DataSource {
292         private String JavaDoc name;
293         private String JavaDoc poolName;
294         private String JavaDoc jndiName;
295         private boolean emulate;
296         private boolean declaredAsTX;
297
298         public String JavaDoc getName() {
299             return name;
300         }
301
302         public void setName(String JavaDoc name) {
303             this.name = name;
304         }
305
306         public String JavaDoc getPoolName() {
307             return poolName;
308         }
309
310         public void setPoolName(String JavaDoc poolName) {
311             this.poolName = poolName;
312         }
313
314         public String JavaDoc getJndiName() {
315             return jndiName;
316         }
317
318         public void setJndiName(String JavaDoc jndiName) {
319             this.jndiName = jndiName;
320         }
321
322         public boolean isEmulate() {
323             return emulate;
324         }
325
326         public void setEmulate(boolean emulate) {
327             this.emulate = emulate;
328         }
329
330         public boolean isDeclaredAsTX() {
331             return declaredAsTX;
332         }
333
334         public void setDeclaredAsTX(boolean declaredAsTX) {
335             this.declaredAsTX = declaredAsTX;
336         }
337     }
338
339     public static class ConnectionPool {
340         private String JavaDoc name;
341         private String JavaDoc driverName;
342         private Integer JavaDoc min, max;
343         private String JavaDoc url;
344         private String JavaDoc username;
345         private String JavaDoc password;
346         private Integer JavaDoc reserveTimeoutSecs;
347         private Integer JavaDoc idleTimeoutSecs;
348         private Integer JavaDoc cacheSize;
349         private String JavaDoc initSQL;
350         private String JavaDoc testTable;
351         private Properties JavaDoc properties = new Properties JavaDoc();
352         private List JavaDoc dataSources = new ArrayList JavaDoc();
353
354         public boolean hasEmulate() {
355             for (int i = 0; i < dataSources.size(); i++) {
356                 DataSource ds = (DataSource) dataSources.get(i);
357                 if(ds.isEmulate()) {
358                     return true;
359                 }
360             }
361             return false;
362         }
363
364         public boolean hasNonTX() {
365             for (int i = 0; i < dataSources.size(); i++) {
366                 DataSource ds = (DataSource) dataSources.get(i);
367                 if(!ds.isDeclaredAsTX()) {
368                     return true;
369                 }
370             }
371             return false;
372         }
373
374         public boolean hasXADriverName() {
375             return driverName.toUpperCase().indexOf("XA") > -1;
376         }
377
378         public String JavaDoc getName() {
379             return name;
380         }
381
382         public void setName(String JavaDoc name) {
383             this.name = name;
384         }
385
386         public String JavaDoc getDriverName() {
387             return driverName;
388         }
389
390         public void setDriverName(String JavaDoc driverName) {
391             this.driverName = driverName;
392         }
393
394         public String JavaDoc getUsername() {
395             return username;
396         }
397
398         public void setUsername(String JavaDoc username) {
399             this.username = username;
400         }
401
402         public String JavaDoc getPassword() {
403             return password;
404         }
405
406         public void setPassword(String JavaDoc password) {
407             this.password = password;
408         }
409
410         public String JavaDoc getInitSQL() {
411             return initSQL;
412         }
413
414         public void setInitSQL(String JavaDoc initSQL) {
415             this.initSQL = initSQL;
416         }
417
418         public String JavaDoc getTestTable() {
419             return testTable;
420         }
421
422         public void setTestTable(String JavaDoc testTable) {
423             this.testTable = testTable;
424         }
425
426         public Properties JavaDoc getProperties() {
427             return properties;
428         }
429
430         public void setProperties(Properties JavaDoc properties) {
431             this.properties = properties;
432         }
433
434         public List JavaDoc getDataSources() {
435             return dataSources;
436         }
437
438         public void setDataSources(List JavaDoc dataSources) {
439             this.dataSources = dataSources;
440         }
441
442         public Integer JavaDoc getMin() {
443             return min;
444         }
445
446         public void setMin(Integer JavaDoc min) {
447             this.min = min;
448         }
449
450         public Integer JavaDoc getMax() {
451             return max;
452         }
453
454         public void setMax(Integer JavaDoc max) {
455             this.max = max;
456         }
457
458         public Integer JavaDoc getReserveTimeoutSecs() {
459             return reserveTimeoutSecs;
460         }
461
462         public void setReserveTimeoutSecs(Integer JavaDoc reserveTimeoutSecs) {
463             this.reserveTimeoutSecs = reserveTimeoutSecs;
464         }
465
466         public Integer JavaDoc getIdleTimeoutSecs() {
467             return idleTimeoutSecs;
468         }
469
470         public void setIdleTimeoutSecs(Integer JavaDoc idleTimeoutSecs) {
471             this.idleTimeoutSecs = idleTimeoutSecs;
472         }
473
474         public Integer JavaDoc getCacheSize() {
475             return cacheSize;
476         }
477
478         public void setCacheSize(Integer JavaDoc cacheSize) {
479             this.cacheSize = cacheSize;
480         }
481
482         public String JavaDoc getUrl() {
483             return url;
484         }
485
486         public void setUrl(String JavaDoc url) {
487             this.url = url;
488         }
489     }
490 }
491
Popular Tags