<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <br>
    <div class="moz-cite-prefix">On 11/13/2014 04:27 PM, arjan tijms
      wrote:<br>
    </div>
    <blockquote
cite="mid:CAE=-AhCU3tjnvHZX5LCN5tPb9wkyJLVGKFPEgD1fPaQWFtipcw@mail.gmail.com"
      type="cite">Hi,<br>
      <br>
      On Wednesday, November 12, 2014, Jozef Hartinger &lt;<a
        moz-do-not-send="true" href="mailto:jharting@redhat.com">jharting@redhat.com</a>&gt;
      wrote:<br>
      <blockquote class="gmail_quote" style="margin:0 0 0
        .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Arjan,<br>
        <br>
        there is a bug in Weld (WELD-1785) preventing this from working
        which is going to be fixed in the next release. What you are
        doing should work IMO as long as the interceptor does not call
        any other methods on the target instance. </blockquote>
      <div><br>
      </div>
      <div>That's great to hear really.</div>
      <div><br>
      </div>
      <div>I'm slightly confused through why Mark thinks this cannot
        really work, while you say it should.</div>
    </blockquote>
    Don't be confused by that. This often happens to me and Mark :-)<br>
    <blockquote
cite="mid:CAE=-AhCU3tjnvHZX5LCN5tPb9wkyJLVGKFPEgD1fPaQWFtipcw@mail.gmail.com"
      type="cite">
      <div><br>
      </div>
      <div>Is there something in the spec that may need to be clarified
        here? Ie some words about what an interceptor is at least
        allowed to do and what is definitely not allowed?</div>
      <div><br>
      </div>
      <div> </div>
      <blockquote class="gmail_quote" style="margin:0 0 0
        .8ex;border-left:1px #ccc solid;padding-left:1ex">In addition it
        must count with the target instance being destroyed within the
        instance.destroy() call.</blockquote>
      <div><br>
      </div>
      <div>Sorry, I don't fully follow this. You mean something must be
        counted?</div>
    </blockquote>
    I mean that the interceptor (and others in the chain) should be
    tolerant and not be surprised finding itself and the target bean
    destroyed immediately after the instance.destroy() call.<br>
    <blockquote
