001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.workflow.util;
006:
007: import junit.framework.Assert;
008:
009: import net.sf.hibernate.SessionFactory;
010: import net.sf.hibernate.cfg.Configuration;
011: import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
012:
013: import org.apache.commons.lang.StringUtils;
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016:
017: import org.hibernate.dialect.MckoiDialect;
018:
019: import java.io.*;
020:
021: import java.net.URL;
022:
023: import java.sql.Connection;
024: import java.sql.SQLException;
025: import java.sql.Statement;
026:
027: import javax.naming.InitialContext;
028:
029: import javax.sql.DataSource;
030:
031: /**
032: * @author Eric Pugh
033: *
034: * This helper class populates a test database.
035: */
036: public class DatabaseHelper {
037: //~ Static fields/initializers /////////////////////////////////////////////
038:
039: private static final Log log = LogFactory
040: .getLog(DatabaseHelper.class);
041:
042: //~ Methods ////////////////////////////////////////////////////////////////
043:
044: public static org.hibernate.SessionFactory createHibernate3SessionFactory()
045: throws Exception {
046: org.hibernate.cfg.Configuration configuration = new org.hibernate.cfg.Configuration();
047:
048: configuration.setProperty("hibernate.dialect",
049: MckoiDialect.class.getName());
050:
051: URL currentStep = DatabaseHelper.class
052: .getResource("/com/opensymphony/workflow/spi/hibernate3/HibernateCurrentStep.hbm.xml");
053: URL historyStep = DatabaseHelper.class
054: .getResource("/com/opensymphony/workflow/spi/hibernate3/HibernateHistoryStep.hbm.xml");
055: URL workflowEntry = DatabaseHelper.class
056: .getResource("/com/opensymphony/workflow/spi/hibernate3/HibernateWorkflowEntry.hbm.xml");
057: Assert.assertTrue(currentStep != null);
058: Assert.assertTrue(historyStep != null);
059: Assert.assertTrue(workflowEntry != null);
060: configuration.addURL(currentStep);
061: configuration.addURL(historyStep);
062: configuration.addURL(workflowEntry);
063:
064: new org.hibernate.tool.hbm2ddl.SchemaExport(configuration)
065: .create(false, true);
066:
067: return configuration.buildSessionFactory();
068: }
069:
070: /**
071: * Use the default Hibernate *.hbm.xml files. These build the primary keys
072: * based on an identity or sequence, whatever is native to the database.
073: * @throws Exception
074: */
075: public static SessionFactory createHibernateSessionFactory()
076: throws Exception {
077: Configuration configuration = new Configuration();
078:
079: //cfg.addClass(HibernateHistoryStep.class);
080: URL currentStep = DatabaseHelper.class
081: .getResource("/com/opensymphony/workflow/spi/hibernate/HibernateCurrentStep.hbm.xml");
082: URL historyStep = DatabaseHelper.class
083: .getResource("/com/opensymphony/workflow/spi/hibernate/HibernateHistoryStep.hbm.xml");
084: URL workflowEntry = DatabaseHelper.class
085: .getResource("/com/opensymphony/workflow/spi/hibernate/HibernateWorkflowEntry.hbm.xml");
086: URL propertySet = DatabaseHelper.class
087: .getResource("/com/opensymphony/module/propertyset/hibernate/PropertySetItemImpl.hbm.xml");
088: Assert.assertTrue(currentStep != null);
089: Assert.assertTrue(historyStep != null);
090: Assert.assertTrue(workflowEntry != null);
091: Assert.assertTrue(propertySet != null);
092: configuration.addURL(currentStep);
093: configuration.addURL(historyStep);
094: configuration.addURL(workflowEntry);
095: configuration.addURL(propertySet);
096:
097: new SchemaExport(configuration).create(false, true);
098:
099: return configuration.buildSessionFactory();
100: }
101:
102: public static SessionFactory createPropertySetSessionFactory()
103: throws Exception {
104: Configuration configuration = new Configuration();
105:
106: URL propertySet = DatabaseHelper.class
107: .getResource("/com/opensymphony/module/propertyset/hibernate/PropertySetItemImpl.hbm.xml");
108:
109: Assert.assertTrue(propertySet != null);
110:
111: configuration.addURL(propertySet);
112:
113: new SchemaExport(configuration).create(false, true);
114:
115: return configuration.buildSessionFactory();
116: }
117:
118: /**
119: * Create the database by loading a URL pointing at a SQL script.
120: */
121: public static void runScript(URL url, String dsLocation) {
122: Assert.assertNotNull("Database url is null", url);
123:
124: try {
125: String sql = getDatabaseCreationScript(url);
126: runScript(sql, dsLocation);
127: } catch (IOException e) {
128: log.error(e.getMessage(), e);
129: }
130: }
131:
132: /**
133: * Create a new database and initialize it with the specified sql script.
134: * @param sql the sql to execute
135: */
136: public static void runScript(String sql, String dsLocation) {
137: Connection connection;
138: Statement statement = null;
139: String sqlLine = null;
140:
141: try {
142: InitialContext context = new InitialContext();
143: DataSource ds = (DataSource) context.lookup(dsLocation);
144: connection = ds.getConnection();
145: statement = connection.createStatement();
146:
147: String[] sqls = StringUtils.split(sql, ";");
148:
149: for (int i = 0; i < sqls.length; i++) {
150: sqlLine = StringUtils.stripToEmpty(sqls[i]);
151: sqlLine = StringUtils.replace(sqlLine, "\r", "");
152: sqlLine = StringUtils.replace(sqlLine, "\n", "");
153:
154: //String s = sqls[i];
155: if ((sqlLine.length() > 0)
156: && (sqlLine.charAt(0) != '#')) {
157: try {
158: statement.executeQuery(sqlLine);
159: } catch (SQLException e) {
160: if (sqlLine.toLowerCase().indexOf("drop") == -1) {
161: log.error("Error executing " + sqlLine, e);
162: }
163: }
164: }
165: }
166: } catch (Exception e) {
167: log
168: .error("Database creation error. sqlLine:"
169: + sqlLine, e);
170: } finally {
171: if (statement != null) {
172: try {
173: statement.close();
174: } catch (Exception ex) {
175: //not catch
176: }
177: }
178: }
179: }
180:
181: private static String getDatabaseCreationScript(URL url)
182: throws IOException {
183: InputStreamReader reader = new InputStreamReader(url
184: .openStream());
185: StringBuffer sb = new StringBuffer(100);
186: int c = 0;
187:
188: while (c > -1) {
189: c = reader.read();
190:
191: if (c > -1) {
192: sb.append((char) c);
193: }
194: }
195:
196: return sb.toString();
197: }
198: }
|