Coverage Report - org.mule.transport.jdbc.util.MuleDerbyUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleDerbyUtils
80%
28/35
50%
1/2
1.875
 
 1  
 /*
 2  
  * $Id: MuleDerbyUtils.java 12181 2008-06-26 20:05:55Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.transport.jdbc.util;
 12  
 
 13  
 import org.mule.util.ClassUtils;
 14  
 import org.mule.util.FileUtils;
 15  
 
 16  
 import java.io.File;
 17  
 import java.io.IOException;
 18  
 import java.lang.reflect.Method;
 19  
 import java.net.URL;
 20  
 import java.sql.Driver;
 21  
 import java.sql.DriverManager;
 22  
 import java.sql.SQLException;
 23  
 import java.util.Properties;
 24  
 
 25  
 public class MuleDerbyUtils
 26  
 {
 27  
     private static final String DERBY_DRIVER_CLASS = "org.apache.derby.jdbc.EmbeddedDriver";
 28  
     
 29  
     //class cannot be instantiated
 30  
     private MuleDerbyUtils()
 31  
     {
 32  0
         super();
 33  0
     }
 34  
     
 35  
     //by default, set the derby home to the target directory
 36  
     public static String setDerbyHome()
 37  
     {
 38  6
         return setDerbyHome("target");
 39  
     }
 40  
     
 41  
     public static String setDerbyHome(String path)
 42  
     {
 43  6
         File derbySystemHome = new File(System.getProperty("user.dir"), path);
 44  6
         System.setProperty("derby.system.home",  derbySystemHome.getAbsolutePath());
 45  6
         return derbySystemHome.getAbsolutePath();
 46  
     }
 47  
     
 48  
     /**
 49  
      * Properly shutdown an embedded Derby database
 50  
      * 
 51  
      * @throws SQLException
 52  
      * @see <h href="http://db.apache.org/derby/docs/10.3/devguide/tdevdvlp20349.html">Derby docs</a>
 53  
      */
 54  
     public static void stopDatabase() throws SQLException
 55  
     {
 56  
         try
 57  
         {
 58  
             // force loading the driver so it's available even if no prior connection to the
 59  
             // database was made
 60  6
             ClassUtils.instanciateClass(DERBY_DRIVER_CLASS, new Object[0]);
 61  
 
 62  6
             DriverManager.getConnection("jdbc:derby:;shutdown=true");
 63  
         }
 64  6
         catch (SQLException sqlex)
 65  
         {
 66  
             // this exception is documented to be thrown upon shutdown
 67  6
             if (!"XJ015".equals(sqlex.getSQLState()))
 68  
             {
 69  0
                 throw sqlex;
 70  
             }
 71  
         }
 72  0
         catch (Exception ex)
 73  
         {
 74  
             // this can only happen when the driver class is not in classpath. In this case, just
 75  
             // throw up
 76  0
             throw new RuntimeException(ex);
 77  6
         }
 78  6
     }
 79  
     
 80  
     public static void cleanupDerbyDb(String derbySystemHome, String databaseName) throws IOException, SQLException
 81  
     {
 82  6
         stopDatabase();
 83  6
         FileUtils.deleteTree(new File(derbySystemHome + File.separator + databaseName));
 84  6
     }
 85  
     
 86  
     public static void createDataBase(String databaseName) throws SQLException
 87  
     {
 88  
         // Do not use the EmbeddedDriver class here directly to avoid compile time references
 89  
         // on derby.jar
 90  
         try
 91  
         {
 92  6
             Driver derbyDriver = (Driver) ClassUtils.instanciateClass(DERBY_DRIVER_CLASS, new Object[0]);
 93  
             
 94  6
             Method connectMethod = derbyDriver.getClass().getMethod("connect", 
 95  
                 new Class[] { String.class, Properties.class });
 96  
             
 97  6
             String connectionName = "jdbc:derby:" + databaseName + ";create=true";
 98  6
             connectMethod.invoke(derbyDriver, new Object[] { connectionName, null });
 99  
         }
 100  0
         catch (Exception ex)
 101  
         {
 102  0
             throw new RuntimeException("Error creating the database " + databaseName, ex);
 103  6
         }
 104  6
     }
 105  
     
 106  
     public static String loadDatabaseName(String propertiesLocation, String propertyName) throws IOException
 107  
     {
 108  6
         Properties derbyProperties = new Properties();
 109  6
         URL resource = ClassUtils.getResource(propertiesLocation, MuleDerbyUtils.class);
 110  6
         derbyProperties.load(resource.openStream());
 111  6
         return derbyProperties.getProperty(propertyName);
 112  
     }
 113  
 
 114  
     public static void defaultDerbyCleanAndInit(String propertiesLocation, String propertyName) throws IOException, SQLException
 115  
     {
 116  6
         String derbyHome = setDerbyHome();
 117  6
         String dbName = loadDatabaseName(propertiesLocation, propertyName);
 118  6
         cleanupDerbyDb(derbyHome, dbName);
 119  6
         createDataBase(dbName);
 120  6
     }
 121  
 }
 122  
 
 123