Skip to content
Permalink
Browse files

Make work with isolated classloader

  • Loading branch information
vietj committed Jun 17, 2015
1 parent 9ccf910 commit 3f0df557437b769f26f9666c4600da1e1aefc423
@@ -23,6 +23,10 @@ import io.vertx.core.logging.Logger
import io.vertx.core.logging.LoggerFactory
import io.vertx.core.spi.VerticleFactory
import org.codehaus.groovy.control.CompilerConfiguration

import java.lang.reflect.Constructor
import java.lang.reflect.Method

/**
* Placeholder
*
@@ -77,13 +81,18 @@ public class GroovyVerticleFactory implements VerticleFactory {
instance = clazz.newInstance();
}

//
Class<?> scriptClass = instance.getClass().getClassLoader().loadClass(Script.class.getName())
Class<?> groovyVerticle = instance.getClass().getClassLoader().loadClass(GroovyVerticle.class.getName())

Verticle verticle;
if (instance instanceof GroovyVerticle) {
GroovyVerticle groovyVerticle = (GroovyVerticle) instance;
verticle = groovyVerticle.asJavaVerticle();
} else if (instance instanceof Script) {
Script script = (Script) instance;
verticle = new ScriptVerticle(script);
if (groovyVerticle.isInstance(instance)) {
Method asJavaVerticle = groovyVerticle.getMethod("asJavaVerticle");
verticle = (Verticle) asJavaVerticle.invoke(instance);
} else if (scriptClass.isInstance(instance)) {
Class<?> scriptVerticleClass = instance.getClass().getClassLoader().loadClass(ScriptVerticle.class.getName());
Constructor<?> ctor = scriptVerticleClass.getConstructor(scriptClass);
verticle = (Verticle) ctor.newInstance(instance);
} else {
throw new UnsupportedOperationException("Not yet implemented");
}
@@ -25,7 +25,7 @@
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import junit.framework.AssertionFailedError;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;

import java.io.File;
@@ -34,7 +34,6 @@
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;

import static org.junit.Assert.*;
@@ -46,8 +45,13 @@

// TODO - needs some tests to ensure config, deploymentID etc are populated correctly in Verticle

public static final AtomicBoolean started = new AtomicBoolean();
public static final AtomicBoolean stopped = new AtomicBoolean();
boolean isStarted() {
return System.getProperty("started").equals("true");
}

boolean isStopped() {
return System.getProperty("stopped").equals("true");
}

private static <T> T assertResult(AsyncResult<T> asyncResult) {
if (asyncResult.succeeded()) {
@@ -86,10 +90,10 @@ private String getAbsolutePath(String classpathResource) throws Exception {
return new File(url.toURI()).getAbsolutePath();
}

@Before
public void before() {
started.set(false);
stopped.set(false);
@After
public void after() {
System.clearProperty("started");
System.clearProperty("stopped");
}

@Test
@@ -98,8 +102,8 @@ public void testDeployVerticleClass() throws Exception {
vertx.deployVerticle(
"io/vertx/lang/groovy/LifeCycleVerticleClass.groovy",
onDeploy));
assertTrue(started.get());
assertTrue(stopped.get());
assertTrue(isStarted());
assertTrue(isStopped());
}

@Test
@@ -108,8 +112,8 @@ public void testAsyncDeployVerticleClass() throws Exception {
vertx.deployVerticle(
"io/vertx/lang/groovy/LifeCycleAsyncVerticleClass.groovy",
onDeploy));
assertTrue(started.get());
assertTrue(stopped.get());
assertTrue(isStarted());
assertTrue(isStopped());
}

@Test
@@ -120,8 +124,8 @@ public void testDeployVerticleClassInstance() throws Exception {
vertx.deployVerticle(
verticle.asJavaVerticle(),
onDeploy));
assertTrue(started.get());
assertTrue(stopped.get());
assertTrue(isStarted());
assertTrue(isStopped());
}

@Test
@@ -130,8 +134,8 @@ public void testDeployVerticleScript() throws Exception {
vertx.deployVerticle(
"io/vertx/lang/groovy/LifeCycleVerticleScript.groovy",
onDeploy));
assertTrue(started.get());
assertTrue(stopped.get());
assertTrue(isStarted());
assertTrue(isStopped());
}

@Test
@@ -143,8 +147,8 @@ public void testDeployVerticleScriptInstance() throws Exception {
vertx.deployVerticle(
verticle,
onDeploy));
assertTrue(started.get());
assertTrue(stopped.get());
assertTrue(isStarted());
assertTrue(isStopped());
}

@Test
@@ -153,17 +157,17 @@ public void testAsyncDeployVerticleScript() throws Exception {
vertx.deployVerticle(
"io/vertx/lang/groovy/LifeCycleAsyncVerticleScript.groovy",
onDeploy));
assertTrue(started.get());
assertTrue(stopped.get());
assertTrue(isStarted());
assertTrue(isStopped());
}

@Test
public void testDeployVerticleClassFromRelativeFile() throws Exception {
String relativePath = getRelativePath("io/vertx/lang/groovy/LifeCycleVerticleClass.groovy");
assertDeploy((vertx, onDeploy) ->
vertx.deployVerticle(relativePath, onDeploy));
assertTrue(started.get());
assertTrue(stopped.get());
assertTrue(isStarted());
assertTrue(isStopped());
}

@Test
@@ -173,8 +177,8 @@ public void testDeployVerticleClassFromAbsoluteFile() throws Exception {
vertx.deployVerticle(
verticlePath,
onDeploy));
assertTrue(started.get());
assertTrue(stopped.get());
assertTrue(isStarted());
assertTrue(isStopped());
}

@Test
@@ -184,7 +188,7 @@ public void testResolveSamePackageFromClassPath() throws Exception {
"io/vertx/lang/groovy/ResolveSamePackageVerticleClass.groovy",
new DeploymentOptions().setConfig(new JsonObject()),
onDeploy));
assertTrue(started.get());
assertTrue(isStarted());
}

@Test
@@ -195,7 +199,7 @@ public void testResolveSamePackageFromRelativeFile() throws Exception {
relativePath,
new DeploymentOptions().setConfig(new JsonObject()),
onDeploy));
assertTrue(started.get());
assertTrue(isStarted());
}

@Test
@@ -206,7 +210,7 @@ public void testResolveSamePackageFromAbsoluteFile() throws Exception {
relativePath,
new DeploymentOptions().setConfig(new JsonObject()),
onDeploy));
assertTrue(started.get());
assertTrue(isStarted());
}

@Test
@@ -216,7 +220,7 @@ public void testResolveChildPackageFromClassPath() throws Exception {
"io/vertx/lang/groovy/ResolveChildPackageVerticleClass.groovy",
new DeploymentOptions().setConfig(new JsonObject()),
onDeploy));
assertTrue(started.get());
assertTrue(isStarted());
}

@Test
@@ -227,7 +231,7 @@ public void testResolveChildPackageFromRelativeFile() throws Exception {
relativePath,
new DeploymentOptions().setConfig(new JsonObject()),
onDeploy));
assertTrue(started.get());
assertTrue(isStarted());
}

@Test
@@ -238,7 +242,7 @@ public void testResolveChildPackageFromAbsoluteFile() throws Exception {
relativePath,
new DeploymentOptions().setConfig(new JsonObject()),
onDeploy));
assertTrue(started.get());
assertTrue(isStarted());
}

@Test
@@ -248,7 +252,7 @@ public void testResolveVertxInClass() throws Exception {
"io/vertx/lang/groovy/ResolveVertxVerticleClass.groovy",
new DeploymentOptions().setConfig(new JsonObject()),
onDeploy));
assertTrue(started.get());
assertTrue(isStarted());
}

@Test
@@ -258,7 +262,7 @@ public void testResolveVertxInScript() throws Exception {
"io/vertx/lang/groovy/ResolveVertxVerticleScript.groovy",
new DeploymentOptions().setConfig(new JsonObject()),
onDeploy));
assertTrue(started.get());
assertTrue(isStarted());
}

@Test
@@ -271,7 +275,7 @@ public void testReuseBindingInScript() throws Exception {
ScriptVerticle verticle = new ScriptVerticle(script);
assertDeploy((vertx, onDeploy) ->
vertx.deployVerticle(verticle, onDeploy));
assertTrue(started.get());
assertTrue(isStarted());
}

@Test
@@ -306,4 +310,24 @@ private void assertDeploy(BiConsumer<Vertx, Handler<AsyncResult<String>>> deploy
vertx.close();
}
}

@Test
public void testRedeployVerticleScript() throws Exception {
assertDeploy((vertx, onDeploy) ->
vertx.deployVerticle(
"io/vertx/lang/groovy/ResolveVertxVerticleScript.groovy",
new DeploymentOptions().setConfig(new JsonObject()).setRedeploy(true),
onDeploy));
assertEquals("true", System.getProperty("started"));
}

@Test
public void testRedeployVerticleClass() throws Exception {
assertDeploy((vertx, onDeploy) ->
vertx.deployVerticle(
"io/vertx/lang/groovy/ResolveVertxVerticleClass.groovy",
new DeploymentOptions().setConfig(new JsonObject()).setRedeploy(true),
onDeploy));
assertEquals("true", System.getProperty("started"));
}
}
@@ -26,15 +26,15 @@ public class LifeCycleAsyncVerticleClass extends GroovyVerticle {
@Override
void start(Future<Void> startFuture) throws Exception {
vertx.timerStream(200).handler({ id ->
DeploymentTest.started.set(true)
System.setProperty("started", "true");
startFuture.complete()
});
}

@Override
void stop(Future<Void> stopFuture) throws Exception {
vertx.timerStream(200).handler({ id ->
DeploymentTest.stopped.set(true)
System.setProperty("stopped", "true");
stopFuture.complete()
});
}
@@ -19,11 +19,11 @@ package io.vertx.lang.groovy
import io.vertx.groovy.core.Future

void vertxStart(Future start) {
DeploymentTest.started.set(true);
System.setProperty("started", "true");
start.complete()
}

void vertxStop(Future stop) {
DeploymentTest.stopped.set(true);
System.setProperty("stopped", "true");
stop.complete()
}
@@ -4,13 +4,13 @@ package io.vertx.lang.groovy
*/
public class LifeCycleVerticleClass extends GroovyVerticle {

@Override
void start() throws Exception {
DeploymentTest.started.set(true)
}
@Override
void start() throws Exception {
System.setProperty("started", "true");
}

@Override
void stop() throws Exception {
DeploymentTest.stopped.set(true)
}
@Override
void stop() throws Exception {
System.setProperty("stopped", "true");
}
}
@@ -1,9 +1,9 @@
package io.vertx.lang.groovy

void vertxStart() {
DeploymentTest.started.set(true);
System.setProperty("started", "true");
}

void vertxStop() {
DeploymentTest.stopped.set(true);
System.setProperty("stopped", "true");
}
@@ -1,5 +1,4 @@
package io.vertx.lang.groovy

import io.vertx.lang.groovy.DeploymentTest
System.setProperty("started", "true");

DeploymentTest.started.set(true)
@@ -7,14 +7,14 @@ import io.vertx.lang.groovy.sub.InChildPackage
*/
public class ResolveChildPackageVerticleClass extends GroovyVerticle {

@Override
void start() throws Exception {
InChildPackage o = new InChildPackage()
DeploymentTest.started.set(true)
}
@Override
void start() throws Exception {
InChildPackage o = new InChildPackage()
System.setProperty("started", "true");
}

@Override
void stop() throws Exception {
DeploymentTest.stopped.set(true)
}
@Override
void stop() throws Exception {
System.setProperty("stopped", "true");
}
}

0 comments on commit 3f0df55

Please sign in to comment.
You can’t perform that action at this time.