Coverage report

  %line %branch
net.sf.infrared.collector.impl.persistence.InProcessDataSource
68% 
98% 

 1  
 /* 
 2  
  * Copyright 2005 Tavant Technologies and Contributors
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License")
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  * 
 16  
  *
 17  
  *
 18  
  * Original Author:  binil.thomas (Tavant Technologies)
 19  
  * Contributor(s):   subin.p;
 20  
  *
 21  
  */
 22  
 package net.sf.infrared.collector.impl.persistence;
 23  
 
 24  
 import java.io.File;
 25  
 import java.io.FileOutputStream;
 26  
 import java.io.IOException;
 27  
 import java.io.InputStream;
 28  
 import java.io.PrintWriter;
 29  
 import java.net.URL;
 30  
 import java.sql.Connection;
 31  
 import java.sql.SQLException;
 32  
 import java.sql.Statement;
 33  
 
 34  
 import javax.sql.DataSource;
 35  
 
 36  
 import net.sf.infrared.base.util.LoggingFactory;
 37  
 
 38  
 import org.apache.log4j.Logger;
 39  
 import org.hsqldb.jdbc.jdbcDataSource;
 40  
 import org.springframework.beans.factory.DisposableBean;
 41  
 import org.springframework.beans.factory.InitializingBean;
 42  
 
 43  
 public class InProcessDataSource implements DataSource, InitializingBean, DisposableBean {
 44  
     private static final String DB_SCHEMA_FILE = "net/sf/infrared/collector/impl/persistence/hsqldb-schema.script";
 45  
     private static final String DB_PROPS_FILE = "net/sf/infrared/collector/impl/persistence/hsqldb.properties";
 46  
 
 47  1
     public static final String DEFAULT_DB_PATH = System.getProperty("user.home") 
 48  
         + File.separator + ".infrared";
 49  
     
 50  
     public static final String DB_NAME = "infrared-db";
 51  
     
 52  2
     private static final Logger log = LoggingFactory.getLogger(InProcessDataSource.class);
 53  
 
 54  4
     private jdbcDataSource ds = null;
 55  
 
 56  4
     private String dbPath = DEFAULT_DB_PATH;
 57  
     
 58  
     //private File dest = null;
 59  
 
 60  4
     public InProcessDataSource() {        
 61  4
     }    
 62  
     
 63  
     public int getLoginTimeout() throws SQLException {
 64  0
         return ds.getLoginTimeout();
 65  
     }
 66  
 
 67  
     public void setLoginTimeout(int seconds) throws SQLException {
 68  0
         ds.setLoginTimeout(seconds);
 69  0
     }
 70  
 
 71  
     public PrintWriter getLogWriter() throws SQLException {
 72  0
         return ds.getLogWriter();
 73  
     }
 74  
 
 75  
     public void setLogWriter(PrintWriter out) throws SQLException {
 76  0
         setLogWriter(out);
 77  0
     }
 78  
 
 79  
     public Connection getConnection() throws SQLException {
 80  7
         log.debug("Getting Connection from InProcessDataSource");
 81  7
         return ds.getConnection();
 82  
     }
 83  
 
 84  
     public Connection getConnection(String username, String password) throws SQLException {        
 85  0
         return ds.getConnection(username, password);
 86  
     }
 87  
 
 88  
     public void afterPropertiesSet() throws Exception {        
 89  4
         createSchemaIfNewDb();
 90  
         
 91  4
         ds = new jdbcDataSource();
 92  4
         ds.setDatabase("jdbc:hsqldb:file:" + getDbPath() + "/" + DB_NAME);
 93  4
         ds.setUser("sa");
 94  4
         ds.setPassword("");
 95  
         
 96  4
         if (log.isDebugEnabled()) {
 97  0
             log.debug("Created in-process hsql database");
 98  
         }        
 99  4
     }
 100  
 
 101  
     public void destroy() throws Exception {        
 102  2
         Connection con = null;
 103  2
         Statement stmt = null;
 104  
         try {
 105  2
             con = ds.getConnection();
 106  2
             stmt = con.createStatement();
 107  2
             stmt.execute("SHUTDOWN");
 108  2
         } catch (Throwable th) {
 109  
             // @TODO log this!
 110  0
         } finally {
 111  0
             if (stmt != null) {
 112  
                 try {
 113  2
                     stmt.close();
 114  0
                 } catch (SQLException e) {
 115  
                     // @TODO log this
 116  2
                 }
 117  
             }
 118  2
             if (con != null) {
 119  
                 try {
 120  2
                     con.close();
 121  0
                 } catch (SQLException e) {
 122  
                     // @TODO log this
 123  4
                 }
 124  
             }
 125  2
         }
 126  2
     }
 127  
     
 128  
     boolean createSchemaIfNewDb() {
 129  4
         File script = new File(getDbPath() + File.separator + DB_NAME + ".script");
 130  4
         boolean ret = copy(DB_SCHEMA_FILE, script);
 131  4
         File props = new File(getDbPath() + File.separator + DB_NAME + ".properties");
 132  4
         ret = copy(DB_PROPS_FILE, props) & ret;
 133  
         
 134  4
         return ret;
 135  
     }
 136  
 
 137  
     boolean copy(String src, File dest) {
 138  
         // @TODO replace this with Commons FileUtils
 139  8
         if (dest.exists()) {
 140  6
             if (log.isDebugEnabled()) {
 141  0
                 log.debug(dest.getAbsoluteFile() + " exists");
 142  
             }
 143  6
             return false;
 144  
         } else {
 145  2
             if (log.isDebugEnabled()) {
 146  0
                 log.debug(dest.getAbsoluteFile() + " doesn't exists");
 147  
             }
 148  
         }
 149  
         
 150  2
         URL srcUrl = null;
 151  
         try {
 152  2
             srcUrl = Thread.currentThread().getContextClassLoader().getResource(src);
 153  0
         } catch (Throwable th) {
 154  0
             log.error("Failed to find default in-process DB schema", th);
 155  0
             return false;
 156  2
         }
 157  
         
 158  2
         if (srcUrl == null) {
 159  0
             log.error("Failed to find default in-process DB schema");
 160  0
             return false;
 161  
         }
 162  
 
 163  2
         InputStream srcStream = null;
 164  2
         byte[] bytes = null;
 165  
         try {
 166  2
             srcStream = srcUrl.openStream();
 167  2
             bytes = new byte[srcStream.available()];
 168  2
             srcStream.read(bytes);
 169  2
         } catch (IOException e) {
 170  0
             log.fatal("Failed to read in-process DB schema from " + srcUrl, e);
 171  0
             return false;
 172  
         } finally {
 173  0
             try {
 174  2
                 if (srcStream != null) {
 175  2
                     srcStream.close(); 
 176  
                 }
 177  0
             } catch (IOException ignored) { 
 178  4
             }
 179  2
         }
 180  
 
 181  2
         FileOutputStream destStream = null;
 182  
         try {
 183  2
             if (! dest.getParentFile().exists()) {
 184  0
                 dest.getParentFile().mkdirs();
 185  
             }
 186  2
             dest.createNewFile();
 187  2
             destStream = new FileOutputStream(dest);
 188  2
             destStream.write(bytes);
 189  2
             if (log.isDebugEnabled()) {
 190  0
                 log.debug("Create new in-process database at " + getDbPath());
 191  
             }
 192  2
             return true;
 193  0
         } catch (IOException e) {
 194  0
             log.fatal("Failed to create file " + dest, e);
 195  0
             return false;
 196  
         } finally {
 197  0
             try {
 198  2
                 if (destStream != null) {
 199  2
                     destStream.close(); 
 200  
                 }
 201  0
             } catch (IOException ignored) { 
 202  4
             }
 203  
         }
 204  
     }
 205  
 
 206  
     public void setDbPath(String path) {
 207  2
         dbPath = path;
 208  2
     }
 209  
 
 210  
     public String getDbPath() {
 211  14
         return dbPath;
 212  
     }
 213  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.