<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
Passing MetaData through Invocations
</h3>
<span style="margin-bottom: 10px;">
created by <a href="http://community.jboss.org/people/stevenodb">Steven Op de beeck</a> in <i>JBoss AOP</i> - <a href="http://community.jboss.org/message/603708#603708">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>I'm trying to use the JBoss AOP *Invocation structure to pass metadata between two advices on the same Jointpoint (one call, the other exection). I read this in the documentation of JBossAOP:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="padding-left: 30px;">You can attach untyped metadata to the invocation object, or even to the response. This allows advices to pass contextual data to one another in the incoming invocation or outgoing response for instance if you had advices running on a remote client that wanted to pass contextual data to server-side aspects. This method on invocation gets you access to a <tt class="literal">org.jboss.aop.metadata.SimpleMetaData</tt> instance so that you can attach or read data. SimpleMetaData getMetaData()</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>So I was hopeful that I could push some metadata on in one advice, and pull it of in a later advice. However, I don't seem to be able to get it to work. All I'm pulling off at the receiving end are null-pointers.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Some example code:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Caller side aspect:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;"><span style="color: #777777;">@Aspect</span>(scope=Scope.<span style="color: #0023c7;"><em>PER_INSTANCE</em></span>)</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;"><span style="color: #9a1867;"><strong>public</strong></span> <span style="color: #9a1867;"><strong>class</strong></span> HelloWorldCaller {</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #382ffa;"><span style="color: #000000;">  </span><span style="color: #777777;">@PointcutDef</span><span style="color: #000000;">(</span>"call(java.lang.String $instanceof{backend.HelloBackEndRemote}->getValue(..))"<span style="color: #000000;">)</span></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #0023c7;"><span style="color: #000000;">  </span><span style="color: #9a1867;"><strong>public</strong></span><span style="color: #000000;"> </span><span style="color: #9a1867;"><strong>static</strong></span><span style="color: #000000;"> Pointcut </span><em>getValueCallerPointcut</em><span style="color: #000000;">;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #382ffa;"><span style="color: #000000;">  </span><span style="color: #777777;">@Bind</span><span style="color: #000000;">(pointcut=</span>"hello.HelloWorldCaller.getValueCallerPointcut"<span style="color: #000000;">, type=AdviceType.</span><span style="color: #0023c7;"><em>AROUND</em></span><span style="color: #000000;">)</span></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">          <span style="color: #9a1867;"><strong>public</strong></span> Object helloAdv(CallerInvocation invocation) <span style="color: #9a1867;"><strong>throws</strong></span> Throwable {</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                    Object invResult = <span style="color: #9a1867;"><strong>null</strong></span>;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #429073;">  <span style="color: #333333;">SimpleMetaData <span style="text-decoration: underline;">md</span> = invocation.getMetaData();</span></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #429073;"><span style="color: #333333;">  md.addMetaData("<span style="text-decoration: underline;">pom</span>", "<span style="text-decoration: underline;">appel</span>", "peer", PayloadKey.AS_IS);</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                    <span style="color: #9a1867;"><strong>try</strong></span> {</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                                   invResult = invocation.invokeNext();</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                    } <span style="color: #9a1867;"><strong>catch</strong></span> (Exception e) {</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                              e.printStackTrace();</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                    }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; min-height: 14.0px;">  <span style="color: #9a1867;"><strong>return</strong></span> <span style="color: #382ffa;">"[caller "</span> + invResult + <span style="color: #382ffa;">"]"</span>;</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">          }</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Callee side aspect:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="color: #777777;">@Aspect</span>(scope=Scope.<span style="color: #0023c7;"><em>PER_INSTANCE</em></span>)</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;"><span style="color: #9a1867;"><strong>public</strong></span> <span style="color: #9a1867;"><strong>class</strong></span> HelloWorldCallee {</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #382ffa;"><span style="color: #000000;">  </span><span style="color: #777777;">@PointcutDef</span><span style="color: #000000;">(</span>"execution(java.lang.String $instanceof{backend.HelloBackEndRemote}->getValue(..))"<span style="color: #000000;">)</span></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #0023c7;"><span style="color: #000000;">  </span><span style="color: #9a1867;"><strong>public</strong></span><span style="color: #000000;"> </span><span style="color: #9a1867;"><strong>static</strong></span><span style="color: #000000;"> Pointcut </span><em>getValueCalleePointcut</em><span style="color: #000000;">;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #382ffa;"><span style="color: #000000;">  </span><span style="color: #777777;">@Bind</span><span style="color: #000000;">(pointcut=</span>"hellocruel.HelloWorldCallee.getValueCalleePointcut"<span style="color: #000000;">, type=AdviceType.</span><span style="color: #0023c7;"><em>AROUND</em></span><span style="color: #000000;">)</span></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">          <span style="color: #9a1867;"><strong>public</strong></span> Object helloAdv(MethodInvocation invocation) <span style="color: #9a1867;"><strong>throws</strong></span> Throwable {</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                    Object invResult = <span style="color: #9a1867;"><strong>null</strong></span>;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                    SimpleMetaData <span style="text-decoration: underline;">md</span> = invocation.getMetaData();</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                    Object <span style="text-decoration: underline;">obj</span> = invocation.getMetaData(<span style="color: #382ffa;">"pom"</span>, <span style="color: #382ffa;">"appel"</span>);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                    Object <span style="text-decoration: underline;">obj2</span> = invocation.getResponseAttachment(<span style="color: #382ffa;">"appel"</span>);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                    <span style="color: #9a1867;"><strong>try</strong></span> {</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                              invResult = invocation.invokeNext();</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                    } <span style="color: #9a1867;"><strong>catch</strong></span> (Exception e) {</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                              e.printStackTrace();</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                    }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                    String result = <span style="color: #382ffa;">""</span>;</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                    <span style="color: #9a1867;"><strong>if</strong></span> (invResult != <span style="color: #9a1867;"><strong>null</strong></span>) {</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                              result = (String)invResult;</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">                    }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #382ffa;"><span style="color: #000000;">  </span><span style="color: #9a1867;"><strong>return</strong></span><span style="color: #000000;"> result + </span>" [callee Goodbye World.]"<span style="color: #000000;">;</span></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">          }</p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo;">}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Even when I'm debugging the respectitive MetaData fields in the MethodInvocation object are null. </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Is this supported behaviour? Am I doing something wrong?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Thanks in advance.</p><p>Steven.</p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/603708#603708">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in JBoss AOP at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2027">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>