1
2
3
4
5
6
7
8
9 package it.imolinfo.jbi4corba.jbi.wsdl;
10
11 import it.imolinfo.jbi4corba.Logger;
12 import it.imolinfo.jbi4corba.LoggerFactory;
13 import it.imolinfo.jbi4corba.jbi.Messages;
14
15 import java.io.StringWriter;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Iterator;
19 import java.util.List;
20
21 import javax.wsdl.Binding;
22 import javax.wsdl.Definition;
23 import javax.wsdl.Port;
24 import javax.wsdl.PortType;
25 import javax.wsdl.Service;
26 import javax.wsdl.WSDLException;
27 import javax.wsdl.extensions.ExtensibilityElement;
28 import javax.wsdl.extensions.ExtensionRegistry;
29 import javax.wsdl.factory.WSDLFactory;
30 import javax.wsdl.xml.WSDLWriter;
31 import javax.xml.namespace.QName;
32
33
34
35
36
37
38 public class Jbi4CorbaExtensionUtils {
39
40
41
42
43 private static final Logger LOG
44 = LoggerFactory.getLogger(Jbi4CorbaExtensionUtils.class);
45
46 @SuppressWarnings("unused")
47 private static final Messages MESSAGES =
48 Messages.getMessages(Jbi4CorbaExtensionUtils.class);
49
50
51
52
53 public Jbi4CorbaExtensionUtils(){
54 }
55
56
57
58
59
60
61
62
63
64 @SuppressWarnings("unchecked")
65 public static void removeCorbaElements(Definition def)
66 throws WSDLException {
67 LOG.debug("removeCorbaElements - Definition=" + def);
68
69 Port port = null;
70 WSDLFactory factory = WSDLFactory.newInstance();
71
72 ExtensionRegistry registry = factory.newPopulatedExtensionRegistry();
73 Jbi4CorbaExtension.register(registry);
74
75
76 String jbi4CorbaNamespace = null;
77 for (Object namespaceKey: def.getNamespaces().keySet()) {
78 LOG.debug("namespaceKey=" + namespaceKey);
79
80 String value = (String) def.getNamespaces().get(namespaceKey);
81 LOG.debug("NameSpace[" + namespaceKey + "]=" + value);
82
83 if (value.equalsIgnoreCase(Jbi4CorbaExtension.NS_URI_JBI4CORBA)) {
84 jbi4CorbaNamespace = (String) namespaceKey;
85 LOG.debug("jbi4CorbaNamespace=" + jbi4CorbaNamespace);
86 }
87 }
88
89 if (jbi4CorbaNamespace != null) {
90 LOG.debug("FOUND jbi4corba namespace.");
91 def.getNamespaces().remove(jbi4CorbaNamespace);
92 } else {
93 LOG.debug("jbi4corba namespace NOT found.");
94 }
95
96 LOG.debug("Start removing the Jbi4Corba extensibility elements");
97
98
99 for (Object serviceAsObject : def.getServices().values()) {
100 Service service = (Service) serviceAsObject;
101 LOG.debug("Service=" + service + "; Service.QName=" + service.getQName());
102
103 for (Object obj : service.getPorts().values()) {
104 port = (Port) obj;
105 Collection<Jbi4CorbaAddress> toRemoveFromPort
106 = new ArrayList<Jbi4CorbaAddress>();
107
108 LOG.debug("Port=" + port + "; Port.name" + port.getName());
109
110 for (Object element : port.getExtensibilityElements()) {
111
112
113 if (element instanceof Jbi4CorbaAddress) {
114 LOG.debug("The element IS a Jbi4CorbaAddress. Element=" + element);
115 Jbi4CorbaAddress jbi4CorbaAddress = (Jbi4CorbaAddress) element;
116 LOG.debug("Found address (It will be removed):" + jbi4CorbaAddress);
117 toRemoveFromPort.add(jbi4CorbaAddress);
118 } else {
119 LOG.debug("The element is NOT a Jbi4CorbaAddress. Element=" + element);
120 }
121
122 Collection<Jbi4CorbaBinding> toRemoveFromBinding = new ArrayList<Jbi4CorbaBinding>();
123
124 for (Object bindingEstension : port.getBinding().getExtensibilityElements()) {
125 if (bindingEstension instanceof Jbi4CorbaBinding) {
126 LOG.debug("bindingEstension instanceof Jbi4CorbaBinding.bindingEstension=" + bindingEstension);
127 Jbi4CorbaBinding jbi4CorbaBinding = (Jbi4CorbaBinding)bindingEstension;
128 LOG.debug("Found binding (It will be removed):" + jbi4CorbaBinding);
129 toRemoveFromBinding.add(jbi4CorbaBinding);
130 } else {
131 LOG.debug("bindingEstension instanceof Jbi4CorbaBinding.bindingEstension=" + bindingEstension);
132 }
133 }
134
135 LOG.debug("toRemoveFromBinding.size=" + toRemoveFromBinding.size());
136 port.getBinding().getExtensibilityElements().removeAll(toRemoveFromBinding);
137 }
138
139 LOG.debug("toRemoveFromPort.size=" + toRemoveFromPort.size());
140 port.getExtensibilityElements().removeAll(toRemoveFromPort);
141 }
142 }
143
144 }
145
146
147
148
149
150
151
152
153
154
155
156 public static Binding getBinding(final Definition def,
157 final String serviceName, final String endpointName) {
158 final Service svc = def.getService(QName.valueOf(serviceName));
159
160 if (LOG.isDebugEnabled()) {
161 LOG.debug("Looking for service: " + serviceName + " in wsdl " + getWSDLStringFromDefinition(def));
162 }
163
164 if (svc == null) {
165 LOG.debug("CRB000301_Service_not_found_in_WSDL_for_serviceName",
166 new Object[]{serviceName});
167
168 return null;
169 }
170
171 final Port port = svc.getPort(endpointName);
172
173 if (port == null) {
174 LOG.debug("CRB000302_Port_not_found_in_WSDL_for_endpointName",
175 new Object[]{endpointName});
176 return null;
177 } else {
178 return port.getBinding();
179 }
180 }
181
182
183
184
185
186
187
188
189
190 public static Service getService(final Definition def,
191 final String serviceName) {
192 final Service svc = def.getService(QName.valueOf(serviceName));
193
194 if (svc == null) {
195 return null;
196 }
197
198 return svc;
199 }
200
201
202
203
204
205
206
207
208
209
210 @SuppressWarnings("unchecked")
211 public static Jbi4CorbaBinding getCorbaBinding(final Definition def,
212 final String serviceName, final String endpointName) {
213 Jbi4CorbaBinding corbaBinding = null;
214 final Binding binding = getBinding(def, serviceName, endpointName);
215
216 if (binding != null) {
217 final List extElems = binding.getExtensibilityElements();
218
219 Iterator extIter = null;
220 if (extElems != null) {
221 extIter = extElems.iterator();
222 }
223
224 while ((extIter != null) && extIter.hasNext() &&
225 (corbaBinding == null)) {
226 final ExtensibilityElement ee = (ExtensibilityElement) extIter
227 .next();
228
229 if (Jbi4CorbaBinding.class.isInstance(ee)) {
230 corbaBinding = (Jbi4CorbaBinding) ee;
231 }
232 }
233 }
234
235 return corbaBinding;
236 }
237
238
239
240
241
242
243
244
245
246
247 @SuppressWarnings("unchecked")
248 public static Jbi4CorbaAddress getCorbaAddress(final Definition def,
249 final String serviceName, final String endpointName) {
250 Jbi4CorbaAddress address = null;
251 final Service svc = def.getService(QName.valueOf(serviceName));
252
253 if (svc == null) {
254 return null;
255 }
256
257 final Port port = svc.getPort(QName.valueOf(endpointName)
258 .getLocalPart());
259
260 if (port != null) {
261 final List extElems = port.getExtensibilityElements();
262
263 Iterator extIter = null;
264
265 if (extElems != null) {
266 extIter = extElems.iterator();
267 }
268
269 while ((extIter != null) && extIter.hasNext() && (address == null)) {
270 final ExtensibilityElement ee = (ExtensibilityElement) extIter
271 .next();
272
273 if (Jbi4CorbaAddress.class.isInstance(ee)) {
274 address = (Jbi4CorbaAddress) ee;
275 }
276 }
277 }
278 return address;
279 }
280
281
282
283
284
285
286
287
288
289
290 public static PortType getPortType(final Definition def,
291 final String serviceName, final String endpointName) {
292 final Service svc = def.getService(QName.valueOf(serviceName));
293
294 if (svc == null) {
295 return null;
296 }
297
298 final Port port = svc.getPort(QName.valueOf(endpointName)
299 .getLocalPart());
300
301 Binding binding = null;
302 if (port == null) {
303 return null;
304 } else {
305 binding = port.getBinding();
306 }
307 PortType portType = null;
308 if (binding != null) {
309 portType = binding.getPortType();
310 }
311 return portType;
312 }
313
314
315
316
317
318
319
320
321
322 public static WSDLWriter getExtendedWSDLWriter() throws WSDLException {
323 WSDLFactory factory = WSDLFactory.newInstance();
324 WSDLWriter writer = factory.newWSDLWriter();
325 ExtensionRegistry registry = factory.newPopulatedExtensionRegistry();
326 Jbi4CorbaExtension.register(registry);
327 return writer;
328 }
329
330
331
332
333
334
335
336
337
338 public static String getWSDLStringFromDefinition(Definition wsdl) {
339
340 StringWriter strWriter = new StringWriter();
341 try {
342 getExtendedWSDLWriter().writeWSDL(wsdl, strWriter);
343 } catch (WSDLException e) {
344 LOG.warn("CRB000303_Warning_in_getting_WSDL_string_from_WSDL_definition",
345 new Object[]{e.getMessage()});
346 }
347 return strWriter.toString();
348 }
349
350
351 }