View Javadoc
1 /* ====================================================================
2 * License:
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. The end-user documentation included with the redistribution,
17 * if any, must include the following acknowledgment:
18 * "This product includes software developed by
19 * Robert Half International (http://www.rhi.com/)."
20 * Alternately, this acknowledgment may appear in the software itself,
21 * if and wherever such third-party acknowledgments normally appear.
22 *
23 * 4. The names "Parc", "RHI", and "Robert Half International" must
24 * not be used to endorse or promote products derived from this
25 * software without prior written permission. For written
26 * permission, please contact pete.mckinstry@rhi.com.
27 *
28 * 5. Products derived from this software may not be called "PARC",
29 * nor may "PARC" appear in their name, without prior written
30 * permission of Robert Half International.
31 *
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
36 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
39 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
41 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
42 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 * ====================================================================
45 *
46 */
47 package com.rhi.architecture.parc.adapter.file;
48
49 import com.rhi.architecture.parc.Error;
50 import com.rhi.architecture.parc.ProcessingException;
51 import com.rhi.architecture.parc.Record;
52 import com.rhi.architecture.parc.SingleThreadModel;
53 import com.rhi.architecture.parc.adapter.AbstractOutputAdapter;
54 import com.rhi.architecture.resource.InitializationException;
55 import com.rhi.architecture.xa.Transaction;
56
57 import java.io.BufferedWriter;
58 import java.io.File;
59 import java.io.FileNotFoundException;
60 import java.io.FileWriter;
61 import java.io.IOException;
62 import java.util.ArrayList;
63 import java.util.Collection;
64 import java.util.Iterator;
65 import java.util.List;
66 import java.util.ListIterator;
67 import java.util.Properties;
68
69 /***
70 * Flat File Output Adapter. Uses toString() method on Record
71 * to dump output to file.
72 *
73 * @author Pete McKinstry
74 * @copyright 2002, Robert Half Int'l., Inc. All rights reserved.
75 *
76 * @since 1.0
77 */
78 public class FlatFileOutputAdapter
79 extends AbstractOutputAdapter
80 implements SingleThreadModel {
81
82 private BufferedWriter validWriter;
83 private BufferedWriter errorWriter;
84
85 /***
86 * Default Constructor.
87 *
88 * @since 1.0
89 */
90 public FlatFileOutputAdapter() {
91 this.validWriter = null;
92 this.errorWriter = null;
93 }
94
95 /***
96 * Initialize the Castor data binding facilities
97 * so that the doWrite() method is ready to go.
98 * @param props
99 * @throws InitializationException
100 */
101 public void init(Properties props) throws InitializationException {
102 super.init(props);
103 log().debug("running init");
104 try {
105 // Initialization of Writer for valid records
106 {
107 String filename =
108 props.getProperty("output_file", "records")
109 + "."
110 + props.getProperty("file_sequence1");
111 File file = new File(filename);
112 if (file.createNewFile() == false) {
113 throw new InitializationException(
114 "output file already exists = " + filename);
115 }
116 FileWriter fwriter = new FileWriter(file);
117 validWriter = new BufferedWriter(fwriter);
118 }
119
120 // Initialization of Writer for errored records
121 {
122 String filename =
123 props.getProperty("error_file", "errors")
124 + "."
125 + props.getProperty("file_sequence1");
126 File file = new File(filename);
127 if (file.createNewFile() == false) {
128 log().error("output file already exists = " + filename);
129 }
130 FileWriter fwriter = new FileWriter(file);
131 errorWriter = new BufferedWriter(fwriter);
132 }
133
134 }
135 catch (FileNotFoundException fnfe) {
136 log().error("FileNotFoundException", fnfe);
137 throw new InitializationException(fnfe.toString(), fnfe);
138 }
139 catch (IOException ioe) {
140 log().error("IOException", ioe);
141 throw new InitializationException(ioe.toString(), ioe);
142 }
143 }
144
145 /***
146 * cleanup any resources.
147 */
148 public void cleanup() {
149 if (log() != null) {
150 log().debug("running cleanup");
151 }
152 try {
153 if (validWriter != null) {
154 validWriter.close();
155 }
156 }
157 catch (IOException ioe) {
158 log().error("writer.close() throws IOE", ioe);
159 }
160 try {
161 if (errorWriter != null) {
162 errorWriter.close();
163 }
164 }
165 catch (IOException ioe) {
166 if (log() != null) {
167 log().error("writer.close() throws IOE", ioe);
168 }
169 }
170
171 super.cleanup();
172 }
173
174 /***
175 * Persist Valid Records - FlatFileOutputAdapter does not
176 *
177 * @param c - valid records
178 * @param t - the transaction to use in committing the records.
179 * @return Collection - any records failing during persistence.
180 *
181 * @exception ProcessingException
182 *
183 * @since 1.0
184 */
185 public Collection handleValidRecords(Collection c, Transaction t)
186 throws ProcessingException {
187 int valid = 0;
188 List errors = new ArrayList();
189 try {
190 Iterator iter = c.iterator();
191 while (iter.hasNext()) {
192 Record obj = (Record) iter.next();
193 if (obj.isErrored() == true) {
194 errors.add(obj);
195 }
196 else {
197 ++valid;
198 validWriter.write(obj.toString());
199 validWriter.newLine();
200 }
201 }
202 validWriter.flush();
203
204 log().debug("persisted " + valid + " valid records.");
205 }
206 catch (IOException ioe) {
207 log().error("IOException writing records.", ioe);
208 throw new ProcessingException(ioe.toString(), ioe);
209 }
210 return errors;
211 }
212
213 /***
214 * General Case implementation for persisting error records.
215 *
216 * @param c - errored records
217 * @param t - the transaction to use in committing the records.
218 *
219 * @exception ProcessingException
220 *
221 * @since 1.0
222 */
223 protected void handleErrorRecords(Collection c, Transaction t)
224 throws ProcessingException {
225 log().debug("handleErrorRecords()");
226
227 try {
228 log().debug("persisting " + c.size() + " error records");
229 Iterator iter = c.iterator();
230 while (iter.hasNext()) {
231 Record obj = (Record) iter.next();
232 errorWriter.write(obj.toString());
233 ListIterator liter = obj.getErrors().listIterator();
234 while (liter.hasNext()) {
235 Error error = (Error) liter.next();
236 errorWriter.write(error.toString() + "|");
237 }
238 errorWriter.newLine();
239 }
240 errorWriter.flush();
241 }
242 catch (IOException ioe) {
243 log().error("IOException writing records.", ioe);
244 throw new ProcessingException(ioe.toString(), ioe);
245 }
246 }
247
248 }
This page was automatically generated by Maven