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.gen21;
030:
031: import com.caucho.ejb.gen.*;
032: import com.caucho.make.ClassDependency;
033: import com.caucho.vfs.MergePath;
034: import com.caucho.vfs.PersistentDependency;
035:
036: import java.io.IOException;
037: import java.lang.reflect.Method;
038: import java.util.ArrayList;
039:
040: /**
041: * Generator for stubs.
042: */
043: public class JVMObjectStubGenerator extends JVMStubGenerator {
044: private ArrayList<PersistentDependency> _dependList;
045:
046: /**
047: * Creates an instance of the generator
048: */
049: public JVMObjectStubGenerator(Class remoteClass, boolean isProxy) {
050: _remoteClass = remoteClass;
051:
052: _isProxy = isProxy;
053:
054: if (isProxy)
055: setFullClassName(remoteClass.getName() + "__JVMProxy");
056: else
057: setFullClassName(remoteClass.getName() + "__JVMStub");
058:
059: MergePath mergePath = new MergePath();
060: setSearchPath(mergePath);
061:
062: _dependList = new ArrayList<PersistentDependency>();
063:
064: _dependList.add(new ClassDependency(remoteClass));
065: }
066:
067: /**
068: * Creates the object stub for the object interface
069: */
070: public Class generateStub() throws Exception {
071: Class object = preload();
072: if (object != null)
073: return object;
074:
075: generate();
076:
077: return compile();
078: }
079:
080: /**
081: * Generates the Java source.
082: *
083: * @param methods the methods to generate
084: */
085: public void generateJava() throws IOException {
086: printHeader();
087:
088: Method[] methods = _remoteClass.getMethods();
089: for (int i = 0; i < methods.length; i++) {
090: Method method = methods[i];
091: String methodName = method.getName();
092: Class declaringClass = method.getDeclaringClass();
093:
094: if (declaringClass.getName().startsWith("javax.ejb."))
095: continue;
096:
097: Class[] exns = method.getExceptionTypes();
098: for (int j = 0; j < exns.length; j++) {
099: if (exns[j]
100: .isAssignableFrom(java.rmi.RemoteException.class)) {
101: printMethod(method.getName(), method);
102: break;
103: }
104: }
105: }
106:
107: printRemove();
108:
109: printDependList(_dependList);
110:
111: printFooter();
112: }
113:
114: /**
115: * Prints the header for a ObjectStub
116: */
117: protected void printHeader() throws IOException {
118: if (getPackageName() != null)
119: println("package " + getPackageName() + ";");
120:
121: println();
122: println("import java.io.*;");
123: println("import java.rmi.*;");
124: println("import " + _remoteClass.getName() + ";");
125:
126: println();
127: println("public class " + getClassName());
128: println(" extends com.caucho.ejb.JVMObject");
129: println(" implements " + _remoteClass.getName());
130:
131: println("{");
132: pushDepth();
133: }
134:
135: protected void printRemove() throws IOException {
136: Class ret = void.class;
137: Class[] params = new Class[0];
138:
139: println();
140: println("public void remove() throws java.rmi.RemoteException, javax.ejb.RemoveException");
141:
142: printMethodHead(params, ret);
143:
144: printCall("remove", params, void.class);
145:
146: printMethodFooter(ret, false);
147: }
148: }
|