KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > transaction > enhancer > TransactionEnhancer


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.transaction.enhancer;
31
32 import com.caucho.bytecode.JAnnotation;
33 import com.caucho.bytecode.JMethod;
34 import com.caucho.config.ConfigException;
35 import com.caucho.java.gen.BaseMethod;
36 import com.caucho.java.gen.CallChain;
37 import com.caucho.java.gen.GenClass;
38 import com.caucho.loader.enhancer.MethodEnhancer;
39 import com.caucho.util.L10N;
40
41 import javax.ejb.TransactionAttributeType JavaDoc;
42 import java.lang.reflect.Method JavaDoc;
43
44 /**
45  * Enhancing a method objects.
46  */

47 public class TransactionEnhancer implements MethodEnhancer {
48   private static final L10N L = new L10N(TransactionEnhancer.class);
49
50   private Class JavaDoc _annotationType;
51
52   /**
53    * Sets the annotation type.
54    */

55   public void setAnnotation(Class JavaDoc type)
56     throws ConfigException
57   {
58     _annotationType = type;
59
60     try {
61       Method method = type.getMethod("value");
62
63       if (method != null &&
64       method.getReturnType().equals(TransactionAttributeType JavaDoc.class))
65     return;
66     } catch (Throwable JavaDoc e) {
67     }
68
69     throw new ConfigException(L.l("'{0}' is an illegal annotation type for TransactionEnhancer. The annotation must have a value() method returning a TransactionAttributeType.",
70                   type.getName()));
71   }
72   
73   /**
74    * Enhances the method.
75    *
76    * @param genClass the generated class
77    * @param jMethod the method to be enhanced
78    * @param jAnn the annotation to be enhanced
79    */

80   public void enhance(GenClass genClass,
81               JMethod jMethod,
82               JAnnotation jAnn)
83   {
84     TransactionAttributeType JavaDoc type;
85     type = (TransactionAttributeType JavaDoc) jAnn.get("value");
86
87     BaseMethod genMethod = genClass.createMethod(jMethod);
88     CallChain call = genMethod.getCall();
89
90     switch (type) {
91     case MANDATORY:
92       call = new MandatoryCallChain(call);
93       break;
94     case REQUIRED:
95       call = new RequiredCallChain(call);
96       break;
97     case REQUIRES_NEW:
98       call = new RequiresNewCallChain(call);
99       break;
100     case NEVER:
101       call = new NeverCallChain(call);
102       break;
103     case SUPPORTS:
104       call = new SupportsCallChain(call);
105       break;
106     case NOT_SUPPORTED:
107       call = new SuspendCallChain(call);
108       break;
109     default:
110       break;
111     }
112
113     genMethod.setCall(call);
114   }
115 }
116
Popular Tags