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.xml;
48
49 import com.rhi.architecture.config.ConfigurationException;
50 import com.rhi.architecture.logging.LogUtil;
51 import com.rhi.architecture.logging.Logger;
52 import com.rhi.architecture.parc.ProcessingException;
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.FileNotFoundException;
58 import java.io.FileOutputStream;
59 import java.io.IOException;
60 import java.io.OutputStreamWriter;
61 import java.util.ArrayList;
62 import java.util.Collection;
63 import java.util.Iterator;
64 import java.util.List;
65 import java.util.Properties;
66
67 import org.exolab.castor.mapping.Mapping;
68 import org.exolab.castor.mapping.MappingException;
69 import org.exolab.castor.xml.MarshalException;
70 import org.exolab.castor.xml.Marshaller;
71 import org.exolab.castor.xml.ValidationException;
72
73 /***
74 * XML Output Adapter. Uses Castor to perform mapping from java
75 * objects to XML.
76 *
77 * @author Pete McKinstry
78 * @copyright 2002, Robert Half Int'l., Inc. All rights reserved.
79 *
80 * @since 1.0
81 */
82 public class XMLOutputAdapter extends AbstractOutputAdapter {
83
84 private static Logger log = null;
85
86 // number of records to persist @ a time.
87 private int MAX_RECORD_CNT;
88
89 private Mapping mapping;
90 private Marshaller validMarshaller;
91 private Marshaller errorMarshaller;
92 private String validFilename;
93 private String errorFilename;
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 try {
103 log = LogUtil.getLogger();
104 }
105 catch (ConfigurationException e) {
106 throw new InitializationException(e.toString());
107 }
108 log.debug("running init");
109 try {
110 this.mapping = new Mapping();
111 mapping.loadMapping(
112 props.getProperty("jdo_mapping_file", "mapping.xml"));
113 String rec_count = props.getProperty("oa_max_rec_count", "100");
114 MAX_RECORD_CNT = Integer.parseInt(rec_count);
115
116 // Valid file.
117 this.validFilename =
118 props.getProperty("jdo_output_file", "valid_records.xml");
119 FileOutputStream vStr = new FileOutputStream(validFilename);
120 validMarshaller = new Marshaller(new OutputStreamWriter(vStr));
121 validMarshaller.setMapping(mapping);
122
123 // Error file
124 this.errorFilename =
125 props.getProperty("jdo_output_file", "error_records.xml");
126 FileOutputStream eStr = new FileOutputStream(validFilename);
127 errorMarshaller = new Marshaller(new OutputStreamWriter(eStr));
128 errorMarshaller.setMapping(mapping);
129 }
130 catch (FileNotFoundException fnfe) {
131 log.error("FileNotFoundException = " + fnfe);
132 throw new InitializationException(fnfe.toString());
133 }
134 catch (IOException ioe) {
135 log.error("IOException = " + ioe);
136 throw new InitializationException(ioe.toString());
137 }
138 catch (MappingException me) {
139 log.error("MappingException = " + me);
140 throw new InitializationException(me.toString());
141 }
142 super.init(props);
143 }
144
145 /***
146 * Get filename for valid records.
147 *
148 * @since 1.0
149 * @return
150 */
151 public String getValidFilename() {
152 return this.validFilename;
153 }
154
155 /***
156 * Get filename for errored records.
157 *
158 * @since 1.0
159 * @return
160 */
161 public String getErrorFilename() {
162 return this.errorFilename;
163 }
164
165 /***
166 * cleanup any resources.
167 */
168 public void cleanup() {
169 log.debug("running cleanup");
170 super.cleanup();
171 }
172
173 /***
174 * Handle persistence of valid records from the framework.
175 *
176 * @param c - valid records.
177 * @param t - the transaction to use persisting records.
178 * @return Collection - records errored during persistence.
179 * @exception ProcessingException
180 *
181 * @since 1.0
182 */
183 public Collection handleValidRecords(Collection c, Transaction t)
184 throws ProcessingException {
185 log.debug("handleValidRecords()");
186 List errors = new ArrayList();
187
188 try {
189 log.debug("persisting collection of " + c.size() + " objects");
190 Iterator iter = c.iterator();
191 while (iter.hasNext()) {
192 Object obj = iter.next();
193 validMarshaller.marshal(obj);
194 }
195
196 // if necessary
197 transform(getValidFilename());
198 }
199 catch (MarshalException me) {
200 throw new ProcessingException(me.toString());
201 }
202 catch (ValidationException ve) {
203 throw new ProcessingException(ve.toString());
204 }
205 return errors;
206 }
207
208 /***
209 * Placeholder to support XSLT transformations on the XML output
210 * as part of the interface. This method is designed to be
211 * overridden.
212 * @param filename
213 * @since 1.0
214 */
215 public void transform(String filename) {
216 // override this in sub-class to perform some transformation
217 // --OR--
218 // fix this to perform some default XSLT transformation on file.
219 }
220
221 /***
222 * Stream errored records to file in XML format.
223 * @param c
224 * @param t
225 * @throws ProcessingException
226 */
227 protected void handleErrorRecords(Collection c, Transaction t)
228 throws ProcessingException {
229 log.debug("handleErrorRecords()");
230
231 try {
232 log.debug("persisting collection of " + c.size() + " objects");
233 Iterator iter = c.iterator();
234 while (iter.hasNext()) {
235 Object obj = iter.next();
236 errorMarshaller.marshal(obj);
237 }
238
239 // if necessary
240 transform(getErrorFilename());
241 }
242 catch (MarshalException me) {
243 throw new ProcessingException(me.toString());
244 }
245 catch (ValidationException ve) {
246 throw new ProcessingException(ve.toString());
247 }
248 }
249
250 }
This page was automatically generated by Maven