001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.converter.jboss;
017:
018: import java.io.Reader;
019: import java.io.IOException;
020: import java.util.List;
021: import java.util.ArrayList;
022: import javax.xml.parsers.DocumentBuilderFactory;
023: import javax.xml.parsers.DocumentBuilder;
024: import javax.xml.parsers.ParserConfigurationException;
025: import org.apache.geronimo.converter.DatabaseConversionStatus;
026: import org.apache.geronimo.converter.JDBCPool;
027: import org.apache.geronimo.converter.XADatabasePool;
028: import org.apache.geronimo.converter.AbstractDatabasePool;
029: import org.apache.geronimo.converter.DOMUtils;
030: import org.apache.geronimo.kernel.util.XmlUtil;
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033: import org.w3c.dom.NodeList;
034: import org.w3c.dom.Node;
035: import org.xml.sax.InputSource;
036: import org.xml.sax.SAXException;
037:
038: /**
039: * Converts database pools from JBoss 4 to Geronimo
040: *
041: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
042: */
043: public class JBoss4DatabaseConverter extends DOMUtils {
044: public static DatabaseConversionStatus convert(Reader dsXml)
045: throws IOException {
046: List status = new ArrayList();
047: List noTx = new ArrayList();
048: List local = new ArrayList();
049: List xa = new ArrayList();
050:
051: DocumentBuilderFactory factory = XmlUtil
052: .newDocumentBuilderFactory();
053: factory.setValidating(false);
054: try {
055: DocumentBuilder builder = factory.newDocumentBuilder();
056: Document doc = builder.parse(new InputSource(dsXml));
057: dsXml.close();
058: parseDocument(doc, status, noTx, local, xa);
059: } catch (ParserConfigurationException e) {
060: throw (IOException) new IOException().initCause(e);
061: } catch (SAXException e) {
062: throw (IOException) new IOException().initCause(e);
063: }
064:
065: DatabaseConversionStatus result = new DatabaseConversionStatus();
066: result.setMessages((String[]) status.toArray(new String[status
067: .size()]));
068: result.setNoTXPools((JDBCPool[]) noTx.toArray(new JDBCPool[noTx
069: .size()]));
070: result.setJdbcPools((JDBCPool[]) local
071: .toArray(new JDBCPool[noTx.size()]));
072: result.setXaPools((XADatabasePool[]) xa
073: .toArray(new XADatabasePool[xa.size()]));
074: return result;
075: }
076:
077: private static void parseDocument(Document doc, List status,
078: List noTx, List local, List xa) {
079: Element datasources = doc.getDocumentElement();
080: if (!datasources.getNodeName().equalsIgnoreCase("datasources")) {
081: if (datasources.getNodeName()
082: .equals("connection-factories")) {
083: status
084: .add("ERROR: Geronimo cannot parse a JBoss data source configured using conection-factories. This typically means a custom RAR file is required.");
085: return;
086: } else {
087: status.add("ERROR: Unrecognized file beginning with "
088: + datasources.getNodeName()
089: + " element. Expected a JBoss *-ds.xml file.");
090: return;
091: }
092: }
093: NodeList list = datasources.getChildNodes();
094: for (int i = 0; i < list.getLength(); i++) {
095: Node node = list.item(i);
096: if (node.getNodeType() == Node.ELEMENT_NODE) {
097: String name = node.getNodeName();
098: if (name.equalsIgnoreCase("no-tx-datasource")) {
099: addJDBCDataSource((Element) node, status, noTx);
100: } else if (name.equalsIgnoreCase("local-tx-datasource")) {
101: addJDBCDataSource((Element) node, status, local);
102: } else if (name.equalsIgnoreCase("xa-datasource")) {
103: addXADataSource((Element) node, status, xa);
104: } else if (name.equalsIgnoreCase("mbean")) {
105: status.add("Skipping MBean element");
106: } else {
107: status.add("WARN: Skipped element " + name);
108: }
109: }
110: }
111: }
112:
113: private static void addDataSourceCommon(Element root,
114: AbstractDatabasePool pool, List status) {
115: pool.setJndiName(getChildText(root, "jndi-name"));
116: pool.setName(pool.getJndiName());
117: if (pool.getJndiName() != null
118: && pool.getJndiName().indexOf('/') > -1) {
119: status.add("NOTE: pool will use name '"
120: + pool.getJndiName()
121: + "' though Geronimo doesn't put it in JNDI");
122: }
123: String test = getChildText(root, "min-pool-size");
124: if (test != null && !test.equals(""))
125: pool.setMinSize(new Integer(test));
126: test = getChildText(root, "max-pool-size");
127: if (test != null && !test.equals(""))
128: pool.setMaxSize(new Integer(test));
129: test = getChildText(root, "blocking-timeout-millis");
130: if (test != null && !test.equals(""))
131: pool.setBlockingTimeoutMillis(new Integer(test));
132: test = getChildText(root, "idle-timeout-minutes");
133: if (test != null && !test.equals(""))
134: pool.setIdleTimeoutMillis(new Integer(Integer
135: .parseInt(test) * 60 * 1000));
136: pool.setNewConnectionSQL(getChildText(root,
137: "new-connection-sql"));
138: pool.setTestConnectionSQL(getChildText(root,
139: "check-valid-connection-sql"));
140: String sorter = getChildText(root,
141: "exception-sorter-class-name");
142: if (sorter != null) {
143: if (sorter.indexOf("Oracle") > -1)
144: pool.setVendor(AbstractDatabasePool.VENDOR_ORACLE);
145: if (sorter.indexOf("MySQL") > -1)
146: pool.setVendor(AbstractDatabasePool.VENDOR_MYSQL);
147: if (sorter.indexOf("Sybase") > -1)
148: pool.setVendor(AbstractDatabasePool.VENDOR_SYBASE);
149: if (sorter.indexOf("Informix") > -1)
150: pool.setVendor(AbstractDatabasePool.VENDOR_INFORMIX);
151: }
152: test = getChildText(root, "prepared-statement-cache-size");
153: if (test != null && !test.equals(""))
154: pool.setStatementCacheSize(new Integer(test));
155: }
156:
157: private static void addJDBCDataSource(Element root, List status,
158: List results) {
159: JDBCPool pool = new JDBCPool();
160: addDataSourceCommon(root, pool, status);
161: pool.setJdbcURL(getChildText(root, "connection-url"));
162: pool.setDriverClass(getChildText(root, "driver-class"));
163: NodeList list = root
164: .getElementsByTagName("connection-property");
165: for (int i = 0; i < list.getLength(); i++) {
166: Element prop = (Element) list.item(i);
167: pool.getConnectionProperties().setProperty(
168: prop.getAttribute("name"), getText(prop));
169: }
170: pool.setUsername(getChildText(root, "user-name"));
171: pool.setPassword(getChildText(root, "password"));
172:
173: if (pool.getName() != null && !pool.getName().equals("")) {
174: results.add(pool);
175: } else {
176: status.add("WARN: Ignoring pool with no JNDI name");
177: }
178: }
179:
180: private static void addXADataSource(Element root, List status,
181: List results) {
182: XADatabasePool pool = new XADatabasePool();
183: addDataSourceCommon(root, pool, status);
184: pool.setXaDataSourceClass(getChildText(root,
185: "xa-datasource-class"));
186: NodeList list = root
187: .getElementsByTagName("xa-datasource-property");
188: for (int i = 0; i < list.getLength(); i++) {
189: Element prop = (Element) list.item(i);
190: pool.getProperties().setProperty(prop.getAttribute("name"),
191: getText(prop));
192: }
193:
194: if (pool.getName() != null && !pool.getName().equals("")) {
195: results.add(pool);
196: } else {
197: status.add("WARN: Ignoring pool with no JNDI name");
198: }
199: }
200:
201: /*
202: public static void main(String[] args) {
203: File dir = new File("/Users/ammulder/temp/jboss-4.0.3SP1/docs/examples/jca/");
204: File[] files = dir.listFiles(new FilenameFilter() {
205: public boolean accept(File dir, String name) {
206: return name.endsWith("-ds.xml");
207: }
208: });
209: for (int i = 0; i < files.length; i++) {
210: File file = files[i];
211: System.out.println("Reading "+file.getName());
212: try {
213: FileReader reader = new FileReader(file);
214: DatabaseConversionStatus status = JBoss4DatabaseConverter.convert(reader);
215: for (int j = 0; j < status.getMessages().length; j++) {
216: String message = status.getMessages()[j];
217: System.out.println(" "+message);
218: }
219: System.out.println(" FOUND "+status.getNoTXPools().length+" NoTX Pools");
220: System.out.println(" FOUND "+status.getJdbcPools().length+" JDBC Pools");
221: System.out.println(" FOUND "+status.getXaPools().length+" XA Pools");
222: } catch (IOException e) {
223: e.printStackTrace();
224: }
225: }
226: } */
227: }
|