I coded this today, just used the field instead of a setter method. Your 
pseudo code was very helpful, thanks :-)

Maybe the following snippet is useful for someone with the same problem.
Should I post this on the wiki as well? I am not sure if it is a good 
solution - but it works for me.

Can someone please tell me if it is ok to call this every time an action needs 
the service, or would it be better to cache the service somewhere in 
application scope? I am quite new to this whole ejb thing.

Piero


---- EJBInterceptor.java ----
public class EJBInterceptor extends AbstractInterceptor implements Interceptor 
{
    public String intercept(ActionInvocation actionInvocation) throws 
Exception {
        Object action = actionInvocation.getAction();
        for (Field f : action.getClass().getDeclaredFields()) {
            if (f.isAnnotationPresent(EJB.class)) {
                f.setAccessible(true);
                String serviceName = f.getType().getName();
                Object service = null;
                try {
                    InitialContext ic = new InitialContext();
                    service = ic.lookup(serviceName);
                } catch (Exception ex) {
                    System.out.println("Error: "+ex.getMessage());
                }
                f.set(action, f.getType().cast(service));
                break;
            }
        }
        return actionInvocation.invoke();
    }
}
---- code ----

---- EJB.java ----
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface EJB {
}
----- code ----


On Tuesday 23 January 2007 01:05:57 Ian Roughley wrote:
> I can't provide the code - it is owned my a client.  But here is the
> pseudo code:
>
> class EJB3Interceptor implements Interceptor {
>
>    public String intercept(ActionInvocation actionInvocation) throws
> java.lang.Exception {
>         Object action = actionInvocation.getAction();
>         for all methods {
>           if( this is a setter and it is annotated as expected ) {
>              look up the EJB
>              set the EJB on the action
>           }
>        }
>     }
>
> }
>
>  From here you would need to ensure that the interceptor is applied to
> the actions that need ejb's.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to