KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > LockMixin


1 /*
2  * Copyright 2002-2005 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 org.springframework.aop.framework;
18
19 import org.aopalliance.intercept.MethodInvocation;
20 import org.springframework.aop.support.DelegatingIntroductionInterceptor;
21
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 public class LockMixin extends DelegatingIntroductionInterceptor implements Lockable {
32     
33     /** This field demonstrates additional state in the mixin */
34     private boolean locked;
35     
36     public void lock() {
37         this.locked = true;
38     }
39     
40     public void unlock() {
41         this.locked = false;
42     }
43
44     /**
45      * @see org.springframework.aop.framework.AopProxyTests.Lockable#locked()
46      */

47     public boolean locked() {
48         return this.locked;
49     }
50
51     /**
52      * Note that we need to override around advice.
53      * If the method is a setter and we're locked, prevent execution.
54      * Otherwise let super.invoke() handle it, and do normal
55      * Lockable(this) then target behaviour.
56      * @see org.aopalliance.MethodInterceptor#invoke(org.aopalliance.MethodInvocation)
57      */

58     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
59         if (locked() && invocation.getMethod().getName().indexOf("set") == 0)
60             throw new LockedException();
61         return super.invoke(invocation);
62     }
63
64 }
Popular Tags