Skip to content

Instantly share code, notes, and snippets.

@makotan
Created August 17, 2012 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makotan/3378414 to your computer and use it in GitHub Desktop.
Save makotan/3378414 to your computer and use it in GitHub Desktop.
vert.x IDE Runner
com.github.makotan.FileCheck -conf target/test-classes/filecheck.json -cp target/classes:target/test-classes
package com.github.makotan;
import org.vertx.java.core.Handler;
import org.vertx.java.core.SimpleHandler;
import org.vertx.java.core.impl.DefaultVertx;
import org.vertx.java.core.impl.VertxInternal;
import org.vertx.java.core.json.DecodeException;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.core.logging.Logger;
import org.vertx.java.core.logging.impl.LoggerFactory;
import org.vertx.java.deploy.impl.Args;
import org.vertx.java.deploy.impl.VerticleManager;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* Created with IntelliJ IDEA.
* User: makotan
* Date: 2012/08/17
* Time: 20:46
* To change this template use File | Settings | File Templates.
*/
public class IDERunner {
private static final Logger log = LoggerFactory.getLogger(IDERunner.class);
private static CountDownLatch cdl = new CountDownLatch(1);
private static final String CP_SEPARATOR = System.getProperty("os.name").startsWith("Windows") ? ";" : ":";
private static VerticleManager mgr;
public static void main(String args[]) throws InterruptedException {
IDERunner runner = new IDERunner();
runner.execute(args);
cdl.await();
}
public void execute(String arg[]) {
Args args = new Args(arg);
String repo = args.map.get("-repo");
boolean worker = args.map.get("-worker") != null;
String cp = args.map.get("-cp");
if (cp == null) {
cp = ".";
}
JsonObject config = getConfig(args);
URL[] urls = getCp(args);
VertxInternal vertx = new DefaultVertx();
final VerticleManager mgr = new VerticleManager(vertx,repo);
Handler<String> doneHandler = new Handler<String>() {
public void handle(String id) {
if (id == null) {
// Failed to deploy
mgr.unblock();
}
}
};
String main = arg[0];
mgr.deploy(worker, main, config, urls, 1, null, doneHandler);
addShutdownHook();
mgr.block();
}
JsonObject getConfig(Args args) {
String configFile = args.map.get("-conf");
JsonObject conf = null;
if (configFile != null) {
try {
String sconf = new Scanner(new File(configFile)).useDelimiter("\\A").next();
try {
conf = new JsonObject(sconf);
} catch (DecodeException e) {
log.error("Configuration file does not contain a valid JSON object");
return conf;
}
} catch (FileNotFoundException e) {
log.error("Config file " + configFile + " does not exist");
return conf;
}
}
return conf;
}
URL[] getCp(Args args) {
String cp = args.map.get("-cp");
if (cp == null) {
cp = ".";
}
// Convert to URL[]
String[] parts;
if (cp.contains(CP_SEPARATOR)) {
parts = cp.split(CP_SEPARATOR);
} else {
parts = new String[] { cp };
}
int index = 0;
final URL[] urls = new URL[parts.length];
for (String part: parts) {
try {
URL url = new File(part).toURI().toURL();
urls[index++] = url;
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Invalid path " + part + " in cp " + cp) ;
}
}
return urls;
}
private void addShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
final CountDownLatch latch = new CountDownLatch(1);
mgr.undeployAll(new SimpleHandler() {
public void handle() {
latch.countDown();
}
});
while (true) {
try {
if (!latch.await(30, TimeUnit.SECONDS)) {
log.error("Timed out waiting to undeploy");
}
break;
} catch (InterruptedException e) {
//OK - can get spurious interupts
}
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment