KickJava   Java API By Example, From Geeks To Geeks.

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


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
31 import sun.rmi.rmic.IndentingWriter;
32
33 import static java.lang.reflect.Modifier JavaDoc.*;
34 import static com.sun.corba.ee.spi.codegen.Wrapper.*;
35 import com.sun.corba.ee.spi.codegen.Type;
36 import com.sun.corba.ee.spi.codegen.Expression;
37 import com.sun.corba.ee.impl.codegen.ClassGenerator;
38
39 import javax.ejb.EnterpriseBean JavaDoc;
40 import javax.ejb.SessionBean JavaDoc;
41 import javax.ejb.EntityBean JavaDoc;
42 import com.sun.ejb.EJBUtils;
43 import com.sun.ejb.containers.InternalEJBContainerException;
44 import com.sun.enterprise.deployment.*;
45 import com.sun.enterprise.util.JarClassLoader;
46 import com.sun.enterprise.util.LocalStringManagerImpl;
47
48 /**
49  *
50  */

51
52 public class Remote30WrapperGenerator extends Generator
53     implements ClassGeneratorFactory {
54
55     private static LocalStringManagerImpl localStrings =
56     new LocalStringManagerImpl(WrapperGenerator.class);
57     private static Logger _logger=null;
58     static{
59        _logger=LogDomains.getLogger(LogDomains.DPL_LOGGER);
60     }
61
62     private String JavaDoc remoteInterfaceName;
63     private Class JavaDoc businessInterface;
64     private String JavaDoc remoteClientClassName;
65     private String JavaDoc remoteClientPackageName;
66     private String JavaDoc remoteClientSimpleName;
67     private Method JavaDoc[] bizMethods;
68
69     private ClassLoader JavaDoc loader;
70
71     public String JavaDoc getGeneratedClass() {
72         return remoteClientClassName;
73     }
74     
75     // For corba codegen infrastructure
76
public String JavaDoc className() {
77         return getGeneratedClass();
78     }
79
80     /**
81      * Construct the Wrapper generator with the specified deployment
82      * descriptor and class loader.
83      * @exception GeneratorException.
84      */

85     public Remote30WrapperGenerator
86         (ClassLoader JavaDoc cl, String JavaDoc businessIntfName, String JavaDoc remoteIntfName)
87     throws GeneratorException
88     {
89     super();
90
91         remoteInterfaceName = remoteIntfName;
92
93         loader = cl;
94
95     try {
96         this.businessInterface = cl.loadClass(businessIntfName);
97     } catch (ClassNotFoundException JavaDoc ex) {
98         throw new InvalidBean(
99         localStrings.getLocalString(
100         "generator.remote_interface_not_found",
101         "Business interface " + businessInterface + " not found "));
102     }
103
104         if( javax.ejb.EJBObject JavaDoc.class.isAssignableFrom(businessInterface) ) {
105             throw new GeneratorException("Invalid Remote Business Interface " +
106                  businessInterface + ". A Remote Business interface MUST " +
107                  "not extend javax.ejb.EJBObject.");
108         }
109
110         remoteClientClassName = EJBUtils.
111             getGeneratedRemoteWrapperName(businessInterface.getName());
112
113         remoteClientPackageName = getPackageName(remoteClientClassName);
114         remoteClientSimpleName = getBaseName(remoteClientClassName);
115
116     bizMethods = removeDups(businessInterface.getMethods());
117
118         // NOTE : no need to remove ejb object methods because EJBObject
119
// is only visible through the RemoteHome view.
120

121     }
122
123     public ClassGenerator evaluate() {
124
125         _clear();
126
127         if (remoteClientPackageName != null) {
128         _package(remoteClientPackageName);
129         } else {
130             // no-arg _package() call is required for default package
131
_package();
132         }
133
134         _class(PUBLIC, remoteClientSimpleName,
135                _t("com.sun.ejb.containers.RemoteBusinessWrapperBase"),
136                _t(businessInterface.getName()));
137         
138         _data(PRIVATE, _t(remoteInterfaceName), "delegate_");
139
140         _constructor( PUBLIC ) ;
141         _arg(_t(remoteInterfaceName), "stub");
142         _arg(_String(), "busIntf");
143
144         _body();
145         _expr(_super(_s(_void(), _t("java.rmi.Remote"), _String()),
146                      _v("stub"), _v("busIntf"))) ;
147         _assign(_v("delegate_"), _v("stub"));
148         _end();
149
150         for(int i = 0; i < bizMethods.length; i++) {
151         printMethodImpl(bizMethods[i]);
152     }
153
154         _end();
155
156         /*
157         System.out.println("Generating byte code for remote30rwappergen");
158         try {
159             java.util.Properties p = new java.util.Properties();
160             p.put("Wrapper.DUMP_AFTER_SETUP_VISITOR", "true");
161             p.put("Wrapper.TRACE_BYTE_CODE_GENERATION", "true");
162             p.put("Wrapper.USE_ASM_VERIFIER", "true");
163             _byteCode(loader, null, p);
164         } catch(Exception e) {
165             System.out.println("Got exception when generating byte code");
166             e.printStackTrace();
167         }
168         */

169
170         return _classGenerator() ;
171
172     }
173
174             
175     /**
176      * Generate the code to the specified output stream.
177      * @param the output stream
178      * @exception GeneratorException on a generation error
179      * @exception IOException on an IO error
180      */

181     public void generate(OutputStream out)
182     throws GeneratorException, IOException
183     {
184     IndentingWriter p = new IndentingWriter(new OutputStreamWriter(out));
185
186     if (remoteClientPackageName != null) {
187         p.pln("package " + remoteClientPackageName + ";");
188         }
189
190     p.plnI("public final class " + remoteClientSimpleName +
191            " extends com.sun.ejb.containers.RemoteBusinessWrapperBase" +
192            " implements " + businessInterface.getName() + " {");
193
194     p.pln("");
195         p.pln("private " + remoteInterfaceName + " delegate_;");
196     p.pln("");
197
198     // this is the constructor
199
p.plnI("public " + remoteClientSimpleName + "(" +
200                remoteInterfaceName + " stub" + " , " +
201                "java.lang.String busIntf" + ")" + " {");
202         p.pln("super(stub, busIntf);");
203     p.pln("");
204         p.pln("delegate_ = stub;");
205     p.pOln("}");
206         p.pln("");
207
208     // each remote method
209
for(int i = 0; i < bizMethods.length; i++) {
210         printMethodImpl(p, bizMethods[i]);
211     }
212
213     p.pOln("}");
214     p.close();
215     }
216
217
218     private void printMethodImpl(Method JavaDoc m)
219     {
220
221         List<Type> exceptionList = new LinkedList<Type>();
222     for(Class JavaDoc exception : m.getExceptionTypes()) {
223             exceptionList.add(Type.type(exception));
224     }
225
226         _method( PUBLIC, Type.type(m.getReturnType()),
227                  m.getName(), exceptionList);
228
229         int i = 0;
230         List<Type> expressionListTypes = new LinkedList<Type>();
231         List<Expression> expressionList = new LinkedList<Expression>();
232         for(Class JavaDoc param : m.getParameterTypes()) {
233             String JavaDoc paramName = "param" + i;
234             _arg(Type.type(param), paramName);
235             i++;
236             expressionListTypes.add(Type.type(param));
237             expressionList.add(_v(paramName));
238     }
239
240         _body();
241
242
243         _try();
244
245         Class JavaDoc returnType = m.getReturnType();
246
247         if( returnType == void.class ) {
248             _expr( _call( _v("delegate_"), m.getName(),
249                           _s(Type.type(returnType), expressionListTypes),
250                           expressionList));
251         } else {
252             _return( _call( _v("delegate_"), m.getName(),
253                             _s(Type.type(returnType), expressionListTypes),
254                             expressionList) );
255         }
256
257         boolean doExceptionTranslation =
258             !java.rmi.Remote JavaDoc.class.isAssignableFrom(businessInterface);
259         if( doExceptionTranslation ) {
260
261             _catch( _t("javax.transaction.TransactionRolledbackException"),
262                     "trex");
263
264                 _define( _t("java.lang.RuntimeException"), "r",
265                          _new( _t("javax.ejb.EJBTransactionRolledbackException"),
266                            _s(_void())));
267                 _expr( _call( _v("r"), "initCause",
268                               _s(_t("java.lang.Throwable"),
269                                  _t("java.lang.Throwable")),
270                               _v("trex")));
271                 _throw(_v("r"));
272
273             _catch( _t("javax.transaction.TransactionRequiredException"),
274                     "treqex");
275
276                 _define( _t("java.lang.RuntimeException"), "r",
277                          _new( _t("javax.ejb.EJBTransactionRequiredException"),
278                            _s(_void())));
279                 _expr( _call( _v("r"), "initCause",
280                               _s(_t("java.lang.Throwable"),
281                                  _t("java.lang.Throwable")),
282                               _v("treqex")));
283                 _throw(_v("r"));
284
285             _catch( _t("java.rmi.NoSuchObjectException"),
286                     "nsoe");
287
288                 _define( _t("java.lang.RuntimeException"), "r",
289                          _new( _t("javax.ejb.NoSuchEJBException"),
290                            _s(_void())));
291                 _expr( _call( _v("r"), "initCause",
292                               _s(_t("java.lang.Throwable"),
293                                  _t("java.lang.Throwable")),
294                               _v("nsoe")));
295                 _throw(_v("r"));
296
297             _catch(_t("com.sun.ejb.containers.InternalEJBContainerException"),
298                    "iejbcEx");
299             // There's only one kind of InternalEJBContainerException, so
300
// we know it's a ParallelAccessException
301
_define(_t("com.sun.ejb.containers.ParallelAccessException"),
302                     "paEx",
303                     _cast(_t("com.sun.ejb.containers.ParallelAccessException"),
304                           _v("iejbcEx")));
305                     
306                 _define(_t("javax.ejb.ConcurrentAccessException"), "r", _new(
307                         _t("javax.ejb.ConcurrentAccessException"), _s(_void())));
308                 _expr(_call(_v("r"), "initCause", _s(_t("java.lang.Throwable"),
309                         _t("java.lang.Throwable")), _v("paEx")));
310                 _throw(_v("r"));
311
312             _catch( _t("java.rmi.RemoteException"), "re");
313             
314                 _throw( _new( _t("javax.ejb.EJBException"),
315                               _s(_void(), _t("java.lang.Exception")),
316                               _v("re")));
317             _end();
318
319         } else {
320             _catch(_t("com.sun.ejb.containers.InternalEJBContainerException"), "iejbcEx");
321                 _throw( _new( _t("com.sun.ejb.containers.InternalRemoteException"),
322                               _s(_void(), _t("com.sun.ejb.containers.InternalEJBContainerException")),
323                               _v("iejbcEx")));
324             _end();
325         }
326
327         _end();
328     }
329
330     /**
331      * Generate the code for a single method.
332      * @param the writer.
333      * @param the method to generate code for.
334      * @exception IOException.
335      */

336     private void printMethodImpl(IndentingWriter p, Method JavaDoc m)
337     throws IOException
338     {
339
340     // print method signature and exceptions
341
p.p("public " + printType(m.getReturnType()) + " "
342         + m.getName() + "(");
343     Class JavaDoc[] params = m.getParameterTypes();
344     for(int i = 0; i < params.length; i++) {
345         if (i != 0)
346         p.p(", ");
347         p.p(printType(params[i]) + " param" + i);
348     }
349     p.p(") ");
350     Class JavaDoc[] exceptions = m.getExceptionTypes();
351     for(int i = 0; i < exceptions.length; i++) {
352         if (i == 0)
353         p.p("throws ");
354         else
355         p.p(", ");
356         p.p(exceptions[i].getName());
357     }
358     p.plnI("{");
359
360         if( !java.rmi.Remote JavaDoc.class.isAssignableFrom(businessInterface) ) {
361             p.pln("try {");
362         }
363
364         if (m.getReturnType() != void.class) {
365             p.p(" return ");
366     }
367
368     // print code for calling biz method on bean class
369

370     p.p(" delegate_." + m.getName() + "(");
371     for(int i = 0; i < params.length; i++) {
372             if( i != 0 ) {
373                 p.p(" , ");
374             }
375             p.p("param" + i);
376     }
377         p.pln(");");
378
379         if( !java.rmi.Remote JavaDoc.class.isAssignableFrom(businessInterface) ) {
380             
381             p.pln("} catch(javax.transaction.TransactionRolledbackException trex) {");
382             p.pln("\tjava.lang.RuntimeException r = new javax.ejb.EJBTransactionRolledbackException();");
383             p.pln("\tr.initCause(trex);");
384             p.pln("\tthrow r;");
385
386             p.pln("} catch(java.rmi.NoSuchObjectException nsoe) {");
387             p.pln("\tjava.lang.RuntimeException r = new javax.ejb.NoSuchEJBException();");
388             p.pln("\tr.initCause(nsoe);");
389             p.pln("\tthrow r;");
390
391             p.pln("} catch(javax.transaction.TransactionRequiredException txre) {");
392             p.pln("\tjava.lang.RuntimeException r = new javax.ejb.EJBTransactionRequiredException();");
393             p.pln("\tr.initCause(txre);");
394             p.pln("\tthrow r;");
395
396             p.pln("} catch(java.rmi.RemoteException re) {");
397             p.pln("\tthrow new javax.ejb.EJBException(re);");
398             p.pln("} ");
399
400         }
401
402         p.pln("");
403     p.pOln("} ");
404         p.pln("");
405
406     }
407         
408 }
409
Popular Tags