ConnectionPerThreadDataSourceComponent.java

biz/hammurapi/sql/ConnectionPerThreadDataSourceComponent.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 089 Undocumented top level type 2 42:1
Java Inspector 089 Undocumented constructor 2 50:9
Java Inspector 089 Undocumented method 2 54:9
Java Inspector 089 Undocumented method 2 63:9
Java Inspector 089 Undocumented method 2 80:9
Java Inspector 089 Undocumented method 2 89:49
Java Inspector 089 Undocumented method 2 103:9
Java Inspector 089 Undocumented method 2 110:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 56:57
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 65:89
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 66:83
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 67:82
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 68:86
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 69:73
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 75:58
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 99:58

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.sql;
24
25import java.sql.SQLException;
26import java.util.ArrayList;
27import java.util.Collection;
28import java.util.Iterator;
29
30import org.w3c.dom.Element;
31import org.w3c.dom.Node;
32import org.w3c.dom.NodeList;
33
34import biz.hammurapi.config.Component;
35import biz.hammurapi.config.ConfigurationException;
36import biz.hammurapi.config.Context;
37import biz.hammurapi.config.DomConfigurable;
38import biz.hammurapi.config.Wrapper;
39import biz.hammurapi.xml.dom.DOMUtils;
40
41
42public class ConnectionPerThreadDataSourceComponent implements Wrapper, DomConfigurable, Component {
43
44 private ConnectionPerThreadDataSource master;
45 private String driverClass;
46 private String dbUrl;
47 private String user;
48 private String password;
49
50 public ConnectionPerThreadDataSourceComponent() {
51 super();
52 }
53
54 public Object getMaster() {
55 if (master==null) {
56 throw new IllegalStateException("Not yet started");
57 }
58 return master;
59 }
60
61 private Collection initStatements=new ArrayList();
62
63 public void configure(Node configNode, Context context, ClassLoader classLoader) throws ConfigurationException {
64 try {
65 driverClass=DOMUtils.getSingleElementText((Element) configNode, "driver-class");
66 dbUrl=DOMUtils.getSingleElementText((Element) configNode, "connection-url");
67 user=DOMUtils.getSingleElementText((Element) configNode, "user");
68 password=DOMUtils.getSingleElementText((Element) configNode, "password");
69 NodeList nl=DOMUtils.selectNodeList(configNode, "init-connection");
70 for (int i=0, l=nl.getLength(); i<l; ++i) {
71 Element el = (Element) nl.item(i);
72 initStatements.add(DOMUtils.getElementText(el));
73 }
74 } catch (Exception e) {
75 throw new ConfigurationException("Cannot read parameters", e);
76 }
77
78 }
79
80 public void start() throws ConfigurationException {
81 try {
82 master=new ConnectionPerThreadDataSource(
83 driverClass,
84 dbUrl,
85 user,
86 password,
87 new Transaction() {
88
89 public boolean execute(SQLProcessor processor) throws SQLException {
90 Iterator it=initStatements.iterator();
91 while (it.hasNext()) {
92 processor.processUpdate((String) it.next(), null);
93 }
94 return true;
95 }
96
97 });
98 } catch (ClassNotFoundException e) {
99 throw new ConfigurationException("Driver class not found", e);
100 }
101 }
102
103 public void stop() throws ConfigurationException {
104 if (master!=null) {
105 master.shutdown();
106 }
107
108 }
109
110 public void setOwner(Object owner) {
111 // Nothing
112 }
113}
114