1 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 77 public class DBTool { 78 79 82 private DBConnectionManager _connections; 83 84 87 private RDBMSTool _tool; 88 89 92 private static final Log _log = LogFactory.getLog(DBTool.class); 93 94 95 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 140 public void create() throws PersistenceException { 141 Database schema = SchemaHelper.getSchema(); 142 _tool.create(schema); 143 } 144 145 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 164 public void drop() throws PersistenceException { 165 Database schema = SchemaHelper.getSchema(); 166 _tool.drop(schema); 167 } 168 169 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 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 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 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 } | Popular Tags |