1 /*
2 *
3 * The Seasar Software License, Version 1.1
4 *
5 * Copyright (c) 2003-2004 The Seasar Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above
12 * copyrightnotice, this list of conditions and the following
13 * disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 *
20 * 3. The end-user documentation included with the redistribution,
21 * if any, must include the following acknowledgement:
22 * "This product includes software developed by the
23 * Seasar Project (http://www.seasar.org/)."
24 * Alternately, this acknowledgement may appear in the software
25 * itself, if and wherever such third-party acknowledgements
26 * normally appear.
27 *
28 * 4. Neither the names "The Seasar Project" nor the name of its
29 * contributors ay be used to endour or promote products derived
30 * from this software without specific prior written permission of
31 * the Seasar Project.
32 *
33 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR
34 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 * WARRANTIESOF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE SEASER PROJECT
37 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
38 * INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42 * WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING
43 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
44 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 */
46 package org.seasar.groovy;
47
48 import groovy.lang.Closure;
49
50 import org.aopalliance.intercept.MethodInterceptor;
51 import org.aopalliance.intercept.MethodInvocation;
52
53 /***
54 * Groovyのクロージャを呼びだすMethodInterceptor。
55 * MethodInvocationはClosureAroundAdviceのコンストラクターに渡されたClosureへと
56 * 引数としてそのまま渡される。
57 * <p>
58 * Closureの戻り値はそのままMethodInterceptorの戻り値として扱われるので、
59 * なんらかの値を返さなくてはならない場合は、Closure内で値を返すこと。
60 * <p>
61 * サンプル:
62 * <pre>
63 * closure = {invocation|
64 * invocation.getArgs()[1][0] // => 1
65 * invocation.getArgs()[1][1] // => 2
66 * result = invocation.proceed() // => 3
67 * return result + 1 // => 4
68 * }
69 * pointcut = new PointcutImpl(Calc)
70 * aspect = new AspectImpl(new ClosureAroundAdvice(closure), pointcut);
71 * proxy = new AopProxy(Calc, new Aspect[]{ aspect })
72 * calc = proxy.create()
73 * println calc.plus(1, 2)
74 * </pre>
75 *
76 * @since 0.2
77 * @author takai
78 */
79 public class ClosureAroundAdvice implements MethodInterceptor {
80
81 private Closure _closure;
82
83 /***
84 * コンストラクター。
85 * コンストラクターに渡されたクロージャがJoinPointを処理する。
86 *
87 * @param closure JoinPointが渡されるクロージャ。
88 */
89 public ClosureAroundAdvice(Closure closure) {
90 _closure = closure;
91 }
92
93 /***
94 * @param invocation ジョインポイント。
95 * @return クロージャの戻り値。
96 */
97 public Object invoke(MethodInvocation invocation) throws Throwable {
98 return _closure.call(invocation);
99 }
100
101 }