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   package it.imolinfo.jbi4corba.jbi.wsdl;
9   
10  import java.util.Enumeration;
11  import java.util.Properties;
12  
13  import it.imolinfo.jbi4corba.Logger;
14  import it.imolinfo.jbi4corba.LoggerFactory;
15  
16  import javax.wsdl.Definition;
17  import javax.wsdl.WSDLException;
18  import javax.wsdl.extensions.ExtensibilityElement;
19  import javax.wsdl.extensions.ExtensionRegistry;
20  import javax.wsdl.extensions.ExtensionSerializer;
21  import javax.xml.namespace.QName;
22  
23  import com.ibm.wsdl.util.xml.DOMUtils;
24  
25  /**
26   * Serializer for the Jbi4Corba WSDL Extension (addess element), according with
27   * JWSDL specs. See JSR 110.
28   * 
29   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a>
30   */
31  public class Jbi4CorbaAddressSerializer implements ExtensionSerializer {
32  
33      /**
34       * The logger for this class and its instances.
35       */
36      private static final Logger LOG = LoggerFactory.getLogger(Jbi4CorbaAddressSerializer.class);
37  
38      /**
39       * Default constructor.
40       */
41      public Jbi4CorbaAddressSerializer() {
42      }
43  
44      /**
45       * @param parentType
46       *            The parent type
47       * @param elementType
48       *            The element tipe
49       * @param extension
50       *            The extension
51       * @param pw
52       *            The io print writer
53       * @param def
54       *            The definition
55       * @param extReg
56       *            The extension registry
57       * @throws WSDLException
58       *             The WSDL exception
59       */
60      @SuppressWarnings("unchecked")
61      public void marshall(Class parentType, QName elementType,
62              ExtensibilityElement extension, java.io.PrintWriter pw,
63              Definition def, ExtensionRegistry extReg) throws WSDLException {
64  
65          // Gets the QN prefix
66          String prefix = DOMUtils.getPrefix(Jbi4CorbaExtension.NS_URI_JBI4CORBA,
67                  def);
68          prefix += ":";
69          LOG.debug("prefix found: " + prefix);
70  
71          // If prefix is null, adds it
72          if (prefix == null) {
73              prefix = Jbi4CorbaExtension.DEFAULT_PREFIX;
74              // Adds the namespace
75              def.addNamespace(Jbi4CorbaExtension.DEFAULT_PREFIX,
76                      Jbi4CorbaExtension.NS_URI_JBI4CORBA);
77          }
78  
79          if (extension instanceof Jbi4CorbaAddress) {
80              Jbi4CorbaAddress jbi4CorbaAddress = (Jbi4CorbaAddress) extension;
81  
82              pw.print("<" + prefix + Jbi4CorbaExtension.ADDRESS_ELEMENT);
83              DOMUtils.printAttribute(Jbi4CorbaExtension.NAME_ATTRIBUTE,
84                      jbi4CorbaAddress.getName(), pw);
85              DOMUtils.printAttribute(
86                      Jbi4CorbaExtension.LOCALIZATION_TYPE_ATTRIBUTE,
87                      jbi4CorbaAddress.getLocalizationType(), pw);
88  
89              DOMUtils.printAttribute(
90                      Jbi4CorbaExtension.PERSISTENT_ATTRIBUTE,
91                      jbi4CorbaAddress.isPeristent().toString(), pw);
92  
93              pw.print(">\n");
94  
95              // ORB Properties
96              printORBElement(jbi4CorbaAddress, pw, prefix);
97  
98              pw.print("</" + prefix + Jbi4CorbaExtension.ADDRESS_ELEMENT + ">\n");
99          } else {
100             LOG.warn("CRB000300_The_extension_element_is_not_a",
101                     Jbi4CorbaExtension.ADDRESS_ELEMENT);
102         }
103 
104     }
105 
106     /**
107      * Prints the ORB element.
108      * 
109      * @param jbi4CorbaBinding
110      *            The jbi4Corba binding
111      * @param pw
112      *            The print writer
113      * @param prefix
114      *            The prefix
115      */
116     private void printORBElement(Jbi4CorbaAddress jbi4CorbaAddress,
117             java.io.PrintWriter pw, String prefix) {
118         if (jbi4CorbaAddress.getOrbProperties() != null) {
119             pw.print("<" + prefix + Jbi4CorbaExtension.ORB_ELEMENT + ">\n");
120             Properties orbProperties = jbi4CorbaAddress.getOrbProperties();
121             Enumeration<Object> enKeys = orbProperties.keys();
122             for (; enKeys.hasMoreElements();) {
123                 String key = (String) enKeys.nextElement();
124                 String prop = (String) orbProperties.get(key);
125                 pw.print("<" + prefix + Jbi4CorbaExtension.PROPERTY_ELEMENT);
126                 DOMUtils.printAttribute(Jbi4CorbaExtension.NAME_ATTRIBUTE, key,
127                         pw);
128                 DOMUtils.printAttribute(Jbi4CorbaExtension.VALUE_ATTRIBUTE,
129                         prop, pw);
130                 pw.print("/>\n");
131             }
132 
133             pw.print("</" + prefix + Jbi4CorbaExtension.ORB_ELEMENT + ">\n");
134         }
135     }
136 }