Skip to content

Commit

Permalink
rework ConnectionProfileHelper to allow for use in MigrationPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
koentsje committed Mar 21, 2013
1 parent 69208fa commit eaa8800
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 50 deletions.
71 changes: 63 additions & 8 deletions src/main/java/org/hibernate/forge/common/Constants.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.hibernate.forge.common;


public interface Constants {

final String NAME = "name";
Expand Down Expand Up @@ -35,12 +36,12 @@ public interface Constants {
final String PASSWORD = "password";
final String PASSWORD_HELP = "Password for JDBC connection.";
final String PASSWORD_PROMPT = "Enter the password for JDBC connection.";

final String SAVE_PASSWORD = "savePassword";
final String SAVE_PASSWORD_HELP = "Should password for JDBC connection be saved?";
final String SAVE_PASSWORD_PROMPT = "Save password for JDBC connection?";
final String SAVE_PASSWORD_DEFAULT = "false";

final String CONNECTION_PROFILE = "connection-profile";
final String CONNECTION_PROFILE_HELP = "Name of the connection profile to use.";

Expand All @@ -59,17 +60,71 @@ public interface Constants {
final String ENTITY_PACKAGE = "entityPackage";
final String ENTITY_PACKAGE_HELP = "Package to use for generated entities.";
final String ENTITY_PACKAGE_PROMPT = "In which package you'd like to generate the entities, or enter for default:";

final String DETECT_MANY_TO_MANY_ID = "detectManyToMany";
final String DETECT_MANY_TO_MANY_HELP = "Detect many-to-many associations between tables.";
// final Boolean DETECT_MANY_TO_MANY_DEFAULT = Boolean.TRUE;
//
// final Boolean DETECT_MANY_TO_MANY_DEFAULT = Boolean.TRUE;
//
final String DETECT_ONE_TO_ONE_ID = "detectOneToOne";
final String DETECT_ONE_TO_ONE_HELP = "Detect one-to-one associations between tables.";
// final Boolean DETECT_ONE_TO_ONE_DEFAULT = Boolean.TRUE;
//
// final Boolean DETECT_ONE_TO_ONE_DEFAULT = Boolean.TRUE;
//
final String DETECT_OPTIMISTIC_LOCK_ID = "detectOptimisticLock";
final String DETECT_OPTIMISTIC_LOCK_HELP = "Detect optimistic locking tables, i.e. if a table has a column named 'version' with a numeric type optimistic locking will be setup for that table.";
// final Boolean DETECT_OPTIMISTIC_LOCK_DEFAULT = Boolean.TRUE;
// final Boolean DETECT_OPTIMISTIC_LOCK_DEFAULT = Boolean.TRUE;

final String FROM_CONNECTION_PROFILE = "from-connection-profile";
final String FROM_CONNECTION_PROFILE_HELP = "Name of the source connection profile to use.";

final String FROM_URL = "from-url";
final String FROM_URL_HELP = "URL for the source connection.";
final String FROM_URL_PROMPT = "Specify the URL for the source connection.";

final String FROM_USER = "from-user";
final String FROM_USER_HELP = "User name for the source connection.";
final String FROM_USER_PROMPT = "Enter the user name for the source connection.";

final String FROM_PASSWORD = "from-password";
final String FROM_PASSWORD_HELP = "Password for the source connection.";
final String FROM_PASSWORD_PROMPT = "Enter the password for the source connection.";

final String FROM_DIALECT = "from-dialect";
final String FROM_DIALECT_HELP = "Dialect to use for the source datasource.";
final String FROM_DIALECT_PROMPT = "Enter the dialect to use for the source datasource.";

final String FROM_DRIVER = "from-driver";
final String FROM_DRIVER_HELP = "Class name for the driver to use for the source datasource.";
final String FROM_DRIVER_PROMPT = "Specify the class name for the JDBC driver to use for the source datasource.";

final String FROM_PATH_TO_DRIVER = "from-pathToDriver";
final String FROM_PATH_TO_DRIVER_HELP = "Path in the local file system to the jar file containing the JDBC driver for the source datasource.";
final String FROM_PATH_TO_DRIVER_PROMPT = "Enter the path in the local file system to the jar file containing the JDBC driver for the source datasource.";

final String TO_CONNECTION_PROFILE = "to-connection";
final String TO_CONNECTION_PROFILE_HELP = "Name of the target connection profile to use.";

final String TO_URL = "to-url";
final String TO_URL_HELP = "URL for the target connection.";
final String TO_URL_PROMPT = "Specify the URL for the target connection.";

final String TO_USER = "to-user";
final String TO_USER_HELP = "User name for the target connection.";
final String TO_USER_PROMPT = "Enter the user name for the target connection.";

final String TO_PASSWORD = "to-password";
final String TO_PASSWORD_HELP = "Password for the target connection.";
final String TO_PASSWORD_PROMPT = "Enter the password for the target connection.";

final String TO_DIALECT = "to-dialect";
final String TO_DIALECT_HELP = "Dialect to use for the target datasource.";
final String TO_DIALECT_PROMPT = "Enter the dialect to use for the target datasource.";

final String TO_DRIVER = "to-driver";
final String TO_DRIVER_HELP = "Class name for the driver to use for the target datasource.";
final String TO_DRIVER_PROMPT = "Specify the class name for the JDBC driver to use for the target datasource.";

final String TO_PATH_TO_DRIVER = "to-pathToDriver";
final String TO_PATH_TO_DRIVER_HELP = "Path in the local file system to the jar file containing the JDBC driver for the target datasource.";
final String TO_PATH_TO_DRIVER_PROMPT = "Enter the path in the local file system to the jar file containing the JDBC driver for the target datasource.";

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,110 +70,110 @@ public void saveConnectionProfiles(Collection<ConnectionProfile> connectionProfi
}
}

public String determineDialect(String dialect, ConnectionProfileType type) {
public String determineDialect(String prompt, String dialect, ConnectionProfileType type) {
if (dialect != null)
return dialect;
if (type != null && type.getDialect() != null)
return type.getDialect();
return determineDialect(DIALECT_DEFAULT);
return determineDialect(prompt, DIALECT_DEFAULT);
}

public String determineDialect(String dialect, ConnectionProfile descriptor) {
public String determineDialect(String prompt, String dialect, ConnectionProfile descriptor) {
if (dialect != null)
return dialect;
if (descriptor != null && descriptor.dialect != null && !"".equals(descriptor.dialect.trim()))
return descriptor.dialect;
return determineDialect(DIALECT_DEFAULT);
return determineDialect(prompt, DIALECT_DEFAULT);
}

public String determineDialect(String defaultDialect) {
return shell.prompt(DIALECT_PROMPT, defaultDialect);
public String determineDialect(String prompt, String defaultDialect) {
return shell.prompt(prompt, defaultDialect);
}

public String determineDriverClass(String driver, ConnectionProfileType type) {
public String determineDriverClass(String prompt, String driver, ConnectionProfileType type) {
if (driver != null)
return driver;
if (type != null) {
ArrayList<String> candidates = new ArrayList<String>(type
.getDrivers().keySet());
if (candidates.size() > 1) {
return candidates.get(shell.promptChoice(DRIVER_PROMPT,
return candidates.get(shell.promptChoice(prompt,
candidates));
} else if (candidates.size() == 1) {
return candidates.get(0);
}
}
return determineDriverClass(DRIVER_DEFAULT);
return determineDriverClass(prompt, DRIVER_DEFAULT);
}

public String determineDriverClass(String driver, ConnectionProfile descriptor) {
public String determineDriverClass(String prompt, String driver, ConnectionProfile descriptor) {
if (driver != null)
return driver;
if (descriptor != null && descriptor.driver != null && !"".equals(descriptor.driver.trim())) {
return descriptor.driver;
}
return determineDriverClass(DRIVER_DEFAULT);
return determineDriverClass(prompt, DRIVER_DEFAULT);
}

public String determineDriverClass(String defaultDriverClass) {
return shell.prompt(DRIVER_PROMPT, defaultDriverClass);
public String determineDriverClass(String prompt, String defaultDriverClass) {
return shell.prompt(prompt, defaultDriverClass);
}

public String determineDriverPath(String path, ConnectionProfileType type) {
public String determineDriverPath(String prompt, String path, ConnectionProfileType type) {
if (path != null)
return path;
// TODO resolve driver location in maven repo if possible
return shell.prompt(PATH_TO_DRIVER_PROMPT, (String) null);
return shell.prompt(prompt, (String) null);
}

public String determineDriverPath(String path, ConnectionProfile descriptor) {
public String determineDriverPath(String prompt, String path, ConnectionProfile descriptor) {
if (path != null)
return path;
if (descriptor != null && descriptor.path != null && !"".equals(descriptor.path.trim())) {
return descriptor.path;
}
return shell.prompt(PATH_TO_DRIVER_PROMPT, (String) null);
return shell.prompt(prompt, (String) null);
}

public String determineURL(String url, ConnectionProfileType type, String driverClass) {
public String determineURL(String prompt, String url, ConnectionProfileType type, String driverClass) {
if (url != null)
return url;
// TODO suggest the proper url format based on the type and the
// driverClass
return shell.prompt(URL_PROMPT, URL_DEFAULT);
return shell.prompt(prompt, URL_DEFAULT);
}

public String determineURL(String url, ConnectionProfile descriptor) {
public String determineURL(String prompt, String url, ConnectionProfile descriptor) {
if (url != null)
return url;
if (descriptor != null && descriptor.url != null && !"".equals(descriptor.url.trim())) {
return descriptor.url;
}
return shell.prompt(URL_PROMPT, URL_DEFAULT);
return shell.prompt(prompt, URL_DEFAULT);
}

public String determineUser(String user) {
public String determineUser(String prompt, String user) {
if (user != null)
return user;
return shell.prompt(USER_PROMPT, USER_DEFAULT);
return shell.prompt(prompt, USER_DEFAULT);
}

public String determineUser(String user, ConnectionProfile descriptor) {
public String determineUser(String prompt, String user, ConnectionProfile descriptor) {
if (user != null)
return user;
if (descriptor != null && descriptor.user != null && !"".equals(descriptor.user.trim())) {
return descriptor.user;
}
return shell.prompt(USER_PROMPT, (String) null);
return shell.prompt(prompt, (String) null);
}

public String determinePassword(String password, ConnectionProfile descriptor) {
public String determinePassword(String prompt, String password, ConnectionProfile descriptor) {
if (password != null)
return password;
if (descriptor != null && descriptor.password != null && !"".equals(descriptor.password.trim())) {
return descriptor.password;
}
return shell.promptSecret(PASSWORD_PROMPT, "");
return shell.promptSecret(prompt, "");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ private void editConnectionProfile(
ConnectionProfileType connectionProfileType = ConnectionProfileType.allTypes().get(type);
connectionProfile.name = name;
connectionProfile.dialect = connectionProfileHelper.determineDialect(
dialect, connectionProfileType);
DIALECT_PROMPT, dialect, connectionProfileType);
connectionProfile.driver = connectionProfileHelper
.determineDriverClass(driver, connectionProfileType);
.determineDriverClass(DRIVER_PROMPT, driver, connectionProfileType);
connectionProfile.path = connectionProfileHelper
.determineDriverPath(path, connectionProfileType);
connectionProfile.url = connectionProfileHelper.determineURL(url,
.determineDriverPath(PATH_TO_DRIVER_PROMPT, path, connectionProfileType);
connectionProfile.url = connectionProfileHelper.determineURL(URL_PROMPT, url,
connectionProfileType, connectionProfile.driver);
connectionProfile.user = connectionProfileHelper.determineUser(user);
connectionProfile.user = connectionProfileHelper.determineUser(USER_PROMPT, user);
connectionProfiles.put(name, connectionProfile);
connectionProfileHelper.saveConnectionProfiles(connectionProfiles.values());
saveSuccess(connectionProfile, out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void generateEntities(
@Option(name = DETECT_OPTIMISTIC_LOCK_ID, help = DETECT_OPTIMISTIC_LOCK_HELP, required = false, defaultValue = "true") Boolean optimisticLock,
@Option(name = ENTITY_PACKAGE, help = ENTITY_PACKAGE_HELP, required = false) String packageName)
{
ConnectionProfile connectionProfile = getOrCreateConnectionProfile(connectionProfileName, url, user, password, dialect, driver, path);
ConnectionProfile connectionProfile = buildConnectionProfile(connectionProfileName, url, user, password, dialect, driver, path);
JDBCMetaDataConfiguration jmdc = configureMetaData(connectionProfile);
jmdc.setReverseEngineeringStrategy(createReverseEngineeringStrategy(packageName, manyToMany, oneToOne, optimisticLock));
try {
Expand All @@ -85,7 +85,7 @@ public void generateEntities(

}

private ConnectionProfile getOrCreateConnectionProfile(
private ConnectionProfile buildConnectionProfile(
String connectionProfile,
String url,
String user,
Expand All @@ -100,12 +100,12 @@ private ConnectionProfile getOrCreateConnectionProfile(
if (result == null) {
result = new ConnectionProfile();
}
result.url = connectionProfileHelper.determineURL(url, result);
result.user = connectionProfileHelper.determineUser(user, result);
result.password = connectionProfileHelper.determinePassword(password, result);
result.dialect = connectionProfileHelper.determineDialect(dialect, result);
result.driver = connectionProfileHelper.determineDriverClass(driver, result);
result.path = connectionProfileHelper.determineDriverPath(path, result);
result.url = connectionProfileHelper.determineURL(URL_PROMPT, url, result);
result.user = connectionProfileHelper.determineUser(USER_PROMPT, user, result);
result.password = connectionProfileHelper.determinePassword(PASSWORD_PROMPT, password, result);
result.dialect = connectionProfileHelper.determineDialect(DIALECT_PROMPT, dialect, result);
result.driver = connectionProfileHelper.determineDriverClass(DRIVER_PROMPT, driver, result);
result.path = connectionProfileHelper.determineDriverPath(PATH_TO_DRIVER_PROMPT, path, result);
return result;
}

Expand Down
40 changes: 39 additions & 1 deletion src/main/java/org/hibernate/forge/migrate/MigrationPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,30 @@
import java.util.List;
import java.util.Properties;

import javax.inject.Inject;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

import org.hibernate.cfg.JDBCMetaDataConfiguration;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.forge.common.Constants;
import org.hibernate.forge.common.UrlClassLoaderExecutor;
import org.hibernate.forge.connections.ConnectionProfileHelper;
import org.hibernate.forge.connections.ConnectionProfileNameCompleter;
import org.hibernate.tool.hbm2x.ArtifactCollector;
import org.hibernate.tool.hbm2x.POJOExporter;
import org.jboss.forge.shell.Shell;
import org.jboss.forge.shell.plugins.Alias;
import org.jboss.forge.shell.plugins.DefaultCommand;
import org.jboss.forge.shell.plugins.Help;
import org.jboss.forge.shell.plugins.Option;
import org.jboss.forge.shell.plugins.Plugin;

public class MigrationPlugin {
@Alias("migrate-database")
@Help("Migrate a source database to a target database.")
public class MigrationPlugin implements Plugin, Constants {

private File baseDir, srcDir, binDir;
private String[] script;
Expand All @@ -35,6 +47,32 @@ private void setUp() {
binDir.mkdir();
}

@Inject
private Shell shell;

@Inject
private ConnectionProfileHelper connectionProfileHelper;

@DefaultCommand
public void migrateDatabase(
@Option(name = FROM_CONNECTION_PROFILE, help = FROM_CONNECTION_PROFILE_HELP, required = false, completer = ConnectionProfileNameCompleter.class) String fromConnectionProfileName,
@Option(name = FROM_URL, help = FROM_URL_HELP, required = false) String fromUrl,
@Option(name = FROM_USER, help = FROM_USER_HELP, required = false) String fromUser,
@Option(name = FROM_PASSWORD, help = FROM_PASSWORD_HELP, required = false) String fromPassword,
@Option(name = FROM_DIALECT, help = FROM_DIALECT_HELP, required = false) String fromDialect,
@Option(name = FROM_DRIVER, help = FROM_DRIVER_HELP, required = false) String fromDriver,
@Option(name = FROM_PATH_TO_DRIVER, help = FROM_PATH_TO_DRIVER_HELP, required = false) String fromPath,
@Option(name = TO_CONNECTION_PROFILE, help = TO_CONNECTION_PROFILE_HELP, required = false, completer = ConnectionProfileNameCompleter.class) String toConnectionProfileName,
@Option(name = TO_URL, help = TO_URL_HELP, required = false) String toUrl,
@Option(name = TO_USER, help = TO_USER_HELP, required = false) String toUser,
@Option(name = TO_PASSWORD, help = TO_PASSWORD_HELP, required = false) String toPassword,
@Option(name = TO_DIALECT, help = TO_DIALECT_HELP, required = false) String toDialect,
@Option(name = TO_DRIVER, help = TO_DRIVER_HELP, required = false) String toDriver,
@Option(name = TO_PATH_TO_DRIVER, help = TO_PATH_TO_DRIVER_HELP, required = false) String toPath) {

}


public void testSomething() {
try {
setUp();
Expand Down

0 comments on commit eaa8800

Please sign in to comment.