KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > codegen > RemoteGenerator


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.ejb.codegen;
24
25 import java.lang.reflect.Method JavaDoc;
26 import java.io.*;
27 import java.util.*;
28 import java.util.logging.*;
29 import com.sun.logging.*;
30 import com.sun.ejb.EJBUtils;
31
32 import static java.lang.reflect.Modifier JavaDoc.*;
33 import static com.sun.corba.ee.spi.codegen.Wrapper.*;
34 import com.sun.corba.ee.spi.codegen.Type;
35 import com.sun.corba.ee.impl.codegen.ClassGenerator;
36
37
38 import sun.rmi.rmic.IndentingWriter;
39
40 import javax.ejb.EnterpriseBean JavaDoc;
41 import javax.ejb.SessionBean JavaDoc;
42 import javax.ejb.EntityBean JavaDoc;
43 import com.sun.enterprise.deployment.*;
44 import com.sun.enterprise.util.JarClassLoader;
45 import com.sun.enterprise.util.LocalStringManagerImpl;
46
47 /**
48  * This class is used to generate the RMI-IIOP version of a
49  * remote business interface.
50  */

51
52 public class RemoteGenerator extends Generator
53     implements ClassGeneratorFactory {
54
55     private static LocalStringManagerImpl localStrings =
56     new LocalStringManagerImpl(RemoteGenerator.class);
57     private static Logger _logger=null;
58     static{
59        _logger=LogDomains.getLogger(LogDomains.DPL_LOGGER);
60     }
61
62     private Class JavaDoc businessInterface;
63     private Method JavaDoc[] bizMethods;
64     private String JavaDoc remoteInterfacePackageName;
65     private String JavaDoc remoteInterfaceSimpleName;
66     private String JavaDoc remoteInterfaceName;
67
68     /**
69      * Get the fully qualified name of the generated class.
70      * Note: the remote/local implementation class is in the same package
71      * as the bean class, NOT the remote/local interface.
72      * @return the name of the generated class.
73      */

74     public String JavaDoc getGeneratedClass() {
75         return remoteInterfaceName;
76     }
77
78     // For corba codegen infrastructure
79
public String JavaDoc className() {
80         return getGeneratedClass();
81     }
82     
83     /**
84      * Construct the Wrapper generator with the specified deployment
85      * descriptor and class loader.
86      * @exception GeneratorException.
87      */

88     public RemoteGenerator(ClassLoader JavaDoc cl, String JavaDoc businessIntf)
89     throws GeneratorException
90     {
91     super();
92
93     try {
94         businessInterface = cl.loadClass(businessIntf);
95     } catch (ClassNotFoundException JavaDoc ex) {
96         throw new InvalidBean(
97         localStrings.getLocalString(
98         "generator.remote_interface_not_found",
99         "Remote interface not found "));
100     }
101
102         remoteInterfaceName = EJBUtils.getGeneratedRemoteIntfName
103             (businessInterface.getName());
104
105     remoteInterfacePackageName = getPackageName(remoteInterfaceName);
106         remoteInterfaceSimpleName = getBaseName(remoteInterfaceName);
107     
108     bizMethods = removeDups(businessInterface.getMethods());
109         
110         // NOTE : no need to remove ejb object methods because EJBObject
111
// is only visible through the RemoteHome view.
112
}
113
114     public ClassGenerator evaluate() {
115
116         _clear();
117
118     if (remoteInterfacePackageName != null) {
119         _package(remoteInterfacePackageName);
120         } else {
121             // no-arg _package() call is required for default package
122
_package();
123         }
124
125         _interface(PUBLIC, remoteInterfaceSimpleName,
126                    _t("java.rmi.Remote"),
127                    _t("com.sun.ejb.containers.RemoteBusinessObject"));
128
129         for(int i = 0; i < bizMethods.length; i++) {
130         printMethod(bizMethods[i]);
131     }
132
133         _end();
134
135         return _classGenerator() ;
136
137     }
138
139
140
141     /**
142      * Generate the code to the specified output stream.
143      * @param the output stream
144      * @exception GeneratorException on a generation error
145      * @exception IOException on an IO error
146      */

147     public void generate(OutputStream out)
148     throws GeneratorException, IOException
149     {
150     IndentingWriter p = new IndentingWriter(new OutputStreamWriter(out));
151
152         p.pln("");
153
154     if (remoteInterfacePackageName != null) {
155         p.pln("package " + remoteInterfacePackageName + ";");
156         }
157
158         p.pln("");
159
160     p.plnI("public interface " + remoteInterfaceSimpleName + " extends " +
161             "java.rmi.Remote , com.sun.ejb.containers.RemoteBusinessObject {");
162
163         p.pln("");
164
165     // each remote method
166
for(int i = 0; i < bizMethods.length; i++) {
167         printMethod(p, bizMethods[i]);
168     }
169
170     p.pOln("}");
171     p.close();
172     }
173
174     private void printMethod(Method JavaDoc m)
175     {
176
177         boolean throwsRemoteException = false;
178         List<Type> exceptionList = new LinkedList<Type>();
179     for(Class JavaDoc exception : m.getExceptionTypes()) {
180             exceptionList.add(Type.type(exception));
181             if( exception.getName().equals("java.rmi.RemoteException") ) {
182                 throwsRemoteException = true;
183             }
184     }
185         if( !throwsRemoteException ) {
186             exceptionList.add(_t("java.rmi.RemoteException"));
187         }
188
189         exceptionList.add(_t("com.sun.ejb.containers.InternalEJBContainerException"));
190         _method( PUBLIC | ABSTRACT, Type.type(m.getReturnType()),
191                  m.getName(), exceptionList);
192
193         int i = 0;
194         for(Class JavaDoc param : m.getParameterTypes()) {
195             _arg(Type.type(param), "param" + i);
196             i++;
197     }
198
199         _end();
200     }
201
202
203     /**
204      * Generate the code for a single method.
205      * @param the writer.
206      * @param the method to generate code for.
207      * @exception IOException.
208      */

209     private void printMethod(IndentingWriter p, Method JavaDoc m)
210     throws IOException
211     {
212     p.pln("");
213
214     // print method signature and exceptions
215
p.p("public " + printType(m.getReturnType()) + " "
216         + m.getName() + "(");
217     Class JavaDoc[] params = m.getParameterTypes();
218     for(int i = 0; i < params.length; i++) {
219         if (i != 0)
220         p.p(", ");
221         p.p(printType(params[i]) + " param" + i);
222     }
223     p.p(") ");
224     Class JavaDoc[] exceptions = m.getExceptionTypes();
225         boolean throwsRemoteException = false;
226     for(int i = 0; i < exceptions.length; i++) {
227         if (i == 0)
228         p.p("throws ");
229         else
230         p.p(", ");
231             String JavaDoc nextEx = exceptions[i].getName();
232         p.p(nextEx);
233             if( nextEx.equals("java.rmi.RemoteException") ) {
234                 throwsRemoteException = true;
235             }
236     }
237         if( exceptions.length == 0 ) {
238             p.p("throws java.rmi.RemoteException");
239         } else if (!throwsRemoteException) {
240             p.p(", java.rmi.RemoteException");
241         }
242     p.pln(";");
243         p.pln("");
244     }
245
246 }
247
Popular Tags