KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > hibernate > SchemaHelper


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package info.jtrac.hibernate;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.Statement JavaDoc;
21 import javax.sql.DataSource JavaDoc;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.hibernate.cfg.Configuration;
25 import org.hibernate.tool.hbm2ddl.SchemaUpdate;
26
27 /**
28  * Utilities to create the database schema, drop and create tables
29  * Uses Hibernate Schema tools
30  * Used normally at application first start
31  */

32 public class SchemaHelper {
33     
34     private String JavaDoc driverClassName;
35     private String JavaDoc url;
36     private String JavaDoc username;
37     private String JavaDoc password;
38     private String JavaDoc hibernateDialect;
39     private String JavaDoc[] mappingResources;
40     
41     private DataSource JavaDoc dataSource;
42
43     private final Log logger = LogFactory.getLog(SchemaHelper.class);
44     
45     public void setDataSource(DataSource JavaDoc dataSource) {
46         this.dataSource = dataSource;
47     }
48
49     public void setDriverClassName(String JavaDoc driverClassName) {
50         this.driverClassName = driverClassName;
51     }
52
53     public void setHibernateDialect(String JavaDoc hibernateDialect) {
54         this.hibernateDialect = hibernateDialect;
55     }
56     
57     public void setMappingResources(String JavaDoc[] mappingResources) {
58         this.mappingResources = mappingResources;
59     }
60     
61     public void setUrl(String JavaDoc url) {
62         this.url = url;
63     }
64
65     public void setUsername(String JavaDoc username) {
66         this.username = username;
67     }
68
69     public void setPassword(String JavaDoc password) {
70         this.password = password;
71     }
72     
73     /**
74      * create tables using the given Hibernate configuration
75      */

76     public void createSchema() {
77         Configuration cfg = new Configuration();
78         cfg.setProperty("hibernate.connection.driver_class", driverClassName);
79         cfg.setProperty("hibernate.connection.url", url);
80         cfg.setProperty("hibernate.connection.username", username);
81         cfg.setProperty("hibernate.connection.password", password);
82         cfg.setProperty("hibernate.dialect", hibernateDialect);
83         for (String JavaDoc resource : mappingResources) {
84             cfg.addResource(resource);
85         }
86         new SchemaUpdate(cfg).execute(true, true);
87     }
88     
89     /**
90      * This is not mandatory, but makes the re-start cycle faster during development
91      */

92     public void stopEmbeddedDb() throws Exception JavaDoc {
93         if (url.startsWith("jdbc:hsqldb:file")) {
94             logger.info("attempting to shutdown embedded HSQLDB database");
95             Connection JavaDoc con = dataSource.getConnection();
96             Statement JavaDoc stmt = con.createStatement();
97             stmt.executeUpdate("SHUTDOWN");
98             stmt.close();
99             con.close();
100             logger.info("embedded HSQLDB database stopped successfully");
101         }
102     }
103     
104 }
105
Popular Tags