1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.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 | |
|
26 | |
|
27 | |
|
28 | |
public class MuleDerbyUtils |
29 | |
{ |
30 | |
private static final String DERBY_DRIVER_CLASS = "org.apache.derby.jdbc.EmbeddedDriver"; |
31 | |
|
32 | |
|
33 | |
private MuleDerbyUtils() |
34 | |
{ |
35 | 0 | super(); |
36 | 0 | } |
37 | |
|
38 | |
|
39 | |
public static String setDerbyHome() |
40 | |
{ |
41 | 2 | return setDerbyHome("target"); |
42 | |
} |
43 | |
|
44 | |
public static String setDerbyHome(String path) |
45 | |
{ |
46 | 2 | File derbySystemHome = new File(System.getProperty("user.dir"), path); |
47 | 2 | System.setProperty("derby.system.home", derbySystemHome.getAbsolutePath()); |
48 | 2 | return derbySystemHome.getAbsolutePath(); |
49 | |
} |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public static void stopDatabase() throws SQLException |
58 | |
{ |
59 | |
try |
60 | |
{ |
61 | |
|
62 | |
|
63 | 2 | ClassUtils.instanciateClass(DERBY_DRIVER_CLASS, new Object[0]); |
64 | |
|
65 | 2 | DriverManager.getConnection("jdbc:derby:;shutdown=true"); |
66 | |
} |
67 | 2 | catch (SQLException sqlex) |
68 | |
{ |
69 | |
|
70 | 2 | if (!"XJ015".equals(sqlex.getSQLState())) |
71 | |
{ |
72 | 0 | throw sqlex; |
73 | |
} |
74 | |
} |
75 | 0 | catch (Exception ex) |
76 | |
{ |
77 | |
|
78 | |
|
79 | 0 | throw new RuntimeException(ex); |
80 | 2 | } |
81 | 2 | } |
82 | |
|
83 | |
public static void cleanupDerbyDb(String derbySystemHome, String databaseName) throws IOException, SQLException |
84 | |
{ |
85 | 2 | stopDatabase(); |
86 | 2 | FileUtils.deleteTree(new File(derbySystemHome + File.separator + databaseName)); |
87 | 2 | } |
88 | |
|
89 | |
public static void createDataBase(String databaseName) throws SQLException |
90 | |
{ |
91 | |
|
92 | |
|
93 | |
try |
94 | |
{ |
95 | 2 | Driver derbyDriver = (Driver) ClassUtils.instanciateClass(DERBY_DRIVER_CLASS, new Object[0]); |
96 | |
|
97 | 2 | Method connectMethod = derbyDriver.getClass().getMethod("connect", |
98 | 6 | new Class[] { String.class, Properties.class }); |
99 | |
|
100 | 2 | String connectionName = "jdbc:derby:" + databaseName + ";create=true"; |
101 | 2 | connectMethod.invoke(derbyDriver, new Object[] { connectionName, null }); |
102 | |
} |
103 | 0 | catch (Exception ex) |
104 | |
{ |
105 | 0 | throw new RuntimeException("Error creating the database " + databaseName, ex); |
106 | 2 | } |
107 | 2 | } |
108 | |
|
109 | |
public static String loadDatabaseName(String propertiesLocation, String propertyName) throws IOException |
110 | |
{ |
111 | 2 | Properties derbyProperties = new Properties(); |
112 | 2 | URL resource = ClassUtils.getResource(propertiesLocation, MuleDerbyUtils.class); |
113 | 2 | derbyProperties.load(resource.openStream()); |
114 | 2 | return derbyProperties.getProperty(propertyName); |
115 | |
} |
116 | |
|
117 | |
public static void defaultDerbyCleanAndInit(String propertiesLocation, String propertyName) throws IOException, SQLException |
118 | |
{ |
119 | 2 | String derbyHome = setDerbyHome(); |
120 | 2 | String dbName = loadDatabaseName(propertiesLocation, propertyName); |
121 | 2 | cleanupDerbyDb(derbyHome, dbName); |
122 | 2 | createDataBase(dbName); |
123 | 2 | } |
124 | |
} |
125 | |
|
126 | |
|