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.misc.processor;
019:
020: import java.io.IOException;
021: import java.io.Writer;
022: import java.util.Iterator;
023: import java.util.Map;
024: import javax.wsdl.Binding;
025: import javax.wsdl.Port;
026: import javax.wsdl.Service;
027: import javax.wsdl.WSDLException;
028: import javax.wsdl.extensions.ExtensibilityElement;
029: import javax.wsdl.extensions.ExtensionRegistry;
030: import javax.wsdl.xml.WSDLWriter;
031: import javax.xml.namespace.QName;
032:
033: import org.apache.cxf.common.i18n.Message;
034: import org.apache.cxf.tools.common.ToolConstants;
035: import org.apache.cxf.tools.common.ToolException;
036: import org.apache.cxf.tools.misc.processor.address.Address;
037: import org.apache.cxf.tools.misc.processor.address.AddressFactory;
038: import org.apache.cxf.wsdl.WSDLConstants;
039: import org.apache.cxf.wsdl.WSDLExtensibilityPlugin;
040:
041: public class WSDLToServiceProcessor extends AbstractWSDLToProcessor {
042:
043: private static final String NEW_FILE_NAME_MODIFIER = "-service";
044:
045: private Map services;
046: private Service service;
047: private Map ports;
048: private Port port;
049: private Binding binding;
050:
051: public void process() throws ToolException {
052: init();
053: if (isServicePortExisted()) {
054: Message msg = new Message("SERVICE_PORT_EXIST", LOG);
055: throw new ToolException(msg);
056: }
057: if (!isBindingExisted()) {
058: Message msg = new Message("BINDING_NOT_EXIST", LOG);
059: throw new ToolException(msg);
060: }
061: doAppendService();
062: }
063:
064: private boolean isServicePortExisted() {
065: return isServiceExisted() && isPortExisted();
066: }
067:
068: private boolean isServiceExisted() {
069: services = wsdlDefinition.getServices();
070: if (services == null) {
071: return false;
072: }
073: Iterator it = services.keySet().iterator();
074: while (it.hasNext()) {
075: QName serviceQName = (QName) it.next();
076: String serviceName = serviceQName.getLocalPart();
077: if (serviceName.equals(env.get(ToolConstants.CFG_SERVICE))) {
078: service = (Service) services.get(serviceQName);
079: break;
080: }
081: }
082: return (service == null) ? false : true;
083: }
084:
085: private boolean isPortExisted() {
086: ports = service.getPorts();
087: if (ports == null) {
088: return false;
089: }
090: Iterator it = ports.keySet().iterator();
091: while (it.hasNext()) {
092: String portName = (String) it.next();
093: if (portName.equals(env.get(ToolConstants.CFG_PORT))) {
094: port = (Port) ports.get(portName);
095: break;
096: }
097: }
098: return (port == null) ? false : true;
099: }
100:
101: private boolean isBindingExisted() {
102: Map bindings = wsdlDefinition.getBindings();
103: if (bindings == null) {
104: return false;
105: }
106: Iterator it = bindings.keySet().iterator();
107: while (it.hasNext()) {
108: QName bindingQName = (QName) it.next();
109: String bindingName = bindingQName.getLocalPart();
110: String attrBinding = (String) env
111: .get(ToolConstants.CFG_BINDING_ATTR);
112: if (attrBinding.equals(bindingName)) {
113: binding = (Binding) bindings.get(bindingQName);
114: }
115: }
116: return (binding == null) ? false : true;
117: }
118:
119: protected void init() throws ToolException {
120: parseWSDL((String) env.get(ToolConstants.CFG_WSDLURL));
121: }
122:
123: private void doAppendService() throws ToolException {
124: if (service == null) {
125: service = wsdlDefinition.createService();
126: service.setQName(new QName(WSDLConstants.WSDL_PREFIX,
127: (String) env.get(ToolConstants.CFG_SERVICE)));
128: }
129: if (port == null) {
130: port = wsdlDefinition.createPort();
131: port.setName((String) env.get(ToolConstants.CFG_PORT));
132: port.setBinding(binding);
133: }
134: setAddrElement();
135: service.addPort(port);
136: wsdlDefinition.addService(service);
137:
138: WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter();
139: Writer outputWriter = getOutputWriter(NEW_FILE_NAME_MODIFIER);
140: try {
141: wsdlWriter.writeWSDL(wsdlDefinition, outputWriter);
142: } catch (WSDLException wse) {
143: Message msg = new Message("FAIL_TO_WRITE_WSDL", LOG);
144: throw new ToolException(msg, wse);
145: }
146: try {
147: outputWriter.close();
148: } catch (IOException ioe) {
149: Message msg = new Message("FAIL_TO_CLOSE_WSDL_FILE", LOG);
150: throw new ToolException(msg, ioe);
151: }
152:
153: }
154:
155: private void setAddrElement() throws ToolException {
156: ExtensionRegistry extReg = this .wsdlReader
157: .getExtensionRegistry();
158: if (extReg == null) {
159: extReg = wsdlFactory.newPopulatedExtensionRegistry();
160: }
161: String transport = (String) env
162: .get(ToolConstants.CFG_TRANSPORT);
163: Address address = AddressFactory.getInstance().getAddresser(
164: transport);
165:
166: Map<String, String> ns = address.getNamespaces(env);
167: for (String key : ns.keySet()) {
168: wsdlDefinition.addNamespace(key, ns.get(key));
169: }
170:
171: WSDLExtensibilityPlugin plugin = getWSDLPlugin(transport,
172: Port.class);
173: try {
174: ExtensibilityElement extElement = plugin
175: .createExtension(address.buildAddressArguments(env));
176: port.addExtensibilityElement(extElement);
177: } catch (WSDLException wse) {
178: Message msg = new Message("FAIL_TO_CREATE_SOAP_ADDRESS",
179: LOG);
180: throw new ToolException(msg, wse);
181: }
182: }
183: }
|