KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > tx > TxInterceptorFactory


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software 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 GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.tx;
23
24 import java.lang.reflect.Method JavaDoc;
25 import javax.ejb.TransactionAttribute JavaDoc;
26 import javax.ejb.TransactionAttributeType JavaDoc;
27 import javax.ejb.TransactionManagementType JavaDoc;
28 import org.jboss.annotation.ejb.TransactionTimeout;
29 import org.jboss.aop.Advisor;
30 import org.jboss.aop.joinpoint.Joinpoint;
31 import org.jboss.aop.joinpoint.MethodJoinpoint;
32 import org.jboss.aspects.tx.TxInterceptor;
33 import org.jboss.logging.Logger;
34 import org.jboss.tm.TransactionManagerLocator;
35 import org.jboss.tm.TxManager;
36 import org.jboss.ejb3.stateful.StatefulContainer;
37 import org.jboss.ejb3.stateless.StatelessContainer;
38
39 /**
40  * This interceptor handles transactions for AOP
41  *
42  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
43  * @version $Revision: 56987 $
44  */

45 public class TxInterceptorFactory extends org.jboss.aspects.tx.TxInterceptorFactory
46 {
47    private static final Logger log = Logger
48    .getLogger(TxInterceptorFactory.class);
49
50    protected String JavaDoc resolveTxType(Advisor advisor, Joinpoint jp)
51    {
52       Method JavaDoc method = ((MethodJoinpoint) jp).getMethod();
53       TransactionAttribute JavaDoc tx = (TransactionAttribute JavaDoc) advisor.resolveAnnotation(method, TransactionAttribute JavaDoc.class);
54
55       if (tx == null)
56          tx = (TransactionAttribute JavaDoc) advisor.resolveAnnotation(TransactionAttribute JavaDoc.class);
57
58       String JavaDoc value = "REQUIRED";
59       if (tx != null)
60       {
61          TransactionAttributeType JavaDoc type = tx.value();
62
63          if (type == null)
64          {
65             value = "REQUIRED";
66          }
67          else if (type == TransactionAttributeType.NOT_SUPPORTED)
68          {
69             value = "NOTSUPPORTED";
70          }
71          else if (type == TransactionAttributeType.REQUIRES_NEW)
72          {
73             value = "REQUIRESNEW";
74          }
75          else
76          {
77             value = type.name();
78          }
79       }
80
81       return value;
82    }
83
84    protected int resolveTransactionTimeout(Advisor advisor, Method JavaDoc method)
85    {
86       TransactionTimeout annotation = (TransactionTimeout)advisor.resolveAnnotation(method, TransactionTimeout.class);
87       if (annotation != null)
88       {
89          return annotation.value();
90       }
91
92       return -1;
93    }
94
95    protected void initializePolicy()
96    {
97       policy = new Ejb3TxPolicy();
98    }
99
100    public Object JavaDoc createPerJoinpoint(Advisor advisor, Joinpoint jp)
101    {
102       // We have to do this until AOP supports matching based on annotation attributes
103
TransactionManagementType JavaDoc type = TxUtil.getTransactionManagementType(advisor);
104       if (type == TransactionManagementType.BEAN)
105          return new BMTInterceptor(TxUtil.getTransactionManager(), !(advisor instanceof StatefulContainer));
106
107       Method JavaDoc method = ((MethodJoinpoint) jp).getMethod();
108       int timeout = resolveTransactionTimeout(advisor, method);
109
110       if (policy == null);
111          super.initialize();
112
113       String JavaDoc txType = resolveTxType(advisor, jp).toUpperCase();
114       if (txType.equals("REQUIRED"))
115       {
116          return new TxInterceptor.Required(TxUtil.getTransactionManager(), policy, timeout);
117       }
118       else if (txType.equals("REQUIRESNEW"))
119       {
120          return new TxInterceptor.RequiresNew(TxUtil.getTransactionManager(), policy, timeout);
121       }
122       else
123       {
124          return super.createPerJoinpoint(advisor, jp);
125       }
126    }
127
128
129    public String JavaDoc getName()
130    {
131       return getClass().getName();
132    }
133 }
134
Popular Tags