1 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 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 classes, String methods, String objects) { 46 pointcut( 47 objects, 48 classes, 49 methods, 50 new SynchronizationWrapper(this), 51 null); 52 } 53 54 57 58 public class SynchronizationWrapper extends Wrapper { 59 Semaphore semaphore = new Semaphore(1); 62 63 public SynchronizationWrapper(AspectComponent ac) { 64 super(ac); 65 } 66 67 public Object synchronize(Interaction interaction) { 68 Object 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 invoke(MethodInvocation invocation) throws Throwable { 85 return synchronize((Interaction) invocation); 86 } 87 88 public Object construct(ConstructorInvocation invocation) 89 throws Throwable { 90 return synchronize((Interaction) invocation); 91 } 92 93 } 94 } 95 | Popular Tags |