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.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 * The task name, used for error messages.
27 */
28 private static final String TASK = "deploy";
29
30 /**
31 * The logger for this class and its instances.
32 */
33 private static final Logger LOG
34 = LoggerFactory.getLogger(Jbi4cicsDeployer.class);
35
36 /**
37 * The responsible to translate localized messages.
38 */
39 private static final Messages MESSAGES
40 = Messages.getMessages(Jbi4cicsDeployer.class);
41
42 /**
43 * The path of the exploded service unit.
44 */
45 private String suRootPath;
46
47 /**
48 * Initializes this deployer for the specified component.
49 *
50 * @param component the component that will be deployed by this instance.
51 */
52 public Jbi4cicsDeployer(final BaseComponent component) {
53 super(component);
54 }
55
56 /**
57 * Deploys the given service unit and build a <code>ServiceUnit</code>
58 * object that contains endpoints.
59 *
60 * @param suName the name of the service unit.
61 * @param suRootPath the path of the exploded service unit.
62 * @return a service unit containing endpoints.
63 * @throws DeploymentException if an error occurs.
64 */
65 @Override
66 public ServiceUnit deploy(final String suName, final String suRootPath)
67 throws DeploymentException { // Overridden
68 this.suRootPath = suRootPath;
69 return super.deploy(suName, suRootPath);
70 }
71
72 /**
73 * Validates the specified endpoint.
74 *
75 * @param endpoint the endpoint to validate.
76 * @throws DeploymentException if <code>endpoint</code> is not an instance
77 * of {@link Jbi4cicsEndpoint} class or can't
78 * be validated.
79 */
80 @Override
81 protected void validate(final Endpoint endpoint) // Overridden
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 // The copy Cobol used for input and, optionally, for output
93 fileName = cicsEndpoint.getCopyCobolFileName();
94 cicsEndpoint.setCopyCobol(
95 readFile(suRootPath + File.separator + fileName));
96
97 // If found, the output copy Cobol differs from input copy Cobol
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 * Gets the specified file content as a string.
117 *
118 * @param fileName the name of the file to read. Must be not
119 * <code>null</code>.
120 * @return a string representing the file content. The returned string is
121 * never <code>null</code>.
122 * @throws DeploymentException if the file does not exist, is a directory
123 * rather than a regular file, for some other
124 * reason cannot be opened for reading or an
125 * I/O error occurs.
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 * Creates a new <code>DeploymentException</code> caused by the specified
157 * <code>IOException</code> obtained while accessing the file named
158 * <code>fileName</code>, logging also the <code>IOException</code> itself.
159 *
160 * @param fileName the name of the file to be read. Must be
161 * not <code>null</code>.
162 * @param ioe the exception received while accessing the
163 * file named <code>fileName</code>. Must be
164 * not <code>null</code>.
165 * @return the newly created failure, containing error details regarding
166 * <code>fileName</code> and <code>ioe</code>.
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 }