1
2
3
4
5
6
7
8
9
10 package it.imolinfo.jbi4cics.jbi;
11
12 import it.imolinfo.jbi4cics.Logger;
13 import it.imolinfo.jbi4cics.LoggerFactory;
14 import java.io.File;
15 import java.io.FileReader;
16 import java.io.IOException;
17 import javax.jbi.management.DeploymentException;
18 import org.apache.servicemix.common.BaseComponent;
19 import org.apache.servicemix.common.Endpoint;
20 import org.apache.servicemix.common.ServiceUnit;
21 import org.apache.servicemix.common.xbean.AbstractXBeanDeployer;
22
23 public final class Jbi4cicsDeployer extends AbstractXBeanDeployer {
24
25
26
27
28 private static final String TASK = "deploy";
29
30
31
32
33 private static final Logger LOG
34 = LoggerFactory.getLogger(Jbi4cicsDeployer.class);
35
36
37
38
39 private static final Messages MESSAGES
40 = Messages.getMessages(Jbi4cicsDeployer.class);
41
42
43
44
45 private String suRootPath;
46
47
48
49
50
51
52 public Jbi4cicsDeployer(final BaseComponent component) {
53 super(component);
54 }
55
56
57
58
59
60
61
62
63
64
65 @Override
66 public ServiceUnit deploy(final String suName, final String suRootPath)
67 throws DeploymentException {
68 this.suRootPath = suRootPath;
69 return super.deploy(suName, suRootPath);
70 }
71
72
73
74
75
76
77
78
79
80 @Override
81 protected void validate(final Endpoint endpoint)
82 throws DeploymentException {
83 Jbi4cicsEndpoint cicsEndpoint;
84 String fileName;
85
86 if (!(endpoint instanceof Jbi4cicsEndpoint)) {
87 throw failure(TASK,
88 MESSAGES.getString("CIC001010_No_jbi4cics_endpoint"), null);
89 }
90 cicsEndpoint = (Jbi4cicsEndpoint) endpoint;
91
92
93 fileName = cicsEndpoint.getCopyCobolFileName();
94 cicsEndpoint.setCopyCobol(
95 readFile(suRootPath + File.separator + fileName));
96
97
98 fileName = cicsEndpoint.getOutputCopyCobolFileName();
99 if ((fileName != null) && (fileName.length() > 0)) {
100 cicsEndpoint.setOutputCopyCobol(
101 readFile(suRootPath + File.separator + fileName));
102 }
103
104 try {
105 cicsEndpoint.registerService();
106 } catch (Exception e) {
107 Object[] args = new Object[] { e.getLocalizedMessage() };
108
109 LOG.error("CIC001012_Error_registering_endpoint", args, e);
110 throw failure(TASK, MESSAGES.getString(
111 "CIC001012_Error_registering_endpoint", args), e);
112 }
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
127 private String readFile(final String fileName) throws DeploymentException {
128 File file = new File(fileName);
129 char[] buf = new char[(int) file.length()];
130 FileReader reader = null;
131
132 try {
133 int offset = 0;
134
135 reader = new FileReader(file);
136 do {
137 int count = reader.read(buf, offset, buf.length - offset);
138
139 offset += count;
140 } while (offset < buf.length);
141 } catch (IOException e) {
142 throw createFailure(fileName, e);
143 } finally {
144 if (reader != null) {
145 try {
146 reader.close();
147 } catch (IOException e) {
148 throw createFailure(fileName, e);
149 }
150 }
151 }
152 return new String(buf);
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 private DeploymentException createFailure(final String fileName,
169 final IOException ioe) {
170 Object[] args = new Object[] { fileName, ioe.getLocalizedMessage() };
171 String errorMsg = MESSAGES.getString(
172 "CIC001011_Error_reading_copy_file", args);
173
174 LOG.error("CIC001011_Error_reading_copy_file", args, ioe);
175 return failure(TASK, errorMsg, ioe);
176 }
177 }