View Javadoc
1   /////////////////////////////////////////////////////////////////////
2   //
3   // (c) University of Southampton IT Innovation Centre, 2005
4   //
5   // Copyright in this software belongs to the IT Innovation Centre of
6   // 2 Venture Road, Chilworth Science Park, Southampton SO16 7NP, UK.
7   //
8   // This software may not be used, sold, licensed, transferred, copied
9   // or reproduced in whole or in part in any manner or form or in or
10  // on any media by any person other than in accordance with the terms
11  // of the Licence Agreement supplied with the software, or otherwise
12  // without the prior written consent of the copyright owners.
13  //
14  // This software is distributed WITHOUT ANY WARRANTY, without even the
15  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16  // PURPOSE, except where stated in the Licence Agreement supplied with
17  // the software.
18  //
19  //  Modified By :          Nikolaos Matskanis
20  //  Modified Date :        23/02/2007
21  //  Modified for Project : NextGRID
22  //
23  /////////////////////////////////////////////////////////////////////////
24  //
25  //     Dependencies : Based on code from Mindswap under the MIT License
26  //
27  /////////////////////////////////////////////////////////////////////////
28  // The MIT License
29  //
30  // Copyright (c) 2004 Evren Sirin
31  //
32  // Permission is hereby granted, free of charge, to any person obtaining a copy
33  // of this software and associated documentation files (the "Software"), to
34  // deal in the Software without restriction, including without limitation the
35  // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
36  // sell copies of the Software, and to permit persons to whom the Software is
37  // furnished to do so, subject to the following conditions:
38  //
39  // The above copyright notice and this permission notice shall be included in
40  // all copies or substantial portions of the Software.
41  //
42  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
45  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47  // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
48  // IN THE SOFTWARE.
49  
50  package uk.ac.soton.itinnovation.wsdlutils;
51  
52  import java.net.URI;
53  import java.util.ArrayList;
54  import java.util.HashMap;
55  import java.util.Iterator;
56  import java.util.List;
57  import java.util.Map;
58  import java.util.Vector;
59  
60  import javax.wsdl.Binding;
61  import javax.wsdl.Message;
62  import javax.wsdl.Operation;
63  import javax.wsdl.Port;
64  import javax.wsdl.Service;
65  import javax.wsdl.extensions.soap.SOAPAddress;
66  import javax.xml.namespace.QName;
67  
68  import org.apache.axis.client.Call;
69  import org.apache.axis.EngineConfiguration;
70  import org.apache.axis.client.AxisClient;
71  import org.apache.axis.message.SOAPHeaderElement;
72  import org.apache.axis.configuration.FileProvider;
73  import org.apache.axis.wsdl.gen.Parser;
74  import org.apache.axis.wsdl.symbolTable.BindingEntry;
75  import org.apache.axis.wsdl.symbolTable.Parameter;
76  import org.apache.axis.wsdl.symbolTable.Parameters;
77  import org.apache.axis.wsdl.symbolTable.ServiceEntry;
78  import org.apache.axis.wsdl.symbolTable.SymTabEntry;
79  import org.apache.axis.wsdl.symbolTable.SymbolTable;
80  import org.w3c.dom.Node;
81  
82  
83  /**
84   * Represents a WSDL service
85   * 
86   * @author Evren Sirin
87   */
88  public class WSDLService { 
89  
90      public static boolean DEBUG = false;
91      //private boolean USE_TCPMON = false;
92  
93      private Parser wsdlParser = null;
94      private URI uri = null; 
95      
96      private Map operations = new HashMap();
97  		
98      private EngineConfiguration config=null;
99      private AxisClient engine=null;
100     //private ArrayList<SOAPHeaderElement> params=null;
101 
102     public WSDLService(URI wsdlURL) throws Exception {
103             this(wsdlURL/*, null*/,null);
104     }
105 
106     public WSDLService(URI wsdlURL/*, SOAPHeaderElement param*/, String conf) throws Exception {
107 	uri = wsdlURL;
108         //params=new ArrayList<SOAPHeaderElement>();
109         //if (param!=null)addHeader(param);
110         if (conf!=null)setConfiguration(conf);
111 
112         // Start by reading in the WSDL using Parser
113         wsdlParser = new Parser();
114         if(DEBUG) wsdlParser.setVerbose(true);
115         String strURL=wsdlURL.toString();
116         if (strURL.endsWith("?wsdl")); else strURL=strURL+"?wsdl";
117         wsdlParser.run(strURL);
118         
119         if(DEBUG) System.out.println("Reading operations");
120 	readOperations();        
121     }
122 
123         public void setConfiguration(String confFile){ 
124                 if (confFile!=null){ 
125                         config=new FileProvider(confFile); 
126                         engine=new AxisClient(config);
127                 }
128         }
129         
130 
131      /*   public void addHeader(SOAPHeaderElement param){ 
132                 params.add(param);
133         }*/
134 
135 	static public WSDLService createService(String wsdlLoc) throws Exception {
136 	    return createService(URI.create(wsdlLoc),null);
137         }
138 	static public WSDLService createService(String wsdlLoc, String conf) throws Exception {
139 	    return createService(URI.create(wsdlLoc),conf);
140 	}
141 	
142 	static public WSDLService createService(URI wsdlLoc, String conf) throws Exception {
143 		return new WSDLService(wsdlLoc,conf);
144 	} 	
145 	
146 	private String createURI(QName qname) {
147 	    return qname.getNamespaceURI() + "#" + qname.getLocalPart();
148 	}
149 	
150 	private String createURI(String localName) {
151 	    return uri + "#" + localName;
152 	}
153 
154 	private void readOperations() {
155 		try {
156         String serviceNS = null;
157         String serviceName = null;
158         String operationName = null;
159         String portName = null;
160         
161         Service service = selectService(serviceNS, serviceName);
162         org.apache.axis.client.Service dpf = 
163         	new org.apache.axis.client.Service(wsdlParser, service.getQName());
164         if (engine!=null){
165                 dpf.setEngine(engine);
166         }
167 
168         Port port = selectPort(service.getPorts(), portName);
169         if (portName == null) {
170             portName = port.getName();
171         }
172         Binding binding = port.getBinding();
173 
174         SymbolTable symbolTable = wsdlParser.getSymbolTable();
175         BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName());
176         Parameters parameters = null;
177         Iterator i = bEntry.getParameters().keySet().iterator();
178 
179         while (i.hasNext()) {
180             Operation o = (Operation) i.next();
181             operationName = o.getName(); 
182             
183             Call call = (Call) dpf.createCall(QName.valueOf(portName), QName.valueOf(operationName)); 
184             
185             /*for (int par=0; par< params.size();par++){
186                     call.addHeader(params.get(par));
187             }*/
188 
189 	    WSDLOperation op = new WSDLOperation(call);
190 	    op.setService(this);
191 	    
192 	    operations.put(operationName, op);
193 
194 	    Message inputMessage = o.getInput().getMessage(); 
195             Message outputMessage = o.getOutput().getMessage();
196 		        		    
197 	    op.setOperationName(createURI(operationName));
198 	    op.setInputMessageName(createURI(inputMessage.getQName()));
199 	    op.setOutputMessageName(createURI(outputMessage.getQName()));
200 	    op.setPortName(createURI(port.getName()));	
201 		    
202 	    if(DEBUG) {
203 	    	System.out.println(" Operation : " + operationName+", "+ call.getOperationName());
204 	    	System.out.println(" Port      : " + portName + " -> " + op.getPortName());
205 	    	System.out.println(" Input Msg : " + inputMessage.getQName() + " -> " + op.getInputMessageName());
206 	    	System.out.println(" Output Msg: " + outputMessage.getQName() + " -> " + op.getOutputMessageName());
207 	    }		    
208 	
209 		    
210 	    if(o.getDocumentationElement() != null) {
211 	    	Node doc = o.getDocumentationElement().getFirstChild(); 
212 	    	if(doc != null) op.setDocumentation(doc.getNodeValue());
213             }
214 		    
215             parameters = (Parameters) bEntry.getParameters().get(o);
216 
217 	        // loop over parameters and set up in/out params
218 	        for (int j = 0; j < parameters.list.size(); ++j) {
219 	            Parameter p = (Parameter) parameters.list.get(j);
220 				String name = createURI(p.getName());
221 				QName type = p.getType().getQName();				
222 	
223 	            if (p.getMode() == Parameter.IN) {           // IN	                
224 					op.addInput(name, type);
225 
226 					if(DEBUG) 
227 						System.out.println(" Input     : " + name + " " +  type);
228 	            } else if (p.getMode() == Parameter.OUT) {    // OUT
229 					op.addOutput(name, type);
230 
231 					if(DEBUG) 
232 						System.out.println(" Output    : " + name + " " +  type);
233 	            } else if (p.getMode() == Parameter.INOUT) {    // INOUT
234 					op.addInput(name, type);
235 					op.addOutput(name, type);
236 					System.err.println("WARNING: A wsdl parameter is defined as INOUT is not tested yet");
237 					System.err.println("         Parameter = " + name);
238 					
239 					if(DEBUG)
240 						System.out.println(" InOut     : " + name + " " +  type);
241 	            }
242 	            
243 	        }
244 	
245 	        // set output type
246 	        if (parameters.returnParam != null) {
247 	        	Parameter p = parameters.returnParam;
248 				String name = createURI(p.getName());
249 				QName type = p.getType().getQName();
250 				
251 				op.addOutput(name, type);
252 
253 				if(DEBUG) 
254 					System.out.println(" Return    : " + name + " " +  type);
255 	        }
256 	        
257 			if(DEBUG) {
258 				System.out.println(" Inputs    : " + op.getInputs().size());
259 				System.out.println(" Outputs   : " + op.getOutputs().size());
260 				System.out.println(" Document  : " + op.getDocumentation());
261 				System.out.println();
262 			}	        
263         }
264 
265 		} catch(Exception e) {
266 			System.err.println(e);
267 			e.printStackTrace();
268 		}
269     }
270 
271 	private Service selectService(String serviceNS, String serviceName)
272             throws Exception {
273         QName serviceQName = (((serviceNS != null)
274                 && (serviceName != null))
275                 ? new QName(serviceNS, serviceName)
276                 : null);
277         ServiceEntry serviceEntry = (ServiceEntry) getSymTabEntry(serviceQName,
278                                                                   ServiceEntry.class);
279         return serviceEntry.getService();
280     }
281 
282     private SymTabEntry getSymTabEntry(QName qname, Class cls) {
283         HashMap map = wsdlParser.getSymbolTable().getHashMap();
284         Iterator iterator = map.entrySet().iterator();
285 
286         while (iterator.hasNext()) {
287             Map.Entry entry = (Map.Entry) iterator.next();
288             Vector v = (Vector) entry.getValue();
289 
290             if ((qname == null) || qname.equals(qname)) {
291                 for (int i = 0; i < v.size(); ++i) {
292                     SymTabEntry symTabEntry = (SymTabEntry) v.elementAt(i);
293 
294                     if (cls.isInstance(symTabEntry)) {
295                         return symTabEntry;
296                     }
297                 }
298             }
299         }
300         return null;
301     }
302 
303     private Port selectPort(Map ports, String portName) throws Exception {
304         Iterator valueIterator = ports.keySet().iterator();
305         while (valueIterator.hasNext()) {
306             String name = (String) valueIterator.next();
307 
308             if ((portName == null) || (portName.length() == 0)) {
309                 Port port = (Port) ports.get(name);
310                 List list = port.getExtensibilityElements();
311 
312                 for (int i = 0; (list != null) && (i < list.size()); i++) {
313                     Object obj = list.get(i);
314                     if (obj instanceof SOAPAddress) {
315                         return port;
316                     }
317                 }
318             } else if ((name != null) && name.equals(portName)) {
319                 return (Port) ports.get(name);
320             }
321         }
322         return null;
323     }
324 	
325 	public URI getFileURI() {
326 		return uri;
327 	}
328 	
329 	public List getOperations() {
330 		return new ArrayList(operations.values());
331 	}
332 	
333 	public WSDLOperation getOperation(String opName) {
334 		return (WSDLOperation) operations.get(opName);
335 	}
336 }