KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > gen > JVMObjectStubGenerator


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.gen;
30
31 import com.caucho.make.ClassDependency;
32 import com.caucho.vfs.MergePath;
33 import com.caucho.vfs.PersistentDependency;
34
35 import java.io.IOException JavaDoc;
36 import java.lang.reflect.Method JavaDoc;
37 import java.util.ArrayList JavaDoc;
38
39 /**
40  * Generator for stubs.
41  */

42 public class JVMObjectStubGenerator extends JVMStubGenerator {
43   private ArrayList JavaDoc<PersistentDependency> _dependList;
44   
45   /**
46    * Creates an instance of the generator
47    */

48   public JVMObjectStubGenerator(Class JavaDoc remoteClass, boolean isProxy)
49   {
50     _remoteClass = remoteClass;
51
52     _isProxy = isProxy;
53
54     if (isProxy)
55       setFullClassName(remoteClass.getName() + "__JVMProxy");
56     else
57       setFullClassName(remoteClass.getName() + "__JVMStub");
58     
59     MergePath mergePath = new MergePath();
60     setSearchPath(mergePath);
61
62     _dependList = new ArrayList JavaDoc<PersistentDependency>();
63
64     _dependList.add(new ClassDependency(remoteClass));
65   }
66   
67   /**
68    * Creates the object stub for the object interface
69    */

70   public Class JavaDoc generateStub()
71     throws Exception JavaDoc
72   {
73     Class JavaDoc object = preload();
74     if (object != null)
75       return object;
76
77     generate();
78     
79     return compile();
80   }
81
82   /**
83    * Generates the Java source.
84    *
85    * @param methods the methods to generate
86    */

87   public void generateJava()
88     throws IOException JavaDoc
89   {
90     printHeader();
91     
92     Method JavaDoc []methods = _remoteClass.getMethods();
93     for (int i = 0; i < methods.length; i++) {
94       Method JavaDoc method = methods[i];
95       String JavaDoc methodName = method.getName();
96       Class JavaDoc declaringClass = method.getDeclaringClass();
97
98       if (declaringClass.getName().startsWith("javax.ejb."))
99         continue;
100
101       Class JavaDoc []exns = method.getExceptionTypes();
102       for (int j = 0; j < exns.length; j++) {
103         if (exns[j].isAssignableFrom(java.rmi.RemoteException JavaDoc.class)) {
104           printMethod(method.getName(), method);
105           break;
106         }
107       }
108     }
109
110     printRemove();
111
112     printDependList(_dependList);
113
114     printFooter();
115   }
116
117   /**
118    * Prints the header for a ObjectStub
119    */

120   protected void printHeader()
121     throws IOException JavaDoc
122   {
123     if (getPackageName() != null)
124       println("package " + getPackageName() + ";");
125
126     println();
127     println("import java.io.*;");
128     println("import java.rmi.*;");
129     println("import " + _remoteClass.getName() + ";");
130     
131     println();
132     println("public class " + getClassName());
133     println(" extends com.caucho.ejb.JVMObject");
134     println(" implements " + _remoteClass.getName());
135     
136     println("{");
137     pushDepth();
138   }
139   
140   protected void printRemove()
141     throws IOException JavaDoc
142   {
143     Class JavaDoc ret = void.class;
144     Class JavaDoc []params = new Class JavaDoc[0];
145
146     println();
147     println("public void remove() throws java.rmi.RemoteException, javax.ejb.RemoveException");
148     
149     printMethodHead(params, ret);
150     
151     printCall("remove", params, void.class);
152     
153     printMethodFooter(ret, false);
154   }
155 }
156
Popular Tags