KickJava   Java API By Example, From Geeks To Geeks.

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


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 EJBObjectGenerator extends AbstractGenerator {
40   protected Class JavaDoc _skelClass;
41   protected Class JavaDoc _remoteClass;
42   protected String JavaDoc prefix = "_ejb_lo_";
43   
44   /**
45    * Creates an instance of the generator
46    */

47   public EJBObjectGenerator(Class JavaDoc remoteClass, Class JavaDoc skelClass)
48   {
49     _remoteClass = remoteClass;
50     _skelClass = skelClass;
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         continue;
73
74       printMethod(method.getName(), method);
75     }
76
77     printFooter();
78   }
79
80   /**
81    * Prints the header for a ObjectStub
82    */

83   protected void printHeader()
84     throws IOException JavaDoc
85   {
86     if (getPackageName() != null)
87       println("package " + getPackageName() + ";");
88
89     println();
90     println("import java.io.*;");
91     println("import java.rmi.*;");
92     println("import " + _skelClass.getName() + ";");
93     println("import " + _remoteClass.getName() + ";");
94     
95     println();
96     println("public class " + getClassName());
97     println(" extends com.caucho.ejb.SessionObject");
98     println(" implements " + _remoteClass.getName());
99     
100     println("{");
101     pushDepth();
102
103     println("private " + _skelClass.getName() + " _obj;");
104
105     println();
106     println("public javax.ejb.SessionBean _getObject()");
107     println("{");
108     println(" return _obj;");
109     println("}");
110     
111     println();
112     println("public void _setObject(javax.ejb.SessionBean bean)");
113     println("{");
114     println(" _obj = (" + _skelClass.getName() + ") bean;");
115     println("}");
116   }
117
118   /**
119    * Prints a direct call for use in the same JVM.
120    *
121    * @param method the method to generate
122    */

123   protected void printMethod(String JavaDoc name, Method JavaDoc method)
124     throws IOException JavaDoc
125   {
126     Class JavaDoc ret = method.getReturnType();
127     Class JavaDoc []params = method.getParameterTypes();
128
129     printMethodHeader(name, method);
130     
131     println("{");
132     pushDepth();
133
134     if (! ret.getName().equals("void")) {
135       printClass(ret);
136       println(" _ret;");
137     }
138     
139     if (! ret.getName().equals("void"))
140       print("_ret = ");
141
142     print("_obj." + prefix + method.getName() + "(");
143     for (int i = 0; i < params.length; i++) {
144       if (i != 0)
145         print(", ");
146       print("a" + i);
147     }
148     println(");");
149
150     if (! ret.getName().equals("void"))
151       println("return _ret;");
152     
153     popDepth();
154     println("}");
155   }
156
157   /**
158    * Prints the class footer for the generated stub.
159    */

160   void printFooter()
161     throws IOException JavaDoc
162   {
163     popDepth();
164     println("}");
165   }
166 }
167
Popular Tags