KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > synchronization > SynchronizationAC


1 /*
2   Copyright (C) 2001-2002 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.synchronization;
19
20 import org.aopalliance.intercept.ConstructorInvocation;
21 import org.aopalliance.intercept.MethodInvocation;
22 import org.apache.log4j.Logger;
23 import org.objectweb.jac.core.AspectComponent;
24 import org.objectweb.jac.core.Interaction;
25 import org.objectweb.jac.core.Wrapper;
26 import org.objectweb.jac.util.Log;
27 import org.objectweb.jac.util.Semaphore;
28
29 /**
30  * This aspect component allows the programmer to synchronize a set of
31  * methods on different objects or classes in a centralized way (do
32  * not use the synchronized java keyword anymore).
33  *
34  * <p>The monitor implementation to provide synchronization is
35  * implemented by a semaphore.
36  *
37  * @see org.objectweb.jac.util.Semaphore */

38
39 public class SynchronizationAC
40     extends AspectComponent
41     implements SynchronizationConf {
42
43     static Logger logger = Logger.getLogger("synchronization");
44
45     public void synchronize(String JavaDoc classes, String JavaDoc methods, String JavaDoc objects) {
46         pointcut(
47             objects,
48             classes,
49             methods,
50             new SynchronizationWrapper(this),
51             null);
52     }
53
54     /**
55      * This inner wrapper implements the methods synchronization with a
56      * semaphore. */

57
58     public class SynchronizationWrapper extends Wrapper {
59         // one resource is available by default
60
// (only on thread can enter)
61
Semaphore semaphore = new Semaphore(1);
62
63         public SynchronizationWrapper(AspectComponent ac) {
64             super(ac);
65         }
66
67         public Object JavaDoc synchronize(Interaction interaction) {
68             Object JavaDoc ret = null;
69             logger.debug(interaction.wrappee
70                          + " acquires semaphore for "
71                          + interaction.method);
72             semaphore.acquire();
73             try {
74                 proceed(interaction);
75             } finally {
76                 logger.debug(interaction.wrappee
77                              + " releases semaphore for "
78                              + interaction.method);
79                 semaphore.release();
80             }
81             return ret;
82         }
83
84         public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
85             return synchronize((Interaction) invocation);
86         }
87
88         public Object JavaDoc construct(ConstructorInvocation invocation)
89             throws Throwable JavaDoc {
90             return synchronize((Interaction) invocation);
91         }
92
93     }
94 }
95
Popular Tags