001: /*
002: * Copyright (c) 2006 Your Corporation. All Rights Reserved.
003: */
004:
005: /*
006: * Created by IntelliJ IDEA.
007: * User: Jacques
008: * Date: Feb 25, 2006
009: * Time: 11:49:15 PM
010: */
011: package com.tacitknowledge.util.migration.jdbc;
012:
013: import java.util.Properties;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017:
018: /**
019: * Launches the migration process as a standalone application.
020: * <p>
021: * This class expects the following Java environment parameters:
022: * <ul>
023: * <li>migration.systemname - the name of the logical system being migrated</li>
024: * </ul>
025: * <p>
026: * Below is an example of how this class can be configured in build.xml:
027: * <pre>
028: * ...
029: * <target name="patch.database" description="Runs the migration system">
030: * <java
031: * fork="true"
032: * classpathref="patch.classpath"
033: * failonerror="true"
034: * classname="com.tacitknowledge.util.migration.jdbc.StandaloneMigrationLauncher">
035: * <sysproperty key="migration.systemname" value="${application.name}"/>
036: * </java>
037: * </target>
038: * ...
039: * </pre>
040: *
041: * @author Mike Hardy (mike@tacitknowledge.com)
042: * @version $Id: StandaloneMigrationLauncher.java,v 1.7 2005/09/07 22:20:34 chrisa Exp $
043: * @see com.tacitknowledge.util.migration.MigrationProcess
044: */
045: public class StandaloneMigrationLauncher {
046: /**
047: * Class logger
048: */
049: private static Log log = LogFactory
050: .getLog(StandaloneMigrationLauncher.class);
051:
052: /**
053: * Private constructor - this object shouldn't be instantiated
054: */
055: private StandaloneMigrationLauncher() {
056: // does nothing
057: }
058:
059: /**
060: * Run the migrations for the given system name
061: *
062: * @param arguments the command line arguments, if any (none are used)
063: * @exception Exception if anything goes wrong
064: */
065: public static void main(String[] arguments) throws Exception {
066: String systemName = getRequiredParam("migration.systemname",
067: System.getProperties(), arguments);
068:
069: // The MigrationLauncher is responsible for handling the interaction
070: // between the PatchTable and the underlying MigrationTasks; as each
071: // task is executed, the patch level is incremented, etc.
072: try {
073: JdbcMigrationLauncherFactory launcherFactory = MigrationLauncherFactoryLoader
074: .createFactory();
075: JdbcMigrationLauncher launcher = launcherFactory
076: .createMigrationLauncher(systemName);
077: launcher.doMigrations();
078: } catch (Exception e) {
079: log.error(e);
080: throw e;
081: }
082: }
083:
084: /**
085: * Returns the value of the specified servlet context initialization parameter.
086: *
087: * @param param the parameter to return
088: * @param properties the <code>Properties</code> for the Java system
089: * @param arguments optionally takes the arguments passed into the main to
090: * use as the migration system name
091: * @return the value of the specified system initialization parameter
092: * @throws IllegalArgumentException if the parameter does not exist
093: */
094: private static String getRequiredParam(String param,
095: Properties properties, String[] arguments)
096: throws IllegalArgumentException {
097: String value = properties.getProperty(param);
098: if (value == null) {
099: if ((arguments != null) && (arguments.length > 0)) {
100: value = arguments[0].trim();
101: } else {
102: throw new IllegalArgumentException("'" + param
103: + "' is a required "
104: + "initialization parameter. Aborting.");
105: }
106: }
107: return value;
108: }
109: }
|