001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.wsdlto.frontend.jaxws.processor.internal;
019:
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.logging.Level;
024:
025: import org.apache.cxf.service.model.FaultInfo;
026: import org.apache.cxf.service.model.MessageInfo;
027: import org.apache.cxf.service.model.MessagePartInfo;
028: import org.apache.cxf.service.model.OperationInfo;
029: import org.apache.cxf.tools.common.ToolConstants;
030: import org.apache.cxf.tools.common.ToolContext;
031: import org.apache.cxf.tools.common.ToolException;
032: import org.apache.cxf.tools.common.model.JavaAnnotation;
033: import org.apache.cxf.tools.common.model.JavaInterface;
034: import org.apache.cxf.tools.common.model.JavaMethod;
035: import org.apache.cxf.tools.common.model.JavaParameter;
036: import org.apache.cxf.tools.common.model.JavaReturn;
037: import org.apache.cxf.tools.wsdlto.frontend.jaxws.customization.JAXWSBinding;
038: import org.apache.cxf.tools.wsdlto.frontend.jaxws.processor.internal.annotator.SoapBindingAnnotator;
039: import org.apache.cxf.tools.wsdlto.frontend.jaxws.processor.internal.annotator.WebMethodAnnotator;
040: import org.apache.cxf.tools.wsdlto.frontend.jaxws.processor.internal.annotator.WebResultAnnotator;
041: import org.apache.cxf.tools.wsdlto.frontend.jaxws.processor.internal.annotator.WrapperAnnotator;
042: import org.apache.cxf.tools.wsdlto.frontend.jaxws.processor.internal.mapper.MethodMapper;
043:
044: public class OperationProcessor extends AbstractProcessor {
045:
046: private JavaParameter wrapperRequest;
047: private JavaParameter wrapperResponse;
048:
049: public OperationProcessor(ToolContext c) {
050: super (c);
051: }
052:
053: @SuppressWarnings("unchecked")
054: public void process(JavaInterface intf, OperationInfo operation)
055: throws ToolException {
056: JavaMethod method = new MethodMapper().map(operation);
057: method.setInterface(intf);
058: processMethod(method, operation, null);
059: Collection<FaultInfo> faults = operation.getFaults();
060: FaultProcessor faultProcessor = new FaultProcessor(context);
061: faultProcessor.process(method, faults);
062: intf.addMethod(method);
063: }
064:
065: @SuppressWarnings("unchecked")
066: void processMethod(JavaMethod method, OperationInfo operation,
067: JAXWSBinding globalBinding) throws ToolException {
068: if (isAsynCMethod(method)) {
069: return;
070: }
071: MessageInfo inputMessage = operation.getInput();
072: MessageInfo outputMessage = operation.getOutput();
073:
074: if (inputMessage == null) {
075: LOG.log(Level.WARNING, "NO_INPUT_MESSAGE",
076: new Object[] { operation.getName() });
077: org.apache.cxf.common.i18n.Message msg = new org.apache.cxf.common.i18n.Message(
078: "INVALID_MEP", LOG, new Object[] { operation
079: .getName() });
080: throw new ToolException(msg);
081: }
082:
083: ParameterProcessor paramProcessor = new ParameterProcessor(
084: context);
085: method.clear();
086: paramProcessor.process(method, inputMessage, outputMessage,
087: operation.getParameterOrdering());
088:
089: method.annotate(new WebMethodAnnotator());
090:
091: if (method.isWrapperStyle()) {
092: setWrapper(operation);
093: method.annotate(new WrapperAnnotator(wrapperRequest,
094: wrapperResponse));
095: }
096:
097: method.annotate(new WebResultAnnotator());
098: method.annotate(new SoapBindingAnnotator());
099:
100: JAXWSBinding opBinding = (JAXWSBinding) operation
101: .getExtensor(JAXWSBinding.class);
102:
103: boolean enableAsync = false;
104: if (globalBinding != null
105: && globalBinding.isEnableAsyncMapping()
106: || opBinding != null
107: && opBinding.isEnableAsyncMapping()) {
108: enableAsync = true;
109: }
110:
111: if (!method.isOneWay() && enableAsync
112: && !isAddedAsyMethod(method)) {
113: addAsyncMethod(method);
114: }
115:
116: if (globalBinding != null && globalBinding.isEnableMime()
117: || opBinding != null && opBinding.isEnableMime()) {
118: method.setMimeEnable(true);
119: }
120: }
121:
122: private void setWrapper(OperationInfo operation) {
123: MessagePartInfo inputPart = null;
124: if (operation.getInput() != null
125: && operation.getInput().getMessageParts() != null) {
126: inputPart = operation.getInput().getMessageParts()
127: .iterator().next();
128: }
129: MessagePartInfo outputPart = null;
130: if (operation.getOutput() != null
131: && operation.getOutput().getMessageParts() != null) {
132: outputPart = operation.getOutput().getMessageParts()
133: .iterator().next();
134: }
135:
136: if (inputPart != null) {
137: wrapperRequest = new JavaParameter();
138: wrapperRequest.setName(ProcessorUtil
139: .resolvePartName(inputPart));
140: wrapperRequest
141: .setType(ProcessorUtil.getPartType(inputPart));
142: wrapperRequest.setTargetNamespace(ProcessorUtil
143: .resolvePartNamespace(inputPart));
144:
145: wrapperRequest.setClassName(ProcessorUtil.getFullClzName(
146: inputPart, context, false));
147:
148: }
149: if (outputPart != null) {
150: wrapperResponse = new JavaParameter();
151: wrapperResponse.setName(ProcessorUtil
152: .resolvePartName(outputPart));
153: wrapperResponse.setType(ProcessorUtil
154: .getPartType(outputPart));
155: wrapperResponse.setTargetNamespace(ProcessorUtil
156: .resolvePartNamespace(outputPart));
157:
158: wrapperResponse.setClassName(ProcessorUtil.getFullClzName(
159: outputPart, context, false));
160: }
161: }
162:
163: private boolean isAsynCMethod(JavaMethod method) {
164: if (method
165: .getName()
166: .toLowerCase()
167: .equals(
168: (method.getOperationName() + ToolConstants.ASYNC_METHOD_SUFFIX)
169: .toLowerCase())) {
170: return true;
171: }
172: return false;
173: }
174:
175: private void addAsyncMethod(JavaMethod method) throws ToolException {
176: addPollingMethod(method);
177: addCallbackMethod(method);
178: method.getInterface().addImport("javax.xml.ws.AsyncHandler");
179: method.getInterface().addImport("java.util.concurrent.Future");
180: method.getInterface().addImport("javax.xml.ws.Response");
181: }
182:
183: private void addPollingMethod(JavaMethod method)
184: throws ToolException {
185: JavaMethod pollingMethod = new JavaMethod(method.getInterface());
186: pollingMethod.setName(method.getName()
187: + ToolConstants.ASYNC_METHOD_SUFFIX);
188: pollingMethod.setStyle(method.getStyle());
189: pollingMethod.setWrapperStyle(method.isWrapperStyle());
190: pollingMethod.setSoapAction(method.getSoapAction());
191: pollingMethod.setOperationName(method.getOperationName());
192:
193: JavaReturn future = new JavaReturn();
194: future.setClassName("Future<?>");
195: pollingMethod.setReturn(future);
196:
197: // REVISIT: test the operation name in the annotation
198: pollingMethod.annotate(new WebMethodAnnotator());
199: pollingMethod.addAnnotation("ResponseWrapper", method
200: .getAnnotationMap().get("ResponseWrapper"));
201: pollingMethod.addAnnotation("RequestWrapper", method
202: .getAnnotationMap().get("RequestWrapper"));
203: pollingMethod.addAnnotation("SOAPBinding", method
204: .getAnnotationMap().get("SOAPBinding"));
205:
206: for (Iterator iter = method.getParameters().iterator(); iter
207: .hasNext();) {
208: pollingMethod.addParameter((JavaParameter) iter.next());
209: }
210:
211: JavaParameter asyncHandler = new JavaParameter();
212: asyncHandler.setName("asyncHandler");
213: asyncHandler.setClassName(getAsyncClassName(method,
214: "AsyncHandler"));
215: JavaAnnotation asyncHandlerAnnotation = new JavaAnnotation(
216: "WebParam");
217: asyncHandlerAnnotation.addArgument("name", "asyncHandler");
218: asyncHandlerAnnotation.addArgument("targetNamespace", "");
219: asyncHandler.setAnnotation(asyncHandlerAnnotation);
220:
221: pollingMethod.addParameter(asyncHandler);
222:
223: method.getInterface().addMethod(pollingMethod);
224: }
225:
226: private void addCallbackMethod(JavaMethod method)
227: throws ToolException {
228: JavaMethod callbackMethod = new JavaMethod(method
229: .getInterface());
230: callbackMethod.setName(method.getName()
231: + ToolConstants.ASYNC_METHOD_SUFFIX);
232: callbackMethod.setStyle(method.getStyle());
233: callbackMethod.setWrapperStyle(method.isWrapperStyle());
234: callbackMethod.setSoapAction(method.getSoapAction());
235: callbackMethod.setOperationName(method.getOperationName());
236:
237: JavaReturn response = new JavaReturn();
238: response.setClassName(getAsyncClassName(method, "Response"));
239: callbackMethod.setReturn(response);
240:
241: // REVISIT: test the operation name in the annotation
242: callbackMethod.annotate(new WebMethodAnnotator());
243: callbackMethod.addAnnotation("RequestWrapper", method
244: .getAnnotationMap().get("RequestWrapper"));
245: callbackMethod.addAnnotation("ResponseWrapper", method
246: .getAnnotationMap().get("ResponseWrapper"));
247: callbackMethod.addAnnotation("SOAPBinding", method
248: .getAnnotationMap().get("SOAPBinding"));
249:
250: for (Iterator iter = method.getParameters().iterator(); iter
251: .hasNext();) {
252: callbackMethod.addParameter((JavaParameter) iter.next());
253: }
254:
255: method.getInterface().addMethod(callbackMethod);
256: }
257:
258: private String getAsyncClassName(JavaMethod method, String clzName) {
259: String response;
260: if (wrapperResponse != null) {
261: response = wrapperResponse.getClassName();
262: } else {
263: response = method.getReturn().getClassName();
264: }
265:
266: StringBuffer sb = new StringBuffer();
267: sb.append(clzName);
268: sb.append("<");
269: sb.append(response);
270: sb.append(">");
271: return sb.toString();
272: }
273:
274: private boolean isAddedAsyMethod(JavaMethod method) {
275: List<JavaMethod> jmethods = method.getInterface().getMethods();
276: int counter = 0;
277: for (JavaMethod jm : jmethods) {
278: if (jm.getOperationName().equals(method.getOperationName())) {
279: counter++;
280: }
281: }
282: return counter > 1 ? true : false;
283: }
284:
285: }
|