KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > jbi > serviceengine > util > soap > EndpointMetaData


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.jbi.serviceengine.util.soap;
24
25 import java.util.*;
26 import javax.wsdl.*;
27 import javax.wsdl.extensions.*;
28 import javax.wsdl.extensions.soap.*;
29 import java.util.logging.*;
30 import javax.xml.soap.SOAPMessage JavaDoc;
31 import javax.xml.namespace.QName JavaDoc;
32 import org.w3c.dom.NodeList JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34
35 import com.sun.logging.LogDomains;
36
37 /**
38  * Represents wsdl metadata of an endpoint. This contains
39  * partially resolved operation names, which will be compared
40  * with the soap message for getting the exact operation name.
41  *
42  * @author Binod PG
43  */

44 public class EndpointMetaData {
45     
46     /**
47      * Internal handle to the logger instance
48      */

49     protected static final Logger logger =
50             LogDomains.getLogger(LogDomains.SERVER_LOGGER);
51
52     private Definition def = null;
53     private QName JavaDoc serviceName = null;
54     private String JavaDoc epName = null;
55
56     private OperationMetaData operationMetaData; // saves the meta-data in case of only one operation per port.
57
private OperationMetaData[] opmds = null;
58
59     /**
60      * Constructor. Saves required information for resolving the operation.
61      */

62     public EndpointMetaData(Definition def, QName JavaDoc serviceName, String JavaDoc epName) {
63         this.def = def;
64         this.serviceName = serviceName;
65         this.epName = epName;
66     }
67
68     /**
69      * Constructor without service name and endpoint name.
70      * Such an endpoint metadata is just a holder object
71      * for wsdl definition object.
72      */

73     public EndpointMetaData(Definition def) {
74         this.def = def;
75     }
76
77     /**
78      * Partially resolve the operation name.
79      *
80      * 1. If there is only one operation, use that as the operation name.
81      * 2. If there is more than one operation name, save the input parameter
82      * for matching with the soap message.
83      * 3. Since we want to work with any kind of binding, we need to only
84      * consider the abstract WSDL.
85      */

86     public void resolve() {
87         if (logger.isLoggable(Level.FINE)) {
88             logger.fine("Resolving the WSDL for : " + serviceName + " : " + epName);
89         }
90         Binding binding = getBinding(def, serviceName, epName);
91         javax.wsdl.Operation[] ops = getOperations(binding);
92         if (ops.length == 1) {
93             operationMetaData = new OperationMetaData();
94             operationMetaData.setOperationName(ops[0].getName());
95             operationMetaData.setOneWay(javax.wsdl.OperationType.ONE_WAY.equals(ops[0].getStyle()));
96         } else if (ops.length > 1) {
97             opmds = new OperationMetaData[ops.length];
98             int i = 0 ;
99             for (javax.wsdl.Operation op : ops) {
100                  Message JavaDoc input = null;
101                  if (op.getInput() != null) {
102                      input = op.getInput().getMessage();
103                  }
104                  Set s = getInputPartNames(input);
105                  OperationMetaData md = new OperationMetaData();
106                  md.setOperationName(op.getName());
107                  md.setInputParameters(s);
108                  md.setOneWay(javax.wsdl.OperationType.ONE_WAY.equals(op.getStyle()));
109                  opmds[i++] = md;
110             }
111         } else {
112             //C'mon we dont have an operation ?
113
throw new RuntimeException JavaDoc("WSDL operation not resolved");
114         }
115     }
116
117     public OperationMetaData getOperationMetaData(SOAPMessage JavaDoc soapMsg) {
118         if (this.operationMetaData != null) {
119             return this.operationMetaData;
120         }
121         
122         List JavaDoc nodeNames = new ArrayList();
123         try {
124             javax.xml.soap.SOAPBody JavaDoc soapBody = soapMsg.getSOAPBody();
125             NodeList JavaDoc nl = soapBody.getChildNodes();
126
127             for (int i = 0; i < nl.getLength(); i++) {
128                 Node JavaDoc n = nl.item(i);
129
130                 if (Node.ELEMENT_NODE == n.getNodeType()) {
131                     String JavaDoc nodeName = n.getLocalName();
132                     if (nodeName != null) {
133                         nodeNames.add(nodeName);
134                     }
135                 }
136             }
137         } catch (Exception JavaDoc e) {
138             e.printStackTrace();
139         }
140
141         for (OperationMetaData om : opmds) {
142             if (logger.isLoggable(Level.FINEST)) {
143                logger.finest("Matching for" + om.getOperationName());
144             }
145             Set inputs = om.getInputParameters();
146             if (logger.isLoggable(Level.FINEST)) {
147                 logger.finest("Inputs" + inputs);
148                 logger.finest("Nodes" + nodeNames);
149             }
150             if (inputs.containsAll(nodeNames)) {
151                 return om;
152             }
153         }
154         return null;
155     }
156
157     public Definition getDefinition() {
158         return def;
159     }
160
161     private Set getInputPartNames(Message JavaDoc msg) {
162         Set set = new HashSet();
163
164         if (logger.isLoggable(Level.FINE)) {
165             logger.fine("Getting input parameters for: "+ msg);
166         }
167         if (msg != null) {
168             Iterator bodyIterator = null;
169             Map msgParts = msg.getParts();
170             if (logger.isLoggable(Level.FINE)) {
171                 logger.fine("Message Parts are: "+ msgParts);
172             }
173             if (msgParts != null) {
174                 bodyIterator = msgParts.keySet().iterator();
175             }
176             // construct a set of expected names
177
while (bodyIterator != null && bodyIterator.hasNext()) {
178                 String JavaDoc bodyPart = (String JavaDoc) bodyIterator.next();
179                 Part part = msg.getPart(bodyPart);
180                 if (part == null) {
181                     throw new IllegalStateException JavaDoc("WSDL error");
182                 }
183                 QName JavaDoc typeQName = part.getTypeName();
184                 QName JavaDoc elemQName = part.getElementName();
185
186                 if (typeQName != null) {
187                     // it uses type, so the root node name is the part name
188
set.add(part.getName());
189                     if (logger.isLoggable(Level.FINE)) {
190                         logger.fine("Added partName: "+ part.getName());
191                     }
192                 } else if (elemQName != null) {
193                     //it uses element, so the root node name is the element name
194
if (logger.isLoggable(Level.FINE)) {
195                         logger.fine("Added root node: "+ elemQName.getLocalPart());
196                         logger.fine("Part name is : " + part.getName());
197                     }
198                     set.add(elemQName.getLocalPart());
199                 }
200             }
201         }
202
203         return set;
204     }
205
206     public static javax.wsdl.Operation[] getOperations(Binding binding) {
207         if (binding != null) {
208             PortType pt = binding.getPortType();
209             if (pt != null) {
210                 List JavaDoc l = pt.getOperations();
211                 if (l != null && l.size() > 0) {
212                     return (javax.wsdl.Operation[])
213                     l.toArray(new javax.wsdl.Operation[0]);
214                 }
215             }
216         }
217         return null;
218     }
219
220     public static Binding getBinding(Definition def, QName JavaDoc serviceName, String JavaDoc endpointName) {
221         String JavaDoc location = null;
222         Service svc = def.getService(serviceName);
223         if (svc == null) {
224             return null;
225         }
226         Port port = svc.getPort(QName.valueOf(endpointName).getLocalPart());
227         if (port == null) {
228             return null;
229         } else {
230             return port.getBinding();
231         }
232     }
233
234     /**
235      * Class that holds operation and input parameters.
236      */

237     class OperationMetaData {
238         private String JavaDoc operationName;
239         private Set inputParams;
240         private boolean oneWay;
241         
242         void setOperationName(String JavaDoc name) {
243             this.operationName = name;
244         }
245
246         private void setInputParameters(Set params) {
247             this.inputParams = params;
248         }
249
250         String JavaDoc getOperationName() {
251             return this.operationName;
252         }
253         
254         private Set getInputParameters() {
255             return inputParams;
256         }
257         
258         boolean isOneWay() {
259             return oneWay;
260         }
261         
262         void setOneWay(boolean oneWay) {
263             this.oneWay = oneWay;
264         }
265     }
266 }
267
Popular Tags