KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > cfg > EjbMethod


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.ejb.cfg;
31
32 import com.caucho.bytecode.JMethod;
33 import com.caucho.config.ConfigException;
34 import com.caucho.ejb.gen.BeanAssembler;
35 import com.caucho.ejb.gen.ViewClass;
36 import com.caucho.java.JavaWriter;
37 import com.caucho.java.gen.BaseMethod;
38 import com.caucho.java.gen.CallChain;
39 import com.caucho.util.L10N;
40
41 import java.io.IOException JavaDoc;
42
43 /**
44  * Configuration for a method of a view.
45  */

46 public class EjbMethod {
47   private static final L10N L = new L10N(EjbMethod.class);
48   
49   public static final int TRANS_BEAN = 0;
50   public static final int TRANS_NOT_SUPPORTED = TRANS_BEAN + 1;
51   public static final int TRANS_SUPPORTS = TRANS_NOT_SUPPORTED + 1;
52   public static final int TRANS_REQUIRED = TRANS_SUPPORTS + 1;
53   public static final int TRANS_REQUIRES_NEW = TRANS_REQUIRED + 1;
54   public static final int TRANS_MANDATORY = TRANS_REQUIRES_NEW + 1;
55   public static final int TRANS_NEVER = TRANS_MANDATORY + 1;
56   public static final int TRANS_SINGLE_READ = TRANS_NEVER + 1;
57
58   public final static int RESIN_DATABASE = 0;
59   public final static int RESIN_READ_ONLY = 1;
60   public final static int RESIN_ROW_LOCKING = 2;
61
62   private EjbView _view;
63
64   private JMethod _apiMethod;
65   private JMethod _implMethod;
66
67   /**
68    * Creates a new method.
69    *
70    * @param view the owning view
71    * @param apiMethod the method from the view
72    * @param implMethod the method from the implementation
73    */

74   public EjbMethod(EjbView view, JMethod apiMethod, JMethod implMethod)
75   {
76     if (apiMethod == null)
77       throw new NullPointerException JavaDoc();
78     
79     _view = view;
80     _apiMethod = apiMethod;
81     _implMethod = implMethod;
82   }
83
84   /**
85    * Returns the view.
86    */

87   public EjbView getView()
88   {
89     return _view;
90   }
91
92   /**
93    * Returns the view prefix.
94    */

95   public String JavaDoc getViewPrefix()
96   {
97     return _view.getPrefix();
98   }
99
100   /**
101    * Returns the API method.
102    */

103   public JMethod getApiMethod()
104   {
105     return _apiMethod;
106   }
107
108   /**
109    * Returns the Impl method.
110    */

111   public JMethod getImplMethod()
112   {
113     return _implMethod;
114   }
115
116   /**
117    * Assembles the bean method.
118    */

119   public void assembleBean(BeanAssembler beanAssembler, String JavaDoc fullClassName)
120     throws ConfigException
121   {
122   }
123
124   /**
125    * Assembles the method.
126    */

127   public BaseMethod assemble(ViewClass viewAssembler, String JavaDoc fullClassName)
128     throws ConfigException
129   {
130     if (getImplMethod() == null)
131       throw new NullPointerException JavaDoc("no impl: " + getApiMethod());
132
133     BaseMethod method = viewAssembler.createBusinessMethod(this);
134
135     method.setCall(assembleCallChain(method.getCall()));
136     
137     return method;
138   }
139
140   /**
141    * Assembles the call chain.
142    */

143   protected CallChain assembleCallChain(CallChain call)
144   {
145     call = getView().getTransactionChain(call,
146                      getApiMethod(),
147                      getViewPrefix());
148     
149     call = getView().getSecurityChain(call,
150                       getApiMethod(),
151                       getViewPrefix());
152
153     return call;
154   }
155
156   /**
157    * Pushes a required transaction.
158    */

159   protected void pushRequired(JavaWriter out)
160     throws IOException JavaDoc
161   {
162
163     out.println("com.caucho.ejb.xa.TransactionContext xa = _xaManager.beginRequired();");
164     out.println("try {");
165     out.pushDepth();
166   }
167
168   /**
169    * Pops a required transaction.
170    */

171   protected void popRequired(JavaWriter out)
172     throws IOException JavaDoc
173   {
174     out.popDepth();
175     out.println("} catch (RuntimeException e) {");
176     out.println(" throw trans.setRollbackOnly(e);");
177     out.println("} finally {");
178     out.println(" trans.commit();");
179     out.println("}");
180   }
181
182   /**
183    * Returns true if these are equivalent.
184    */

185   public boolean equals(Object JavaDoc o)
186   {
187     if (! (o instanceof EjbMethod))
188       return false;
189
190     EjbMethod method = (EjbMethod) o;
191
192     if (_view != method._view)
193       return false;
194     else if (! _apiMethod.equals(method._apiMethod))
195       return false;
196     else
197       return true;
198   }
199
200   public String JavaDoc toString()
201   {
202     return "EJBMethod[" + _apiMethod + "]";
203   }
204 }
205
Popular Tags