View Javadoc
1   /*******************************************************************************
2    *  Copyright (c) 2005, 2006, 2007 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   package it.imolinfo.jbi4ejb.jbi.wsdl;
9   
10  import it.imolinfo.jbi4ejb.Logger;
11  import it.imolinfo.jbi4ejb.LoggerFactory;
12  
13  import java.util.Iterator;
14  import java.util.List;
15  
16  import javax.wsdl.Binding;
17  import javax.wsdl.Definition;
18  import javax.wsdl.Port;
19  import javax.wsdl.PortType;
20  import javax.wsdl.Service;
21  import javax.wsdl.extensions.ExtensibilityElement;
22  import javax.xml.namespace.QName;
23  
24  /**
25   * Jbi4Corba wsdl extensions helper class.
26   * 
27   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a>
28   */
29  public final class Jbi4EjbExtensionUtils {
30  
31      /** The logger for this class and its instances. */
32      private static final Logger LOG = LoggerFactory
33      .getLogger(Jbi4EjbExtensionUtils.class);
34      
35      /**
36       * Instantiates a new jbi4 ejb extension utils.
37       */
38      private Jbi4EjbExtensionUtils() {}
39  
40  
41      /**
42       * Gets the <code>Binding</code>.
43       * 
44       * @param def the WSDL definition
45       * @param serviceName the service name
46       * @param endpointName the endpoint name
47       * 
48       * @return the Binding
49       */
50      public static Binding getBinding(final Definition def,
51              final String serviceName, final String endpointName) {
52          final Service svc = def.getService(QName.valueOf(serviceName));
53  
54          if (svc == null) {
55              return null;
56          }
57  
58          final Port port = svc.getPort(endpointName);
59  
60          if (port == null) {
61              return null;
62          } else {
63              return port.getBinding();
64          }
65      }
66  
67      /**
68       * Gets the <code>PortType</code>.
69       * 
70       * @param def the WSDL definition
71       * @param serviceName the Service Name
72       * @param endpointName the Endpoint name
73       * 
74       * @return the WSDL port type
75       */
76      public static PortType getPortType(final Definition def,
77              final String serviceName, final String endpointName) {
78          final Service svc = def.getService(QName.valueOf(serviceName));
79  
80          if (svc == null) {
81              return null;
82          }
83  
84          final Port port = svc.getPort(QName.valueOf(endpointName)
85                  .getLocalPart());
86  
87          Binding binding = null;
88          if (port == null) {
89              return null;
90          } else {
91              binding = port.getBinding();
92          }
93          PortType portType = null;
94          if (binding != null) {           
95              portType = binding.getPortType();
96          }
97          return portType;
98      }    
99  
100     /**
101      * Gets the <code>Types</code> jbi4ejb extension.
102      * 
103      * @param def the WSDL definition
104      * 
105      * @return the <code>Jbi4EjbTypes</code> extensibility element
106      */
107     @SuppressWarnings("unchecked")
108     public static Jbi4EjbTypes getEjbTypes(final Definition def) {
109         
110         Jbi4EjbTypes ejbTypes = null;
111 
112         List extElems = def.getExtensibilityElements();
113         
114         Iterator extIter = null;
115         if (extElems != null) {
116             extIter = extElems.iterator();
117         }
118 
119         while ((extIter != null) && extIter.hasNext() && (ejbTypes == null)) {
120             final ExtensibilityElement ee = (ExtensibilityElement) extIter
121             .next();
122             LOG.debug("Inspecting: " + ee);
123             if (Jbi4EjbTypes.class.isInstance(ee)) {
124                 ejbTypes = (Jbi4EjbTypes) ee;
125                 LOG.debug("Found a Jbi4EjbTypes instance: " + ejbTypes.getTypesSerialVersionUIDs());
126             }
127         }
128         if (ejbTypes == null) {
129             LOG.debug("No jbi4EjbTypes instance found, returning an empty one");
130             ejbTypes = new Jbi4EjbTypes(); 
131         }
132         return ejbTypes;
133     }
134 
135     /**
136      * Gets the <code>Jbi4EjbBinding</code>.
137      * 
138      * @param def the WSDL definition
139      * @param serviceName the service name
140      * @param endpointName the endpoint name
141      * 
142      * @return the <code>Jbi4EjbBinding</code>
143      */
144     @SuppressWarnings("unchecked")
145     public static Jbi4EjbBinding getEjbBinding(final Definition def,
146             final String serviceName, final String endpointName) {
147         Jbi4EjbBinding ejbBinding = null;
148         final Binding binding = getBinding(def, serviceName, endpointName);
149 
150         if (binding != null) {
151             final List extElems = binding.getExtensibilityElements();
152 
153             Iterator extIter = null;
154             if (extElems != null) {
155                 extIter = extElems.iterator();
156             }            
157 
158             while ((extIter != null) && extIter.hasNext() &&
159                     (ejbBinding == null)) {
160                 final ExtensibilityElement ee = (ExtensibilityElement) extIter
161                 .next();
162 
163                 if (Jbi4EjbBinding.class.isInstance(ee)) {
164                     ejbBinding = (Jbi4EjbBinding) ee;
165                 }
166             }
167         }
168 
169         return ejbBinding;
170     }
171 
172     /**
173      * Gets the <code>Jbi4EjbAddress</code>.
174      * 
175      * @param def the WSDL definition
176      * @param serviceName the service name
177      * @param endpointName the endpoint name
178      * 
179      * @return the <code>Jbi4EjbAddress</code>
180      */
181     @SuppressWarnings("unchecked")
182     public static Jbi4EjbAddress getEjbAddress(final Definition def,
183             final String serviceName, final String endpointName) {
184         Jbi4EjbAddress address = null;
185         final Service svc = def.getService(QName.valueOf(serviceName));
186 
187         if (svc == null) {
188             return null;
189         }
190 
191         final Port port = svc.getPort(QName.valueOf(endpointName)
192                 .getLocalPart());
193 
194         if (port != null) {
195             final List extElems = port.getExtensibilityElements();
196 
197             Iterator extIter = null;
198             
199             if (extElems != null) {
200                 extIter = extElems.iterator();
201             }
202 
203             while ((extIter != null) && extIter.hasNext() && (address == null)) {
204                 final ExtensibilityElement ee = (ExtensibilityElement) extIter
205                 .next();
206 
207                 if (Jbi4EjbAddress.class.isInstance(ee)) {
208                     address = (Jbi4EjbAddress) ee;
209                 }
210             }
211         }
212         return address;
213     }
214                 
215 }