KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aspects > 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.aspects.tx;
23
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.Field JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import org.jboss.aop.Advisor;
29 import org.jboss.aop.InstanceAdvisor;
30 import org.jboss.aop.joinpoint.ConstructorJoinpoint;
31 import org.jboss.aop.joinpoint.Joinpoint;
32 import org.jboss.aop.joinpoint.MethodJoinpoint;
33 import org.jboss.aop.joinpoint.FieldJoinpoint;
34 import org.jboss.tm.TransactionManagerLocator;
35
36 /**
37  * This interceptor handles transactions for AOP
38  *
39  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
40  * @version $Revision: 43966 $
41  */

42 public class TxInterceptorFactory implements org.jboss.aop.advice.AspectFactory
43 {
44    protected TxPolicy policy;
45    protected HashMap JavaDoc nameMap = new HashMap JavaDoc();
46
47    protected void initializePolicy()
48    {
49       policy = new TxPolicy();
50    }
51
52    public void initialize()
53    {
54       if (policy != null) return;
55       initializePolicy();
56       nameMap.put("NEVER", new TxInterceptor.Never(TransactionManagerLocator.getInstance().locate(), policy));
57       nameMap.put("NOTSUPPORTED", new TxInterceptor.NotSupported(TransactionManagerLocator.getInstance().locate(), policy));
58       nameMap.put("SUPPORTS", new TxInterceptor.Supports(TransactionManagerLocator.getInstance().locate(), policy));
59       nameMap.put("REQUIRED", new TxInterceptor.Required(TransactionManagerLocator.getInstance().locate(), policy));
60       nameMap.put("REQUIRESNEW", new TxInterceptor.RequiresNew(TransactionManagerLocator.getInstance().locate(), policy));
61       nameMap.put("MANDATORY", new TxInterceptor.Mandatory(TransactionManagerLocator.getInstance().locate(), policy));
62    }
63
64    protected String JavaDoc resolveTxType(Advisor advisor, Joinpoint jp)
65    {
66       if (jp instanceof ConstructorJoinpoint)
67       {
68          Constructor JavaDoc con = ((ConstructorJoinpoint)jp).getConstructor();
69          String JavaDoc txType = (String JavaDoc)advisor.getConstructorMetaData().getConstructorMetaData(con, "transaction", "trans-attribute");
70          if (txType != null) return txType;
71
72          txType = (String JavaDoc)advisor.getDefaultMetaData().getMetaData("transaction", "trans-attribute");
73          if (txType != null) return txType;
74
75          Tx tx = (Tx)advisor.resolveAnnotation(con, Tx.class);
76          if (tx == null)
77          {
78             tx = (Tx)advisor.resolveAnnotation(Tx.class);
79          }
80          if (tx == null) return "REQUIRED";
81          return tx.value().name();
82       }
83       else if (jp instanceof MethodJoinpoint)
84       {
85          Method JavaDoc con = ((MethodJoinpoint)jp).getMethod();
86          String JavaDoc txType = (String JavaDoc)advisor.getMethodMetaData().getMethodMetaData(con, "transaction", "trans-attribute");
87          if (txType != null) return txType;
88
89          txType = (String JavaDoc)advisor.getDefaultMetaData().getMetaData("transaction", "trans-attribute");
90          if (txType != null) return txType;
91
92          Tx tx = (Tx)advisor.resolveAnnotation(con, Tx.class);
93          if (tx == null)
94          {
95             tx = (Tx)advisor.resolveAnnotation(Tx.class);
96          }
97          if (tx == null) return "REQUIRED";
98          return tx.value().name();
99       }
100       else
101       {
102          Field JavaDoc con = ((FieldJoinpoint)jp).getField();
103          String JavaDoc txType = (String JavaDoc)advisor.getFieldMetaData().getFieldMetaData(con, "transaction", "trans-attribute");
104          if (txType != null) return txType;
105
106          txType = (String JavaDoc)advisor.getDefaultMetaData().getMetaData("transaction", "trans-attribute");
107          if (txType != null) return txType;
108
109          Tx tx = (Tx)advisor.resolveAnnotation(con, Tx.class);
110          if (tx == null)
111          {
112             tx = (Tx)advisor.resolveAnnotation(Tx.class);
113          }
114          if (tx == null) return "REQUIRED";
115          return tx.value().name();
116       }
117    }
118
119
120    public Object JavaDoc createPerJoinpoint(Advisor advisor, Joinpoint jp)
121    {
122       initialize();
123       String JavaDoc txType = resolveTxType(advisor, jp);
124       Object JavaDoc rtn = nameMap.get(txType.toUpperCase());
125       if (rtn == null) throw new RuntimeException JavaDoc("TX TYPE was null for: " + txType);
126       return rtn;
127    }
128
129    public Object JavaDoc createPerVM()
130    {
131       throw new IllegalStateException JavaDoc("Scope not allowed");
132    }
133
134    public Object JavaDoc createPerClass(Advisor advisor)
135    {
136       throw new IllegalStateException JavaDoc("Scope not allowed");
137    }
138
139    public Object JavaDoc createPerInstance(Advisor advisor, InstanceAdvisor instanceAdvisor)
140    {
141       throw new IllegalStateException JavaDoc("Scope not allowed");
142    }
143
144    public Object JavaDoc createPerJoinpoint(Advisor advisor, InstanceAdvisor instanceAdvisor, Joinpoint jp)
145    {
146       throw new IllegalStateException JavaDoc("Scope not allowed");
147    }
148
149    public String JavaDoc getName()
150    {
151       return getClass().getName();
152    }
153 }
154
Popular Tags