Re: Performing Java EE resource injections dynamically

2016-03-30 Thread Romain Manni-Bucau
Do you use a custome classloader? If so try to override equals/hascode to
simulate webapp one
Le 31 mars 2016 06:43, "Dimitri"  a écrit :

> 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
>
>


Re: Performing Java EE resource injections dynamically

2016-03-30 Thread Dimitri
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



Re: Fwd: [jira] [Resolved] (TOMEE-1762) Add a timeout to DataSource shutdown

2016-03-30 Thread Romain Manni-Bucau
well idea was to not have it by default (in particular no medium
duration timeout which can break apps flushing at that moment) so
wouldn't break anything since a choice. We can go further and activate
it by resource with another virtual property if desired but
datasources are not particular there. When I did it I was thinking to
AMQ as well where it can be useful.

Only issue I see is creating a thread which can leak by design but
while it is for diagnostic it is fine I guess. We can (should?) wire
it to an event (ResourceDestructionTimeoutEvent? do we want the
creation one?) to be able to plug an alert in such a case.

Romain Manni-Bucau
@rmannibucau |  Blog | Github | LinkedIn | Tomitriber


2016-03-30 22:30 GMT+02:00 Andy :
> Sure, just took the smallest step to solve the known issue but did think
> that the whole resource destroy block could work the same way - Just was not
> sure if 'all' the resource destroy methods could be called safely from a
> thread.
>
>
> On 30/03/2016 00:37, Romain Manni-Bucau wrote:
>>
>> Master got https://issues.apache.org/jira/browse/TOMEE-1761
>>
>> Do we want to merge it with 1.7.x instead of using another config?
>> -- Message transféré --
>> De : "Andy Gumbrecht (JIRA)" 
>> Date : 30 mars 2016 00:29
>> Objet : [jira] [Resolved] (TOMEE-1762) Add a timeout to DataSource
>> shutdown
>> À : 
>> Cc :
>>
>>
>>   [
>>
>> https://issues.apache.org/jira/browse/TOMEE-1762?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
>> ]
>>
>> Andy Gumbrecht resolved TOMEE-1762.
>> ---
>>  Resolution: Fixed
>>
>> Added system property to TomEE 1.7.5-SNAPSHOT -
>> tomee.datasource.destroy.timeout.ms
>> The default if not specified is 1000
>> Min: 50
>> Max: 3
>>
>>> Add a timeout to DataSource shutdown
>>> 
>>>
>>>  Key: TOMEE-1762
>>>  URL: https://issues.apache.org/jira/browse/TOMEE-1762
>>>  Project: TomEE
>>>   Issue Type: Improvement
>>> Affects Versions: 1.7.4
>>> Reporter: Andy Gumbrecht
>>> Assignee: Andy Gumbrecht
>>> Priority: Minor
>>>  Fix For: 1.7.5
>>>
>>>Original Estimate: 2h
>>>   Remaining Estimate: 2h
>>>
>>> Add a timeout governed by the system property '
>>
>> tomee.datasource.destroy.timeout.ms' that indicates how long a DataSource
>> destroy call will wait for the actual termination.
>>
>>
>>
>> --
>> This message was sent by Atlassian JIRA
>> (v6.3.4#6332)
>>
>
> --
>   Andy Gumbrecht
>   https://twitter.com/AndyGeeDe
>


Re: Performing Java EE resource injections dynamically

2016-03-30 Thread Romain Manni-Bucau
True, we are based on a static analysis at startup so we likely miss
JSP - means if you use it somewhere else it should work. Fixed in
https://issues.apache.org/jira/browse/TOMEE-1764

Romain Manni-Bucau
@rmannibucau |  Blog | Github | LinkedIn | Tomitriber


2016-03-30 1:52 GMT+02:00 Dimitri :
>
>> In the case of resources.xml-defined entry, I didn't manage to
>> resolve it even through JNDI manually.
>
> Sorry, I was wrong, it is resolvable as "openejb:Resource/XXX" in JNDI. But 
> still no way to use @Resource annotation.
>
> Dimitri
>