    1   package org.apache.commons.configuration.test;
    2
    3   /*
    4    * Licensed to the Apache Software Foundation (ASF) under one or more
    5    * contributor license agreements.  See the NOTICE file distributed with
    6    * this work for additional information regarding copyright ownership.
    7    * The ASF licenses this file to You under the Apache License, Version 2.0
    8    * (the "License"); you may not use this file except in compliance with
    9    * the License.  You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19
    20  import java.io.FileReader;
    21
    22  import java.sql.Connection;
    23  import java.sql.DriverManager;
    24  import java.sql.SQLException;
    25  import java.sql.Statement;
    26
    27  import org.apache.commons.lang.StringUtils;
    28  import org.apache.commons.logging.Log;
    29  import org.apache.commons.logging.LogFactory;
    30
    31  /***
    32   * Stolen from Turbine
    33   *
    34   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    35   * @version $Id: HsqlDB.java 439648 2006-09-02 20:42:10Z oheger $
    36   */
    37
    38  public class HsqlDB
    39  {
    40      private Connection connection = null;
    41      private static Log log = LogFactory.getLog(HsqlDB.class);
    42
    43      public HsqlDB(String uri, String databaseDriver, String loadFile)
    44              throws Exception
    45      {
    46          Class.forName(databaseDriver);
    47
    48          this.connection = DriverManager.getConnection(uri, "sa", "");
    49
    50              if (StringUtils.isNotEmpty(loadFile))
    51              {
    52                  loadSqlFile(loadFile);
    53              }
    54          this.connection.commit();
    55      }
    56
    57      public Connection getConnection()
    58      {
    59          return connection;
    60      }
    61
    62      public void close()
    63      {
    64          try
    65          {
    66              connection.close();
    67          }
    68          catch (Exception e)
    69          {
    70          }
    71      }
    72
    73      private void loadSqlFile(String fileName)
    74              throws Exception
    75      {
    76          Statement statement = null;
    77          try
    78          {
    79              statement = connection.createStatement();
    80              String commands = getFileContents(fileName);
    81
    82              for (int targetPos = commands.indexOf(';'); targetPos > -1; targetPos = commands.indexOf(';'))
    83              {
    84                  String cmd = commands.substring(0, targetPos + 1);
    85                  try
    86                  {
    87                      statement.execute(cmd);
    88                  }
    89                  catch (SQLException sqle)
    90                  {
    91                      log.warn("Statement: " + cmd + ": " + sqle.getMessage());
    92                  }
    93
    94                  commands = commands.substring(targetPos + 2);
    95              }
    96          }
    97          finally
    98          {
    99              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