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  
11  import it.imolinfo.jbi4ejb.Logger;
12  import it.imolinfo.jbi4ejb.LoggerFactory;
13  
14  import java.util.Enumeration;
15  import java.util.Properties;
16  
17  import javax.wsdl.Definition;
18  import javax.wsdl.WSDLException;
19  import javax.wsdl.extensions.ExtensibilityElement;
20  import javax.wsdl.extensions.ExtensionRegistry;
21  import javax.wsdl.extensions.ExtensionSerializer;
22  import javax.xml.namespace.QName;
23  
24  import com.ibm.wsdl.util.xml.DOMUtils;
25  
26  /**
27   * Serializer for the Jbi4Ejb WSDL Extension (binding element), according with JWSDL specs.
28   * See JSR 110.
29   *
30   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a>
31   */
32  public class Jbi4EjbBindingSerializer implements ExtensionSerializer {
33  
34      /**
35       * The logger for this class and its instances.
36       */
37      private static final Logger LOG
38              = LoggerFactory.getLogger(Jbi4EjbBindingSerializer.class);
39  
40      
41      /**
42       * Instantiates a new jbi4 ejb binding serializer.
43       */
44      public Jbi4EjbBindingSerializer() {}
45      
46      /*
47       * (non-Javadoc)
48       *
49       * @see javax.wsdl.extensions.ExtORB_ELEMENTensionDeserializer#unmarshall(java.lang.Class,
50       *      javax.xml.namespace.QName, org.w3c.dom.Element,
51       *      javax.wsdl.Definition, javax.wsdl.extensions.ExtensionRegistry)
52       */
53      
54      /**
55       * Marshall the <code>Jbi4EjbBinding</code> in a WSDL binding fragment.
56       * 
57       * @param parentType the parent type
58       * @param elementType the element type
59       * @param extension the <code>Jbi4EjbBinding</code> extension
60       * @param pw the writer
61       * @param def the WSDL definition
62       * @param extReg the ExtensionRegistry
63       * 
64       * @throws WSDLException if some problem occurs in marshalling
65       */
66      @SuppressWarnings("unchecked")
67      public void marshall(Class parentType, QName elementType,
68              ExtensibilityElement extension, java.io.PrintWriter pw, Definition def, ExtensionRegistry extReg)
69      throws WSDLException {
70  
71          // Gets the QN prefix
72          String prefix = DOMUtils.getPrefix(Jbi4EjbExtension.NS_URI_JBI4EJB, def);
73  
74          // If prefix is null, adds it
75          if (prefix == null) {
76              prefix = Jbi4EjbExtension.DEFAULT_PREFIX;
77              // Adds the namespace
78              def.addNamespace(Jbi4EjbExtension.DEFAULT_PREFIX, Jbi4EjbExtension.NS_URI_JBI4EJB);
79          }
80  
81          prefix += ":";
82  
83          LOG.debug("prefix found: " + prefix);
84  
85          if (extension instanceof Jbi4EjbBinding) {
86              Jbi4EjbBinding jbi4EjbBinding = (Jbi4EjbBinding) extension;
87              pw.print("<" + prefix + Jbi4EjbExtension.BINDING_ELEMENT);
88              pw.print(">\n");
89  
90              // IDL
91              printJNDIElement(jbi4EjbBinding, pw, prefix);
92  
93              // ORB Properties
94              printORBElement(jbi4EjbBinding, pw, prefix);
95  
96              pw.print("</" + prefix + Jbi4EjbExtension.BINDING_ELEMENT+">\n");
97  
98          } else {
99          	LOG.warn("EJB000401_Error_in_extension_element", new Object[]{Jbi4EjbExtension.BINDING_ELEMENT});
100         }
101 
102     }
103 
104     /**
105      * Prints the JNDI element.
106      * 
107      * @param jbi4EjbBinding the binding element
108      * @param pw the writer
109      * @param prefix the namespace prefix
110      */
111     @SuppressWarnings("unchecked")
112     private void printJNDIElement(Jbi4EjbBinding jbi4EjbBinding, java.io.PrintWriter pw, String prefix) {
113         if ((jbi4EjbBinding.getJndiProperties()!= null) && 
114                 (!jbi4EjbBinding.getJndiProperties().isEmpty())) {
115             pw.print("<" + prefix + Jbi4EjbExtension.JNDI_ELEMENT+">\n");
116             Properties jndiProperties = jbi4EjbBinding.getJndiProperties();
117             Enumeration enKeys = jndiProperties.keys();
118             for (; enKeys.hasMoreElements() ;) {
119                 String key = (String)enKeys.nextElement();
120                 String prop = (String) jndiProperties.get(key);
121                 pw.print("<" + prefix + Jbi4EjbExtension.PROPERTY_ELEMENT);
122                 DOMUtils.printAttribute(Jbi4EjbExtension.NAME_ATTRIBUTE, key, pw);
123                 DOMUtils.printAttribute(Jbi4EjbExtension.VALUE_ATTRIBUTE, prop, pw);
124                 pw.print("/>\n");
125             }
126 
127             pw.print("</" + prefix + Jbi4EjbExtension.JNDI_ELEMENT+">\n");
128         } 
129     }
130 
131     /**
132      * Prints the ORB element.
133      * 
134      * @param jbi4EjbBinding the binding element
135      * @param pw the writer
136      * @param prefix the namespace prefix
137      */
138     @SuppressWarnings("unchecked")
139     private void printORBElement(Jbi4EjbBinding jbi4EjbBinding, java.io.PrintWriter pw, String prefix) {
140         if (jbi4EjbBinding.getOrbProperties()!= null) {
141             pw.print("<" + prefix + Jbi4EjbExtension.ORB_ELEMENT+">\n");
142             Properties orbProperties = jbi4EjbBinding.getOrbProperties();
143             Enumeration enKeys = orbProperties.keys();
144             for (; enKeys.hasMoreElements() ;) {
145                 String key = (String)enKeys.nextElement();
146                 String prop = (String) orbProperties.get(key);
147                 pw.print("<" + prefix + Jbi4EjbExtension.PROPERTY_ELEMENT);
148                 DOMUtils.printAttribute(Jbi4EjbExtension.NAME_ATTRIBUTE, key, pw);
149                 DOMUtils.printAttribute(Jbi4EjbExtension.VALUE_ATTRIBUTE, prop, pw);
150                 pw.print("/>\n");
151             }
152 
153             pw.print("</" + prefix + Jbi4EjbExtension.ORB_ELEMENT+">\n");
154         }    
155     }
156 }