001: package org.objectweb.celtix.tools.processors.wsdl2;
002:
003: import java.io.IOException;
004: import java.io.Writer;
005: import java.util.Iterator;
006: import java.util.Map;
007:
008: import javax.wsdl.Binding;
009: import javax.wsdl.Port;
010: import javax.wsdl.Service;
011: import javax.wsdl.WSDLException;
012: import javax.wsdl.extensions.ExtensionRegistry;
013: import javax.wsdl.extensions.soap.SOAPAddress;
014: import javax.wsdl.xml.WSDLWriter;
015: import javax.xml.namespace.QName;
016:
017: import org.objectweb.celtix.common.i18n.Message;
018: import org.objectweb.celtix.tools.common.ToolConstants;
019: import org.objectweb.celtix.tools.common.ToolException;
020: import org.objectweb.celtix.tools.common.WSDLConstants;
021: import org.objectweb.celtix.tools.extensions.jms.JMSAddress;
022: import org.objectweb.celtix.tools.extensions.jms.JMSAddressSerializer;
023:
024: public class WSDLToServiceProcessor extends WSDLToProcessor {
025:
026: private static final String NEW_FILE_NAME_MODIFIER = "-service";
027: private static final String HTTP_PREFIX = "http://localhost:9000";
028:
029: private Map services;
030: private Service service;
031: private Map ports;
032: private Port port;
033: private Binding binding;
034:
035: public void process() throws ToolException {
036: init();
037: if (isServicePortExisted()) {
038: Message msg = new Message("SERVICE_PORT_EXIST", LOG);
039: throw new ToolException(msg);
040: }
041: if (!isBindingExisted()) {
042: Message msg = new Message("BINDING_NOT_EXIST", LOG);
043: throw new ToolException(msg);
044: }
045: doAppendService();
046: }
047:
048: private boolean isServicePortExisted() {
049: return isServiceExisted() && isPortExisted();
050: }
051:
052: private boolean isServiceExisted() {
053: services = wsdlDefinition.getServices();
054: if (services == null) {
055: return false;
056: }
057: Iterator it = services.keySet().iterator();
058: while (it.hasNext()) {
059: QName serviceQName = (QName) it.next();
060: String serviceName = serviceQName.getLocalPart();
061: if (serviceName.equals(env.get(ToolConstants.CFG_SERVICE))) {
062: service = (Service) services.get(serviceQName);
063: break;
064: }
065: }
066: return (service == null) ? false : true;
067: }
068:
069: private boolean isPortExisted() {
070: ports = service.getPorts();
071: if (ports == null) {
072: return false;
073: }
074: Iterator it = ports.keySet().iterator();
075: while (it.hasNext()) {
076: String portName = (String) it.next();
077: if (portName.equals(env.get(ToolConstants.CFG_PORT))) {
078: port = (Port) ports.get(portName);
079: break;
080: }
081: }
082: return (port == null) ? false : true;
083: }
084:
085: private boolean isBindingExisted() {
086: Map bindings = wsdlDefinition.getBindings();
087: if (bindings == null) {
088: return false;
089: }
090: Iterator it = bindings.keySet().iterator();
091: while (it.hasNext()) {
092: QName bindingQName = (QName) it.next();
093: String bindingName = bindingQName.getLocalPart();
094: String attrBinding = (String) env
095: .get(ToolConstants.CFG_BINDING_ATTR);
096: if (attrBinding.equals(bindingName)) {
097: binding = (Binding) bindings.get(bindingQName);
098: }
099: }
100: return (binding == null) ? false : true;
101: }
102:
103: protected void init() throws ToolException {
104: parseWSDL((String) env.get(ToolConstants.CFG_WSDLURL));
105: }
106:
107: private void doAppendService() throws ToolException {
108: if (service == null) {
109: service = wsdlDefinition.createService();
110: service.setQName(new QName(WSDLConstants.WSDL_PREFIX,
111: (String) env.get(ToolConstants.CFG_SERVICE)));
112: }
113: if (port == null) {
114: port = wsdlDefinition.createPort();
115: port.setName((String) env.get(ToolConstants.CFG_PORT));
116: port.setBinding(binding);
117: }
118: setAddrElement();
119: service.addPort(port);
120: wsdlDefinition.addService(service);
121:
122: WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter();
123: Writer outputWriter = getOutputWriter(NEW_FILE_NAME_MODIFIER);
124: try {
125: wsdlWriter.writeWSDL(wsdlDefinition, outputWriter);
126: } catch (WSDLException wse) {
127: Message msg = new Message("FAIl_TO_WRITE_WSDL", LOG);
128: throw new ToolException(msg, wse);
129: }
130: try {
131: outputWriter.close();
132: } catch (IOException ioe) {
133: Message msg = new Message("FAIL_TO_CLOSE_WSDL_FILE", LOG);
134: throw new ToolException(msg, ioe);
135: }
136:
137: }
138:
139: private void setAddrElement() throws ToolException {
140: ExtensionRegistry extReg = this .wsdlReader
141: .getExtensionRegistry();
142: if (extReg == null) {
143: extReg = wsdlFactory.newPopulatedExtensionRegistry();
144: }
145: if ("http".equalsIgnoreCase((String) env
146: .get(ToolConstants.CFG_TRANSPORT))) {
147: SOAPAddress soapAddress = null;
148: try {
149: soapAddress = (SOAPAddress) extReg.createExtension(
150: Port.class,
151: WSDLConstants.NS_SOAP_BINDING_ADDRESS);
152: } catch (WSDLException wse) {
153: Message msg = new Message("FAIl_TO_CREATE_SOAPADDRESS",
154: LOG);
155: throw new ToolException(msg, wse);
156: }
157: if (env.get(ToolConstants.CFG_ADDRESS) != null) {
158: soapAddress.setLocationURI((String) env
159: .get(ToolConstants.CFG_ADDRESS));
160: } else {
161: soapAddress.setLocationURI(HTTP_PREFIX + "/"
162: + env.get(ToolConstants.CFG_SERVICE) + "/"
163: + env.get(ToolConstants.CFG_PORT));
164: }
165: port.addExtensibilityElement(soapAddress);
166: } else if ("jms".equalsIgnoreCase((String) env
167: .get(ToolConstants.CFG_TRANSPORT))) {
168: JMSAddress jmsAddress = null;
169: JMSAddressSerializer jmsAddressSerializer = new JMSAddressSerializer();
170: try {
171: extReg
172: .registerSerializer(JMSAddress.class,
173: ToolConstants.JMS_ADDRESS,
174: jmsAddressSerializer);
175: extReg
176: .registerDeserializer(JMSAddress.class,
177: ToolConstants.JMS_ADDRESS,
178: jmsAddressSerializer);
179: jmsAddress = (JMSAddress) extReg.createExtension(
180: Port.class, ToolConstants.JMS_ADDRESS);
181: if (env.optionSet(ToolConstants.JMS_ADDR_DEST_STYLE)) {
182: jmsAddress.setDestinationStyle((String) env
183: .get(ToolConstants.JMS_ADDR_DEST_STYLE));
184: }
185: if (env.optionSet(ToolConstants.JMS_ADDR_INIT_CTX)) {
186: jmsAddress.setInitialContextFactory((String) env
187: .get(ToolConstants.JMS_ADDR_INIT_CTX));
188: }
189: if (env.optionSet(ToolConstants.JMS_ADDR_JNDI_DEST)) {
190: jmsAddress.setJndiDestinationName((String) env
191: .get(ToolConstants.JMS_ADDR_JNDI_DEST));
192: }
193: if (env.optionSet(ToolConstants.JMS_ADDR_JNDI_FAC)) {
194: jmsAddress
195: .setJndiConnectionFactoryName((String) env
196: .get(ToolConstants.JMS_ADDR_JNDI_FAC));
197: }
198: if (env.optionSet(ToolConstants.JMS_ADDR_JNDI_URL)) {
199: jmsAddress.setJndiProviderURL((String) env
200: .get(ToolConstants.JMS_ADDR_JNDI_URL));
201: }
202: if (env
203: .optionSet(ToolConstants.JMS_ADDR_MSGID_TO_CORRID)) {
204: jmsAddress
205: .setUseMessageIDAsCorrelationID(Boolean
206: .getBoolean((String) env
207: .get(ToolConstants.JMS_ADDR_MSGID_TO_CORRID)));
208: }
209: if (env
210: .optionSet(ToolConstants.JMS_ADDR_SUBSCRIBER_NAME)) {
211: jmsAddress
212: .setDurableSubscriberName((String) env
213: .get(ToolConstants.JMS_ADDR_SUBSCRIBER_NAME));
214: }
215: } catch (WSDLException wse) {
216: Message msg = new Message(
217: "FAIL_TO_CREATE_SOAP_ADDRESS", LOG);
218: throw new ToolException(msg, wse);
219: }
220: port.addExtensibilityElement(jmsAddress);
221: }
222: }
223:
224: }
|