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 { 040 private Connection connection = null; 041 private static Log log = LogFactory.getLog(HsqlDB.class); 042 043 public HsqlDB(String uri, String databaseDriver, String loadFile) 044 throws Exception 045 { 046 Class.forName(databaseDriver); 047 048 this.connection = DriverManager.getConnection(uri, "sa", ""); 049 050 if (StringUtils.isNotEmpty(loadFile)) 051 { 052 loadSqlFile(loadFile); 053 } 054 this.connection.commit(); 055 } 056 057 public Connection getConnection() 058 { 059 return connection; 060 } 061 062 public void close() 063 { 064 try 065 { 066 connection.close(); 067 } 068 catch (Exception e) 069 { 070 } 071 } 072 073 private void loadSqlFile(String fileName) 074 throws Exception 075 { 076 Statement statement = null; 077 try 078 { 079 statement = connection.createStatement(); 080 String commands = getFileContents(fileName); 081 082 for (int targetPos = commands.indexOf(';'); targetPos > -1; targetPos = commands.indexOf(';')) 083 { 084 String cmd = commands.substring(0, targetPos + 1); 085 try 086 { 087 statement.execute(cmd); 088 } 089 catch (SQLException sqle) 090 { 091 log.warn("Statement: " + cmd + ": " + sqle.getMessage()); 092 } 093 094 commands = commands.substring(targetPos + 2); 095 } 096 } 097 finally 098 { 099 if (statement != null) 100 { 101 statement.close(); 102 } 103 } 104 } 105 106 private String getFileContents(String fileName) 107 throws Exception 108 { 109 FileReader fr = new FileReader(fileName); 110 111 char fileBuf[] = new char[1024]; 112 StringBuffer sb = new StringBuffer(1000); 113 int res = -1; 114 115 while ((res = fr.read(fileBuf, 0, 1024)) > -1) 116 { 117 sb.append(fileBuf, 0, res); 118 } 119 fr.close(); 120 return sb.toString(); 121 } 122 } 123