KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > spi > DatabaseHelper


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.spi;
6
7 import com.mckoi.database.jdbc.MSQLException;
8
9 import junit.framework.Assert;
10
11 import net.sf.hibernate.SessionFactory;
12 import net.sf.hibernate.cfg.Configuration;
13 import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
14
15 import org.apache.commons.lang.StringUtils;
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19 import java.io.*;
20
21 import java.net.URL JavaDoc;
22
23 import java.sql.Connection JavaDoc;
24 import java.sql.Statement JavaDoc;
25
26 import javax.naming.InitialContext JavaDoc;
27
28 import javax.sql.DataSource JavaDoc;
29
30
31 /**
32  * @author Eric Pugh
33  *
34  * This helper class populates a test mckoi database.
35  */

36 public class DatabaseHelper {
37     //~ Static fields/initializers /////////////////////////////////////////////
38

39     private static final Log log = LogFactory.getLog(DatabaseHelper.class);
40
41     //~ Methods ////////////////////////////////////////////////////////////////
42

43     /**
44      * Create the database by loading a URL pointing at a SQL script.
45      */

46     public static void createDatabase(URL JavaDoc url) {
47         Assert.assertNotNull("Database url is null", url);
48
49         try {
50             String JavaDoc sql = getDatabaseCreationScript(url);
51             createDatabase(sql);
52         } catch (IOException e) {
53             log.error(e.getMessage(), e);
54         }
55     }
56
57     /**
58      * Create a new database and initialize it with the specified sql script.
59      * @param sql the sql to execute
60      */

61     public static void createDatabase(String JavaDoc sql) {
62         Connection JavaDoc connection;
63         Statement JavaDoc statement = null;
64         String JavaDoc sqlLine = null;
65
66         try {
67             InitialContext JavaDoc context = new InitialContext JavaDoc();
68             DataSource JavaDoc ds = (DataSource JavaDoc) context.lookup("jdbc/CreateDS");
69             connection = ds.getConnection();
70             statement = connection.createStatement();
71
72             String JavaDoc[] sqls = StringUtils.split(sql, ";");
73
74             for (int i = 0; i < sqls.length; i++) {
75                 sqlLine = StringUtils.stripToEmpty(sqls[i]);
76                 sqlLine = StringUtils.replace(sqlLine, "\r", "");
77                 sqlLine = StringUtils.replace(sqlLine, "\n", "");
78
79                 //String s = sqls[i];
80
if ((sqlLine.length() > 0) && (sqlLine.charAt(0) != '#')) {
81                     try {
82                         statement.executeQuery(sqlLine);
83                     } catch (MSQLException e) {
84                         if (sqlLine.toLowerCase().indexOf("drop") == -1) {
85                             log.error("Error executing " + sqlLine, e);
86                         }
87                     }
88                 }
89             }
90         } catch (Exception JavaDoc e) {
91             log.error("Database creation error. sqlLine:" + sqlLine, e);
92         } finally {
93             if (statement != null) {
94                 try {
95                     statement.close();
96                 } catch (Exception JavaDoc ex) {
97                     //not catch
98
}
99             }
100         }
101     }
102
103     /**
104       * Use the default Hibernate *.hbm.xml files. These build the primary keys
105       * based on an identity or sequence, whatever is native to the database.
106       * @throws Exception
107       */

108     public static SessionFactory createHibernateSessionFactory() throws Exception JavaDoc {
109         Configuration configuration = new Configuration();
110
111         //cfg.addClass(HibernateHistoryStep.class);
112
URL JavaDoc currentStep = DatabaseHelper.class.getResource("/com/opensymphony/workflow/spi/hibernate/HibernateCurrentStep.hbm.xml");
113         URL JavaDoc historyStep = DatabaseHelper.class.getResource("/com/opensymphony/workflow/spi/hibernate/HibernateHistoryStep.hbm.xml");
114         URL JavaDoc workflowEntry = DatabaseHelper.class.getResource("/com/opensymphony/workflow/spi/hibernate/HibernateWorkflowEntry.hbm.xml");
115         URL JavaDoc propertySet = DatabaseHelper.class.getResource("/com/opensymphony/module/propertyset/hibernate/PropertySetItemImpl.hbm.xml");
116         Assert.assertTrue(currentStep != null);
117         Assert.assertTrue(historyStep != null);
118         Assert.assertTrue(workflowEntry != null);
119         Assert.assertTrue(propertySet != null);
120         configuration.addURL(currentStep);
121         configuration.addURL(historyStep);
122         configuration.addURL(workflowEntry);
123         configuration.addURL(propertySet);
124
125         new SchemaExport(configuration).create(false, true);
126
127         return configuration.buildSessionFactory();
128     }
129
130     private static String JavaDoc getDatabaseCreationScript(URL JavaDoc url) throws IOException {
131         InputStreamReader reader = new InputStreamReader(url.openStream());
132         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(100);
133         int c = 0;
134
135         while (c > -1) {
136             c = reader.read();
137
138             if (c > -1) {
139                 sb.append((char) c);
140             }
141         }
142
143         return sb.toString();
144     }
145 }
146
Popular Tags