View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.logic.aop;
17  
18  import org.aopalliance.intercept.MethodInterceptor;
19  import org.aopalliance.intercept.MethodInvocation;
20  
21  public class MethodDecorateInterceptorImpl implements MethodInterceptor,
22  		MethodDecorateInterceptor {
23  
24  	private MethodRegister register;
25  
26  	public void setRegister(MethodRegister register) {
27  		this.register = register;
28  	}
29  	
30  	public Object invoke(MethodInvocation invocation) throws Throwable {
31  		MethodInvoker[] beforeMethodInvokers = register.getBeforeMethodInvokers(invocation);
32  		MethodInvoker[] afterMethodInvokers = register.getAfterMethodInvokers(invocation);
33  		MethodInvoker delegateInvoker = register.getDelegateMethodInvoker(invocation);
34  		Object ret = null;
35  		
36  		invoke(beforeMethodInvokers, invocation);
37  		if(delegateInvoker != null){
38  			ret = invoke(delegateInvoker, invocation);
39  		}else{
40  			ret = invocation.proceed();
41  		}
42  		invoke(afterMethodInvokers, invocation);
43  		return ret;
44  	}
45  
46  	private void invoke(MethodInvoker[] invokers, MethodInvocation invocation){
47  		if(invokers==null){
48  			return;
49  		}
50  		for(int i=0; i<invokers.length; i++){
51  			invoke(invokers[i], invocation);
52  		}
53  	}
54  	
55  	private Object invoke(MethodInvoker invoker, MethodInvocation invocation){
56  		return invoker.invoke(invocation.getArguments());
57  	}
58  }