1
2
3
4
5
6
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
28
29
30
31
32
33
34
35
36
37 public final class CommareaFormatter implements MessageFormatter {
38
39
40
41
42 private static final Logger LOG
43 = LoggerFactory.getLogger(CommareaFormatter.class);
44
45
46
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
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
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 }