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.gen;
031:
032: import com.caucho.java.JavaWriter;
033: import com.caucho.ejb.cfg.*;
034: import com.caucho.util.L10N;
035:
036: import java.io.IOException;
037: import javax.ejb.*;
038:
039: /**
040: * Generates the skeleton for a session bean.
041: */
042: public class StatelessGenerator extends SessionGenerator {
043: private static final L10N L = new L10N(StatelessGenerator.class);
044:
045: public StatelessGenerator(String ejbName, ApiClass ejbClass) {
046: super (ejbName, ejbClass);
047: }
048:
049: public boolean isStateless() {
050: return true;
051: }
052:
053: @Override
054: protected View createLocalView(ApiClass api) {
055: return new StatelessLocalView(this , api);
056: }
057:
058: @Override
059: protected View createLocalHomeView(ApiClass api) {
060: return new StatelessLocalHomeView(this , api);
061: }
062:
063: @Override
064: protected View createRemoteView(ApiClass api) {
065: return new StatelessRemoteView(this , api);
066: }
067:
068: @Override
069: protected View createRemoteHomeView(ApiClass api) {
070: return new StatelessRemoteHomeView(this , api);
071: }
072:
073: /**
074: * Generates the stateful session bean
075: */
076: @Override
077: public void generate(JavaWriter out) throws IOException {
078: generateTopComment(out);
079:
080: out.println();
081: out.println("package " + getPackageName() + ";");
082:
083: out.println();
084: out.println("import com.caucho.config.*;");
085: out.println("import com.caucho.ejb.session.*;");
086: out.println();
087: out.println("import javax.ejb.*;");
088: out.println("import javax.transaction.*;");
089:
090: out.println();
091: out.println("public class " + getClassName());
092: out.println(" extends StatelessContext");
093: out.println("{");
094: out.pushDepth();
095:
096: generateContext(out);
097:
098: /*
099: for (View view : getViews()) {
100: view.generateContextPrologue(out);
101: }
102: */
103:
104: generateCreateProvider(out);
105:
106: generateViews(out);
107:
108: out.popDepth();
109: out.println("}");
110: }
111:
112: protected void generateCreateProvider(JavaWriter out)
113: throws IOException {
114: out.println();
115: out.println("public StatelessProvider getProvider(Class api)");
116: out.println("{");
117: out.pushDepth();
118:
119: for (View view : getViews()) {
120: StatelessView sView = (StatelessView) view;
121:
122: sView.generateCreateProvider(out, "api");
123: }
124:
125: out.println();
126: out.println("return super.getProvider(api);");
127:
128: out.popDepth();
129: out.println("}");
130: }
131:
132: @Override
133: protected void generateContext(JavaWriter out) throws IOException {
134: String shortContextName = getEjbClass().getSimpleName();
135:
136: int freeStackMax = 16;
137:
138: out
139: .println("protected static final java.util.logging.Logger __caucho_log = java.util.logging.Logger.getLogger(\""
140: + getFullClassName() + "\");");
141: out
142: .println("protected static final boolean __caucho_isFiner = __caucho_log.isLoggable(java.util.logging.Level.FINER);");
143: out.println();
144: out
145: .println("com.caucho.ejb.xa.EjbTransactionManager _xaManager;");
146:
147: String beanClass = getEjbClass().getName();
148:
149: out.println("private " + beanClass + " []_freeBeanStack = new "
150: + beanClass + "[" + freeStackMax + "];");
151: out.println("private int _freeBeanTop;");
152: out.println();
153: out.println("public " + getClassName()
154: + "(StatelessServer server)");
155: out.println("{");
156: out.pushDepth();
157:
158: out.println("super(server);");
159: out.println("_xaManager = server.getTransactionManager();");
160:
161: for (View view : getViews()) {
162: view.generateContextHomeConstructor(out);
163: }
164:
165: out.popDepth();
166: out.println("}");
167:
168: for (View view : getViews()) {
169: view.generateContextPrologue(out);
170: }
171:
172: out.println();
173: out.println("public void destroy()");
174: out.println("{");
175: out.pushDepth();
176:
177: generateDestroyViews(out);
178:
179: out.popDepth();
180: out.println("}");
181:
182: /*
183: out.println();
184: out.println(beanClass + " _ejb_begin()");
185: out.println("{");
186: out.pushDepth();
187: out.println(beanClass + " bean;");
188: out.println("synchronized (this) {");
189: out.println(" if (_freeBeanTop > 0) {");
190: out.println(" bean = _freeBeanStack[--_freeBeanTop];");
191: out.println(" return bean;");
192: out.println(" }");
193: out.println("}");
194: out.println();
195: out.println("try {");
196: out.println(" bean = new " + beanClass + "();");
197:
198: if (hasMethod("setSessionContext", new Class[] { SessionContext.class })) {
199: out.println(" bean.setSessionContext(this);");
200: }
201:
202: if (hasMethod("ejbCreate", new Class[0])) {
203: // ejb/0fe0: ejbCreate can be private, out.println(" bean.ejbCreate();");
204: out.println(" bean.ejbCreate();");
205:
206: // out.println(" invokeMethod(bean, \"ejbCreate\", new Class[] {}, new Object[] {});");
207: }
208:
209: out.println(" getStatelessServer().initInstance(bean);");
210:
211: out.println(" return bean;");
212: out.println("} catch (Exception e) {");
213: out.println(" throw com.caucho.ejb.EJBExceptionWrapper.create(e);");
214: out.println("}");
215: out.popDepth();
216: out.println("}");
217:
218: out.println();
219: out.println("void _ejb_free(" + beanClass + " bean)");
220: out.println(" throws javax.ejb.EJBException");
221: out.println("{");
222: out.pushDepth();
223: out.println("if (bean == null)");
224: out.println(" return;");
225: out.println();
226: out.println("synchronized (this) {");
227: out.println(" if (_freeBeanTop < _freeBeanStack.length) {");
228: out.println(" _freeBeanStack[_freeBeanTop++] = bean;");
229: out.println(" return;");
230: out.println(" }");
231: out.println("}");
232: */
233:
234: /*
235: if (hasMethod("ejbRemove", new Class[0])) {
236: out.println();
237: // ejb/0fe0: ejbRemove() can be private, out.println("bean.ejbRemove();");
238: out.println("invokeMethod(bean, \"ejbRemove\", new Class[] {}, new Object[] {});");
239: }
240: */
241:
242: /*
243: out.popDepth();
244: out.println("}");
245: */
246:
247: /*
248: out.println();
249: out.println("public void destroy()");
250: out.println("{");
251: out.pushDepth();
252: out.println(beanClass + " ptr;");
253: out.println(beanClass + " []freeBeanStack;");
254: out.println("int freeBeanTop;");
255:
256: out.println("synchronized (this) {");
257: out.println(" freeBeanStack = _freeBeanStack;");
258: out.println(" freeBeanTop = _freeBeanTop;");
259: out.println(" _freeBeanStack = null;");
260: out.println(" _freeBeanTop = 0;");
261: out.println("}");
262:
263: if (hasMethod("ejbRemove", new Class[0])) {
264: out.println();
265: out.println("for (int i = 0; i < freeBeanTop; i++) {");
266: out.pushDepth();
267:
268: out.println("try {");
269: out.println(" if (freeBeanStack[i] != null)");
270: // ejb/0fe0: ejbRemove() can be private out.println(" freeBeanStack[i].ejbRemove();");
271: out.println(" freeBeanStack[i].ejbRemove();");
272: out.println("} catch (Throwable e) {");
273: out.println(" __caucho_log.log(java.util.logging.Level.FINE, e.toString(), e);");
274: out.println("}");
275:
276: out.popDepth();
277: out.println("}");
278: }
279:
280: out.popDepth();
281: out.println("}");
282: */
283: }
284:
285: public void generateViews(JavaWriter out) throws IOException {
286: for (View view : getViews()) {
287: out.println();
288:
289: view.generate(out);
290: }
291: }
292:
293: protected void generateNewInstance(JavaWriter out)
294: throws IOException {
295: }
296: }
|