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.jdo;
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.Record;
54 import com.rhi.architecture.parc.adapter.AbstractOutputAdapter;
55 import com.rhi.architecture.resource.InitializationException;
56 import com.rhi.architecture.xa.Transaction;
57
58 import java.io.IOException;
59 import java.util.ArrayList;
60 import java.util.Collection;
61 import java.util.Iterator;
62 import java.util.Properties;
63
64 import org.exolab.castor.jdo.ClassNotPersistenceCapableException;
65 import org.exolab.castor.jdo.Database;
66 import org.exolab.castor.jdo.DatabaseNotFoundException;
67 import org.exolab.castor.jdo.JDO;
68 import org.exolab.castor.jdo.PersistenceException;
69 import org.exolab.castor.jdo.TransactionNotInProgressException;
70 import org.exolab.castor.mapping.Mapping;
71 import org.exolab.castor.mapping.MappingException;
72
73 /***
74 * JDO Output Adapter. Uses Castor to perform mapping from java
75 * objects to DB rows.
76 *
77 * @author Pete McKinstry
78 * @copyright 2002, Robert Half Int'l., Inc. All rights reserved.
79 *
80 * @since 1.0
81 */
82 public abstract class JDOOutputAdapter 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 static Mapping mapping;
90 private static JDO jdo;
91 private static Database db;
92
93 private String errorLog;
94
95 /***
96 * Default constructor
97 *
98 * @since 1.0
99 */
100 public JDOOutputAdapter() {
101 super();
102 }
103
104 /***
105 * Initialize the Castor data binding facilities
106 * so that the doWrite() method is ready to go.
107 * @param props
108 * @throws InitializationException
109 */
110 public void init(Properties props) throws InitializationException {
111
112 try {
113 log = LogUtil.getLogger();
114 }
115 catch (ConfigurationException e) {
116 throw new InitializationException(e.toString());
117 }
118 log.debug("init");
119 try {
120 mapping = new Mapping();
121 mapping.loadMapping(props.getProperty("jdo_mapping_file"));
122 jdo = new JDO();
123 jdo.setConfiguration(props.getProperty("jdo_db_config_file"));
124 jdo.setDatabaseName(props.getProperty("jdo_db_name"));
125 db = jdo.getDatabase();
126 String rec_count = props.getProperty("oa_max_rec_count", "25");
127 MAX_RECORD_CNT = Integer.parseInt(rec_count);
128 }
129 catch (IOException ioe) {
130 throw new InitializationException(ioe.toString());
131 }
132 catch (MappingException me) {
133 throw new InitializationException(me.toString());
134 }
135 catch (DatabaseNotFoundException dnfe) {
136 throw new InitializationException(dnfe.toString());
137 }
138 catch (PersistenceException pe) {
139 throw new InitializationException(pe.toString());
140 }
141 super.init(props);
142 }
143
144 /***
145 * cleanup any resources.
146 */
147 public void cleanup() {
148 log.debug("running cleanup");
149 super.cleanup();
150 }
151
152 /***
153 * Handle persistence of valid records from the framework.
154 *
155 * @param c - valid records.
156 * @param t - the transaction to use when persisting the records.
157 * @return Collection - records errored during persistence.
158 * @exception ProcessingException
159 *
160 * @since 1.0
161 */
162 public Collection handleValidRecords(Collection c, Transaction t)
163 throws ProcessingException {
164 log.debug("handleValidRecords()");
165
166 ArrayList errors = new ArrayList();
167
168 try {
169 db.begin();
170 log.debug("persisting collection of " + c.size() + " objects");
171 Iterator iter = c.iterator();
172 while (iter.hasNext()) {
173 Record r = (Record) iter.next();
174 if (r.isValid()) {
175 try {
176 db.create(r);
177 }
178 catch (PersistenceException pe) {
179 log.error(
180 "error persisting record."
181 + "e = "
182 + pe
183 + ", object = "
184 + r.toString());
185 errors.add(r);
186 }
187 }
188 else {
189 errors.add(r);
190 }
191 }
192 db.commit();
193 db.close();
194 }
195 catch (ClassNotPersistenceCapableException cnpe) {
196 throw new ProcessingException(cnpe.toString());
197 }
198 catch (TransactionNotInProgressException te) {
199 throw new ProcessingException(te.toString());
200 }
201 catch (PersistenceException pe) {
202 throw new ProcessingException(pe.toString());
203 }
204 return errors;
205 }
206
207 }
This page was automatically generated by Maven