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.Enumeration;
14  import java.util.Properties;
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  /**
27   * Serializer for the Jbi4Ejb WSDL Extension (types 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 Jbi4EjbTypesSerializer implements ExtensionSerializer {
33  
34      /**
35       * The logger for this class and its instances.
36       */
37      private static final Logger LOG
38              = LoggerFactory.getLogger(Jbi4EjbTypesSerializer.class);
39      
40      /**
41       * Instantiates a new jbi4 ejb types serializer.
42       */
43      public Jbi4EjbTypesSerializer() {}    
44  
45  
46      /**
47       * Marshall the Jbi4EJB extensibility element in a WSDL fragment.
48       * 
49       * @param parentType
50       *            the parent type
51       * @param elementType
52       *            the element type
53       * @param extension
54       *            the estensibility element
55       * @param pw
56       *            the Writer
57       * @param def
58       *            the WSDL definition
59       * @param extReg
60       *            the extension registry
61       * 
62       * @throws WSDLException
63       *             if some problem occurs in marshalling
64       */
65      public void marshall(Class parentType, QName elementType,
66              ExtensibilityElement extension, java.io.PrintWriter pw, Definition def, ExtensionRegistry extReg)
67      throws WSDLException {
68  
69          // Gets the QN prefix
70          String prefix = DOMUtils.getPrefix(Jbi4EjbExtension.NS_URI_JBI4EJB, def);
71  
72          // If prefix is null, adds it
73          if (prefix == null) {
74              prefix = Jbi4EjbExtension.DEFAULT_PREFIX;
75              // Adds the namespace
76              def.addNamespace(Jbi4EjbExtension.DEFAULT_PREFIX, Jbi4EjbExtension.NS_URI_JBI4EJB);
77          }
78  
79          prefix += ":";
80  
81          LOG.debug("prefix found: " + prefix);
82  
83          if (extension instanceof Jbi4EjbTypes) {
84              Jbi4EjbTypes jbi4EjbTypes = (Jbi4EjbTypes) extension;
85              pw.print("<" + prefix + Jbi4EjbExtension.TYPES_ELEMENT);
86              pw.print(">\n");
87  
88              // TYPES
89              printTypesElement(jbi4EjbTypes, pw, prefix);
90              
91              pw.print("</" + prefix + Jbi4EjbExtension.TYPES_ELEMENT+">\n");
92  
93          } else {
94          	LOG.warn("EJB000401_Error_in_extension_element", new Object[]{Jbi4EjbExtension.TYPES_ELEMENT});
95          }
96  
97      }
98  
99      /**
100      * Prints-out the Types element.
101      * 
102      * @param pw the writer
103      * @param prefix the namepsace prefix
104      * @param jbi4EjbBiTypes the extensibility element
105      */
106     private void printTypesElement(Jbi4EjbTypes jbi4EjbBiTypes, java.io.PrintWriter pw, String prefix) {
107         if ((jbi4EjbBiTypes.getTypesSerialVersionUIDs() != null) && 
108                 (!jbi4EjbBiTypes.getTypesSerialVersionUIDs().isEmpty())) {            
109             Properties typesProperties = jbi4EjbBiTypes.getTypesSerialVersionUIDs();
110             Enumeration enKeys = typesProperties.keys();
111             for (; enKeys.hasMoreElements() ;) {
112                 String key = (String)enKeys.nextElement();
113                 Object suid = typesProperties.get(key);
114                 String prop = null;
115                 if (suid instanceof Long) {
116                     prop = ((Long)suid).toString();
117                 } else {
118                     prop = (String)suid;
119                 }                
120                 pw.print("<" + prefix + Jbi4EjbExtension.SERIAL_VERSION_UID_ELEMENT);
121                 DOMUtils.printAttribute(Jbi4EjbExtension.CLASSNAME_ATTRIBUTE, key, pw);
122                 DOMUtils.printAttribute(Jbi4EjbExtension.UID_ATTRIBUTE, prop, pw);
123                 pw.print("/>\n");
124             }           
125         } 
126     }    
127 }