1 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 ; 22 23 import java.sql.Connection ; 24 import java.sql.Statement ; 25 26 import javax.naming.InitialContext ; 27 28 import javax.sql.DataSource ; 29 30 31 36 public class DatabaseHelper { 37 39 private static final Log log = LogFactory.getLog(DatabaseHelper.class); 40 41 43 46 public static void createDatabase(URL url) { 47 Assert.assertNotNull("Database url is null", url); 48 49 try { 50 String sql = getDatabaseCreationScript(url); 51 createDatabase(sql); 52 } catch (IOException e) { 53 log.error(e.getMessage(), e); 54 } 55 } 56 57 61 public static void createDatabase(String sql) { 62 Connection connection; 63 Statement statement = null; 64 String sqlLine = null; 65 66 try { 67 InitialContext context = new InitialContext (); 68 DataSource ds = (DataSource ) context.lookup("jdbc/CreateDS"); 69 connection = ds.getConnection(); 70 statement = connection.createStatement(); 71 72 String [] 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 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 e) { 91 log.error("Database creation error. sqlLine:" + sqlLine, e); 92 } finally { 93 if (statement != null) { 94 try { 95 statement.close(); 96 } catch (Exception ex) { 97 } 99 } 100 } 101 } 102 103 108 public static SessionFactory createHibernateSessionFactory() throws Exception { 109 Configuration configuration = new Configuration(); 110 111 URL currentStep = DatabaseHelper.class.getResource("/com/opensymphony/workflow/spi/hibernate/HibernateCurrentStep.hbm.xml"); 113 URL historyStep = DatabaseHelper.class.getResource("/com/opensymphony/workflow/spi/hibernate/HibernateHistoryStep.hbm.xml"); 114 URL workflowEntry = DatabaseHelper.class.getResource("/com/opensymphony/workflow/spi/hibernate/HibernateWorkflowEntry.hbm.xml"); 115 URL 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 getDatabaseCreationScript(URL url) throws IOException { 131 InputStreamReader reader = new InputStreamReader(url.openStream()); 132 StringBuffer sb = new StringBuffer (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 |