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   
9   
10  package it.imolinfo.jbi4cics.jbi.wsdl;
11  
12  import static it.imolinfo.jbi4cics.jbi.wsdl.Jbi4CicsExtension.BINDING_ELEMENT;
13  import static it.imolinfo.jbi4cics.jbi.wsdl.Jbi4CicsExtension.CODE_PAGE_NAME_ATTRIBUTE;
14  import static it.imolinfo.jbi4cics.jbi.wsdl.Jbi4CicsExtension.COPY_COBOL_ELEMENT;
15  import static it.imolinfo.jbi4cics.jbi.wsdl.Jbi4CicsExtension.DEFAULT_PREFIX;
16  import static it.imolinfo.jbi4cics.jbi.wsdl.Jbi4CicsExtension.NS_URI_JBI4CICS;
17  import static it.imolinfo.jbi4cics.jbi.wsdl.Jbi4CicsExtension.OUTPUT_COPY_COBOL_ELEMENT;
18  import static it.imolinfo.jbi4cics.jbi.wsdl.Jbi4CicsExtension.SAME_COPY_COBOL_ATTRIBUTE;
19  import static it.imolinfo.jbi4cics.jbi.wsdl.Jbi4CicsExtension.SERVICE_PACKAGE_NAME_ATTRIBUTE;
20  import it.imolinfo.jbi4cics.Logger;
21  import it.imolinfo.jbi4cics.LoggerFactory;
22  import it.imolinfo.jbi4cics.jbi.Messages;
23  import java.io.PrintWriter;
24  import javax.wsdl.Definition;
25  import javax.wsdl.WSDLException;
26  import javax.wsdl.extensions.ExtensibilityElement;
27  import javax.wsdl.extensions.ExtensionRegistry;
28  import javax.wsdl.extensions.ExtensionSerializer;
29  import javax.xml.namespace.QName;
30  import com.ibm.wsdl.util.xml.DOMUtils;
31  
32  /**
33   * Serializer for the address elemento of Jbi4Cics WSDL Extension, according
34   * with JWSDL specs. See JSR 110.
35   * <p>
36   *
37   * @author amedeocannone
38   * @author marcopiraccini
39   * @author <a href="mailto:mcimatti@imolinfo.it">Marco Cimatti</a>
40   */
41  public class Jbi4CicsBindingSerializer implements ExtensionSerializer {
42  
43      /**
44       * The logger for this class and its instances.
45       */
46      private static final Logger LOG
47              = LoggerFactory.getLogger(Jbi4CicsBindingSerializer.class);
48  
49      /**
50       * The responsible to translate localized messages.
51       */
52      private static final Messages MESSAGES
53              = Messages.getMessages(Jbi4CicsBindingSerializer.class);
54  
55      /**
56       * Initializes a new instance of this class.
57       */
58      Jbi4CicsBindingSerializer() {
59      }
60  
61      /**
62       * Serializes Jbi4Cics extension-specific instances of
63       * <code>ExtensibilityElement</code> into the specified
64       * <code>PrintWriter</code>.
65       *
66       * @param   parentType     a class object indicating where in the WSDL
67       *                         definition this extension was encountered. For
68       *                         example, <code>javax.wsdl.Binding.class</code>
69       *                         would be used to indicate this extensibility
70       *                         element was found in the list of extensibility
71       *                         elements belonging to a
72       *                         <code>javax.wsdl.Binding</code>.
73       * @param   elementType    the qname of the extensibility element.
74       * @param   extension      the extensibility element to serialize.
75       * @param   writer         the writer to write to.
76       * @param   def            the definition this extensibility element was
77       *                         encountered in.
78       * @param   extReg         the <code>ExtensionRegistry</code> to use (if
79       *                         needed again).
80       * @throws  WSDLException  if <code>extension</code> is a
81       *                         {@link Jbi4CicsBinding} instance but doesn't
82       *                         contain mandatory values.
83       */
84      public void marshall(Class parentType, QName elementType,
85              ExtensibilityElement extension, PrintWriter writer,
86              Definition def, ExtensionRegistry extReg) throws WSDLException {
87          String prefix = null;
88  
89          // Gets the QN prefix
90          try {
91              prefix = DOMUtils.getPrefix(NS_URI_JBI4CICS, def);
92          } catch (WSDLException e) {
93              LOG.warn("CIC001300_Jbi4cics_namespace_not_found",
94                       new Object[] { NS_URI_JBI4CICS }, e);
95          }
96  
97          // If prefix (i.e. the namespace) is null, adds it
98          if (prefix == null) {
99              prefix = DEFAULT_PREFIX;
100             def.addNamespace(DEFAULT_PREFIX, NS_URI_JBI4CICS);
101         }
102         prefix = prefix.concat(":");
103 
104         if (LOG.isDebugEnabled()) {
105             LOG.debug("Prefix found: " + prefix);
106         }
107 
108         if (extension instanceof Jbi4CicsBinding) {
109             Jbi4CicsBinding binding = (Jbi4CicsBinding) extension;
110 
111             doMarshall(binding, writer, prefix);
112         } else {
113             LOG.warn("CIC001308_Invalid_extension_element", BINDING_ELEMENT);
114         }
115     }
116 
117     /**
118      * Serializes Jbi4Cics extension-specific instances of
119      * <code>Jbi4CicsBinding</code> into the specified
120      * <code>PrintWriter</code>.
121      *
122      * @param   binding        the extensibility element to serialize. Must be
123      *                         not <code>null</code>.
124      * @param   writer         the writer to write to. Must be not
125      *                         <code>null</code>.
126      * @param   prefix         the namespace prefix to use for WSDL tag. Must be
127      *                         not <code>null</code>.
128      * @throws  WSDLException  if <code>binding</code> doesn't contain mandatory
129      *                         values.
130      */
131     private void doMarshall(Jbi4CicsBinding binding, PrintWriter writer,
132             String prefix) throws WSDLException {
133         Boolean sameCopyCobol = binding.getSameCopyCobol();
134         String string;
135 
136         writer.print("<" + prefix + BINDING_ELEMENT);
137 
138         // servicePackageName
139         string = binding.getServicePackageName();
140         if (string != null) {
141             DOMUtils.printAttribute(SERVICE_PACKAGE_NAME_ATTRIBUTE, string,
142                                     writer);
143         } else {
144             throw createWSDLException("CIC001310_Invalid_service_package_name");
145         }
146 
147         // codePage
148         string = binding.getCodePage();
149         if (string != null) {
150             DOMUtils.printAttribute(CODE_PAGE_NAME_ATTRIBUTE, string, writer);
151         } else {
152             throw createWSDLException("CIC001311_Invalid_code_page");
153         }
154 
155         // sameCopyCobol
156         if (sameCopyCobol != null) {
157             DOMUtils.printAttribute(SAME_COPY_COBOL_ATTRIBUTE,
158                                     sameCopyCobol.toString(), writer);
159         }
160         writer.print(">\n");
161 
162         // copyCobol
163         string = binding.getCopyCobol();
164         if (string == null) {
165             throw createWSDLException("CIC001312_Invalid_copy_cobol");
166         }
167         writer.print("<" + prefix + COPY_COBOL_ELEMENT + ">"
168                      + DOMUtils.cleanString(string) + "</" + prefix
169                      + COPY_COBOL_ELEMENT + ">\n");
170 
171         // outputCopyCobol
172         string = binding.getOutputCopyCobol();
173         if ((string == null) || (string.trim().length() == 0)) {
174             if (Boolean.FALSE.equals(sameCopyCobol)) {
175                 throw createWSDLException("CIC001313_Invalid_copy_cobol");
176             }
177         } else {
178             writer.print("<" + prefix + OUTPUT_COPY_COBOL_ELEMENT + ">"
179                          + DOMUtils.cleanString(string) + "</" + prefix
180                          + OUTPUT_COPY_COBOL_ELEMENT + ">\n");
181         }
182 
183         writer.print("</" + prefix + BINDING_ELEMENT + ">\n");
184     }
185 
186     /**
187      * Creates a new <code>WSDLException</code> with the specific error message,
188      * logging also the error message itself.
189      *
190      * @param   msgKey  the error message bundle key. Must be not
191      *                  <code>null</code>.
192      * @return  the newly created <code>WSDLException</code> containing the
193      *          error message retrieved using the received key.
194      */
195     private static WSDLException createWSDLException(final String msgKey) {
196         LOG.error(msgKey);
197         return new WSDLException(WSDLException.INVALID_WSDL,
198                                  MESSAGES.getString(msgKey));
199     }
200 }