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 2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: DBTool.java,v 1.8 2003/10/28 07:27:29 tanderson Exp $ 44 */ 45 46 package org.exolab.jms.tools.db; 47 48 import java.io.IOException; 49 import java.sql.Connection; 50 import java.sql.SQLException; 51 52 import org.apache.commons.logging.Log; 53 import org.apache.commons.logging.LogFactory; 54 import org.apache.log4j.xml.DOMConfigurator; 55 56 import org.exolab.castor.xml.MarshalException; 57 import org.exolab.castor.xml.ValidationException; 58 import org.exolab.jms.config.Configuration; 59 import org.exolab.jms.config.ConfigurationFileException; 60 import org.exolab.jms.config.ConfigurationManager; 61 import org.exolab.jms.config.FileDoesNotExistException; 62 import org.exolab.jms.config.RdbmsDatabaseConfiguration; 63 import org.exolab.jms.persistence.DBCPConnectionManager; 64 import org.exolab.jms.persistence.DBConnectionManager; 65 import org.exolab.jms.persistence.PersistenceException; 66 import org.exolab.jms.persistence.RDBMSAdapter; 67 import org.exolab.jms.util.CommandLine; 68 69 70 /*** 71 * This class provides support for creating and destroying OpenJMS tables 72 * in RDBMS databases. 73 * 74 * @version $Revision: 1.8 $ $Date: 2003/10/28 07:27:29 $ 75 * @author <a href="mailto:tima@intalio.com">Tim Anderson</a> 76 */ 77 public class DBTool { 78 79 /*** 80 * The connection manager 81 */ 82 private DBConnectionManager _connections; 83 84 /*** 85 * The RDBMS tool 86 */ 87 private RDBMSTool _tool; 88 89 /*** 90 * The logger 91 */ 92 private static final Log _log = LogFactory.getLog(DBTool.class); 93 94 95 /*** 96 * Construct an instance with the path to an openjms XML configuration file 97 * 98 * @param path the path to an openjms XML configuration file 99 * @throws ClassNotFoundException if the JDBC driver cannot be loaded 100 * @throws ConfigurationFileException if the configuration file is invalid 101 * @throws FileDoesNotExistException if the file cannot be found 102 * @throws IOException if the RDBMS configuration contains invalid 103 * values for the paths element 104 * @throws IllegalArgumentException if path is null 105 * @throws PersistenceException if a connection cannot be established 106 */ 107 public DBTool(String path) 108 throws ClassNotFoundException, ConfigurationFileException, 109 FileDoesNotExistException, IOException, 110 PersistenceException { 111 112 if (path == null) { 113 throw new IllegalArgumentException("Argument 'path' is null"); 114 } 115 116 ConfigurationManager.setConfig(path); 117 Configuration config = ConfigurationManager.getConfig(); 118 DOMConfigurator.configure(config.getLoggerConfiguration().getFile()); 119 RdbmsDatabaseConfiguration rdbms = 120 config.getDatabaseConfiguration().getRdbmsDatabaseConfiguration(); 121 if (rdbms == null) { 122 throw new ConfigurationFileException( 123 "Configuration file=" + path + " is not configured to use an" + 124 " RDBMS"); 125 } 126 _connections = new DBCPConnectionManager(); 127 _connections.setDriver(rdbms.getDriver()); 128 _connections.setURL(rdbms.getUrl()); 129 _connections.setUser(rdbms.getUser()); 130 _connections.setPassword(rdbms.getPassword()); 131 _connections.init(); 132 _tool = new RDBMSTool(_connections.getConnection()); 133 } 134 135 /*** 136 * Creates the database tables using the default schema 137 * 138 * @throws PersistenceException if the tables cannot be created 139 */ 140 public void create() throws PersistenceException { 141 Database schema = SchemaHelper.getSchema(); 142 _tool.create(schema); 143 } 144 145 /*** 146 * Creates the database tables from the specified schema 147 * 148 * @param path the path to an XML database schema file 149 * @throws PersistenceException if the tables cannot be created 150 */ 151 public void create(String path) throws PersistenceException { 152 if (path == null) { 153 throw new IllegalArgumentException("Argument 'path' is null"); 154 } 155 Database schema = SchemaHelper.getSchema(path); 156 _tool.create(schema); 157 } 158 159 /*** 160 * Drop the database tables from the default schema 161 * 162 * @throws PersistenceException if the tables cannot be dropped 163 */ 164 public void drop() throws PersistenceException { 165 Database schema = SchemaHelper.getSchema(); 166 _tool.drop(schema); 167 } 168 169 /*** 170 * Drop the database tables from the specified schema 171 * 172 * @param path the path to an XML database schema file 173 * @throws PersistenceException if the tables cannot be dropped 174 */ 175 public void drop(String path) throws PersistenceException { 176 if (path == null) { 177 throw new IllegalArgumentException("Argument 'path' is null"); 178 } 179 Database schema = SchemaHelper.getSchema(path); 180 _tool.drop(schema); 181 } 182 183 /*** 184 * Migrates the database tables to the latest version 185 * 186 * @throws PersistenceException if the database cannot be migrated 187 */ 188 public void migrate() throws PersistenceException { 189 Connection connection = _connections.getConnection(); 190 Database schema = SchemaHelper.getSchema(); 191 192 String fromVersion = SchemaHelper.getSchemaVersion(connection); 193 if (fromVersion == null) { 194 throw new PersistenceException( 195 "Cannot migrate schema - existing schema version cannot be " 196 + "determined"); 197 } 198 String toVersion = RDBMSAdapter.SCHEMA_VERSION; 199 SchemaConverter converter = 200 SchemaConverterFactory.create(fromVersion, toVersion, connection); 201 if (converter != null) { 202 try { 203 _log.info("Migrating schema from version=" + 204 fromVersion + " to version=" + toVersion); 205 converter.convert(); 206 _log.info("Successfully migrated schema"); 207 } catch (PersistenceException exception) { 208 _log.error( 209 "Schema migration from version=" + fromVersion + 210 " to version=" + toVersion + " failed", 211 exception); 212 throw exception; 213 } 214 } else { 215 throw new PersistenceException( 216 "Incompatible schema types. Expected schema version=" + 217 fromVersion + ", but got schema version=" + toVersion); 218 } 219 } 220 221 /*** 222 * Deallocate any resources 223 * 224 * @throws SQLException if the database connection cannot be closed 225 */ 226 public void close() throws SQLException { 227 _tool.close(); 228 } 229 230 public static void main(String args[]) { 231 CommandLine commands = new CommandLine(args); 232 233 DBTool tool = null; 234 String config = commands.value("config"); 235 if (config != null) { 236 try { 237 tool = new DBTool(config); 238 } catch (Exception exception) { 239 _log.error(exception, exception); 240 System.exit(1); 241 } 242 } else { 243 usage(); 244 System.exit(1); 245 } 246 boolean create = commands.exists("create"); 247 boolean drop = commands.exists("drop"); 248 boolean recreate = commands.exists("recreate"); 249 boolean migrate = commands.exists("migrate"); 250 String schema = commands.value("schema"); 251 if (create) { 252 try { 253 if (schema != null) { 254 tool.create(schema); 255 } else { 256 tool.create(); 257 } 258 System.out.println("Successfully created tables"); 259 } catch (Exception exception) { 260 _log.error(exception, exception); 261 System.exit(1); 262 } 263 } else if (drop) { 264 try { 265 if (schema != null) { 266 tool.drop(schema); 267 } else { 268 tool.drop(); 269 } 270 System.out.println("Successfully dropped tables"); 271 } catch (Exception exception) { 272 _log.error(exception, exception); 273 System.exit(1); 274 } 275 } else if (recreate) { 276 try { 277 if (schema != null) { 278 tool.drop(schema); 279 tool.create(schema); 280 } else { 281 tool.drop(); 282 tool.create(); 283 } 284 System.out.println("Successfully recreated tables"); 285 } catch (Exception exception) { 286 _log.error(exception, exception); 287 System.exit(1); 288 } 289 } else if (migrate) { 290 try { 291 tool.migrate(); 292 } catch (Exception exception) { 293 _log.error(exception, exception); 294 System.exit(1); 295 } 296 System.out.println("Sucessfully migrated database"); 297 } else { 298 usage(); 299 System.exit(1); 300 } 301 try { 302 tool.close(); 303 } catch (Exception exception) { 304 _log.error(exception, exception); 305 } 306 } 307 308 /*** 309 * Displays usage information for this tool when invoked from the 310 * command line 311 */ 312 private static void usage() { 313 System.err.println( 314 "usage: " + DBTool.class.getName() + " <arguments> [options]\n" + 315 "arguments:\n" + 316 " -create -config <path> creates the database tables\n" + 317 " -drop -config <path> drops the database tables\n\n" + 318 " -recreate -config <path> recreates the database tables\n\n" + 319 " -migrate -config <path> migrates the database to the latest " 320 + "schema version\n\n" + 321 "options:\n" + 322 " -schema <schema>\n"); 323 System.err.println( 324 "where:\n" + 325 " path is the path to an OpenJMS configuration file\n" + 326 " schema is an XML document specifying the database schema\n" + 327 " If not specified, the default schema will be used"); 328 } 329 330 } //-- DBTool

This page was automatically generated by Maven