Hi all,
I am posting the result of solution to make Weld work as JPMS auto module.
In this solution I use only module path. I manually create JPMS layer and add all modules
to it. For Weld I added the following modules:
<module file="cdi-api-2.0.jar"/>
<module file="javax.annotation-api-1.3.2.jar"/>
<module file="javax.inject-1.jar"/>
<module file="javax.interceptor-api-1.2.jar"/>
<module file="jboss-classfilewriter-1.2.2.Final.jar"/>
<module file="jboss-logging-3.2.1.Final.jar"/>
<module file="guava-18.0.jar"/>
<module file="weld-se-core-3.0.4.Final.jar"/>
<module file="weld-environment-common-3.0.4.Final.jar"/>
<module file="weld-core-impl-3.0.4.Final.jar"/>
<module file="weld-spi-3.0.SP3.jar"/>
<module file="weld-api-3.0.SP3.jar"/>
<module file="jandex-2.0.5.Final.jar"/>
Note, that jandex is 2.0.5 but not 2.0.3 as 2.0.3 doesn't work well with java9.
Besides Weld modules
I have two modules - in beanModule( here I have my cdi beans + beans.xml) and serverModule
(here
I create Weld etc).
In serverModule I have the following code:
Weld builder = new Weld()
.property("org.jboss.weld.construction.relaxed", true)
.setClassLoader(this.getClass().getClassLoader())
.setResourceLoader(new ResourceLoaderImpl());
WeldContainer container = builder.initialize();
BeanManager bm = container.getBeanManager();
This is my ResourceLoaderImpl:
public class ResourceLoaderImpl implements ResourceLoader {
private static final String ERROR_LOADING_CLASS = "Error loading class ";
@Override
public Class<?> classForName(String name) {
try {
return Class.forName(name);
} catch (ClassNotFoundException e) {
throw new ResourceLoadingException(ERROR_LOADING_CLASS + name, e);
} catch (LinkageError e) {
throw new ResourceLoadingException(ERROR_LOADING_CLASS + name, e);
} catch (TypeNotPresentException e) {
throw new ResourceLoadingException(ERROR_LOADING_CLASS + name, e);
}
}
@Override
public URL getResource(String name) {
try {
Module module =
this.getClass().getModule().getLayer().findModule("beanModule").orElse(null);
ModuleReference reference = getReference(module);
Optional<URI> uri = reference.open().find(name);
if (uri.isPresent()) {
return uri.get().toURL();
} else {
return null;
}
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
@Override
public Collection<URL> getResources(String name) {
List<URL> result = new ArrayList<>();
//HERE WE NEED MODULE ITERATION
URL url = this.getResource(name);
if (url != null) {
result.add(url);
}
return result;
}
@Override
public void cleanup() { }
}
In beanModule in module-info I open packages, that contains beans.
The solution WORKS, via BeanManager I can get whatever bean I need. The only problem is
that I can't
understand what I should return in getResouce(String) if I have many modules - what module
first I should
return? For example, when weld calls getResource("META-INF/beans.xml") - ?
It would be very interesting to to get some comments.
--
Best regards, Alex
Show replies by date