Skip to content

Commit

Permalink
Release 3.0: MetaServletCaller CLI added with migrateDatabase action
Browse files Browse the repository at this point in the history
  • Loading branch information
jlolling committed Feb 2, 2017
1 parent e366f37 commit d19fdf6
Show file tree
Hide file tree
Showing 33 changed files with 311 additions and 78 deletions.
30 changes: 28 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.cimt.talendcomp</groupId>
<artifactId>cimt-talendcomp-tacws</artifactId>
<version>2.3</version>
<version>3.0</version>
<name>TAC Rest WS</name>
<repositories>
<repository>
Expand All @@ -28,7 +28,7 @@
<plugin>
<groupId>de.cimt.talendcomp</groupId>
<artifactId>cimt-talendcomp-maven-plugin</artifactId>
<version>1.6</version>
<version>1.7</version>
<configuration>
<componentName>tRunTask</componentName>
</configuration>
Expand All @@ -40,6 +40,27 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<outputFile>${project.basedir}/target/cimt-metaservletcaller-${project.version}.jar</outputFile>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>de.cimt.talendcomp.tac.MetaServletCaller</mainClass>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
Expand All @@ -60,5 +81,10 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.cimt.talendcomp</groupId>
<artifactId>cimt-talendcomp-json</artifactId>
<version>5.4</version>
</dependency>
</dependencies>
</project>
96 changes: 96 additions & 0 deletions src/main/java/de/cimt/talendcomp/tac/MetaServletCaller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package de.cimt.talendcomp.tac;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class MetaServletCaller {

private static final Logger logger = Logger.getLogger(MetaServletCaller.class);

public static void main(String[] args) {
BasicConfigurator.configure();
if (args == null || args.length == 0) {
printUsage();
System.exit(1);
}
String action = args[0];
if ("migrateDatabase".equalsIgnoreCase(action)) {
if (args.length < 2) {
printUsage();
System.out.println("Command to migrate a database: java -jar cimt-metaservletcaller-<version>.jar migrateDatabase /path/to/your-configuration.properties");
System.exit(1);
}
String configFilePath = args[1];
try {
migrateDatabase(configFilePath);
} catch (Exception e) {
logger.error("migrateDatabase failed: " + e.getMessage());
System.exit(2);
}
} else {
printUsage();
System.out.println("Unknown action: " + action);
System.exit(1);
}
System.exit(0);
}

private static void migrateDatabase(String configFile) throws Exception {
Properties properties = loadConfiguration(configFile);
boolean debug = "true".equals(properties.getProperty("debug"));
if (debug) {
Logger.getRootLogger().setLevel(Level.DEBUG);
Logger.getRootLogger().info("Debug level set");
}
System.out.println("Setup migrate database request...");
TACConnection conn = new TACConnection(properties.getProperty("targetTacUrl"));
MigrateDatabaseAction action = new MigrateDatabaseAction(conn);
action.setDbConfigPassword(properties.getProperty("dbConfigPassword"));
action.setSourceUser(properties.getProperty("sourceUser"));
action.setSourcePasswd(properties.getProperty("sourcePasswd"));
action.setSourceUrl(properties.getProperty("sourceUrl"));
action.setTargetUser(properties.getProperty("targetUser"));
action.setTargetPasswd(properties.getProperty("targetPasswd"));
action.setTargetUrl(properties.getProperty("targetUrl"));
System.out.println("Send migrate database request...");
action.execute();
}

private static void printUsage() {
System.out.println("Caller for TAC-Metaservlet");
System.out.println("@cimt AG version 1.0");
System.out.println("Usage: java -jar cimt-metaservletcaller-<version>.jar <action> [<more parameters>]");
}

public static Properties loadConfiguration(String configFilePath) throws Exception {
if (configFilePath == null || configFilePath.trim().isEmpty()) {
throw new IllegalArgumentException("configuration file path cannot be null or empty!");
}
File configFile = new File(configFilePath);
if (configFile.isAbsolute() == false) {
configFile = new File(System.getProperty("work.dir"), configFilePath);
}
if (configFile.exists() == false) {
throw new Exception("Given configuration file: " + configFile.getAbsolutePath() + " does not exist!");
}
System.out.println("Load configuration file: " + configFile.getAbsolutePath());
Properties properties = new Properties();
InputStream in = new FileInputStream(configFile);
try {
properties.load(in);
} catch (Exception e) {
logger.error("loadConfiguration failed: " + e.getMessage());
throw e;
} finally {
in.close();
}
return properties;
}

}
129 changes: 129 additions & 0 deletions src/main/java/de/cimt/talendcomp/tac/MigrateDatabaseAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package de.cimt.talendcomp.tac;

import org.apache.log4j.Logger;

