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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.ejb.hessian;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.java.WorkDir;
033: import com.caucho.make.ClassDependency;
034: import com.caucho.vfs.MergePath;
035: import com.caucho.vfs.PersistentDependency;
036:
037: import java.io.IOException;
038: import java.lang.reflect.Method;
039: import java.util.ArrayList;
040:
041: /**
042: * Generator for stubs.
043: */
044: class StubGenerator extends MarshalGenerator {
045: private ArrayList<PersistentDependency> _dependList;
046:
047: StubGenerator() {
048: setClassDir(WorkDir.getLocalWorkDir().lookup("ejb"));
049: }
050:
051: Class createHomeStub(Class cl) throws ConfigException {
052: return makeClient(cl, "__HessianStub", true);
053: }
054:
055: Class createObjectStub(Class cl) throws ConfigException {
056: return makeClient(cl, "__HessianStub", false);
057: }
058:
059: Class createStub(Class cl) throws ConfigException {
060: return makeClient(cl, "__HessianStub", true);
061: }
062:
063: /**
064: * Creates a client stub.
065: *
066: * @param cl the remote interface of the stub
067: * @param genSuffix the suffix for the generated class
068: */
069: Class makeClient(Class cl, String genSuffix, boolean isHome)
070: throws ConfigException {
071: _cl = cl;
072:
073: try {
074: setFullClassName("_ejb." + cl.getName() + genSuffix);
075:
076: if (cl.getClassLoader() != null)
077: setParentLoader(cl.getClassLoader());
078:
079: MergePath mergePath = new MergePath();
080: if (cl.getClassLoader() != null)
081: mergePath.addClassPath(cl.getClassLoader());
082: else
083: mergePath.addClassPath(Thread.currentThread()
084: .getContextClassLoader());
085: setSearchPath(mergePath);
086:
087: _dependList = new ArrayList<PersistentDependency>();
088:
089: _dependList.add(new ClassDependency(cl));
090:
091: Class stubClass = preload();
092: if (stubClass != null)
093: return stubClass;
094:
095: generate();
096:
097: return compile();
098: } catch (Exception e) {
099: throw ConfigException.create(e);
100: }
101: }
102:
103: public void generateJava() throws IOException {
104: generateJava(_cl.getMethods());
105: }
106:
107: /**
108: * Generates the Java source.
109: *
110: * @param methods the methods to generate
111: */
112: private void generateJava(Method[] methods) throws IOException {
113: if (javax.ejb.EJBHome.class.isAssignableFrom(_cl))
114: printHeader("HomeStub");
115: else if (javax.ejb.EJBObject.class.isAssignableFrom(_cl))
116: printHeader("ObjectStub");
117: else
118: printHeader("ObjectStub");
119:
120: println("public String getHessianType()");
121: println("{");
122: println(" return \"" + _cl.getName() + "\";");
123: println("}");
124:
125: for (int i = 0; i < methods.length; i++) {
126: Method method = methods[i];
127: Class declaringClass = method.getDeclaringClass();
128: String prefix = "";
129:
130: if (declaringClass.getName().startsWith("javax.ejb."))
131: prefix = "_ejb_";
132:
133: /*
134: Class []exns = method.getExceptionTypes();
135: for (int j = 0; j < exns.length; j++) {
136: if (exns[j].isAssignableFrom(java.rmi.RemoteException.class)) {
137: printMethod(prefix + method.getName(), method);
138: break;
139: }
140: }
141: */
142: printMethod(prefix + method.getName(), method);
143: }
144:
145: printDependList(_dependList);
146:
147: printFooter();
148: }
149:
150: /**
151: * Prints the header for a HomeStub
152: */
153: void printHeader(String stubClassName) throws IOException {
154: if (getPackageName() != null)
155: println("package " + getPackageName() + ";");
156:
157: println();
158: println("import java.io.*;");
159: println("import java.rmi.*;");
160: println("import com.caucho.vfs.*;");
161: println("import com.caucho.util.*;");
162: println("import com.caucho.ejb.hessian.*;");
163: println("import com.caucho.hessian.io.*;");
164: println("import " + _cl.getName() + ";");
165:
166: println();
167: println("public class " + getClassName() + " extends "
168: + stubClassName);
169: print(" implements " + _cl.getName());
170:
171: println(" {");
172: pushDepth();
173: }
174:
175: /**
176: * Generates the code for a remote stub method.
177: *
178: * @param name the name of the remote
179: * @param method the reflected object for the method
180: */
181: void printMethod(String name, Method method) throws IOException {
182: Class ret = method.getReturnType();
183: Class[] params = method.getParameterTypes();
184:
185: printMethodDeclaration(name, method);
186:
187: println("{");
188: pushDepth();
189:
190: println("HessianWriter out = _hessian_openWriter();");
191: println("try {");
192: pushDepth();
193:
194: String mangleName = mangleMethodName(method.getName(), method,
195: false);
196:
197: println("out.startCall();");
198: println("_hessian_writeHeaders(out);");
199: println("out.writeMethod(\"" + mangleName + "\");");
200:
201: for (int i = 0; i < params.length; i++)
202: printMarshalType(params[i], "_arg" + i);
203:
204: println("HessianInput in = out.doCall();");
205:
206: if (!void.class.equals(ret)) {
207: printClass(ret);
208: println(" _ret;");
209: print("_ret = ");
210: printUnmarshalType(ret);
211: } else {
212: println("in.readNull();");
213: }
214:
215: println("in.completeReply();");
216:
217: println("_hessian_freeWriter(out);");
218: println("out = null;");
219:
220: if (!void.class.equals(ret))
221: println("return _ret;");
222:
223: popDepth();
224:
225: Class[] exn = method.getExceptionTypes();
226:
227: boolean hasThrowable = false;
228: boolean hasRuntimeException = false;
229:
230: loop: for (int i = 0; i < exn.length; i++) {
231: for (int j = 0; j < i; j++) {
232: if (exn[j].isAssignableFrom(exn[i]))
233: continue loop;
234: }
235:
236: if (!hasThrowable) {
237: println("} catch (" + exn[i].getName() + " e) {");
238: println(" throw e;");
239: }
240:
241: if (exn[i].equals(Throwable.class)) {
242: hasThrowable = true;
243: hasRuntimeException = true;
244: }
245:
246: if (exn[i].equals(Exception.class))
247: hasRuntimeException = true;
248: if (exn[i].equals(RuntimeException.class))
249: hasRuntimeException = true;
250: }
251:
252: if (!hasRuntimeException) {
253: println("} catch (RuntimeException e) {");
254: println(" throw e;");
255: }
256:
257: if (!hasThrowable) {
258: println("} catch (Throwable e) {");
259: println(" throw new com.caucho.ejb.EJBExceptionWrapper(\"stub exception\", e);");
260: }
261:
262: println("} finally {");
263: println(" if (out != null) out.close();");
264: println("}");
265:
266: popDepth();
267: println("}");
268: }
269:
270: /**
271: * Prints the class footer for the generated stub.
272: */
273: void printFooter() throws IOException {
274: println();
275: println("public String toString()");
276: println("{");
277: pushDepth();
278: println("return \"[HessianStub " + _cl.getName()
279: + " \" + _urlPath + \"]\";");
280: popDepth();
281: println("}");
282:
283: popDepth();
284: println("}");
285: }
286:
287: /**
288: * Generates code for version changes.
289: */
290: protected void printVersionChange() throws IOException {
291: println("if (com.caucho.ejb.Version.getVersionId() != "
292: + com.caucho.ejb.Version.getVersionId() + ")");
293: println(" return true;");
294: }
295: }
|