KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > annotation > handlers > TransactionAttributeHandler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.deployment.annotation.handlers;
24
25 import java.lang.annotation.Annotation JavaDoc;
26 import java.lang.annotation.ElementType JavaDoc;
27 import java.lang.reflect.AnnotatedElement JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29
30 import java.util.Set JavaDoc;
31
32 import javax.ejb.TransactionAttribute JavaDoc;
33 import javax.ejb.TransactionAttributeType JavaDoc;
34 import javax.ejb.Timeout JavaDoc;
35 import javax.ejb.TransactionManagement JavaDoc;
36 import javax.ejb.MessageDriven JavaDoc;
37 import javax.ejb.Stateful JavaDoc;
38 import javax.ejb.Stateless JavaDoc;
39
40 import com.sun.enterprise.util.TypeUtil;
41 import com.sun.enterprise.deployment.ContainerTransaction;
42 import com.sun.enterprise.deployment.EjbDescriptor;
43 import com.sun.enterprise.deployment.MethodDescriptor;
44
45 import com.sun.enterprise.deployment.annotation.AnnotationInfo;
46 import com.sun.enterprise.deployment.annotation.AnnotatedElementHandler;
47 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException;
48 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult;
49 import com.sun.enterprise.deployment.annotation.context.EjbContext;
50
51 /**
52  * This handler is responsible for handling the javax.ejb.TransactionAttribute.
53  *
54  * @author Shing Wai Chan
55  */

56 public class TransactionAttributeHandler extends AbstractAttributeHandler
57         implements PostProcessor {
58     
59     public TransactionAttributeHandler() {
60     }
61     
62     /**
63      * @return the annoation type this annotation handler is handling
64      */

65     public Class JavaDoc<? extends Annotation JavaDoc> getAnnotationType() {
66         return TransactionAttribute JavaDoc.class;
67     }
68         
69     protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo,
70             EjbContext[] ejbContexts) throws AnnotationProcessorException {
71         
72         TransactionAttribute JavaDoc taAn =
73             (TransactionAttribute JavaDoc) ainfo.getAnnotation();
74
75         for (EjbContext ejbContext : ejbContexts) {
76             EjbDescriptor ejbDesc = ejbContext.getDescriptor();
77             ContainerTransaction containerTransaction =
78                 getContainerTransaction(taAn.value());
79
80             if (ElementType.TYPE.equals(ainfo.getElementType())) {
81                 ejbContext.addPostProcessInfo(ainfo, this);
82             } else {
83                 Method JavaDoc annMethod = (Method JavaDoc) ainfo.getAnnotatedElement();
84                 
85                 Set JavaDoc txBusMethods = ejbDesc.getTxBusinessMethodDescriptors();
86                 for (Object JavaDoc next : txBusMethods) {
87                     MethodDescriptor nextDesc = (MethodDescriptor) next;
88                     Method JavaDoc m = nextDesc.getMethod(ejbDesc);
89                     if( TypeUtil.sameMethodSignature(m, annMethod) &&
90                             ejbDesc.getContainerTransactionFor(nextDesc) == null ) {
91                         // override by xml
92
ejbDesc.setContainerTransactionFor
93                             (nextDesc, containerTransaction);
94                     }
95                 }
96             }
97         }
98
99         return getDefaultProcessedResult();
100     }
101
102     private ContainerTransaction getContainerTransaction(
103             TransactionAttributeType JavaDoc taType) {
104         switch(taType) {
105             case MANDATORY:
106                 return new ContainerTransaction(
107                         ContainerTransaction.MANDATORY,
108                         ContainerTransaction.MANDATORY);
109             case REQUIRED:
110                 return new ContainerTransaction(
111                         ContainerTransaction.REQUIRED,
112                         ContainerTransaction.REQUIRED);
113             case REQUIRES_NEW:
114                 return new ContainerTransaction(
115                         ContainerTransaction.REQUIRES_NEW,
116                         ContainerTransaction.REQUIRES_NEW);
117             case SUPPORTS:
118                 return new ContainerTransaction(
119                         ContainerTransaction.SUPPORTS,
120                         ContainerTransaction.SUPPORTS);
121             case NOT_SUPPORTED:
122                 return new ContainerTransaction(
123                         ContainerTransaction.NOT_SUPPORTED,
124                         ContainerTransaction.NOT_SUPPORTED);
125             default: // NEVER
126
return new ContainerTransaction(
127                         ContainerTransaction.NEVER,
128                         ContainerTransaction.NEVER);
129         }
130     }
131
132     /**
133      * @return an array of annotation types this annotation handler would
134      * require to be processed (if present) before it processes it's own
135      * annotation type.
136      */

137     public Class JavaDoc<? extends Annotation JavaDoc>[] getTypeDependencies() {
138         return new Class JavaDoc[] {
139             MessageDriven JavaDoc.class, Stateful JavaDoc.class, Stateless JavaDoc.class,
140                 Timeout JavaDoc.class, TransactionManagement JavaDoc.class};
141                 
142     }
143
144     protected boolean supportTypeInheritance() {
145         return true;
146     }
147
148     public void postProcessAnnotation(AnnotationInfo ainfo,
149             AnnotatedElementHandler aeHandler)
150             throws AnnotationProcessorException {
151         EjbContext ejbContext = (EjbContext)aeHandler;
152         EjbDescriptor ejbDesc = ejbContext.getDescriptor();
153         TransactionAttribute JavaDoc taAn =
154             (TransactionAttribute JavaDoc) ainfo.getAnnotation();
155         ContainerTransaction containerTransaction =
156             getContainerTransaction(taAn.value());
157         Class JavaDoc classAn = (Class JavaDoc)ainfo.getAnnotatedElement();
158
159         Set JavaDoc txBusMethods = ejbDesc.getTxBusinessMethodDescriptors();
160         for (Object JavaDoc mdObj : txBusMethods) {
161             MethodDescriptor md = (MethodDescriptor)mdObj;
162             // override by xml
163
if (classAn.equals(ejbContext.getDeclaringClass(md)) &&
164                     ejbDesc.getContainerTransactionFor(md) == null) {
165                 ejbDesc.setContainerTransactionFor(
166                     md, containerTransaction);
167             }
168         }
169     }
170 }
171
Popular Tags