cite="mid:CAE=-AhCU3tjnvHZX5LCN5tPb9wkyJLVGKFPEgD1fPaQWFtipcw@mail.gmail.com"
      type="cite">
      <div> </div>
      <blockquote class="gmail_quote" style="margin:0 0 0
        .8ex;border-left:1px #ccc solid;padding-left:1ex">
        <br>
        Perhaps a nicer way of doing this would be:<br>
        <br>
        @Inject<br>
        @Intercepted<br>
        private Bean&lt;?&gt; bean;<br>
        <br>
                Context context = manager.getContext(bean.getScope());<br>
                if (!(context instanceof AlterableContext)) {<br>
                    throw new IllegalStateException("Context does not
        support removal of instances");<br>
                }<br>
                AlterableContext alterableContext =
        AlterableContext.class.cast(context);<br>
                alterableContext.destroy(bean);</blockquote>
      <div><br>
      </div>
      <div>I tried something close to that as well, just used the bean
        manager to resolve a Bean from the target object. Thanks for the
        suggestion!</div>
      <div><br>
      </div>
      <div>Kind regards,</div>
      <div>Arjan</div>
      <div><br>
      </div>
      <div> </div>
      <blockquote class="gmail_quote" style="margin:0 0 0
        .8ex;border-left:1px #ccc solid;padding-left:1ex">
        <br>
        On 11/10/2014 02:59 PM, arjan tijms wrote:<br>
        <blockquote class="gmail_quote" style="margin:0 0 0
          .8ex;border-left:1px #ccc solid;padding-left:1ex">
          Hi,<br>
          <br>
          I wonder if it would be allowed according to the CDI spec to
          destroy a<br>
          bean instance from within an interceptor.<br>
          <br>
          To test this (on Weld) I used the following code:<br>
          <br>
          @Interceptor<br>
          @DestroyOnError<br>
          @Priority(APPLICATION)<br>
          public class DestroyOnErrorInterceptor implements Serializable
          {<br>
          <br>
               private static final long serialVersionUID = 1L;<br>
          <br>
               @AroundInvoke<br>
               public Object tryInvoke(InvocationContext ctx) throws
          Exception {<br>
          <br>
                   try {<br>
                       return ctx.proceed();<br>
                   } catch (Exception e) {<br>
                       destroy(ctx.getMethod().getDeclaringClass());<br>
                       throw e;<br>
                   }<br>
               }<br>
          <br>
               private &lt;T&gt; void destroy(Class&lt;T&gt; clazz) {<br>
                   Instance&lt;T&gt; instance =
          CDI.current().select(clazz);<br>
                   instance.destroy(instance.get());<br>
               }<br>
          <br>
          }<br>
          <br>
          <br>
          When I use this interceptor on a SessionScoped bean:<br>
          <br>
          @SessionScoped<br>
          public class TestBean implements Serializable {<br>
          <br>
               private static final long serialVersionUID = 1L;<br>
          <br>
               @DestroyOnError<br>
               public void test() {<br>
                   throw new IllegalStateException();<br>
               }<br>
          }<br>
          <br>
          And then inject said bean in say a Servlet and call the test()
          method,<br>
          destruction of the bean happens partially, but as soon as Weld
          tried<br>
          to invocate a preDestroy method, it goes through the bean
          proxy again,<br>
          detects that "the" interceptor handler is already active,
          promptly<br>
          skips its attempt to call a preDestroy method and then to add
          insult<br>
          to injury tries to call a "proceed" method which is always
          null and<br>
          thus throws a NPE.<br>
          <br>
          (this happens in<br>
          org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke)<br>
          <br>
          I tried some alternative methods to destroy the bean such as:<br>
          <br>
          <br>
          Bean&lt;T&gt; bean = resolve(beanManager, beanClass);<br>
          <br>
          AlterableContext context = (AlterableContext)<br>
          beanManager.getContext(bean.getScope());<br>
          context.destroy(bean);<br>
          <br>
          with resolve being:<br>
          <br>
          public static &lt;T&gt; Bean&lt;T&gt; resolve(BeanManager
          beanManager, Class&lt;T&gt; beanClass) {<br>
                   Set&lt;Bean&lt;?&gt;&gt; beans =
          beanManager.getBeans(beanClass);<br>
          <br>
                   for (Bean&lt;?&gt; bean : beans) {<br>
                       if (bean.getBeanClass() == beanClass) {<br>
                           return (Bean&lt;T&gt;)<br>
          beanManager.resolve(Collections.&lt;Bean&lt;?&gt;&gt;singleton(bean));<br>
                       }<br>
                   }<br>
          <br>
                   return (Bean&lt;T&gt;) beanManager.resolve(beans);<br>
               }<br>
          <br>
          But this resulted in the same problem.<br>
          <br>
          Any idea?<br>
          <br>
          Kind regards,<br>
          Arjan Tijms<br>
          _______________________________________________<br>
          cdi-dev mailing list<br>
          <a moz-do-not-send="true">cdi-dev@lists.jboss.org</a><br>
          <a moz-do-not-send="true"
            href="https://lists.jboss.org/mailman/listinfo/cdi-dev"
            target="_blank">https://lists.jboss.org/mailman/listinfo/cdi-dev</a><br>
          <br>
          Note that for all code provided on this list, the provider
          licenses the code under the Apache License, Version 2 (<a
            moz-do-not-send="true"
            href="http://www.apache.org/licenses/LICENSE-2.0.html"
            target="_blank">http://www.apache.org/licenses/LICENSE-2.0.html</a>).
          For all other ideas provided on this list, the provider waives
          all patent and other intellectual property rights inherent in
          such information.<br>
        </blockquote>
        <br>
      </blockquote>
    </blockquote>
    <br>
  </body>
</html>