View Javadoc
1 /* ====================================================================
2 * License:
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. The end-user documentation included with the redistribution,
17 * if any, must include the following acknowledgment:
18 * "This product includes software developed by
19 * Robert Half International (http://www.rhi.com/)."
20 * Alternately, this acknowledgment may appear in the software itself,
21 * if and wherever such third-party acknowledgments normally appear.
22 *
23 * 4. The names "Parc", "RHI", and "Robert Half International" must
24 * not be used to endorse or promote products derived from this
25 * software without prior written permission. For written
26 * permission, please contact pete.mckinstry@rhi.com.
27 *
28 * 5. Products derived from this software may not be called "PARC",
29 * nor may "PARC" appear in their name, without prior written
30 * permission of Robert Half International.
31 *
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
36 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
39 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
41 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
42 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 * ====================================================================
45 *
46 */
47 package com.rhi.architecture.batch;
48
49 import com.rhi.architecture.db.DBUtil;
50 import com.rhi.architecture.resource.InitializationException;
51
52 import java.sql.Connection;
53 import java.sql.DriverManager;
54 import java.sql.PreparedStatement;
55 import java.sql.ResultSet;
56 import java.sql.SQLException;
57 import java.util.Properties;
58
59 import org.apache.commons.logging.Log;
60 import org.apache.commons.logging.LogFactory;
61
62 /***
63 * ConfigMixin which loads configuration data from a database.
64 *
65 * @author <a href="mailto:pete_mckinstry@yahoo.com">Pete McKinstry</a>
66 * @copyright 2003, All rights reserved.
67 */
68 public class DBConfigMixin implements ConfigMixin {
69
70 /***
71 * the configuration key used to store the JDBC driver name
72 */
73 public static final String JDBC_DRIVER = "jdbc.driver";
74 /***
75 * the configuration key used to store the DB user
76 */
77 public static final String JDBC_USER = "jdbc.username";
78 /***
79 * the configuration key used to store the DB password
80 */
81 public static final String JDBC_PASSWORD = "jdbc.password";
82 /***
83 * the configuration key used to store the DB url.
84 */
85 public static final String JDBC_URL = "jdbc.url";
86
87 /***
88 * the configuration key used to store the SQL for selecting configuration
89 * values from the database.
90 */
91 public static final String PROPS_QUERY = "config.select_query";
92 /***
93 * the name of the column in the DB storing configuration keys
94 */
95 public static final String PROPERTY_KEY = "name";
96 /***
97 * the name of the column in the DB storing the value for a
98 * configuration key
99 */
100 public static final String PROPERTY_VALUE = "value";
101
102 private static final Log log = LogFactory.getLog(DBConfigMixin.class);
103
104 /***
105 * constructor
106 */
107 public DBConfigMixin() {
108 super();
109 }
110
111 /***
112 * @see com.rhi.architecture.batch.ConfigMixin#loadConfiguration(java.util.Properties)
113 * @param props
114 * @throws InitializationException
115 */
116 public void loadConfiguration(Properties props)
117 throws InitializationException {
118 String driver = props.getProperty(DBConfigMixin.JDBC_DRIVER);
119 try {
120 if (driver==null) {
121 throw new InitializationException(
122 "jdbc driver not provided in boostrap configuration");
123 }
124 Class.forName(driver);
125 }
126 catch (ClassNotFoundException e) {
127 log.error("ClassNotFoundException: " + e, e);
128 throw new InitializationException(e);
129 }
130
131 String url = props.getProperty(JDBC_URL);
132 String user = props.getProperty(JDBC_USER);
133 String passwd = props.getProperty(JDBC_PASSWORD);
134 if ((url==null) || (user==null) || (passwd==null)) {
135 throw new InitializationException("db parameters missing" +
136 "from bootstrap configuration");
137 }
138
139 Connection conn = null;
140 PreparedStatement stmt = null;
141 ResultSet rs = null;
142 try {
143 // DataSource is not yet initialized, so use a non-pooled
144 // connection for lookup.
145 conn = DriverManager.getConnection(url, user, passwd);
146 String sql = props.getProperty(PROPS_QUERY);
147 if (sql==null) {
148 throw new InitializationException("required property [" +
149 PROPS_QUERY + "] missing from bootstrap configuration");
150 }
151 String app = props.getProperty(ApplicationConfig.PROG_NAME);
152 String env = props.getProperty(ApplicationConfig.ENVIRONMENT);
153 if ((app==null) || (env==null)) {
154 throw new InitializationException("required sql bind values " +
155 "missing from bootstrap configuration");
156 }
157
158 stmt = conn.prepareStatement(sql);
159 stmt.setString(1, app);
160 stmt.setString(2, env);
161 rs = stmt.executeQuery();
162 while (rs.next()) {
163 String name = rs.getString(DBConfigMixin.PROPERTY_KEY);
164 String value = rs.getString(DBConfigMixin.PROPERTY_VALUE);
165 props.setProperty(name, value);
166 }
167 }
168 catch (SQLException e) {
169 log.error("SQLException: " +e, e);
170 throw new InitializationException(e);
171 }
172 finally {
173 DBUtil.close(rs);
174 DBUtil.close(stmt);
175 DBUtil.close(conn);
176 }
177
178 }
179
180 }
This page was automatically generated by Maven