KickJava   Java API By Example, From Geeks To Geeks.

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


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.java.AbstractGenerator;
32
33 import java.io.IOException JavaDoc;
34 import java.lang.reflect.Method JavaDoc;
35
36 /**
37  * Generator for stubs.
38  */

39 class EJBHomeGenerator extends AbstractGenerator {
40   protected Class JavaDoc skelClass;
41   protected Class JavaDoc remoteClass;
42   protected String JavaDoc prefix = "_ejb_lh_";
43   
44   /**
45    * Creates an instance of the generator
46    */

47   EJBHomeGenerator(Class JavaDoc remoteClass, Class JavaDoc skelClass)
48   {
49     this.skelClass = skelClass;
50     this.remoteClass = remoteClass;
51
52     setFullClassName("_ejb." + remoteClass.getName() + "__Stub");
53   }
54
55   /**
56    * Generates the Java source.
57    *
58    * @param methods the methods to generate
59    */

60   public void generateJava()
61     throws IOException JavaDoc
62   {
63     printHeader();
64     
65     Method JavaDoc []methods = remoteClass.getMethods();
66     for (int i = 0; i < methods.length; i++) {
67       Method JavaDoc method = methods[i];
68       String JavaDoc methodName = method.getName();
69       Class JavaDoc declaringClass = method.getDeclaringClass();
70
71       if (declaringClass.getName().startsWith("javax.ejb.") &&
72           ! methodName.equals("getPrimaryKey") &&
73           ! methodName.equals("getEJBMetaData"))
74         continue;
75
76       if (methodName.startsWith("create")) {
77         printCreateMethod(method.getName(), method);
78         continue;
79       }
80
81       printMethod(method.getName(), method);
82     }
83
84     printFooter();
85   }
86
87   /**
88    * Prints the header for a HomeStub
89    */

90   protected void printHeader()
91     throws IOException JavaDoc
92   {
93     if (getPackageName() != null)
94       println("package " + getPackageName() + ";");
95
96     println();
97     println("import java.io.*;");
98     println("import java.rmi.*;");
99     println("import " + skelClass.getName() + ";");
100     println("import " + remoteClass.getName() + ";");
101     
102     println();
103     println("public class " + getClassName());
104     println(" extends com.caucho.ejb.SessionHome");
105     println(" implements " + remoteClass.getName());
106     
107     println("{");
108     pushDepth();
109     
110     println("private " + skelClass.getName() + " obj;");
111
112     println();
113     println("protected void _init(com.caucho.ejb.SessionServer server, javax.ejb.SessionBean obj)");
114     println("{");
115     println(" this.server = server;");
116     println(" this.obj = (" + skelClass.getName() + ") obj;");
117     println("}");
118   }
119
120   /**
121    * Prints a direct call for use in the same JVM.
122    *
123    * @param method the method to generate
124    */

125   protected void printMethod(String JavaDoc name, Method JavaDoc method)
126     throws IOException JavaDoc
127   {
128     Class JavaDoc ret = method.getReturnType();
129     Class JavaDoc []params = method.getParameterTypes();
130
131     printMethodHeader(name, method);
132     
133     println("{");
134     pushDepth();
135
136     if (! ret.getName().equals("void")) {
137       printClass(ret);
138       println(" _ret;");
139     }
140     
141     if (! ret.getName().equals("void"))
142       print("_ret = ");
143
144     print("obj." + prefix + method.getName() + "(");
145     for (int i = 0; i < params.length; i++) {
146       if (i != 0)
147         print(", ");
148       print("a" + i);
149     }
150     println(");");
151
152     if (! ret.getName().equals("void"))
153       println("return _ret;");
154     
155     popDepth();
156     println("}");
157   }
158
159   /**
160    * Prints a direct call for use in the same JVM.
161    *
162    * @param method the method to generate
163    */

164   protected void printCreateMethod(String JavaDoc name, Method JavaDoc method)
165     throws IOException JavaDoc
166   {
167     Class JavaDoc ret = method.getReturnType();
168     Class JavaDoc []params = method.getParameterTypes();
169
170     printMethodHeader(name, method);
171     
172     println("{");
173     pushDepth();
174
175     println("com.caucho.ejb.AbstractContext cxt;");
176     print("cxt = ");
177
178     print("obj." + prefix + method.getName() + "(");
179     for (int i = 0; i < params.length; i++) {
180       if (i != 0)
181         print(", ");
182       print("a" + i);
183     }
184     println(");");
185
186     println("return (" + ret.getName() + ") cxt.getEJBLocalObject();");
187     
188     popDepth();
189     println("}");
190   }
191
192   /**
193    * Prints the class footer for the generated stub.
194    */

195   void printFooter()
196     throws IOException JavaDoc
197   {
198     popDepth();
199     println("}");
200   }
201 }
202
Popular Tags