001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb.cfg;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.ejb.cfg21.EjbView;
034: import com.caucho.ejb.gen.BeanGenerator;
035: import com.caucho.ejb.gen21.BeanAssembler;
036: import com.caucho.ejb.gen21.ViewClass;
037: import com.caucho.java.JavaWriter;
038: import com.caucho.java.gen.BaseMethod;
039: import com.caucho.java.gen.CallChain;
040: import com.caucho.util.L10N;
041:
042: import java.io.IOException;
043:
044: /**
045: * Configuration for a method of a view.
046: */
047: public class EjbMethod {
048: private static final L10N L = new L10N(EjbMethod.class);
049:
050: public static final int TRANS_BEAN = 0;
051: public static final int TRANS_NOT_SUPPORTED = TRANS_BEAN + 1;
052: public static final int TRANS_SUPPORTS = TRANS_NOT_SUPPORTED + 1;
053: public static final int TRANS_REQUIRED = TRANS_SUPPORTS + 1;
054: public static final int TRANS_REQUIRES_NEW = TRANS_REQUIRED + 1;
055: public static final int TRANS_MANDATORY = TRANS_REQUIRES_NEW + 1;
056: public static final int TRANS_NEVER = TRANS_MANDATORY + 1;
057: public static final int TRANS_SINGLE_READ = TRANS_NEVER + 1;
058:
059: public final static int RESIN_DATABASE = 0;
060: public final static int RESIN_READ_ONLY = 1;
061: public final static int RESIN_ROW_LOCKING = 2;
062:
063: private EjbView _view;
064:
065: private ApiMethod _apiMethod;
066: private ApiMethod _implMethod;
067:
068: /**
069: * Creates a new method.
070: *
071: * @param view the owning view
072: * @param apiMethod the method from the view
073: * @param implMethod the method from the implementation
074: */
075: public EjbMethod(EjbView view, ApiMethod apiMethod,
076: ApiMethod implMethod) {
077: if (apiMethod == null)
078: throw new NullPointerException();
079:
080: _view = view;
081: _apiMethod = apiMethod;
082: _implMethod = implMethod;
083: }
084:
085: /**
086: * Returns the view.
087: */
088: public EjbView getView() {
089: return _view;
090: }
091:
092: /**
093: * Returns the view prefix.
094: */
095: public String getViewPrefix() {
096: return _view.getPrefix();
097: }
098:
099: /**
100: * Returns the API method.
101: */
102: public ApiMethod getApiMethod() {
103: return _apiMethod;
104: }
105:
106: /**
107: * Returns the Impl method.
108: */
109: public ApiMethod getImplMethod() {
110: return _implMethod;
111: }
112:
113: /**
114: * Assembles the bean method.
115: */
116: public void assembleBean(BeanAssembler beanAssembler,
117: String fullClassName) throws ConfigException {
118: }
119:
120: /**
121: * Assembles the method.
122: */
123: public BaseMethod assemble(ViewClass viewAssembler,
124: String fullClassName) throws ConfigException {
125: if (getImplMethod() == null)
126: throw new NullPointerException("no impl: " + getApiMethod());
127:
128: BaseMethod method = viewAssembler.createBusinessMethod(this );
129:
130: method.setCall(assembleCallChain(method.getCall()));
131:
132: return method;
133: }
134:
135: /**
136: * Assembles the call chain.
137: */
138: protected CallChain assembleCallChain(CallChain call) {
139: call = getView().getTransactionChain(call, getApiMethod(),
140: getImplMethod(), getViewPrefix());
141:
142: call = getView().getSecurityChain(call, getApiMethod(),
143: getViewPrefix());
144:
145: return call;
146: }
147:
148: /**
149: * Pushes a required transaction.
150: */
151: protected void pushRequired(JavaWriter out) throws IOException {
152:
153: out
154: .println("com.caucho.ejb.xa.TransactionContext xa = _xaManager.beginRequired();");
155: out.println("try {");
156: out.pushDepth();
157: }
158:
159: /**
160: * Pops a required transaction.
161: */
162: protected void popRequired(JavaWriter out) throws IOException {
163: out.popDepth();
164: out.println("} catch (RuntimeException e) {");
165: out.println(" throw trans.setRollbackOnly(e);");
166: out.println("} finally {");
167: out.println(" trans.commit();");
168: out.println("}");
169: }
170:
171: /**
172: * Returns true if these are equivalent.
173: */
174: public boolean equals(Object o) {
175: if (!(o instanceof EjbMethod))
176: return false;
177:
178: EjbMethod method = (EjbMethod) o;
179:
180: if (_view != method._view)
181: return false;
182: else if (!_apiMethod.equals(method._apiMethod))
183: return false;
184: else
185: return true;
186: }
187:
188: public String toString() {
189: return "EJBMethod[" + _apiMethod + "]";
190: }
191: }
|