KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > webservice > ServiceInterfaceGenerator


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
24 package com.sun.enterprise.webservice;
25
26
27 import java.lang.reflect.Method JavaDoc;
28 import java.lang.reflect.Modifier JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.io.OutputStreamWriter JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.LinkedList JavaDoc;
35
36 import java.util.logging.Logger JavaDoc;
37
38 import javax.jws.WebMethod;
39
40 import com.sun.ejb.codegen.Generator;
41 import com.sun.ejb.codegen.GeneratorException;
42
43 import sun.rmi.rmic.IndentingWriter;
44
45 import static java.lang.reflect.Modifier JavaDoc.*;
46 import static com.sun.corba.ee.spi.codegen.Wrapper.*;
47 import com.sun.corba.ee.spi.codegen.Type;
48 import com.sun.corba.ee.impl.codegen.ClassGenerator;
49 import com.sun.ejb.codegen.ClassGeneratorFactory;
50 import com.sun.enterprise.util.LocalStringManagerImpl;
51 import com.sun.logging.LogDomains;
52
53 /**
54  * This class is responsible for generating the SEI when it is not packaged
55  * by the application.
56  *
57  * @author Jerome Dochez
58  */

59 public class ServiceInterfaceGenerator extends Generator
60     implements ClassGeneratorFactory {
61
62     private static LocalStringManagerImpl localStrings =
63     new LocalStringManagerImpl(ServiceInterfaceGenerator.class);
64     private static Logger JavaDoc _logger=null;
65     static{
66        _logger=LogDomains.getLogger(LogDomains.DPL_LOGGER);
67     }
68  
69     Class JavaDoc sib=null;
70     String JavaDoc serviceIntfName;
71     String JavaDoc packageName;
72     String JavaDoc serviceIntfSimpleName;
73     Method JavaDoc[] intfMethods;
74     
75    /**
76      * Construct the Wrapper generator with the specified deployment
77      * descriptor and class loader.
78      * @exception GeneratorException.
79      */

80     public ServiceInterfaceGenerator(ClassLoader JavaDoc cl, Class JavaDoc sib)
81     throws GeneratorException, ClassNotFoundException JavaDoc
82     {
83     super();
84
85         this.sib = sib;
86         serviceIntfSimpleName = getServiceIntfName();
87
88     packageName = getPackageName();
89         serviceIntfName = packageName + "." + serviceIntfSimpleName;
90     
91         intfMethods = calculateMethods(sib, removeDups(sib.getMethods()));
92         
93         // NOTE : no need to remove ejb object methods because EJBObject
94
// is only visible through the RemoteHome view.
95
}
96     
97     public String JavaDoc getServiceIntfName() {
98         String JavaDoc serviceIntfSimpleName = sib.getSimpleName();
99         if (serviceIntfSimpleName.endsWith("EJB")) {
100             return serviceIntfSimpleName.substring(0, serviceIntfSimpleName.length()-3);
101         } else {
102             return serviceIntfSimpleName+"SEI";
103         }
104     }
105     
106     public String JavaDoc getPackageName() {
107         return sib.getPackage().getName()+".internal.jaxws";
108     }
109     
110     /**
111      * Get the fully qualified name of the generated class.
112      * Note: the remote/local implementation class is in the same package
113      * as the bean class, NOT the remote/local interface.
114      * @return the name of the generated class.
115      */

116     public String JavaDoc getGeneratedClass() {
117         return serviceIntfName;
118     }
119
120     // For corba codegen infrastructure
121
public String JavaDoc className() {
122         return getGeneratedClass();
123     }
124     
125     private Method JavaDoc[] calculateMethods(Class JavaDoc sib, Method JavaDoc[] initialList) {
126
127         // we start by assuming the @WebMethod was NOT used on this class
128
boolean webMethodAnnotationUsed = false;
129         List JavaDoc<Method JavaDoc> list = new ArrayList JavaDoc<Method JavaDoc>();
130         
131         for (Method JavaDoc m : initialList) {
132             WebMethod wm = m.getAnnotation(javax.jws.WebMethod.class);
133             if (wm!=null && webMethodAnnotationUsed==false) {
134                 webMethodAnnotationUsed=true;
135                 // reset the list, this is the first annotated method we find
136
list.clear();
137             }
138             if (wm!=null) {
139                 list.add(m);
140             } else {
141                 if (!webMethodAnnotationUsed && !m.getDeclaringClass().equals(java.lang.Object JavaDoc.class)) {
142                     list.add(m);
143                 }
144             }
145         }
146         return list.toArray(new Method JavaDoc[0]);
147     }
148
149     public ClassGenerator evaluate() {
150
151         _clear();
152
153     if (packageName != null) {
154         _package(packageName);
155         }
156
157         _interface(PUBLIC, serviceIntfName);
158
159         for(int i = 0; i < intfMethods.length; i++) {
160         printMethod(intfMethods[i]);
161     }
162
163         _end();
164
165         return _classGenerator() ;
166
167     }
168
169
170
171     /**
172      * Generate the code to the specified output stream.
173      * @param the output stream
174      * @exception GeneratorException on a generation error
175      * @exception IOException on an IO error
176      */

177     public void generate(OutputStream JavaDoc out)
178     throws GeneratorException, IOException JavaDoc
179     {
180     IndentingWriter p = new IndentingWriter(new OutputStreamWriter JavaDoc(out));
181
182         p.pln("");
183
184     if (packageName != null) {
185         p.pln("package " + packageName + ";");
186         }
187
188         p.pln("");
189
190     p.plnI("public interface " + serviceIntfSimpleName + " {");
191
192         p.pln("");
193
194     // each remote method
195
for(int i = 0; i < intfMethods.length; i++) {
196         printMethod(p, intfMethods[i]);
197     }
198
199     p.pOln("}");
200     p.close();
201     }
202
203     private void printMethod(Method JavaDoc m)
204     {
205
206         boolean throwsRemoteException = false;
207         List JavaDoc<Type> exceptionList = new LinkedList JavaDoc<Type>();
208     for(Class JavaDoc exception : m.getExceptionTypes()) {
209             exceptionList.add(Type.type(exception));
210             if( exception.getName().equals("java.rmi.RemoteException") ) {
211                 throwsRemoteException = true;
212             }
213     }
214         if( !throwsRemoteException ) {
215             exceptionList.add(_t("java.rmi.RemoteException"));
216         }
217
218         _method( PUBLIC | ABSTRACT, Type.type(m.getReturnType()),
219                  m.getName(), exceptionList);
220
221         int i = 0;
222         for(Class JavaDoc param : m.getParameterTypes()) {
223             _arg(Type.type(param), "param" + i);
224             i++;
225     }
226
227         _end();
228     }
229
230
231     /**
232      * Generate the code for a single method.
233      * @param the writer.
234      * @param the method to generate code for.
235      * @exception IOException.
236      */

237     private void printMethod(IndentingWriter p, Method JavaDoc m)
238     throws IOException JavaDoc
239     {
240     p.pln("");
241
242     // print method signature and exceptions
243
p.p("public " + printType(m.getReturnType()) + " "
244         + m.getName() + "(");
245     Class JavaDoc[] params = m.getParameterTypes();
246     for(int i = 0; i < params.length; i++) {
247         if (i != 0)
248         p.p(", ");
249         p.p(printType(params[i]) + " param" + i);
250     }
251     p.p(") ");
252     Class JavaDoc[] exceptions = m.getExceptionTypes();
253         boolean throwsRemoteException = false;
254     for(int i = 0; i < exceptions.length; i++) {
255         if (i == 0)
256         p.p("throws ");
257         else
258         p.p(", ");
259             String JavaDoc nextEx = exceptions[i].getName();
260         p.p(nextEx);
261             if( nextEx.equals("java.rmi.RemoteException") ) {
262                 throwsRemoteException = true;
263             }
264     }
265         if( exceptions.length == 0 ) {
266             p.p("throws java.rmi.RemoteException");
267         } else if (!throwsRemoteException) {
268             p.p(", java.rmi.RemoteException");
269         }
270     p.pln(";");
271         p.pln("");
272     }
273     
274 }
275
Popular Tags