001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.websvc.rest.codegen.model;
043:
044: import java.io.File;
045: import java.net.URL;
046: import java.util.ArrayList;
047: import java.util.List;
048: import java.util.Map;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051: import javax.xml.namespace.QName;
052: import org.netbeans.api.project.Project;
053: import org.netbeans.modules.websvc.rest.support.JaxWsUtils;
054: import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport;
055: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModel;
056: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModeler;
057: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModelerFactory;
058: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlOperation;
059: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlParameter;
060: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlPort;
061: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlService;
062: import org.netbeans.modules.websvc.manager.model.WebServiceData;
063: import org.netbeans.modules.websvc.manager.model.WebServiceGroup;
064: import org.netbeans.modules.websvc.manager.model.WebServiceListModel;
065: import org.netbeans.modules.websvc.manager.util.WebServiceLibReferenceHelper;
066: import org.netbeans.modules.websvc.rest.codegen.Constants;
067: import org.netbeans.modules.websvc.rest.wizard.Util;
068: import org.netbeans.modules.xml.retriever.catalog.Utilities;
069: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
070: import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
071: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
072: import org.openide.filesystems.FileObject;
073: import org.openide.filesystems.FileUtil;
074:
075: /**
076: *
077: * @author nam
078: */
079: public class JaxwsOperationInfo {
080:
081: private String categoryName;
082: private String serviceName;
083: private String portName;
084: private String operationName;
085: private String wsdlUrl;
086: private Project project;
087: private WebServiceData webServiceData;
088: private WsdlService service;
089: private WsdlOperation operation;
090: private WsdlPort port;
091:
092: public JaxwsOperationInfo(String categoryName, String serviceName,
093: String portName, String operationName, String wsdlURL,
094: Project project) {
095: this .categoryName = categoryName;
096: this .serviceName = serviceName;
097: this .portName = portName;
098: this .operationName = operationName;
099: this .wsdlUrl = wsdlURL;
100: this .project = project;
101: }
102:
103: public String getCategoryName() {
104: return categoryName;
105: }
106:
107: public String getServiceName() {
108: return serviceName;
109: }
110:
111: public String getPortName() {
112: return portName;
113: }
114:
115: public String getOperationName() {
116: return operationName;
117: }
118:
119: public String getWsdlURL() {
120: return wsdlUrl;
121: }
122:
123: public String getWsdlLocation() {
124: initWsdlModelInfo();
125: return webServiceData.getURL();
126: }
127:
128: public void initWsdlModelInfo() {
129: if (webServiceData != null)
130: return;
131:
132: webServiceData = WebServiceListModel.getInstance()
133: .getWebServiceData(wsdlUrl, serviceName);
134: if (webServiceData == null) {
135: throw new IllegalStateException(
136: "There might have been earlier errors in initializing Web Services");
137: }
138:
139: service = webServiceData.getWsdlService();
140: if (service == null) {
141: throw new IllegalArgumentException("Service " + serviceName
142: + " does not exists");
143: }
144: port = service.getPortByName(portName);
145: if (port == null) {
146: throw new IllegalArgumentException("Port " + portName
147: + " does not exists");
148: }
149: operation = findOperationByName(port, operationName);
150: if (operation == null) {
151: throw new IllegalArgumentException("Operation "
152: + operationName + " does not exists");
153: }
154: WebServiceLibReferenceHelper.addDefaultJaxWsClientJar(project,
155: webServiceData);
156: }
157:
158: public static WsdlOperation findOperationByName(WsdlPort port,
159: String name) {
160: for (WsdlOperation o : port.getOperations()) {
161: if (name.equals(o.getName())) {
162: return o;
163: }
164: }
165: return null;
166: }
167:
168: public WsdlPort getPort() {
169: initWsdlModelInfo();
170: return port;
171: }
172:
173: public WsdlOperation getOperation() {
174: initWsdlModelInfo();
175: return operation;
176: }
177:
178: public WsdlService getService() {
179: initWsdlModelInfo();
180: return service;
181: }
182:
183: //TODO maybe parse SEI class (using Retouche) for @WebParam.Mode annotation
184: public List<WsdlParameter> getOutputParameters() {
185: ArrayList<WsdlParameter> params = new ArrayList<WsdlParameter>();
186: for (WsdlParameter p : getOperation().getParameters()) {
187: if (p.isHolder()) {
188: params.add(p);
189: }
190: }
191: return params;
192: }
193:
194: public static String getParamType(WsdlParameter param) {
195: if (param.isHolder()) {
196: String outputType = param.getTypeName();
197: int iLT = outputType.indexOf('<');
198: int iGT = outputType.indexOf('>');
199: if (iLT > 0 || iGT > 0) {
200: outputType = outputType.substring(iLT + 1, iGT).trim();
201: }
202: return outputType;
203: } else {
204: return param.getTypeName();
205: }
206: }
207:
208: //TODO maybe parse SEI class (using Retouche) for @WebParam.Mode annotation
209: public String getOutputType() {
210: String outputType = getOperation().getReturnTypeName();
211: if (Constants.VOID.equals(outputType)) {
212: for (WsdlParameter p : getOperation().getParameters()) {
213: if (p.isHolder()) {
214: outputType = getParamType(p);
215: break;
216: }
217: }
218: }
219: return outputType;
220: }
221:
222: //TODO maybe parse SEI class (using Retouche) for @WebParam.Mode annotation
223: public String[] getInputParameterNames() {
224: ArrayList<String> names = new ArrayList<String>();
225: for (WsdlParameter p : getOperation().getParameters()) {
226: if (!p.isHolder()) {
227: names.add(p.getName());
228: }
229: }
230:
231: return names.toArray(new String[names.size()]);
232: }
233:
234: //TODO maybe parse SEI class (using Retouche) for @WebParam.Mode annotation
235: public Class[] getInputParameterTypes() {
236: ArrayList<Class> types = new ArrayList<Class>();
237:
238: for (WsdlParameter p : getOperation().getParameters()) {
239: if (!p.isHolder()) {
240: int repeatCount = 0;
241: Class type = null;
242:
243: // This is a hack to wait for the complex type to become
244: // available. We will give up after 120 seconds.
245: synchronized (this ) {
246: try {
247: while (repeatCount < 60) {
248: type = Util.getType(project, p
249: .getTypeName());
250:
251: if (type != null) {
252: break;
253: }
254:
255: repeatCount++;
256: this .wait(2000);
257: }
258: } catch (InterruptedException ex) {
259: }
260: }
261:
262: // RESOLVE:
263: // Need to failure gracefully by displaying an error dialog.
264: // For now, set it to Object.class.
265: if (type == null) {
266: type = Object.class;
267: }
268:
269: types.add(type);
270: }
271: }
272:
273: return types.toArray(new Class[types.size()]);
274: }
275:
276: public boolean needsSoapHandler() {
277: return getSoapHeaderParameters().size() > 0;
278: }
279:
280: private List<ParameterInfo> headerParams;
281:
282: public List<ParameterInfo> getSoapHeaderParameters() {
283: if (headerParams == null) {
284: headerParams = new java.util.ArrayList<ParameterInfo>();
285:
286: Map<QName, String> params = JaxWsUtils
287: .getSoapHandlerParameters(getXamWsdlModel(),
288: getPort(), getOperation());
289: for (Map.Entry<QName, String> entry : params.entrySet()) {
290: Class type = Util.getType(project, entry.getValue());
291: ParameterInfo info = new ParameterInfo(entry.getKey(),
292: type, entry.getValue());
293: info.setIsQueryParam(false);
294: headerParams.add(info);
295: }
296: }
297: return headerParams;
298: }
299:
300: public WSDLModel getXamWsdlModel() {
301: try {
302: FileObject wsdlFO = FileUtil.toFileObject(new File(
303: webServiceData.getURL()));
304: ;
305: return WSDLModelFactory.getDefault().getModel(
306: Utilities.createModelSource(wsdlFO, true));
307: } catch (CatalogModelException ex) {
308: Logger.global.log(Level.INFO, "", ex);
309: }
310: return null;
311: }
312:
313: }
|