Hi,
In Tomee 7.0.0-M3, I've tried to use JavaeeInstanceManager and
DefaultInstanceManager to instantiate dynamically loaded class. (To
obtain the first, I used InstanceManagerFactory; for the second I've
copied the code from your recent commit and used ContainerServlet to
obtain parent StandardContext.)
Excerpt from servlet:
StandardContext webapp = (StandardContext) wrapper.getParent();
DefaultInstanceManager defaultIM = new DefaultInstanceManager(
webapp.getNamingContextListener().getEnvContext(),
TomcatInjections.buildInjectionMap(webapp.getNamingResources()),
webapp,
ParentClassLoaderFinder.Helper.get());
InstanceManager javaeeIM =
InstanceManagerFactory.getInstanceManager(getServletConfig());
Object foo = null, bar = null;
try {
URLClassLoader ucl = new URLClassLoader(
new URL[]{new URL("file:///home/mitya/.../classes/")}, // here
resides external class "my.Foo"
this.getClass().getClassLoader() // otherwise
ClassNotFoundException on UserTransaction, EntityManager etc.
);
foo = javaeeIM.newInstance("my.Foo", ucl);
bar = defaultIM.newInstance("my.Foo", ucl);
} catch (IllegalAccessException | InvocationTargetException |
NamingException | InstantiationException | ClassNotFoundException ex) {
LOG.log(Level.SEVERE, null, ex);
}
Foo.java:
public class Foo {
private static final Logger LOG = Logger.getLogger(Foo.class.getName());
@Resource(name = "foo")
private Integer foo;
@Resource
private UserTransaction tx;
@PersistenceContext
private EntityManager em;
@PostConstruct
public void post() {
LOG.info("Foo::post");
LOG.info("foo = " + foo);
LOG.info("em = " + em);
LOG.info("tx = " + tx);
}
}
With JavaeeInstanceManager, everything works perfect, but *only* if the
my.Foo class is present in classpath at application startup. If loaded
from external location, injections are not processed (nulls
everywhere).
With DefaultInstanceManager, I get the following for every injected
field:
javax.naming.NameNotFoundException: Name [my.Foo/tx] is not bound in this
Context. Unable to find [my.Foo].
Does this all work as intended? Can I do anything about it?
Regards,
Dimitri