Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

UtilRegistryTransformer: restore the runtime context class loader bef…

…ore running Util.loadModule code
  • Loading branch information...
commit 44d64c000d1b1fcbe55f04badeb1eaf8c57bf6dc 1 parent 626a67d
@FroMage FroMage authored
View
2  impl/src/main/java/ceylon/modules/jboss/runtime/CeylonModuleLoader.java
@@ -292,7 +292,7 @@ protected ModuleSpec findModule(ModuleIdentifier moduleIdentifier) throws Module
dependencies.put(moduleIdentifier, deps);
- builder.setClassFileTransformer(new UtilRegistryTransformer(moduleIdentifier, artifact));
+ builder.setClassFileTransformer(new UtilRegistryTransformer(moduleIdentifier, artifact, RUNTIME));
return builder.create();
} catch (Exception e) {
View
18 impl/src/main/java/ceylon/modules/jboss/runtime/SecurityActions.java
@@ -21,6 +21,7 @@
import java.security.AccessController;
import java.security.PrivilegedAction;
+import java.security.PrivilegedExceptionAction;
/**
* Security actions.
@@ -46,4 +47,21 @@ public ClassLoader run() {
return module.getClassLoader();
}
}
+
+ public static ClassLoader setContextClassLoader(final ClassLoader cl) throws Exception {
+ final SecurityManager sm = System.getSecurityManager();
+ if (sm != null) {
+ return (ClassLoader) AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
+ public Object run() throws Exception {
+ ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
+ Thread.currentThread().setContextClassLoader(cl);
+ return oldClassLoader;
+ }
+ });
+ } else {
+ ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
+ Thread.currentThread().setContextClassLoader(cl);
+ return oldClassLoader;
+ }
+ }
}
View
37 impl/src/main/java/ceylon/modules/jboss/runtime/UtilRegistryTransformer.java
@@ -22,7 +22,10 @@
import com.redhat.ceylon.cmr.api.ArtifactResult;
import com.redhat.ceylon.cmr.api.RepositoryManager;
+
+import org.jboss.modules.Module;
import org.jboss.modules.ModuleIdentifier;
+import org.jboss.modules.ModuleLoadException;
/**
* Per module Util registry.
@@ -39,9 +42,12 @@
private final ModuleIdentifier mi;
private final ArtifactResult result;
- public UtilRegistryTransformer(ModuleIdentifier mi, ArtifactResult result) {
+ private ModuleIdentifier runtime;
+
+ public UtilRegistryTransformer(ModuleIdentifier mi, ArtifactResult result, ModuleIdentifier runtime) {
this.mi = mi;
this.result = result;
+ this.runtime = runtime;
}
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
@@ -49,7 +55,34 @@ public UtilRegistryTransformer(ModuleIdentifier mi, ArtifactResult result) {
synchronized (this) {
if (done == false) {
done = true;
- registerModule(mi.getName(), mi.getSlot(), result, loader);
+ ClassLoader runtimeClassLoader = null;
+ try {
+ Module runtimeModule = org.jboss.modules.Module.getBootModuleLoader().loadModule(runtime);
+ runtimeClassLoader = runtimeModule.getClassLoader();
+ } catch (ModuleLoadException e) {
+ e.printStackTrace();
+ }
+ ClassLoader oldClassLoader = null;
+ boolean setClassLoader = false;
+ if(runtimeClassLoader != null){
+ try {
+ oldClassLoader = SecurityActions.setContextClassLoader(runtimeClassLoader);
+ setClassLoader = true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ try{
+ registerModule(mi.getName(), mi.getSlot(), result, loader);
+ }finally{
+ if(setClassLoader){
+ try {
+ SecurityActions.setContextClassLoader(oldClassLoader);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
}
}
}

33 comments on commit 44d64c0

@alesj
Collaborator

Why do we need this?
To fix that Aether error?

@FroMage
Owner

Yes.

@alesj
Collaborator

Ugh, no need for this.
Let me fix it properly ...

@FroMage
Owner

Gladly!

@alesj
Collaborator

OK, done, the true fix is in CMR. ;-)

@FroMage
Owner

Aw man. Let me try.

@FroMage
Owner

So it works for Swing, but the test DefaultTestCase.testVisibility appears to fail now.

@FroMage
Owner
helloworld from [Module "biz.wiz.quiz:1.0.0.GA" from Ceylon ModuleLoader: RootRepositoryManager: FileContentStore: /home/stephane/.ceylon/cache]

java.lang.ClassNotFoundException: helloworld from [Module "biz.wiz.quiz:1.0.0.GA" from Ceylon ModuleLoader: RootRepositoryManager: FileContentStore: /home/stephane/.ceylon/cache]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:468)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:456)
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:120)
at biz.wiz.quiz.run_.main(run_.java:25)
at ceylon.modules.api.runtime.SecurityActions.invokeRunInternal(SecurityActions.java:58)
at ceylon.modules.api.runtime.SecurityActions.invokeRun(SecurityActions.java:48)
at ceylon.modules.api.runtime.AbstractRuntime.invokeRun(AbstractRuntime.java:84)
at ceylon.modules.api.runtime.AbstractRuntime.execute(AbstractRuntime.java:131)
at ceylon.modules.api.runtime.AbstractRuntime.execute(AbstractRuntime.java:119)
at ceylon.modules.Main.execute(Main.java:91)
at ceylon.modules.Main.main(Main.java:46)
at org.jboss.modules.Module.run(Module.java:270)
at org.jboss.modules.Main.main(Main.java:294)
at org.jboss.ceylon.test.modules.ModulesTest.execute(ModulesTest.java:202)
at org.jboss.ceylon.test.modules.ModulesTest.execute(ModulesTest.java:180)
at org.jboss.ceylon.test.modules.ModulesTest.testArchive(ModulesTest.java:124)
at org.jboss.ceylon.test.modules.ModulesTest.testArchive(ModulesTest.java:86)
at org.jboss.ceylon.test.modules.defaultx.test.DefaultTestCase.testVisibility(DefaultTestCase.java:48)
@FroMage
Owner

Note that that test imports the default module, which should not be allowed.

@alesj
Collaborator

Hmmm, strange, it works for me in IDE and Maven, but fails from Ant.

@alesj
Collaborator

Or not ...

Skywalker:ceylon-runtime alesj$ ant test
Buildfile: /Users/alesj/projects/ceylon/ceylon-runtime/build.xml

runtime.classes:

runtime.jar:

build:

dist:

compile.tests:

dist-for-tests:

test:
[junit] Running org.jboss.ceylon.test.modules.defaultx.test.DefaultTestCase
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.459 sec
[junit] Running org.jboss.ceylon.test.modules.examples.test.ExamplesTestCase
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.359 sec
[junit] Running org.jboss.ceylon.test.modules.interop.test.AetherInteropTestCase
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 1.032 sec
[junit] Running org.jboss.ceylon.test.modules.interop.test.InteropTestCase
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.45 sec
[junit] Running org.jboss.ceylon.test.modules.ondemand.test.OnDemandTestCase
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.463 sec
[junit] Running org.jboss.ceylon.test.modules.repo.test.RepositoryTestCase
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.348 sec
[junit] Running org.jboss.ceylon.test.modules.smoke.test.SmokeTestCase
[junit] Tests run: 4, Failures: 0, Errors: 0, Time elapsed: 0.875 sec
[junit] Running org.jboss.ceylon.test.modules.sources.test.SourcesTestCase
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.053 sec
[junitreport] Processing /Users/alesj/projects/ceylon/ceylon-runtime/build/test-reports/TESTS-TestSuites.xml to /var/folders/x0/hk__mkqd78z752spn913k8sr0000gn/T/null140445917
[junitreport] Loading stylesheet jar:file:/usr/share/ant/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 524ms
[junitreport] Deleting: /var/folders/x0/hk__mkqd78z752spn913k8sr0000gn/T/null140445917

BUILD SUCCESSFUL

@quintesse
Collaborator

Might that have to do with the fact that the ant task uses the new launcher that uses the CeylonClassLoader?

@quintesse
Collaborator

BTW this is what I get:

    [junit] Running org.jboss.ceylon.test.modules.defaultx.test.DefaultTestCase
    [junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 1.083 sec
    [junit] Running org.jboss.ceylon.test.modules.examples.test.ExamplesTestCase
    [junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.817 sec
    [junit] Running org.jboss.ceylon.test.modules.interop.test.AetherInteropTestCase
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 2.742 sec
    [junit] Test org.jboss.ceylon.test.modules.interop.test.AetherInteropTestCase FAILED
    [junit] Running org.jboss.ceylon.test.modules.interop.test.InteropTestCase
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 1.68 sec
    [junit] Test org.jboss.ceylon.test.modules.interop.test.InteropTestCase FAILED
    [junit] Running org.jboss.ceylon.test.modules.ondemand.test.OnDemandTestCase
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.826 sec
    [junit] Running org.jboss.ceylon.test.modules.repo.test.RepositoryTestCase
    [junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.577 sec
    [junit] Running org.jboss.ceylon.test.modules.smoke.test.SmokeTestCase
    [junit] Tests run: 4, Failures: 0, Errors: 0, Time elapsed: 3.079 sec
    [junit] Running org.jboss.ceylon.test.modules.sources.test.SourcesTestCase
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.09 sec
@quintesse
Collaborator

The error being:

ceylon.modules.CeylonRuntimeException: Could not find module: org.jboss.modules.jboss-modules/1.1.1.GA (invalid version?)
at ceylon.modules.jboss.runtime.AbstractJBossRuntime.createClassLoader(AbstractJBossRuntime.java:70)
at ceylon.modules.api.runtime.AbstractRuntime.execute(AbstractRuntime.java:105)
at ceylon.modules.Main.execute(Main.java:69)
at ceylon.modules.Main.main(Main.java:42)
at org.jboss.modules.Module.run(Module.java:270)
at org.jboss.modules.Main.main(Main.java:294)
at org.jboss.ceylon.test.modules.ModulesTest.execute(ModulesTest.java:202)
at org.jboss.ceylon.test.modules.interop.test.InteropTestCase.execute(InteropTestCase.java:49)
at org.jboss.ceylon.test.modules.ModulesTest.execute(ModulesTest.java:180)
at org.jboss.ceylon.test.modules.ModulesTest.testArchive(ModulesTest.java:124)
at org.jboss.ceylon.test.modules.ModulesTest.testArchive(ModulesTest.java:86)
at org.jboss.ceylon.test.modules.interop.test.InteropTestCase.testJarUsage(InteropTestCase.java:42)
Caused by: org.jboss.modules.ModuleNotFoundException: org.jboss.modules.jboss-modules:1.1.1.GA
at org.jboss.modules.Module.addPaths(Module.java:864)
at org.jboss.modules.Module.link(Module.java:1206)
at org.jboss.modules.Module.relinkIfNecessary(Module.java:1235)
at org.jboss.modules.ModuleLoader.loadModule(ModuleLoader.java:208)
at ceylon.modules.jboss.runtime.AbstractJBossRuntime.createClassLoader(AbstractJBossRuntime.java:56)
@alesj
Collaborator

Do you have the latest CMR?

@quintesse
Collaborator

Ah, and a different one for the Aether test case:

java.lang.ClassNotFoundException: org.jboss.vfs.VFS from [Module "io.xov.yalp:11.0.2.Final" from Ceylon ModuleLoader: RootRepositoryManager: FileContentStore: /home/tschotan/.ceylon/cache]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:468)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:456)
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:120)
at io.xov.yalp.run_.main(run_.java:26)
at ceylon.modules.api.runtime.SecurityActions.invokeRunInternal(SecurityActions.java:58)
at ceylon.modules.api.runtime.SecurityActions.invokeRun(SecurityActions.java:48)
at ceylon.modules.api.runtime.AbstractRuntime.invokeRun(AbstractRuntime.java:84)
at ceylon.modules.api.runtime.AbstractRuntime.execute(AbstractRuntime.java:131)
at ceylon.modules.api.runtime.AbstractRuntime.execute(AbstractRuntime.java:119)
at ceylon.modules.Main.execute(Main.java:69)
at ceylon.modules.Main.main(Main.java:42)
at org.jboss.modules.Module.run(Module.java:270)
at org.jboss.modules.Main.main(Main.java:294)
at org.jboss.ceylon.test.modules.ModulesTest.execute(ModulesTest.java:202)
at org.jboss.ceylon.test.modules.interop.test.AetherInteropTestCase.execute(AetherInteropTestCase.java:50)
at org.jboss.ceylon.test.modules.ModulesTest.execute(ModulesTest.java:180)
at org.jboss.ceylon.test.modules.ModulesTest.testArchive(ModulesTest.java:124)
at org.jboss.ceylon.test.modules.ModulesTest.testArchive(ModulesTest.java:86)
at org.jboss.ceylon.test.modules.interop.test.AetherInteropTestCase.testMultiJarUsage(AetherInteropTestCase.java:43)
@quintesse
Collaborator

(or should I have pulled from a different branch to test?)

@alesj
Collaborator
@Test
public void testJarUsage() throws Throwable {
    JavaArchive module = ShrinkWrap.create(JavaArchive.class, "edu.fri.summer-1.0.0.Beta23.car");
    module.addClasses(module_.class, run_.class);
    // Note: if this test fails, you might be missing JBoss Modules in your Maven repository
    // See: edu.fri.summer.module_.java for more details
    testArchive(module);
}
@quintesse
Collaborator

Do you have the latest CMR?

If you're talking about master, yes

@alesj
Collaborator

(or should I have pulled from a different branch to test?)

No, latest CMR upstream master is the right one.

@alesj
Collaborator

If you're talking about master, yes

Then you don't have it in sync.
e.g. perhaps re-build build/dist/ in Runtime.

ant clean publish dist-for-tests

@quintesse
Collaborator

Ok, downloading 1.1.1.GA manually fixed the first exception. The second is still there.

@quintesse
Collaborator

Then you don't have it in sync.

Everything is up-to-date and built and published. Still seeing that exception.

@alesj
Collaborator

It might be same case -- no VFS in your Mvn repo.

@quintesse
Collaborator

Ok, but if it's an Aether test shouldn't it download it from a maven repository?

@alesj
Collaborator

Sure, it's just that I don't know if this artifact is in Mvn central.
I guess I could use one that is certainly there ...

@FroMage
Owner

So we each have different failing tests? Great.

@alesj
Collaborator

I have no failing tests. ;-)

@FroMage
Owner

It should fail, you're not allowed to import the default module!

@FroMage
Owner

As for aether tests, did anyone verify that with a clean m2 cache we still download stuff from the maven repos?

@alesj
Collaborator

It should fail, you're not allowed to import the default module!

So should we throw an exception while checking imports?

@alesj
Collaborator

As for aether tests, did anyone verify that with a clean m2 cache we still download stuff from the maven repos?

Not me.
You're welcome to try it. :-)

As I said, I might used deps that are not in Central.
Feel free to change them; e.g. some crappy slf4j is definitely in Central

@FroMage
Owner

I think we should fail indeed. How is another question.

Please sign in to comment.
Something went wrong with that request. Please try again.