View Javadoc
1   /*
2    * Copyright 2002-2012 the original author or authors.
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package test.mixin;
18  
19  import org.aopalliance.intercept.MethodInvocation;
20  
21  import org.springframework.aop.support.DelegatingIntroductionInterceptor;
22  
23  /**
24   * Mixin to provide stateful locking functionality.
25   * Test/demonstration of AOP mixin support rather than a
26   * useful interceptor in its own right.
27   *
28   * @author Rod Johnson
29   * @since 10.07.2003
30   */
31  @SuppressWarnings("serial")
32  public class LockMixin extends DelegatingIntroductionInterceptor implements Lockable {
33  
34  	/** This field demonstrates additional state in the mixin */
35  	private boolean locked;
36  
37  	@Override
38  	public void lock() {
39  		this.locked = true;
40  	}
41  
42  	@Override
43  	public void unlock() {
44  		this.locked = false;
45  	}
46  
47  	/**
48  	 * @see test.mixin.AopProxyTests.Lockable#locked()
49  	 */
50  	@Override
51  	public boolean locked() {
52  		return this.locked;
53  	}
54  
55  	/**
56  	 * Note that we need to override around advice.
57  	 * If the method is a setter and we're locked, prevent execution.
58  	 * Otherwise let super.invoke() handle it, and do normal
59  	 * Lockable(this) then target behaviour.
60  	 * @see org.aopalliance.MethodInterceptor#invoke(org.aopalliance.MethodInvocation)
61  	 */
62  	@Override
63  	public Object invoke(MethodInvocation invocation) throws Throwable {
64  		if (locked() && invocation.getMethod().getName().indexOf("set") == 0)
65  			throw new LockedException();
66  		return super.invoke(invocation);
67  	}
68  
69  }