Source Code Cross Referenced for DatabaseHelper.java in  » Workflow-Engines » OSWorkflow » com » opensymphony » workflow » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Workflow Engines » OSWorkflow » com.opensymphony.workflow.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.