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 javax.ejb.*;
037: import static javax.ejb.TransactionAttributeType.*;
038: import java.io.IOException;
039: import java.lang.reflect.*;
040: import java.util.*;
041:
042: /**
043: * Generates the skeleton for a message bean.
044: */
045: public class MessageGenerator extends BeanGenerator {
046: private static final L10N L = new L10N(BeanGenerator.class);
047:
048: private MessageView _view;
049: private ArrayList<View> _views = new ArrayList<View>();
050:
051: public MessageGenerator(String ejbName, ApiClass ejbClass) {
052: super (toFullClassName(ejbName, ejbClass.getSimpleName()),
053: ejbClass);
054: }
055:
056: private static String toFullClassName(String ejbName,
057: String className) {
058: StringBuilder sb = new StringBuilder();
059:
060: sb.append("_ejb.");
061:
062: if (!Character.isJavaIdentifierStart(ejbName.charAt(0)))
063: sb.append('_');
064:
065: for (int i = 0; i < ejbName.length(); i++) {
066: char ch = ejbName.charAt(i);
067:
068: if (ch == '/')
069: sb.append('.');
070: else if (Character.isJavaIdentifierPart(ch))
071: sb.append(ch);
072: else
073: sb.append('_');
074: }
075:
076: sb.append(".");
077: sb.append(className);
078: sb.append("__EJB");
079:
080: return sb.toString();
081: }
082:
083: public void setApi(ApiClass api) {
084: _view = new MessageView(this , api);
085: _views.add(_view);
086: }
087:
088: public ArrayList<View> getViews() {
089: return _views;
090: }
091:
092: /**
093: * Introspects the bean.
094: */
095: @Override
096: public void introspect() {
097: super .introspect();
098:
099: for (View view : getViews())
100: view.introspect();
101: }
102:
103: /**
104: * Generates the message session bean
105: */
106: @Override
107: public void generate(JavaWriter out) throws IOException {
108: generateTopComment(out);
109:
110: out.println();
111: out.println("package " + getPackageName() + ";");
112:
113: out.println();
114: out.println("import com.caucho.config.*;");
115: out.println("import com.caucho.ejb.*;");
116: out.println("import com.caucho.ejb.message.*;");
117: out.println();
118: out.println("import java.util.*;");
119: out.println("import java.lang.reflect.*;");
120: out.println("import javax.ejb.*;");
121: out.println("import javax.transaction.*;");
122: out.println("import javax.transaction.xa.*;");
123: out.println("import javax.resource.spi.endpoint.*;");
124:
125: out.println();
126: out.println("public class " + getClassName() + " extends "
127: + getEjbClass().getName()
128: + " implements MessageEndpoint, CauchoMessageEndpoint");
129: out.println("{");
130: out.pushDepth();
131:
132: // ejb/0931
133: out.println();
134: out
135: .println("private static final com.caucho.ejb3.xa.XAManager _xa");
136: out.println(" = new com.caucho.ejb3.xa.XAManager();");
137:
138: out
139: .println("private static HashSet<Method> _xaMethods = new HashSet<Method>();");
140: out.println();
141: out.println("private MessageServer _server;");
142: out.println("private XAResource _xaResource;");
143: out.println("private boolean _isXa;");
144:
145: HashMap map = new HashMap();
146: map.put("caucho.ejb.xa", "true");
147: for (View view : getViews()) {
148: // view.generateContextPrologue(out);
149: view.generateBusinessPrologue(out, map);
150: }
151:
152: out.println();
153: out.println("public " + getClassName()
154: + "(MessageServer server)");
155: out.println("{");
156: out.pushDepth();
157:
158: out.println("_server = server;");
159:
160: if (MessageDrivenBean.class.isAssignableFrom(getEjbClass()
161: .getJavaClass())) {
162: out
163: .println("setMessageDrivenContext(server.getMessageContext());");
164: }
165:
166: _view.generateBusinessConstructor(out);
167:
168: out.popDepth();
169: out.println("}");
170:
171: out.println();
172: out.println("static {");
173: out.pushDepth();
174: out.println("try {");
175: out.pushDepth();
176:
177: for (BusinessMethodGenerator bizMethod : _view.getMethods()) {
178: if (REQUIRED.equals(bizMethod.getXa().getTransactionType())) {
179: Method api = bizMethod.getApiMethod();
180:
181: out.print("_xaMethods.add(");
182: out.printClass(api.getDeclaringClass());
183: out.print(".class.getMethod(\"");
184: out.print(api.getName());
185: out.print("\", new Class[] { ");
186:
187: for (Class cl : api.getParameterTypes()) {
188: out.printClass(cl);
189: out.print(".class, ");
190: }
191: out.println("}));");
192: }
193: }
194:
195: out.popDepth();
196: out.println("} catch (Exception e) {");
197: out.println(" throw new RuntimeException(e);");
198: out.println("}");
199:
200: out.popDepth();
201: out.println("}");
202:
203: out.println();
204: out
205: .println("public void __caucho_setXAResource(XAResource xaResource)");
206: out.println("{");
207: out.println(" _xaResource = xaResource;");
208: out.println("}");
209:
210: out.println();
211: out
212: .println("public void beforeDelivery(java.lang.reflect.Method method)");
213: out.println("{");
214: out.println(" if (_xaMethods.contains(method)) {");
215: out.println(" _isXa = (_xa.beginRequired() == null);");
216: out.println(" }");
217: out.println("}");
218:
219: out.println("public void afterDelivery()");
220: out.println("{");
221: out.println(" if (_isXa) {");
222: out.println(" _isXa = false;");
223: out.println(" _xa.commit();");
224: out.println(" }");
225: out.println("}");
226:
227: out.println();
228: out.println("public void release()");
229: out.println("{");
230: out.pushDepth();
231:
232: if (getEjbClass().hasMethod("ejbRemove", new Class[0])) {
233: out.println("ejbRemove();");
234: }
235:
236: out.popDepth();
237: out.println("}");
238:
239: /*
240: for (View view : getViews()) {
241: view.generateContextPrologue(out);
242: }
243: */
244:
245: generateViews(out);
246:
247: out.popDepth();
248: out.println("}");
249: }
250: }
|