KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > tools > processors > java2 > internal > ClassProcessor


1 package org.objectweb.celtix.tools.processors.java2.internal;
2
3 import java.lang.reflect.Method JavaDoc;
4 import java.lang.reflect.Modifier JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.logging.Logger JavaDoc;
8
9 import javax.jws.Oneway;
10 import javax.jws.WebMethod;
11 import javax.jws.WebService;
12 import javax.jws.soap.SOAPBinding;
13 import javax.wsdl.OperationType;
14
15 import org.objectweb.celtix.common.i18n.Message;
16 import org.objectweb.celtix.common.logging.LogUtils;
17 import org.objectweb.celtix.tools.common.ProcessorEnvironment;
18 import org.objectweb.celtix.tools.common.ToolConstants;
19 import org.objectweb.celtix.tools.common.ToolException;
20 import org.objectweb.celtix.tools.common.WSDLConstants;
21 import org.objectweb.celtix.tools.common.model.JavaMethod;
22 import org.objectweb.celtix.tools.common.model.WSDLModel;
23 import org.objectweb.celtix.tools.processors.java2.JavaToWSDLProcessor;
24 import org.objectweb.celtix.tools.utils.AnnotationUtil;
25 import org.objectweb.celtix.tools.utils.URIParserUtil;
26
27 public class ClassProcessor {
28     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(JavaToWSDLProcessor.class);
29
30     Class JavaDoc seiClass;
31
32     WSDLModel model;
33
34     Map JavaDoc<Class JavaDoc, Boolean JavaDoc> useWebMethodClasses = new HashMap JavaDoc<Class JavaDoc, Boolean JavaDoc>();
35
36     private final ProcessorEnvironment env;
37
38     public ClassProcessor(Class JavaDoc clz, ProcessorEnvironment penv) {
39         seiClass = clz;
40         env = penv;
41     }
42
43     public void process(WSDLModel wmodel) {
44         model = wmodel;
45         populateWSDLInfo(seiClass);
46         checkWebMethodUseClass(seiClass);
47         for (Method JavaDoc method : seiClass.getMethods()) {
48             if (method.getDeclaringClass().equals(Object JavaDoc.class) || !isOperationToGen(method, seiClass)) {
49                 continue;
50             }
51             processMethod(wmodel, method);
52         }
53     }
54
55     private void processMethod(WSDLModel wmodel, Method JavaDoc method) {
56         if (!Modifier.isPublic(method.getModifiers())) {
57             return;
58         }
59
60         WebMethod webMethod = AnnotationUtil.getPrivMethodAnnotation(method, WebMethod.class);
61         if (webMethod == null || (webMethod != null && webMethod.exclude())) {
62             return;
63         }
64
65         JavaMethod javaMethod = new JavaMethod();
66
67         // rule 3.5
68

69         String JavaDoc operationName = method.getName();
70
71         if (!method.getDeclaringClass().equals(seiClass)) {
72             try {
73                 Method JavaDoc tmp = seiClass.getMethod(method.getName(), (Class JavaDoc[])method.getParameterTypes());
74                 operationName = tmp.getName();
75             } catch (NoSuchMethodException JavaDoc e) {
76                 throw new ToolException(e.getMessage(), e);
77             }
78         }
79
80         if (webMethod != null) {
81             operationName = webMethod.operationName().length() > 0
82                 ? webMethod.operationName() : operationName;
83         }
84
85         javaMethod.setName(operationName);
86         javaMethod.setSoapAction(webMethod.action());
87
88         
89         if (isAsynMethod(method)) {
90             return;
91         }
92         
93         if (isOneWayMethod(method)) {
94             javaMethod.setStyle(OperationType.ONE_WAY);
95         } else {
96             javaMethod.setStyle(OperationType.REQUEST_RESPONSE);
97         }
98
99         switch (getMethodType(method)) {
100         case WSDLConstants.DOC_BARE:
101             DocBareMethodProcessor docBareProcessor = new DocBareMethodProcessor(model);
102             docBareProcessor.processDocBare(javaMethod, method);
103             break;
104         case WSDLConstants.DOC_WRAPPED:
105             DocWrapperMethodProcessor docWrapperProcessor = new DocWrapperMethodProcessor(model);
106             docWrapperProcessor.process(javaMethod, method);
107             break;
108         case WSDLConstants.RPC_WRAPPED:
109             RPCMethodProcessor rpcMethodProcessor = new RPCMethodProcessor(model);
110             rpcMethodProcessor.process(javaMethod, method);
111             break;
112         default:
113             Message message = new Message("SOAPUSESTYLE_PARAMETERSTYLE_ERROR", LOG, method.getName());
114             throw new ToolException(message);
115         }
116         wmodel.addJavaMethod(javaMethod);
117     }
118
119     private int getMethodType(Method JavaDoc method) {
120         SOAPBinding binding = method.getAnnotation(SOAPBinding.class);
121         int result = WSDLConstants.ERORR_STYLE_USE;
122         if (binding != null) {
123             if (binding.style() == SOAPBinding.Style.RPC) {
124                 result = WSDLConstants.RPC_WRAPPED;
125             }
126             if (binding.style() == SOAPBinding.Style.DOCUMENT
127                 && binding.parameterStyle() == SOAPBinding.ParameterStyle.WRAPPED) {
128                 result = WSDLConstants.DOC_WRAPPED;
129             }
130             if (binding.style() == SOAPBinding.Style.DOCUMENT
131                 && binding.parameterStyle() == SOAPBinding.ParameterStyle.BARE) {
132                 result = WSDLConstants.DOC_BARE;
133             }
134
135         } else {
136             if (model.isRPC() && model.isWrapped()) {
137                 result = WSDLConstants.RPC_WRAPPED;
138             }
139             if (model.isDocLit() && model.isWrapped()) {
140                 result = WSDLConstants.DOC_WRAPPED;
141             }
142             if (model.isDocLit() && !model.isWrapped()) {
143                 result = WSDLConstants.DOC_BARE;
144             }
145         }
146         return result;
147     }
148
149     private boolean isOperationToGen(Method JavaDoc method, Class JavaDoc clazz) {
150         if (clazz.isInterface()) {
151             return true;
152         }
153         Class JavaDoc declareClass = method.getDeclaringClass();
154         WebMethod webMethod = AnnotationUtil.getPrivMethodAnnotation(method, WebMethod.class);
155         if (webMethod != null && !webMethod.exclude()) {
156             return true;
157         }
158         if (AnnotationUtil.getPrivClassAnnotation(declareClass, WebService.class) != null
159             && !useWebMethodClasses.get(declareClass)) {
160             return true;
161         }
162         return false;
163
164     }
165
166     // for rule 3.3
167
private void checkWebMethodUseClass(Class JavaDoc clz) {
168         if (clz == null) {
169             return;
170         }
171         if (clz.isInterface()) {
172             useWebMethodClasses.put(clz, false);
173         } else {
174             WebMethod webMethod;
175             boolean existWebMethod = false;
176             for (Method JavaDoc method : clz.getMethods()) {
177                 if (!method.getDeclaringClass().equals(seiClass)) {
178                     continue;
179                 }
180                 webMethod = AnnotationUtil.getPrivMethodAnnotation(method, WebMethod.class);
181                 if (webMethod != null && !webMethod.exclude()) {
182                     existWebMethod = true;
183                     break;
184                 }
185             }
186             useWebMethodClasses.put(clz, existWebMethod);
187         }
188         checkWebMethodUseClass(clz.getSuperclass());
189     }
190
191     private void populateWSDLInfo(Class JavaDoc clazz) {
192         WebService webService = AnnotationUtil.getPrivClassAnnotation(clazz, WebService.class);
193         if (webService == null) {
194             Message message = new Message("SEI_CLASS_NO_WEBSERVICE_ANNOTATED", LOG);
195             throw new ToolException(message);
196
197         }
198         if (webService.endpointInterface().length() > 0) {
199             clazz = AnnotationUtil.loadClass(webService.endpointInterface(), clazz.getClassLoader());
200             webService = AnnotationUtil.getPrivClassAnnotation(clazz, WebService.class);
201             if (webService == null) {
202                 Message message = new Message("SEI_INTERFACE_NO_WEBSERVICE_ANNOTATED", LOG);
203                 throw new ToolException(message);
204             }
205         }
206
207         String JavaDoc portTypeName = clazz.getSimpleName() + "PortType";
208         if (webService.name().length() > 0) {
209             portTypeName = webService.name();
210         }
211
212         model.setPortTypeName(portTypeName);
213
214         String JavaDoc portName = clazz.getSimpleName() + "Port";
215
216         if (webService.portName().length() > 0) {
217             portName = webService.portName();
218         } else if (webService.name().length() > 0) {
219             portName = webService.name() + "Port";
220
221         }
222         model.setPortName(portName);
223
224         String JavaDoc serviceName = clazz.getSimpleName() + "Service";
225         if (env.optionSet(ToolConstants.CFG_SERVICENAME)) {
226             serviceName = (String JavaDoc)env.get(ToolConstants.CFG_SERVICENAME);
227         } else {
228             if (webService.serviceName().length() > 0) {
229                 serviceName = webService.serviceName();
230             }
231         }
232         model.setServiceName(serviceName);
233
234         String JavaDoc packageName = "";
235         if (clazz.getPackage() != null) {
236             packageName = clazz.getPackage().getName();
237         }
238         model.setPackageName(packageName);
239
240         String JavaDoc targetNamespace = URIParserUtil.getNamespace(packageName);
241         if (env.optionSet(ToolConstants.CFG_TNS)) {
242             targetNamespace = (String JavaDoc)env.get(ToolConstants.CFG_TNS);
243         } else if (webService.targetNamespace().length() > 0) {
244             targetNamespace = webService.targetNamespace();
245         } else if (targetNamespace == null) {
246             Message message = new Message("SEI_CLASS_HASNO_PACKAGE", LOG);
247             throw new ToolException(message);
248         }
249
250         model.setTargetNameSpace(targetNamespace);
251         String JavaDoc wsdlLocation = webService.wsdlLocation();
252         model.setWsdllocation(wsdlLocation);
253
254         javax.jws.soap.SOAPBinding soapBinding = AnnotationUtil
255             .getPrivClassAnnotation(clazz, javax.jws.soap.SOAPBinding.class);
256         if (soapBinding != null) {
257             model.setStyle(soapBinding.style());
258             model.setUse(soapBinding.use());
259             model.setPrameterStyle(soapBinding.parameterStyle());
260         }
261
262     }
263
264     private boolean isAsynMethod(Method JavaDoc method) {
265         return method.getReturnType().equals(java.util.concurrent.Future JavaDoc.class)
266             && method.getName().endsWith("Async")
267             || method.getReturnType().equals(javax.xml.ws.Response.class)
268             && method.getName().endsWith("Async");
269           
270     }
271
272     private boolean isOneWayMethod(Method JavaDoc method) {
273         return method.isAnnotationPresent(Oneway.class);
274     }
275
276 }
277
Popular Tags