Skip to content

Commit

Permalink
https://github.com/vert-x/vert.x/issues/366
Browse files Browse the repository at this point in the history
  • Loading branch information
purplefox committed Oct 4, 2012
1 parent dbca645 commit a991775
Show file tree
Hide file tree
Showing 17 changed files with 92 additions and 122 deletions.
12 changes: 12 additions & 0 deletions src/dist/conf/langs.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Mapping between main file extensions and the verticle factory for the class

java=org.vertx.java.deploy.impl.java.JavaVerticleFactory
class=org.vertx.java.deploy.impl.java.JavaVerticleFactory
js=org.vertx.java.deploy.impl.rhino.RhinoVerticleFactory
rb=org.vertx.java.deploy.impl.jruby.JRubyVerticleFactory
groovy=org.vertx.groovy.deploy.impl.groovy.GroovyVerticleFactory
py=org.vertx.java.deploy.impl.jython.JythonVerticleFactory

# Default - if none match this will be assumed

default=org.vertx.java.deploy.impl.java.JavaVerticleFactory
2 changes: 1 addition & 1 deletion src/dist/scripts/vertx
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,4 @@ function splitJvmOpts() {
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $VERTX_OPTS

exec "$JAVACMD" "${JVM_OPTS[@]}" -Dvertx.install=$APP_HOME -Djava.util.logging.config.file=$APP_HOME/conf/logging.properties -Djruby.home=$JRUBY_HOME -classpath "$CLASSPATH" org.vertx.java.deploy.impl.cli.Starter "$@"
exec "$JAVACMD" "${JVM_OPTS[@]}" -Djava.util.logging.config.file=$APP_HOME/conf/logging.properties -Djruby.home=$JRUBY_HOME -classpath "$CLASSPATH" org.vertx.java.deploy.impl.cli.Starter "$@"
2 changes: 1 addition & 1 deletion src/dist/scripts/vertx.bat
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ set CMD_LINE_ARGS=%$
set CLASSPATH=%APP_HOME%\lib\*;%APP_HOME%\conf;%JYTHON_HOME%\jython.jar;%JRUBY_HOME%\lib\jruby.jar

@rem Execute vertx
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %VERTX_OPTS% %VERTX_MODULE_OPTS% -Dvertx.install="%APP_HOME%" -Djruby.home="%JRUBY_HOME%" -Djython.home="%JYTHON_HOME%" -Djava.util.logging.config.file="%APP_HOME%\conf\logging.properties" -classpath "%CLASSPATH%" org.vertx.java.deploy.impl.cli.Starter %CMD_LINE_ARGS%
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %VERTX_OPTS% %VERTX_MODULE_OPTS% -Djruby.home="%JRUBY_HOME%" -Djython.home="%JYTHON_HOME%" -Djava.util.logging.config.file="%APP_HOME%\conf\logging.properties" -classpath "%CLASSPATH%" org.vertx.java.deploy.impl.cli.Starter %CMD_LINE_ARGS%

:end
@rem End local scope for the variables with windows NT shell
Expand Down
2 changes: 1 addition & 1 deletion vertx-examples/src/main/vertx-dev
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,4 @@ function splitJvmOpts() {
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $VERTX_OPTS

exec "$JAVACMD" "${JVM_OPTS[@]}" -Dvertx.install=$APP_HOME -Djava.util.logging.config.file=$APP_HOME/src/dist/conf/logging.properties -Djruby.home=$JRUBY_HOME -classpath "$CLASSPATH" org.vertx.java.deploy.impl.cli.Starter "$@"
exec "$JAVACMD" "${JVM_OPTS[@]}" -Djava.util.logging.config.file=$APP_HOME/src/dist/conf/logging.properties -Djruby.home=$JRUBY_HOME -classpath "$CLASSPATH" org.vertx.java.deploy.impl.cli.Starter "$@"
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,6 @@ public class GroovyVerticleFactory implements VerticleFactory {
this.mgr = mgr
}

@Override
public String getLanguage() {
return "groovy"
}

@Override
public boolean isFactoryFor(String main) {
if (main.endsWith(".groovy")) {
return true
}
return false
}

public Verticle createVerticle(String main, ClassLoader cl) throws Exception {
URL url = cl.getResource(main)
if (url == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,6 @@ public void init(VerticleManager mgr) {
this.mgr = mgr;
}

@Override
public String getLanguage() {
return "java";
}

@Override
public boolean isFactoryFor(String main) {
if (isJavaSource(main)) {
return true;
}
if (main.endsWith(".class")) {
return true;
}
if (main.endsWith(".jar")) {
return true;
}
return false;
}

private boolean isJavaSource(String main) {
return main.endsWith(".java");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,6 @@ public void init(VerticleManager mgr) {
this.mgr = mgr;
}

@Override
public String getLanguage() {
return "ruby";
}

@Override
public boolean isFactoryFor(String main) {
if (main.endsWith(".rb")) {
return true;
}
return false;
}

public Verticle createVerticle(String main, ClassLoader cl) throws Exception {
if (System.getProperty("jruby.home") == null) {
throw new IllegalStateException("In order to deploy Ruby applications you must set JRUBY_HOME to point " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,6 @@ public void init(VerticleManager mgr) {
this.mgr = mgr;
}

@Override
public String getLanguage() {
return "python";
}

@Override
public boolean isFactoryFor(String main) {
if (main.endsWith(".py")) {
return true;
}
return false;
}

public Verticle createVerticle(String main, ClassLoader cl) throws Exception {
Verticle app = new JythonVerticle(main, cl);
return app;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,6 @@ public void init(VerticleManager mgr) {
this.mgr = mgr;
}

@Override
public String getLanguage() {
return "rhino";
}

@Override
public boolean isFactoryFor(String main) {
if (main.endsWith(".js") || main.endsWith(".coffee")) {
return true;
}
return false;
}

public Verticle createVerticle(String main, ClassLoader cl) throws Exception {
Verticle app = new RhinoVerticle(main, cl);
return app;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ public interface VerticleFactory {
public static final Iterable<VerticleFactory> factories = ServiceLoader.load(VerticleFactory.class);

void init(VerticleManager manager);

String getLanguage();

boolean isFactoryFor(String main);

Verticle createVerticle(String main, ClassLoader parentCL) throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public class Deployment {
public final String name;
public final String modName;
public final int instances;
public final VerticleFactory factory;
public final JsonObject config;
public final URL[] urls;
public final File modDir;
Expand All @@ -40,13 +39,12 @@ public class Deployment {
public final String parentDeploymentName;
public final boolean autoRedeploy;

public Deployment(String name, String modName, int instances, VerticleFactory factory, JsonObject config,
public Deployment(String name, String modName, int instances, JsonObject config,
URL[] urls, File modDir, String parentDeploymentName,
boolean autoRedeploy) {
this.name = name;
this.modName = modName;
this.instances = instances;
this.factory = factory;
this.config = config;
this.urls = urls;
this.modDir = modDir;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ public ParentLastURLClassLoader(URL[] classpath, ClassLoader parent) {
@Override
protected synchronized Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException {

if (name.startsWith("org.vertx.java.core.logging.impl.LoggerFactory")) {
// Don't allow system logger to be used from within app since it maintains static references which hang around
// after app is undeployed
throw new ClassNotFoundException("Do not use org.vertx.java.core.logging.impl.LoggerFactory from within application");
}
Class<?> c = findLoadedClass(name);
if (c == null) {
if (isSystemClass(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.core.logging.Logger;
import org.vertx.java.deploy.Verticle;
import org.vertx.java.deploy.VerticleFactory;

/**
* @author <a href="http://tfox.org">Tim Fox</a>
Expand All @@ -34,18 +35,21 @@ class VerticleHolder implements DeploymentHandle {
//We put the config here too so it's still accessible to the verticle after it has been deployed
//(deploy is async)
final JsonObject config;
final VerticleFactory factory;

VerticleHolder(Deployment deployment, Context context, Verticle verticle, String loggerName,
Logger logger, JsonObject config) {
Logger logger, JsonObject config,
VerticleFactory factory) {
this.deployment = deployment;
this.context = context;
this.verticle = verticle;
this.loggerName = loggerName;
this.logger = logger;
this.config = config;
this.factory = factory;
}

public void reportException(Throwable t) {
deployment.factory.reportException(t);
factory.reportException(t);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class VerticleManager implements ModuleReloader {
// The user mods dir
private final File modRoot;
private final CountDownLatch stopLatch = new CountDownLatch(1);
private Map<String, VerticleFactory> factories;
private Map<String, String> factoryNames = new HashMap<>();
private final String defaultRepo;
private final Redeployer redeployer;

Expand All @@ -85,25 +85,38 @@ public VerticleManager(VertxInternal vertx, String defaultRepo) {
this.defaultRepo = defaultRepo == null ? DEFAULT_REPO_HOST : defaultRepo;
VertxLocator.vertx = vertx;
VertxLocator.container = new Container(this);
String installDir = System.getProperty("vertx.install");
if (installDir == null) {
installDir = ".";
}
String modDir = System.getProperty("vertx.mods");
if (modDir != null && !modDir.trim().equals("")) {
modRoot = new File(modDir);
} else {
// Default to local module directory called 'mods'
modRoot = new File("mods");
}

this.factories = new HashMap<>();
this.redeployer = new Redeployer(vertx, modRoot, this);

// Find and load VerticleFactories
Iterable<VerticleFactory> services = VerticleFactory.factories;
for (VerticleFactory vf : services) {
factories.put(vf.getLanguage(), vf);
InputStream is = null;
try {
is = getClass().getClassLoader().getResourceAsStream("langs.properties");
if (is == null) {
log.warn("No language mappings found!");
} else {
Properties props = new Properties();
props.load(new BufferedInputStream(is));
Enumeration<?> en = props.propertyNames();
while (en.hasMoreElements()) {
String propName = (String)en.nextElement();
factoryNames.put(propName, props.getProperty(propName));
}
}
} catch (IOException e) {
log.error("Failed to load langs.properties: " + e.getMessage());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ignore) {
}
}
}
}

Expand Down Expand Up @@ -580,27 +593,25 @@ private synchronized void doDeploy(String depName,
final File modDir,
final Context context,
final Handler<String> doneHandler) {
// Infer the main type
String language = "java";
for (VerticleFactory vf : factories.values()) {
if (vf.isFactoryFor(main)) {
language = vf.getLanguage();
break;
}
}

final String deploymentName =
depName != null ? depName : "deployment-" + UUID.randomUUID().toString();

log.debug("Deploying name : " + deploymentName + " main: " + main +
" instances: " + instances);

if (!factories.containsKey(language)) {
throw new IllegalArgumentException("Unsupported language: " + language);
int dotIndex = main.lastIndexOf('.');
if (dotIndex == -1) {
throw new IllegalArgumentException("Invalid main: " + main);
}
String extension = main.substring(dotIndex + 1);
String factoryName = factoryNames.get(extension);
if (factoryName == null) {
// Use the default
factoryName = factoryNames.get("default");
if (factoryName == null) {
throw new IllegalArgumentException("No language mapping found and no default specified in langs.properties");
}
}

final VerticleFactory verticleFactory = factories.get(language);
verticleFactory.init(this);

final int instCount = instances;

Expand All @@ -621,7 +632,7 @@ void done(boolean res) {
final AggHandler aggHandler = new AggHandler();

String parentDeploymentName = getDeploymentName();
final Deployment deployment = new Deployment(deploymentName, modName, instances, verticleFactory,
final Deployment deployment = new Deployment(deploymentName, modName, instances,
config == null ? new JsonObject() : config.copy(), urls, modDir, parentDeploymentName,
autoRedeploy);
addDeployment(deploymentName, deployment);
Expand All @@ -644,6 +655,29 @@ void done(boolean res) {
sharedLoader: new ParentLastURLClassLoader(urls, getClass().getClassLoader());
Thread.currentThread().setContextClassLoader(cl);

// We load the VerticleFactory class using the verticle classloader - this allows
// us to put language implementations in modules

Class clazz;
try {
clazz = cl.loadClass(factoryName);
} catch (ClassNotFoundException e) {
log.error("Cannot find class " + factoryName + " to load");
doneHandler.handle(null);
return;
}

final VerticleFactory verticleFactory;
try {
verticleFactory = (VerticleFactory)clazz.newInstance();
} catch (Exception e) {
log.error("Failed to instantiate VerticleFactory: " + e.getMessage());
doneHandler.handle(null);
return;
}

verticleFactory.init(this);

Runnable runner = new Runnable() {
public void run() {

Expand Down Expand Up @@ -673,7 +707,7 @@ public void handle() {
verticle.setContainer(new Container(VerticleManager.this));

try {
addVerticle(deployment, verticle);
addVerticle(deployment, verticle, verticleFactory);
if (modDir != null) {
setPathAdjustment(modDir);
}
Expand All @@ -700,12 +734,14 @@ public void handle() {
}

// Must be synchronized since called directly from different thread
private synchronized void addVerticle(Deployment deployment, Verticle verticle) {
private synchronized void addVerticle(Deployment deployment, Verticle verticle,
VerticleFactory factory) {
String loggerName = deployment.name + "-" + deployment.verticles.size();
Logger logger = LoggerFactory.getLogger(loggerName);
Context context = Context.getContext();
VerticleHolder holder = new VerticleHolder(deployment, context, verticle,
loggerName, logger, deployment.config);
loggerName, logger, deployment.config,
factory);
deployment.verticles.add(holder);
context.setDeploymentHandle(holder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public class Starter {

private static final Logger log = LoggerFactory.getLogger(Starter.class);

private static final String CP_SEPARATOR =
System.getProperty("os.name").startsWith("Windows") ? ";" : ":";
private static final String CP_SEPARATOR = System.getProperty("path.separator");

private static final String VERSION = "vert.x-1.2.3.final";

Expand Down
Loading

0 comments on commit a991775

Please sign in to comment.