1 22 package org.jboss.aspects.txlock; 23 24 import org.jboss.aop.Advisor; 25 import org.jboss.aop.InstanceAdvisor; 26 import org.jboss.aop.joinpoint.Joinpoint; 27 import org.jboss.aop.metadata.SimpleMetaData; 28 import org.jboss.aop.util.PayloadKey; 29 30 import javax.naming.InitialContext ; 31 import javax.naming.NamingException ; 32 import javax.transaction.TransactionManager ; 33 34 39 public class TxLockInterceptorFactory implements org.jboss.aop.advice.AspectFactory 40 { 41 42 private static TransactionManager getTransactionManager() 43 { 44 try 45 { 46 InitialContext ctx = new InitialContext (); 47 TransactionManager tm = (TransactionManager ) ctx.lookup("java:/TransactionManager"); 48 return tm; 49 } 50 catch (NamingException e) 51 { 52 throw new RuntimeException (e); 53 } 54 } 55 56 public Object createPerVM() 57 { 58 throw new RuntimeException ("this interceptor only supports PER_CLASS and PER_INSTANCE"); 59 } 60 61 public Object createPerClass(Advisor advisor) 62 { 63 QueuedTxLock lock = new QueuedTxLock(); 64 advisor.getClassMetaData().addMetaData(QueuedTxLock.TXLOCK, QueuedTxLock.TXLOCK, lock, PayloadKey.TRANSIENT); 65 return new TxLockInterceptor(getTransactionManager(), lock); 66 67 } 68 69 public Object createPerInstance(Advisor advisor, InstanceAdvisor instanceAdvisor) 70 { 71 SimpleMetaData instanceData = instanceAdvisor.getMetaData(); 72 QueuedTxLock lock = null; 73 synchronized(instanceData) 74 { 75 lock = (QueuedTxLock)instanceData.getMetaData(QueuedTxLock.TXLOCK, QueuedTxLock.TXLOCK); 76 if (lock == null) 77 { 78 lock = new QueuedTxLock(); 79 instanceData.addMetaData(QueuedTxLock.TXLOCK, QueuedTxLock.TXLOCK, lock, PayloadKey.TRANSIENT); 80 } 81 } 82 return new TxLockInterceptor(getTransactionManager(), lock); 83 } 84 85 public Object createPerJoinpoint(Advisor advisor, Joinpoint jp) 86 { 87 throw new RuntimeException ("this interceptor only supports PER_CLASS and PER_INSTANCE"); 88 } 89 90 public Object createPerJoinpoint(Advisor advisor, InstanceAdvisor instanceAdvisor, Joinpoint jp) 91 { 92 throw new RuntimeException ("this interceptor only supports PER_CLASS and PER_INSTANCE"); 93 } 94 95 public String getName() 96 { 97 return getClass().getName(); 98 } 99 } 100 | Popular Tags |