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: /*
043: * BindingGenerator.java
044: *
045: * Created on September 6, 2006, 4:36 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.modules.xml.wsdl.ui.wizard;
052:
053: import java.util.Collection;
054: import java.util.Iterator;
055: import java.util.Map;
056:
057: import org.netbeans.modules.xml.wsdl.bindingsupport.template.localized.LocalizedTemplate;
058: import org.netbeans.modules.xml.wsdl.model.Binding;
059: import org.netbeans.modules.xml.wsdl.model.BindingFault;
060: import org.netbeans.modules.xml.wsdl.model.BindingInput;
061: import org.netbeans.modules.xml.wsdl.model.BindingOperation;
062: import org.netbeans.modules.xml.wsdl.model.BindingOutput;
063: import org.netbeans.modules.xml.wsdl.model.Fault;
064: import org.netbeans.modules.xml.wsdl.model.Input;
065: import org.netbeans.modules.xml.wsdl.model.NotificationOperation;
066: import org.netbeans.modules.xml.wsdl.model.OneWayOperation;
067: import org.netbeans.modules.xml.wsdl.model.Operation;
068: import org.netbeans.modules.xml.wsdl.model.Output;
069: import org.netbeans.modules.xml.wsdl.model.Port;
070: import org.netbeans.modules.xml.wsdl.model.PortType;
071: import org.netbeans.modules.xml.wsdl.model.RequestResponseOperation;
072: import org.netbeans.modules.xml.wsdl.model.Service;
073: import org.netbeans.modules.xml.wsdl.model.SolicitResponseOperation;
074: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
075: import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElements;
076: import org.netbeans.modules.xml.wsdl.ui.model.StringAttribute;
077: import org.netbeans.modules.xml.xam.Named;
078: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
079:
080: /**
081: *
082: * @author radval
083: */
084: public class BindingGenerator implements Command {
085:
086: private WSDLModel mModel;
087:
088: private PortType mPortType;
089:
090: private Map mConfigurationMap;
091:
092: private WsdlGenerationUtil mUtil;
093:
094: private Binding mBinding;
095:
096: private Service mService;
097:
098: private Port mPort;
099:
100: /** Creates a new instance of BindingGenerator */
101: public BindingGenerator(WSDLModel model, PortType pt,
102: Map configurationMap) {
103: this .mModel = model;
104: this .mPortType = pt;
105: this .mConfigurationMap = configurationMap;
106: this .mUtil = new WsdlGenerationUtil(this .mModel);
107: }
108:
109: public Binding getBinding() {
110: return this .mBinding;
111: }
112:
113: public Service getService() {
114: return this .mService;
115: }
116:
117: public Port getPort() {
118: return this .mPort;
119: }
120:
121: public void execute() {
122: //binding
123: String bindingName = (String) this .mConfigurationMap
124: .get(WizardBindingConfigurationStep.BINDING_NAME);
125: if (bindingName == null) {
126: return;
127: }
128: Binding b = mModel.getFactory().createBinding();
129: this .mBinding = b;
130: b.setName(bindingName);
131: mModel.getDefinitions().addBinding(b);
132: NamedComponentReference<PortType> ptRef = b.createReferenceTo(
133: this .mPortType, PortType.class);
134: b.setType(ptRef);
135:
136: //Not used LocalizedTemplateGroup bindingType = (LocalizedTemplateGroup) this.mConfigurationMap.get(WizardBindingConfigurationStep.BINDING_TYPE);
137: //this could be null for a binding which does not have a sub type
138: LocalizedTemplate bindingSubType = (LocalizedTemplate) this .mConfigurationMap
139: .get(WizardBindingConfigurationStep.BINDING_SUBTYPE);
140: if (bindingSubType != null) {
141: //binding protocol
142: createAndAddBindingProtocol(b, bindingSubType);
143:
144: Collection<Operation> operations = mPortType
145: .getOperations();
146: for (Operation operation : operations) {
147: //binding operation
148: BindingOperation bo = this .mModel.getFactory()
149: .createBindingOperation();
150: b.addBindingOperation(bo);
151: NamedComponentReference<Operation> opRef = bo
152: .createReferenceTo(operation, Operation.class);
153: bo.setOperation(opRef);
154:
155: //binding operation protocol
156: createAndAddBindingOperationProtocolElements(bo,
157: bindingSubType, operation);
158: }
159:
160: } else {
161: //no binding subtype
162:
163: }
164:
165: Boolean autoCreateServicePort = (Boolean) mConfigurationMap
166: .get(WizardBindingConfigurationStep.AUTO_CREATE_SERVICEPORT);
167:
168: if (autoCreateServicePort != null
169: && !autoCreateServicePort.booleanValue()) {
170: return;
171: }
172:
173: //service and port
174: String serviceName = (String) this .mConfigurationMap
175: .get(WizardBindingConfigurationStep.SERVICE_NAME);
176: String servicePortName = (String) this .mConfigurationMap
177: .get(WizardBindingConfigurationStep.SERVICEPORT_NAME);
178:
179: Collection<Service> services = mModel.getDefinitions()
180: .getServices();
181: Service service = null;
182: for (Service svc : services) {
183: if (svc.getName().equals(serviceName)) {
184: service = svc;
185: break;
186: }
187: }
188:
189: if (service == null) {
190: service = mModel.getFactory().createService();
191: this .mService = service;
192: service.setName(serviceName);
193: mModel.getDefinitions().addService(service);
194: }
195:
196: /*
197: 01/02/07, following code replaced to allow the reuse of empty port element in CASA (T. Li)
198: */
199: // Port port = mModel.getFactory().createPort();
200: // this.mPort = port;
201: // port.setName(servicePortName);
202: // NamedComponentReference<Binding> bindingRef = port.createReferenceTo(b, Binding.class);
203: // port.setBinding(bindingRef);
204: // createAndAddServicePortProtocolElements(port, bindingSubType);
205: // service.addPort(port);
206: Collection<Port> ports = service.getPorts();
207: Port port = null;
208: for (Port p : ports) {
209: if (p.getName().equals(servicePortName)) {
210: port = p;
211: break;
212: }
213: }
214:
215: if (port == null) {
216: port = mModel.getFactory().createPort();
217: this .mPort = port;
218: port.setName(servicePortName);
219: service.addPort(port);
220: }
221: NamedComponentReference<Binding> bindingRef = port
222: .createReferenceTo(b, Binding.class);
223: port.setBinding(bindingRef);
224: createAndAddServicePortProtocolElements(port, bindingSubType);
225:
226: }
227:
228: private void createAndAddBindingProtocol(Binding b,
229: LocalizedTemplate bindingSubType) {
230: this .mUtil.createAndAddExtensionElementAndAttribute(
231: WSDLExtensibilityElements.ELEMENT_BINDING,
232: bindingSubType, b);
233: }
234:
235: private void createAndAddBindingOperationProtocolElements(
236: BindingOperation bOperation,
237: LocalizedTemplate bindingSubType,
238: Operation portTypeOperation) {
239:
240: this .mUtil.createAndAddExtensionElementAndAttribute(
241: WSDLExtensibilityElements.ELEMENT_BINDING_OPERATION,
242: bindingSubType, bOperation);
243:
244: if (portTypeOperation instanceof RequestResponseOperation) {
245: Input input = portTypeOperation.getInput();
246: Output output = portTypeOperation.getOutput();
247: Collection<Fault> faults = portTypeOperation.getFaults();
248:
249: if (input != null) {
250: BindingInput bIn = createAndAddBindingOperationInput(
251: bOperation, bindingSubType);
252: if (input.getAttribute(new StringAttribute(
253: Named.NAME_PROPERTY)) != null) {
254: bIn.setName(input.getName());
255: }
256: }
257:
258: if (output != null) {
259: BindingOutput bOut = createAndAddBindingOperationOutput(
260: bOperation, bindingSubType);
261: if (output.getAttribute(new StringAttribute(
262: Named.NAME_PROPERTY)) != null) {
263: bOut.setName(output.getName());
264: }
265: }
266:
267: if (faults != null) {
268: Iterator<Fault> it = faults.iterator();
269: while (it.hasNext()) {
270: Fault fault = it.next();
271: BindingFault bFault = createAndAddBindingOperationFault(
272: bOperation, bindingSubType);
273: bFault.setName(fault.getName());
274: }
275: }
276:
277: } else if (portTypeOperation instanceof OneWayOperation) {
278: Input input = portTypeOperation.getInput();
279:
280: if (input != null) {
281: BindingInput bIn = createAndAddBindingOperationInput(
282: bOperation, bindingSubType);
283: if (input.getAttribute(new StringAttribute(
284: Named.NAME_PROPERTY)) != null) {
285: bIn.setName(input.getName());
286: }
287: }
288:
289: } else if (portTypeOperation instanceof SolicitResponseOperation) {
290: Input input = portTypeOperation.getInput();
291: Output output = portTypeOperation.getOutput();
292: Collection<Fault> faults = portTypeOperation.getFaults();
293:
294: if (input != null) {
295: BindingInput bIn = createAndAddBindingOperationInput(
296: bOperation, bindingSubType);
297: if (input.getAttribute(new StringAttribute(
298: Named.NAME_PROPERTY)) != null) {
299: bIn.setName(input.getName());
300: }
301: }
302:
303: if (output != null) {
304: BindingOutput bOut = createAndAddBindingOperationOutput(
305: bOperation, bindingSubType);
306: if (output.getAttribute(new StringAttribute(
307: Named.NAME_PROPERTY)) != null) {
308: bOut.setName(output.getName());
309: }
310: }
311:
312: if (faults != null) {
313: Iterator<Fault> it = faults.iterator();
314: while (it.hasNext()) {
315: Fault fault = it.next();
316: BindingFault bFault = createAndAddBindingOperationFault(
317: bOperation, bindingSubType);
318: bFault.setName(fault.getName());
319: }
320: }
321: } else if (portTypeOperation instanceof NotificationOperation) {
322: Output output = portTypeOperation.getOutput();
323: if (output != null) {
324: BindingOutput bOut = createAndAddBindingOperationOutput(
325: bOperation, bindingSubType);
326: if (output.getAttribute(new StringAttribute(
327: Named.NAME_PROPERTY)) != null) {
328: bOut.setName(output.getName());
329: }
330: }
331: }
332: }
333:
334: private BindingInput createAndAddBindingOperationInput(
335: BindingOperation bOperation,
336: LocalizedTemplate bindingSubType) {
337: BindingInput bIn = this .mModel.getFactory()
338: .createBindingInput();
339: bOperation.setBindingInput(bIn);
340: this .mUtil
341: .createAndAddExtensionElementAndAttribute(
342: WSDLExtensibilityElements.ELEMENT_BINDING_OPERATION_INPUT,
343: bindingSubType, bIn);
344:
345: return bIn;
346: }
347:
348: private BindingOutput createAndAddBindingOperationOutput(
349: BindingOperation bOperation,
350: LocalizedTemplate bindingSubType) {
351: BindingOutput bOut = this .mModel.getFactory()
352: .createBindingOutput();
353: bOperation.setBindingOutput(bOut);
354: this .mUtil
355: .createAndAddExtensionElementAndAttribute(
356: WSDLExtensibilityElements.ELEMENT_BINDING_OPERATION_OUTPUT,
357: bindingSubType, bOut);
358:
359: return bOut;
360: }
361:
362: private BindingFault createAndAddBindingOperationFault(
363: BindingOperation bOperation,
364: LocalizedTemplate bindingSubType) {
365: BindingFault bFault = this .mModel.getFactory()
366: .createBindingFault();
367: bOperation.addBindingFault(bFault);
368: this .mUtil
369: .createAndAddExtensionElementAndAttribute(
370: WSDLExtensibilityElements.ELEMENT_BINDING_OPERATION_FAULT,
371: bindingSubType, bFault);
372:
373: return bFault;
374: }
375:
376: private void createAndAddServicePortProtocolElements(Port port,
377: LocalizedTemplate bindingSubType) {
378: this.mUtil.createAndAddExtensionElementAndAttribute(
379: WSDLExtensibilityElements.ELEMENT_SERVICE_PORT,
380: bindingSubType, port);
381:
382: }
383: }
|