GeeCON Prague 2014*
The moose likes Web Services and the fly likes WebServiceContext and AspectJ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Web Services
Bookmark "WebServiceContext and AspectJ" Watch "WebServiceContext and AspectJ" New topic
Author

WebServiceContext and AspectJ

Emil Jennings
Ranch Hand

Joined: Jul 09, 2010
Posts: 55
    
    1
If I have a Web Service like



Is it possible to use context (it's value) in an Aspect? If so, how is it accessed? Thanks.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
I'd say that it depends.
Where in the lifecycle of the web service endpoint implementation class do you want to the aspect to execute?
If you create a setter method for the webservice context and annotate it with the @Resource annotation, then add a log statement in the setter method.
This way you will see where in the lifecycle of the endpoint implementation class the web service context is set.
Then add your aspect and have it print some log statement to see where it is invoked.
The above will make it very clear when the context is set and when the aspect is invoked.

Capturing the target of a method call with AspectJ is usually done using target() or this().
For an example, see: http://books.google.com/books?id=AKuBlJGl7iUC&pg=PA56&dq=capture+target+of+method+call&hl=en&ei=lH0nTb-iCouUOtT3rLUC&sa=X&oi=book_result&ct=result&resnum=1&ved=0CCgQ6AEwAA#v=onepage&q=capture%20target%20of%20method%20call&f=false
(hope the link works )
When you have obtained the target object of the method invocation, retrieving the web service context should be trivial.
Best wishes!


My free books and tutorials: http://www.slideshare.net/krizsan
Emil Jennings
Ranch Hand

Joined: Jul 09, 2010
Posts: 55
    
    1
Thanks for the link Ivan. What I did was

With


The example from the link uses the call (which I always get advise not applied when I try to use it), so I use the execution. The server log shows

Which tells me that the setContext() executes before @Logable (if figured this would be the case). I was hoping to capture the context in order for the Aspect to do System.out.println("IP Address: " + req.getRemoteAddr()); but this advice is never used. I'm more than a bit stumped at this point...
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
You are really close - a few minor modifications to your code and I were able to retrieve the web service context from the aspect.
From the LoggingAspect.aj:

Note that I have added the requirement to the pointcut that the target object has to be an instance of MyService.
In the advise method I retrieve the target object by calling thisJoinPoint.getTarget() and since I have the above requirement of the target object, I can safely cast it to MyService.

From the MyService.java class:

Note that I have excluded the getter and setter methods from being published in the web service interface.
Best wishes!
Emil Jennings
Ranch Hand

Joined: Jul 09, 2010
Posts: 55
    
    1
Thanks for your help Ivan. So to make sure I have it straight, by creating the target of MyService, the class becomes available to the Aspect and then objects can be instantiated in the Aspect.

The advice is easy enough to follow, and good tip on the web service excludes.

Thanks again.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
Gunny Jennings wrote:So to make sure I have it straight, by creating the target of MyService, the class becomes available to the Aspect and then objects can be instantiated in the Aspect.

Note quite. By using target(MyService), only calls to methods in the MyService class will be captured - we need to make sure that the aspect only sees calls to objects of the MyService class, since we cast the reference to the target object to MyClass.
No objects are instantiated by the aspect. When the aspect intercepts the call to the web service endpoint implementation class, the web service context has already been injected and the aspect is thus able to get it, using the MyService.getContext() method.
Best wishes!
Emil Jennings
Ranch Hand

Joined: Jul 09, 2010
Posts: 55
    
    1
I re-read your earlier post and realized what you were saying regarding the pointcut with target(MyService) being selection criteria. Thanks for the clarification on the injection part, and everything else, I have a better understanding of this now.
Emil Jennings
Ranch Hand

Joined: Jul 09, 2010
Posts: 55
    
    1
After more studying of AspectJ, I came across another solution (just in case others are trying this out too). The solution above illustrates reflection-style context collection, but the AspectJ in Action book also illustrates pointcut-style context collection. The pointcut style for the above code looks like:



This version adds parameter MyService theTargetObject to the pointcut, which then is used throughout the Aspect. Since the target is declared in the pointcut as theTargetObject (which is of type MyService) there is no need for casting.
 
GeeCON Prague 2014
 
subject: WebServiceContext and AspectJ