001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.sql;
024:
025: import java.sql.SQLException;
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Iterator;
029:
030: import javax.xml.transform.TransformerException;
031:
032: import org.apache.xpath.XPathAPI;
033: import org.w3c.dom.Element;
034: import org.w3c.dom.Node;
035: import org.w3c.dom.traversal.NodeIterator;
036:
037: import biz.hammurapi.config.Component;
038: import biz.hammurapi.config.ConfigurationException;
039: import biz.hammurapi.config.Context;
040: import biz.hammurapi.config.DomConfigurable;
041: import biz.hammurapi.config.Wrapper;
042: import biz.hammurapi.xml.dom.DOMUtils;
043:
044: public class ConnectionPerThreadDataSourceComponent implements Wrapper,
045: DomConfigurable, Component {
046:
047: private ConnectionPerThreadDataSource master;
048: private String driverClass;
049: private String dbUrl;
050: private String user;
051: private String password;
052:
053: public ConnectionPerThreadDataSourceComponent() {
054: super ();
055: }
056:
057: public Object getMaster() {
058: if (master == null) {
059: throw new IllegalStateException("Not yet started");
060: }
061: return master;
062: }
063:
064: private Collection initStatements = new ArrayList();
065:
066: public void configure(Node configNode, Context context)
067: throws ConfigurationException {
068: try {
069: driverClass = DOMUtils.getSingleElementText(
070: (Element) configNode, "driver-class");
071: dbUrl = DOMUtils.getSingleElementText((Element) configNode,
072: "connection-url");
073: user = DOMUtils.getSingleElementText((Element) configNode,
074: "user");
075: password = DOMUtils.getSingleElementText(
076: (Element) configNode, "password");
077: NodeIterator nit = XPathAPI.selectNodeIterator(configNode,
078: "init-connection");
079: Element el;
080: while ((el = (Element) nit.nextNode()) != null) {
081: initStatements.add(DOMUtils.getElementText(el));
082: }
083: } catch (TransformerException e) {
084: throw new ConfigurationException("Cannot read parameters",
085: e);
086: }
087:
088: }
089:
090: public void start() throws ConfigurationException {
091: try {
092: master = new ConnectionPerThreadDataSource(driverClass,
093: dbUrl, user, password, new Transaction() {
094:
095: public boolean execute(SQLProcessor processor)
096: throws SQLException {
097: Iterator it = initStatements.iterator();
098: while (it.hasNext()) {
099: processor.processUpdate((String) it
100: .next(), null);
101: }
102: return true;
103: }
104:
105: });
106: } catch (ClassNotFoundException e) {
107: throw new ConfigurationException("Driver class not found",
108: e);
109: }
110: }
111:
112: public void stop() throws ConfigurationException {
113: if (master != null) {
114: master.shutdown();
115: }
116:
117: }
118:
119: public void setOwner(Object owner) {
120: // Nothing
121: }
122: }
|