Can ejb interceptors change the return value of a method before the calling class gets it?
Asked Answered
K

1

8

If we have code in the field that has a method that another product calls, and gets a list of objects back. And we need to make changes to the code to make it more flexible in populating the list to return, can we in the interim create an interceptor for the customer, that intercepts the method before it returns the list, and remove items from the list before the product that calls the method gets the list.

e.g.

OurCode.search() returns a list of foundObjects

Other product calls OurCode.search, receives 100 items

Can we create an interceptor that intercepts before OurCode.search returns, and alter the List of foundObjects, removing unnecessary items? This would only be a temporary fix until a future release

Knew answered 25/7, 2012 at 23:2 Comment(0)
B
14

While I don't recommend doing it that way (for sake of understandability, and as in my experience the "temporary fix" will become a permanent one) you can do this with Interceptors.

@AroundInvoke
Object filterSearchResults(InvocationContext ctx) throws Exception {
    Object result = ctx.proceed();
    if ( result != null) {
        List<SearchResult> results = (List<SearchResult>)result;
        // do whatever you want to to with your results here
        return results;
    }
    return result;
}
Burka answered 26/7, 2012 at 6:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.