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 2002 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: Exporter.java,v 1.4 2001/11/20 20:56:28 tima Exp $ 44 */ 45 46 package org.exolab.jms.tools.migration; 47 48 import java.io.IOException; 49 import java.sql.Connection; 50 import java.sql.SQLException; 51 import java.util.Iterator; 52 53 import javax.jms.JMSException; 54 import javax.sql.DataSource; 55 56 import org.apache.commons.logging.Log; 57 import org.apache.commons.logging.LogFactory; 58 import org.apache.log4j.xml.DOMConfigurator; 59 60 import org.exolab.castor.xml.MarshalException; 61 import org.exolab.castor.xml.ValidationException; 62 import org.exolab.jms.config.Configuration; 63 import org.exolab.jms.config.ConfigurationFileException; 64 import org.exolab.jms.config.ConfigurationManager; 65 import org.exolab.jms.config.FileDoesNotExistException; 66 import org.exolab.jms.config.RdbmsDatabaseConfiguration; 67 import org.exolab.jms.persistence.DBCPConnectionManager; 68 import org.exolab.jms.persistence.DBConnectionManager; 69 import org.exolab.jms.persistence.DatabaseService; 70 import org.exolab.jms.persistence.PersistenceException; 71 import org.exolab.jms.tools.db.Database; 72 import org.exolab.jms.tools.db.RDBMSTool; 73 import org.exolab.jms.tools.db.SchemaHelper; 74 import org.exolab.jms.tools.migration.master.ConsumerExporter; 75 import org.exolab.jms.tools.migration.master.DestinationExporter; 76 import org.exolab.jms.tools.migration.master.MessageExporter; 77 import org.exolab.jms.tools.migration.proxy.ConsumerImporter; 78 import org.exolab.jms.tools.migration.proxy.Consumers; 79 import org.exolab.jms.tools.migration.proxy.Context; 80 import org.exolab.jms.tools.migration.proxy.DestinationImporter; 81 import org.exolab.jms.tools.migration.proxy.Destinations; 82 import org.exolab.jms.tools.migration.proxy.MessageImporter; 83 import org.exolab.jms.tools.migration.proxy.Messages; 84 import org.exolab.jms.util.CommandLine; 85 import org.xml.sax.SAXException; 86 87 88 public class Exporter { 89 90 /*** 91 * The connection manager 92 */ 93 private DBConnectionManager _manager; 94 95 /*** 96 * The path to the migration database schema, as a resource 97 */ 98 private static final String SCHEMA_PATH = 99 "/org/exolab/jms/tools/migration/schema.xml"; 100 101 /*** 102 * The logger 103 */ 104 private static final Log _log = LogFactory.getLog(Exporter.class); 105 106 107 public Exporter(String output) { 108 _manager = new DBCPConnectionManager(); 109 _manager.setDriver("org.hsqldb.jdbcDriver"); 110 _manager.setURL("jdbc:hsqldb:" + output); 111 _manager.setUserName("sa"); 112 _manager.setUserPassword(""); 113 } 114 115 public void apply() 116 throws IOException, JMSException, PersistenceException, SQLException { 117 118 Connection source; 119 120 // get a connection to the OpenJMS database 121 try { 122 source = DatabaseService.getConnection(); 123 } catch (PersistenceException exception) { 124 throw new SQLException(exception.getMessage()); 125 } 126 127 // get a connection to the migration database 128 Connection target = _manager.getConnection(); 129 130 // create the migration database schema 131 RDBMSTool tool = new RDBMSTool(target); 132 Database schema = SchemaHelper.getSchemaFromResource(SCHEMA_PATH); 133 tool.create(schema); 134 135 // set up a context for migration 136 Context context = new Context(target); 137 context.setDestinations(new Destinations(target)); 138 context.setMessages(new Messages(context)); 139 context.setConsumers(new Consumers(context)); 140 141 // export data from the OpenJMS database to the migration database 142 apply(new DestinationExporter(source), 143 new DestinationImporter(context)); 144 145 apply(new MessageExporter(source), new MessageImporter(context)); 146 147 apply(new ConsumerExporter(source), new ConsumerImporter(context)); 148 149 source.close(); 150 target.close(); 151 } 152 153 public static void main(String[] args) { 154 CommandLine commands = new CommandLine(args); 155 156 String config = commands.value("config"); 157 String output = commands.value("export"); 158 if (config == null || output == null) { 159 usage(); 160 System.exit(1); 161 } else { 162 try { 163 init(config); 164 165 Exporter exporter = new Exporter(output); 166 exporter.apply(); 167 System.exit(0); // @todo - hsqldb is hanging... 168 } catch (Exception exception) { 169 _log.error("Failed to export data", exception); 170 System.exit(1); 171 } 172 } 173 } 174 175 private void apply(Export source, Import target) 176 throws JMSException, SQLException { 177 178 Iterator iterator = source.exportCollection(); 179 target.importCollection(iterator); 180 } 181 182 private static void init(String path) 183 throws ConfigurationFileException, FileDoesNotExistException, 184 PersistenceException { 185 186 // initialise the system-wide configuration 187 ConfigurationManager.setConfig(path); 188 Configuration config = ConfigurationManager.getConfig(); 189 190 // initialise log4j 191 DOMConfigurator.configure(config.getLoggerConfiguration().getFile()); 192 193 // initialise the database. 194 // NOTE: DatabaseService.start() is not invoked, as it can 195 // remove expired messages. 196 DatabaseService.instance(); 197 } 198 199 /*** 200 * Displays usage information for this tool when invoked from the 201 * command line 202 */ 203 private static void usage() { 204 System.err.println( 205 "usage: " + Exporter.class.getName() + " <arguments> [options]\n" + 206 "arguments:\n" + 207 " -config <path> specifies the path to an OpenJMS " + 208 "configuration file\n" + 209 " -export <path> specifies the path to export data to\n"); 210 } 211 212 } //-- Exporter

This page was automatically generated by Maven