KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > tools > db > SchemaHelper


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2003-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: SchemaHelper.java,v 1.2 2005/06/10 04:32:23 tanderson Exp $
44  */

45 package org.exolab.jms.tools.db;
46
47 import java.io.FileInputStream JavaDoc;
48 import java.io.FileNotFoundException JavaDoc;
49 import java.io.InputStream JavaDoc;
50 import java.io.InputStreamReader JavaDoc;
51 import java.sql.Connection JavaDoc;
52 import java.sql.PreparedStatement JavaDoc;
53 import java.sql.ResultSet JavaDoc;
54 import java.sql.SQLException JavaDoc;
55
56 import org.exolab.castor.xml.MarshalException;
57 import org.exolab.castor.xml.ValidationException;
58 import org.exolab.jms.persistence.PersistenceException;
59 import org.exolab.jms.persistence.SQLHelper;
60
61
62 /**
63  * Schema utility class.
64  *
65  * @version $Revision: 1.2 $ $Date: 2005/06/10 04:32:23 $
66  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
67  */

68 public class SchemaHelper {
69
70     /**
71      * The schema path
72      */

73     private static final String JavaDoc SCHEMA = "/org/exolab/jms/tools/db/schema.xml";
74
75
76     /**
77      * Get the schema version
78      *
79      * @param connection the connection to use
80      * @return the schema version, or null, if no version has been initialised
81      * @throws PersistenceException for any related persistence exception
82      */

83     public static String JavaDoc getSchemaVersion(Connection JavaDoc connection)
84         throws PersistenceException {
85         String JavaDoc version = null;
86         PreparedStatement JavaDoc query = null;
87         ResultSet JavaDoc result = null;
88         try {
89             query = connection.prepareStatement(
90                 "select version from system_data where id = 1");
91             result = query.executeQuery();
92             if (result.next()) {
93                 version = result.getString(1);
94             }
95         } catch (SQLException JavaDoc exception) {
96             throw new PersistenceException(
97                 "Failed to get the schema version", exception);
98         } finally {
99             SQLHelper.close(result);
100             SQLHelper.close(query);
101         }
102         return version;
103     }
104
105     public static void setVersion(Connection JavaDoc connection, String JavaDoc version)
106         throws PersistenceException {
107         PreparedStatement JavaDoc update = null;
108         try {
109             update = connection.prepareStatement(
110                 "update system_data set version=? where id = 1");
111             update.setString(1, version);
112             if (update.executeUpdate() != 1) {
113                 throw new PersistenceException(
114                     "Failed to update system_data.version");
115             }
116         } catch (SQLException JavaDoc exception) {
117             throw new PersistenceException(
118                 "Failed to update system_data.version", exception);
119         } finally {
120             SQLHelper.close(update);
121         }
122     }
123
124     public static Table getTable(Database schema, String JavaDoc name) {
125         Table result = null;
126         Table[] tables = schema.getTable();
127         for (int i = 0; i < tables.length; ++i) {
128             if (tables[i].getName().equalsIgnoreCase(name)) {
129                 result = tables[i];
130                 break;
131             }
132         }
133         return result;
134     }
135
136     public static Attribute getAttribute(Table table, String JavaDoc name) {
137         Attribute result = null;
138         Attribute[] attributes = table.getAttribute();
139         for (int i = 0; i < attributes.length; ++i) {
140             if (attributes[i].getName().equalsIgnoreCase(name)) {
141                 result = attributes[i];
142                 break;
143             }
144         }
145         return result;
146     }
147
148     public static Database getSchema() throws PersistenceException {
149         return getSchemaFromResource(SCHEMA);
150     }
151
152     public static Database getSchemaFromResource(String JavaDoc path)
153         throws PersistenceException {
154         Database schema = null;
155         InputStream JavaDoc stream = SchemaHelper.class.getResourceAsStream(path);
156         if (stream == null) {
157             throw new PersistenceException("Cannot locate resource: " +
158                 path);
159         }
160         try {
161             schema = Database.unmarshal(new InputStreamReader JavaDoc(stream));
162         } catch (MarshalException exception) {
163             throw new PersistenceException(exception.getMessage());
164         } catch (ValidationException exception) {
165             throw new PersistenceException(exception.getMessage());
166         }
167         return schema;
168     }
169
170     public static Database getSchema(String JavaDoc path) throws PersistenceException {
171         Database schema = null;
172         InputStream JavaDoc stream = null;
173         try {
174             stream = new FileInputStream JavaDoc(path);
175         } catch (FileNotFoundException JavaDoc exception) {
176             throw new PersistenceException(exception.getMessage(), exception);
177         }
178
179         try {
180             schema = Database.unmarshal(new InputStreamReader JavaDoc(stream));
181         } catch (MarshalException exception) {
182             throw new PersistenceException(exception.getMessage());
183         } catch (ValidationException exception) {
184             throw new PersistenceException(exception.getMessage());
185         }
186         return schema;
187     }
188
189 }
190
Popular Tags