View Javadoc

1   /*
2    *  Copyright (c) 2005, 2006 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.messageformat.commarea;
11  
12  import it.imolinfo.jbi4cics.Logger;
13  import it.imolinfo.jbi4cics.LoggerFactory;
14  import it.imolinfo.jbi4cics.exception.FormatException;
15  import it.imolinfo.jbi4cics.messageformat.FieldDescriptor;
16  import it.imolinfo.jbi4cics.messageformat.MappingDescriptor;
17  import it.imolinfo.jbi4cics.messageformat.MessageFormatter;
18  import it.imolinfo.jbi4cics.service.ServiceContext;
19  import it.imolinfo.jbi4cics.typemapping.cobol.CobolFieldFormatter;
20  import it.imolinfo.jbi4cics.typemapping.cobol.CobolTypeDescriptor;
21  import it.imolinfo.jbi4cics.typemapping.cobol.HexDump;
22  import java.util.Map;
23  import org.apache.commons.beanutils.ConvertingWrapDynaBean;
24  import org.apache.commons.beanutils.WrapDynaBean;
25  
26  /**
27   * Questa classe serve a formattare una commarea secondo quanto definito nel
28   * CommareaBeanMappingDescriptor known limitation: ad oggi non sono supportati i
29   * filler e i nested bean known bugs: ci sono dei problemi di conversione da e
30   * verso Float e Double (temo ineliminabili perche' dovuti alla limitata
31   * precisione del formato dati) c'รจ un problema di conversione con BigDecimal
32   * non meglio identificato, vedere SimpleCommareaMappingTest.
33   *
34   * @author raffaele
35   * @author <a href="mailto:mcimatti@imolinfo.it">Marco Cimatti</a>
36   */
37  public final class CommareaFormatter implements MessageFormatter {
38  
39      /**
40       * The logger for this class and its instances.
41       */
42      private static final Logger LOG
43              = LoggerFactory.getLogger(CommareaFormatter.class);
44  
45      /**
46       * Initializes a new instance of this class.
47       */
48      public CommareaFormatter() {
49      }
50  
51      public void mapInputBeanToInputMessage(ServiceContext context)
52              throws FormatException {
53          MappingDescriptor desc = context.getInputMappingDescriptor();
54          WrapDynaBean bean = new WrapDynaBean(context.getInputBean());
55          byte[] buffer;
56          int offset = 0;
57  
58          // If we're here then we must have a CommareaBeanMappingDescriptor
59          if (!(desc instanceof CommareaBeanMappingDescriptor)) {
60              LOG.error("CIC001702_Expected_commarea_bean_mapping_descriptor",
61                        desc.getClass());
62              throw new FormatException(
63                      "CIC001702_Expected_commarea_bean_mapping_descriptor",
64                      new Object[] { desc.getClass() });
65          }
66          buffer = new byte[
67                  ((CommareaBeanMappingDescriptor) desc).getBufferedLength()];
68  
69          for (Map.Entry<String, FieldDescriptor> entry
70                  : desc.getFieldMap().entrySet()) {
71              CobolTypeDescriptor ctDesc = (CobolTypeDescriptor) entry.getValue();
72              String propertyName = entry.getKey();
73              Object value = bean.get(propertyName);
74  
75              CobolFieldFormatter.format(value, buffer, ctDesc, offset);
76              offset += ctDesc.getBufferedLength();
77          }
78          context.setInputMessage(buffer);
79  
80          if (LOG.isDebugEnabled()) {
81              LOG.debug("formatted input bean: [" + context.getInputBean()
82                        + "]\nin input buffer: " + HexDump.toHex(buffer)
83                        + "\nformatted input bean: [" + context.getInputBean()
84                        + "]\nin input buffer: [" + HexDump.dump(buffer, 0, 0)
85                        + "]");
86          }
87      }
88  
89      public void mapOutputMessageToOutputBean(ServiceContext context)
90              throws FormatException {
91          MappingDescriptor desc = context.getOutputMappingDescriptor();
92          Object outputMessage = context.getOutputMessage();
93          WrapDynaBean bean;
94          byte[] buffer;
95          int offset = 0;
96  
97          // If we're here then we must have a CommareaBeanMappingDescriptor
98          if (!(desc instanceof CommareaBeanMappingDescriptor)) {
99              LOG.error("CIC001702_Expected_commarea_bean_mapping_descriptor",
100                       desc.getClass());
101             throw new FormatException(
102                     "CIC001702_Expected_commarea_bean_mapping_descriptor",
103                     new Object[] { desc.getClass() });
104         }
105 
106         if (!(outputMessage instanceof byte[])) {
107             Object[] args = { outputMessage.getClass() };
108 
109             LOG.error("CIC001703_Expected_byte_array", args);
110             throw new FormatException("CIC001703_Expected_byte_array", args);
111         }
112         buffer = (byte[]) outputMessage;
113 
114         try {
115             Object instance = desc.getBeanClass().newInstance();
116 
117             bean = new ConvertingWrapDynaBean(instance);
118         } catch (IllegalAccessException e) {
119             LOG.error(e.getLocalizedMessage(), e);
120             throw new FormatException(e);
121         } catch (InstantiationException e) {
122             LOG.error(e.getLocalizedMessage(), e);
123             throw new FormatException(e);
124         }
125         for (Map.Entry<String, FieldDescriptor> entry
126                 : desc.getFieldMap().entrySet()) {
127             CobolTypeDescriptor ctDesc = (CobolTypeDescriptor) entry.getValue();
128             String propertyName = entry.getKey();
129             Object value = CobolFieldFormatter.unformat(buffer, ctDesc, offset);
130 
131             bean.set(propertyName, value);
132             offset += ctDesc.getBufferedLength();
133         }
134         context.setOutputBean(bean.getInstance());
135 
136         if (LOG.isDebugEnabled()) {
137             LOG.debug("formatted output buffer: [" + HexDump.toHex(buffer)
138                       + "]\nin output bean: " + context.getOutputBean()
139                       + "\nformatted output buffer: ["
140                       + HexDump.dump(buffer, 0, 0) + "]\nin output bean: "
141                       + context.getOutputBean());
142         }
143     }
144 }