Source Code Cross Referenced for JbpmSchemaUpdate.java in  » Workflow-Engines » jbpm-jpdl-3.2.2 » org » jbpm » db » compatibility » 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 » jbpm jpdl 3.2.2 » org.jbpm.db.compatibility 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.jbpm.db.compatibility;
002:
003:        import java.io.File;
004:        import java.io.FileInputStream;
005:        import java.io.FileWriter;
006:        import java.io.IOException;
007:        import java.sql.Connection;
008:        import java.sql.SQLException;
009:        import java.sql.Statement;
010:        import java.util.ArrayList;
011:        import java.util.List;
012:        import java.util.Properties;
013:
014:        import org.apache.commons.logging.Log;
015:        import org.apache.commons.logging.LogFactory;
016:        import org.hibernate.HibernateException;
017:        import org.hibernate.cfg.Configuration;
018:        import org.hibernate.cfg.NamingStrategy;
019:        import org.hibernate.cfg.Settings;
020:        import org.hibernate.connection.ConnectionProvider;
021:        import org.hibernate.connection.ConnectionProviderFactory;
022:        import org.hibernate.dialect.Dialect;
023:        import org.hibernate.tool.hbm2ddl.DatabaseMetadata;
024:        import org.hibernate.util.ReflectHelper;
025:
026:        /**
027:         * This is a modified version of the hibernate tools schema update.
028:         * The modification is to support saving of the update script to a file.
029:         *
030:         * @author Christoph Sturm
031:         * @author Koen Aers
032:         */
033:        public class JbpmSchemaUpdate {
034:
035:            private static final Log log = LogFactory
036:                    .getLog(JbpmSchemaUpdate.class);
037:            private ConnectionProvider connectionProvider;
038:            private Configuration configuration;
039:            private Dialect dialect;
040:            private List exceptions;
041:
042:            public JbpmSchemaUpdate(Configuration cfg)
043:                    throws HibernateException {
044:                this (cfg, cfg.getProperties());
045:            }
046:
047:            public JbpmSchemaUpdate(Configuration cfg,
048:                    Properties connectionProperties) throws HibernateException {
049:                this .configuration = cfg;
050:                dialect = Dialect.getDialect(connectionProperties);
051:                Properties props = new Properties();
052:                props.putAll(dialect.getDefaultProperties());
053:                props.putAll(connectionProperties);
054:                connectionProvider = ConnectionProviderFactory
055:                        .newConnectionProvider(props);
056:                exceptions = new ArrayList();
057:            }
058:
059:            public JbpmSchemaUpdate(Configuration cfg, Settings settings)
060:                    throws HibernateException {
061:                this .configuration = cfg;
062:                dialect = settings.getDialect();
063:                connectionProvider = settings.getConnectionProvider();
064:                exceptions = new ArrayList();
065:            }
066:
067:            public static void main(String[] args) {
068:                try {
069:                    Configuration cfg = new Configuration();
070:
071:                    boolean script = true;
072:                    // If true then execute db updates, otherwise just generate and display updates
073:                    boolean doUpdate = true;
074:                    String propFile = null;
075:
076:                    File out = null;
077:
078:                    for (int i = 0; i < args.length; i++) {
079:                        if (args[i].startsWith("--")) {
080:                            if (args[i].equals("--quiet")) {
081:                                script = false;
082:                            } else if (args[i].startsWith("--properties=")) {
083:                                propFile = args[i].substring(13);
084:                            } else if (args[i].startsWith("--config=")) {
085:                                cfg.configure(args[i].substring(9));
086:                            } else if (args[i].startsWith("--text")) {
087:                                doUpdate = false;
088:                            } else if (args[i].startsWith("--naming=")) {
089:                                cfg
090:                                        .setNamingStrategy((NamingStrategy) ReflectHelper
091:                                                .classForName(
092:                                                        args[i].substring(9))
093:                                                .newInstance());
094:                            } else if (args[i].startsWith("--output=")) {
095:                                out = new File(args[i].substring(9));
096:                            }
097:                        } else {
098:                            cfg.addFile(args[i]);
099:                        }
100:
101:                    }
102:
103:                    if (propFile != null) {
104:                        Properties props = new Properties();
105:                        props.putAll(cfg.getProperties());
106:                        props.load(new FileInputStream(propFile));
107:                        cfg.setProperties(props);
108:                    }
109:
110:                    new JbpmSchemaUpdate(cfg).execute(script, doUpdate, out);
111:                } catch (Exception e) {
112:                    log.error("Error running schema update", e);
113:                    e.printStackTrace();
114:                }
115:            }
116:
117:            /**
118:             * Execute the schema updates
119:             * @param script print all DDL to the console
120:             */
121:            public void execute(boolean script, boolean doUpdate, File out) {
122:
123:                log.info("Running hbm2ddl schema update");
124:
125:                Connection connection = null;
126:                Statement stmt = null;
127:                boolean autoCommitWasEnabled = true;
128:                FileWriter writer = null;
129:
130:                if (script && out != null) {
131:                    try {
132:                        log.info("Creating filewriter to file : "
133:                                + out.getAbsolutePath());
134:                        writer = new FileWriter(out);
135:                    } catch (IOException e) {
136:                        log.debug("IOException while creating filewriter");
137:                        log.debug(e);
138:                    }
139:                }
140:
141:                exceptions.clear();
142:
143:                try {
144:
145:                    DatabaseMetadata meta;
146:                    try {
147:                        log.info("fetching database metadata");
148:                        connection = connectionProvider.getConnection();
149:                        if (!connection.getAutoCommit()) {
150:                            connection.commit();
151:                            connection.setAutoCommit(true);
152:                            autoCommitWasEnabled = false;
153:                        }
154:                        meta = new DatabaseMetadata(connection, dialect);
155:                        stmt = connection.createStatement();
156:                    } catch (SQLException sqle) {
157:                        exceptions.add(sqle);
158:                        log.error("could not get database metadata", sqle);
159:                        throw sqle;
160:                    }
161:
162:                    log.info("updating schema");
163:
164:                    String[] createSQL = configuration
165:                            .generateSchemaUpdateScript(dialect, meta);
166:                    for (int j = 0; j < createSQL.length; j++) {
167:
168:                        final String sql = createSQL[j];
169:                        try {
170:                            if (script) {
171:                                System.out.println(sql);
172:                                if (writer != null) {
173:                                    writer.write(sql + ";\n");
174:                                }
175:                            }
176:                            if (doUpdate) {
177:                                log.debug(sql);
178:                                stmt.executeUpdate(sql);
179:                            }
180:                        } catch (SQLException e) {
181:                            exceptions.add(e);
182:                            log.error("Unsuccessful: " + sql);
183:                            log.error(e.getMessage());
184:                        }
185:                    }
186:
187:                    if (writer != null) {
188:                        writer.close();
189:                    }
190:
191:                    log.info("schema update complete");
192:
193:                } catch (Exception e) {
194:                    exceptions.add(e);
195:                    log.error("could not complete schema update", e);
196:                } finally {
197:
198:                    try {
199:                        if (stmt != null)
200:                            stmt.close();
201:                        if (!autoCommitWasEnabled)
202:                            connection.setAutoCommit(false);
203:                        if (connection != null)
204:                            connection.close();
205:                        if (connectionProvider != null)
206:                            connectionProvider.close();
207:                    } catch (Exception e) {
208:                        exceptions.add(e);
209:                        log.error("Error closing connection", e);
210:                    }
211:
212:                }
213:            }
214:
215:            /**
216:             * Returns a List of all Exceptions which occured during the export.
217:             * @return A List containig the Exceptions occured during the export
218:             */
219:            public List getExceptions() {
220:                return exceptions;
221:            }
222:
223:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.