1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // SynchronizedDecorator.java Copyright (c) 2009 Giovanni Azua Garcia
5 // bravegag@hotmail.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 3
10 // of the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, see <http://www.gnu.org/licenses/>.
19 //
20 //----------------------------------------------------------------------
21 package org.perfectjpattern.core.structural.proxy;
22
23 import java.lang.reflect.*;
24 import java.util.concurrent.locks.*;
25
26 /**
27 * Concrete componentized implementation of <code>IDecorator<C></code>
28 * that provides synchronized protection to any class type.
29 *
30 * @param <C> Decorated Component type
31 *
32 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
33 * @version $Revision: 1.0 $ $Date: Nov 25, 2007 4:11:47 PM $
34 */
35 public
36 class SynchronizedProxy<C>
37 extends AbstractProxy<C>
38 {
39 //------------------------------------------------------------------------
40 // members
41 //------------------------------------------------------------------------
42 /**
43 * Creates a <code>SynchronizedDecorator<E></code> from a Component
44 * interface type and instance.
45 *
46 * @param anInterface The Component interface type.
47 * @param aComponent The Component instance to Decorate.
48 * @throws IllegalArgumentException 'anInterface' must not be null.
49 * @throws IllegalArgumentException 'anInterface' must be an interface type.
50 * @throws IllegalArgumentException 'aComponent' must not be null.
51 */
52 public
53 SynchronizedProxy(Class<C> anInterface, C aComponent)
54 throws IllegalArgumentException
55 {
56 super(anInterface, aComponent);
57 }
58
59 //------------------------------------------------------------------------
60 /**
61 * {@inheritDoc}
62 */
63 @Override
64 protected Object
65 invokeUnderlying(Method anMethod, Object[] anArguments)
66 throws Throwable
67 {
68 try
69 {
70 theLock.lock();
71
72 return super.invokeUnderlying(anMethod, anArguments);
73 }
74 finally
75 {
76 theLock.unlock();
77 }
78 }
79
80 //------------------------------------------------------------------------
81 // members
82 //------------------------------------------------------------------------
83 private final Lock theLock = new ReentrantLock();
84 }