View Javadoc

1    /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/lgpl.html
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   * Jbi4Corba wsdl extensions helper class.
35   * @author marco
36   *
37   */
38  public class Jbi4CorbaExtensionUtils {
39      
40      /**
41       * The logger for this class and its instances.
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       * Default constructor.
52       */
53      public Jbi4CorbaExtensionUtils(){
54      }
55      
56      /**
57       * Removes all  The Jbi4Corba elements from a definition .
58       *
59       * @param   def  The WSDL to add SOAP elements. Must be not
60       *                         <code>null</code>.
61       * @throws  WSDLException  In case of error manipulating the
62       *                         <code>Definition</code>.
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        // removes the jbi4corba namespace        
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        // Removes the Jbi4Corba Prt and bindings        
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             // Removes the jbi4corba address
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             // Removes the jbi4corba binding
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      * Gets the <code>Binding</code>.
149      * 
150      * @param def the WSDL definition
151      * @param serviceName the service name
152      * @param endpointName the endpoint name
153      * 
154      * @return the Binding
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      * Gets the <code>Service</code>.
184      * 
185      * @param def the WSDL definition
186      * @param serviceName the service name
187      * 
188      * @return the Service
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      * Gets the <code>Jbi4CorbaBinding</code>.
203      * 
204      * @param def the WSDL definition
205      * @param serviceName the service name
206      * @param endpointName the endpoint name
207      * 
208      * @return the <code>Jbi4CorbaBinding</code>
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      * Gets the <code>Jbi4CorbaAddress</code>.
240      * 
241      * @param def the WSDL definition
242      * @param serviceName the service name
243      * @param endpointName the endpoint name
244      * 
245      * @return the <code>Jbi4CorbaAddress</code>
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      * Gets the <code>PortType</code>.
283      * 
284      * @param def the WSDL definition
285      * @param serviceName the Service Name
286      * @param endpointName the Endpoint name
287      * 
288      * @return the WSDL port type
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      * Gets the Jbi4Ejb extended WSDL writer.
317      * 
318      * @return the extended WSDL writer
319      * 
320      * @throws WSDLException
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      * Gets the WSDL string from the WSDL definition.
332      * USE THIS METHOD ONLY FOR LOGGING!
333      * 
334      * @param wsdl
335      * 
336      * @return the WSDL string from definition
337      */
338     public static String getWSDLStringFromDefinition(Definition wsdl) {
339         // Asserts...
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 }