View Javadoc

1   /*
2    * Created on May 14, 2004
3    *
4    */
5   package org.neo.swarm.interceptor.appContext;
6   
7   import java.lang.reflect.Method;
8   
9   import org.aopalliance.intercept.MethodInvocation;
10  import org.neo.swarm.ApplicationContext;
11  import org.neo.swarm.core.aop.silc.comp.Invocation;
12  import org.neo.swarm.core.aop.silc.comp.MethodInterceptor;
13  import org.neo.swarm.core.aop.silc.comp.Perspective;
14  
15  
16  /***
17   * Converts all invocations starting with "retrieveComponent(s)(ofType)" to use the Perspective versions
18   *  @author navery
19   */
20  public class GetMethodInterceptor implements MethodInterceptor {
21  	
22  	/***
23  	 * Handle method translation or bypass if agruments are invalid
24  	 */
25  	public Object invoke(MethodInvocation invocation) throws Throwable {
26  		
27  		Method origMethod = invocation.getMethod();
28  		// filter out any rubbish..
29  		if (!origMethod.getName().startsWith("retrieveComponent") || invocation.getArguments().length != 1) {
30  			return invocation.proceed();
31  		}
32  		
33  		// cheeky upcast so we can change the invocation properites and transform it - is this bad AOP ?
34  		Invocation silcInvocation = (Invocation) invocation;
35  		Class param0 = origMethod.getParameterTypes()[0];
36  		Method method = ApplicationContext.class.getMethod(origMethod.getName(), new Class[]{Perspective.class, param0 });
37  		
38  
39  		silcInvocation.setMethod(method);
40  		Object[] args = new Object[2];
41  		args[0] = Perspective.REMOTE;
42  		args[1] = invocation.getArguments()[0];
43  		
44  		silcInvocation.setArguments(args);
45  		return invocation.proceed();
46  	}
47  }