View Javadoc
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 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: SchemaHelper.java,v 1.2 2003/08/07 13:33:11 tanderson Exp $ 44 */ 45 package org.exolab.jms.tools.db; 46 47 import java.io.InputStream; 48 import java.io.InputStreamReader; 49 import java.sql.Connection; 50 import java.sql.PreparedStatement; 51 import java.sql.ResultSet; 52 import java.sql.SQLException; 53 54 import org.exolab.castor.xml.MarshalException; 55 import org.exolab.castor.xml.ValidationException; 56 import org.exolab.jms.persistence.PersistenceException; 57 58 59 /*** 60 * Schema utility class 61 * 62 * @version $Revision: 1.2 $ $Date: 2003/08/07 13:33:11 $ 63 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 64 */ 65 public class SchemaHelper { 66 67 /*** 68 * The schema path 69 */ 70 private static final String SCHEMA = "/org/exolab/jms/tools/db/schema.xml"; 71 72 73 /*** 74 * Get the schema version 75 * 76 * @param connection the connection to use 77 * @return the schema version, or null, if no version has been initialised 78 * @throws PersistenceException for any related persistence exception 79 */ 80 public static String getSchemaVersion(Connection connection) 81 throws PersistenceException { 82 83 String version = null; 84 85 try { 86 PreparedStatement query = connection.prepareStatement( 87 "select * from system_data where id = 1"); 88 ResultSet result = query.executeQuery(); 89 if (result.next()) { 90 version = result.getString("version"); 91 } 92 query.close(); 93 result.close(); 94 } catch (SQLException exception) { 95 throw new PersistenceException( 96 "Failed to get the schema version", exception); 97 } 98 return version; 99 } 100 101 public static void setVersion(Connection connection, String version) 102 throws PersistenceException { 103 try { 104 PreparedStatement update = connection.prepareStatement( 105 "update system_data set version=? where id = 1"); 106 update.setString(1, version); 107 if (update.executeUpdate() != 1) { 108 throw new PersistenceException( 109 "Failed to update system_data.version"); 110 } 111 update.close(); 112 } catch (SQLException exception) { 113 throw new PersistenceException( 114 "Failed to update system_data.version", exception); 115 } 116 } 117 118 public static Table getTable(Database schema, String name) { 119 Table result = null; 120 Table[] tables = schema.getTable(); 121 for (int i = 0; i < tables.length; ++i) { 122 if (tables[i].getName().equalsIgnoreCase(name)) { 123 result = tables[i]; 124 break; 125 } 126 } 127 return result; 128 } 129 130 public static Attribute getAttribute(Table table, String name) { 131 Attribute result = null; 132 Attribute[] attributes = table.getAttribute(); 133 for (int i = 0; i < attributes.length; ++i) { 134 if (attributes[i].getName().equalsIgnoreCase(name)) { 135 result = attributes[i]; 136 break; 137 } 138 } 139 return result; 140 } 141 142 public static Database getSchema() throws PersistenceException { 143 return getSchemaFromResource(SCHEMA); 144 } 145 146 public static Database getSchemaFromResource(String path) 147 throws PersistenceException { 148 Database schema = null; 149 InputStream stream = SchemaHelper.class.getResourceAsStream(path); 150 if (stream == null) { 151 throw new PersistenceException("Cannot locate resource: " + 152 path); 153 } 154 try { 155 schema = Database.unmarshal(new InputStreamReader(stream)); 156 } catch (MarshalException exception) { 157 throw new PersistenceException(exception.getMessage()); 158 } catch (ValidationException exception) { 159 throw new PersistenceException(exception.getMessage()); 160 } 161 return schema; 162 } 163 164 public static Database getSchema(String path) throws PersistenceException { 165 Database schema = null; 166 InputStream stream = SchemaHelper.class.getResourceAsStream(SCHEMA); 167 if (stream == null) { 168 throw new PersistenceException("Cannot locate resource: " + 169 SCHEMA); 170 } 171 try { 172 schema = Database.unmarshal(new InputStreamReader(stream)); 173 } catch (MarshalException exception) { 174 throw new PersistenceException(exception.getMessage()); 175 } catch (ValidationException exception) { 176 throw new PersistenceException(exception.getMessage()); 177 } 178 return schema; 179 } 180 181 } //-- SchemaHelper

This page was automatically generated by Maven