public class MigrateDatabaseAction extends TACAction {

private static Logger logger = Logger.getLogger(MigrateDatabaseAction.class);
private String sourceUrl = null;
private String sourceUser = null;
private String sourcePasswd = null;
private String targetUrl = null;
private String targetUser = null;
private String targetPasswd = null;
private String mode = "synchronous";
private String dbConfigPassword = null;

public MigrateDatabaseAction(TACConnection connection) {
super(connection);
}

@Override
public String getAction() {
return "migrateDatabase";
}

@Override
public void execute() throws Exception {
if (dbConfigPassword == null || dbConfigPassword.trim().isEmpty()) {
throw new Exception("dbConfigPassword not set!");
}
addParam("dbConfigPassword", dbConfigPassword);
addParam("mode", mode);
if (sourceUrl == null || sourceUrl.trim().isEmpty()) {
throw new Exception("sourceUrl not set!");
}
addParam("sourceUrl", sourceUrl);
if (sourceUser == null || sourceUser.trim().isEmpty()) {
throw new Exception("sourceUser not set!");
}
addParam("sourceUser", sourceUser);
if (sourcePasswd == null || sourcePasswd.trim().isEmpty()) {
throw new Exception("sourcePasswd not set!");
}
addParam("sourcePasswd", sourcePasswd);
if (targetUrl == null || targetUrl.trim().isEmpty()) {
throw new Exception("targetUrl not set!");
}
addParam("targetUrl", targetUrl);
if (targetUser == null || targetUser.trim().isEmpty()) {
throw new Exception("targetUser not set!");
}
addParam("targetUser", targetUser);
if (targetPasswd == null || targetPasswd.trim().isEmpty()) {
throw new Exception("targetPasswd not set!");
}
addParam("targetPasswd", targetPasswd);
String result = null;
try {
result = executeRequest();
} catch (Exception e) {
logger.error("Execute migrateDatabase failed: " + e.getMessage(), e);
throw e;
}
if (result != null && result.trim().startsWith(ERROR)) {
logger.error("Execute migrateDatabase failed: " + result);
} else {
logger.info("Response: " + result);
System.out.println("Finished migrating database. Please check the result!");
}
}

public String getSourceUrl() {
return sourceUrl;
}

public void setSourceUrl(String sourceUrl) {
this.sourceUrl = sourceUrl;
}

public String getSourceUser() {
return sourceUser;
}

public void setSourceUser(String sourceUser) {
this.sourceUser = sourceUser;
}

public String getSourcePasswd() {
return sourcePasswd;
}

public void setSourcePasswd(String sourcePasswd) {
this.sourcePasswd = sourcePasswd;
}

public String getTargetUrl() {
return targetUrl;
}

public void setTargetUrl(String targetUrl) {
this.targetUrl = targetUrl;
}

public String getTargetUser() {
return targetUser;
}

public void setTargetUser(String targetUser) {
this.targetUser = targetUser;
}

public String getTargetPasswd() {
return targetPasswd;
}

public void setTargetPasswd(String targetPasswd) {
this.targetPasswd = targetPasswd;
}

public String getDbConfigPassword() {
return dbConfigPassword;
}

public void setDbConfigPassword(String dbConfigPassword) {
this.dbConfigPassword = dbConfigPassword;
}


}
34 changes: 12 additions & 22 deletions src/main/java/de/cimt/talendcomp/tac/RunTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.node.ObjectNode;

import de.cimt.talendcomp.json.JsonDocument;

public class RunTask extends TaskAction {

public static final String ACTION_NAME = "runTask";
Expand Down Expand Up @@ -97,39 +101,25 @@ public void addContextParam(String key, Object value) {
}

private String buildContextParamJson() {
StringBuilder sb = new StringBuilder();
sb.append("{");
boolean firstLoop = true;
JsonDocument doc = new JsonDocument(false);
ObjectNode context = (ObjectNode) doc.getRootNode();
for (Map.Entry<String, Object> entry : contextParams.entrySet()) {
if (firstLoop) {
firstLoop = false;
} else {
sb.append(",");
}
sb.append("\"");
sb.append(entry.getKey());
sb.append("\":");
sb.append(convertValue(entry.getValue()));
context.put(entry.getKey(), convertValue(entry.getValue()));
}
sb.append("}");
return sb.toString();
return doc.toString();
}

private String convertValue(Object value) {
if (value != null) {
if (value instanceof Date) {
return "\"" + date_param_format.format((Date) value) + "\"";
return date_param_format.format((Date) value);
} else if (value instanceof String) {
String s = ((String) value).replace("\\", "\\\\");
s = s.replace("\n", "\\n");
s = s.replace("\r", "\\r");
s = s.replace("\t", "\\t");
return "\"" + s + "\"";
return (String) value;
} else {
return "\"" + value.toString() + "\"";
return value.toString();
}
} else {
return "";
return null;
}
}

Expand Down
Loading

0 comments on commit d19fdf6

Please sign in to comment.