001: package org.apache.commons.configuration.test;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.FileReader;
021:
022: import java.sql.Connection;
023: import java.sql.DriverManager;
024: import java.sql.SQLException;
025: import java.sql.Statement;
026:
027: import org.apache.commons.lang.StringUtils;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: /**
032: * Stolen from Turbine
033: *
034: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
035: * @version $Id: HsqlDB.java 439648 2006-09-02 20:42:10Z oheger $
036: */
037:
038: public class HsqlDB {
039: private Connection connection = null;
040: private static Log log = LogFactory.getLog(HsqlDB.class);
041:
042: public HsqlDB(String uri, String databaseDriver, String loadFile)
043: throws Exception {
044: Class.forName(databaseDriver);
045:
046: this .connection = DriverManager.getConnection(uri, "sa", "");
047:
048: if (StringUtils.isNotEmpty(loadFile)) {
049: loadSqlFile(loadFile);
050: }
051: this .connection.commit();
052: }
053:
054: public Connection getConnection() {
055: return connection;
056: }
057:
058: public void close() {
059: try {
060: connection.close();
061: } catch (Exception e) {
062: }
063: }
064:
065: private void loadSqlFile(String fileName) throws Exception {
066: Statement statement = null;
067: try {
068: statement = connection.createStatement();
069: String commands = getFileContents(fileName);
070:
071: for (int targetPos = commands.indexOf(';'); targetPos > -1; targetPos = commands
072: .indexOf(';')) {
073: String cmd = commands.substring(0, targetPos + 1);
074: try {
075: statement.execute(cmd);
076: } catch (SQLException sqle) {
077: log.warn("Statement: " + cmd + ": "
078: + sqle.getMessage());
079: }
080:
081: commands = commands.substring(targetPos + 2);
082: }
083: } finally {
084: if (statement != null) {
085: statement.close();
086: }
087: }
088: }
089:
090: private String getFileContents(String fileName) throws Exception {
091: FileReader fr = new FileReader(fileName);
092:
093: char fileBuf[] = new char[1024];
094: StringBuffer sb = new StringBuffer(1000);
095: int res = -1;
096:
097: while ((res = fr.read(fileBuf, 0, 1024)) > -1) {
098: sb.append(fileBuf, 0, res);
099: }
100: fr.close();
101: return sb.toString();
102: }
103: }
|