Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
XmlIngesterServiceImpl |
|
| 1.7857142857142858;1.786 | ||||
XmlIngesterServiceImpl$1 |
|
| 1.7857142857142858;1.786 |
1 | /* | |
2 | * Copyright 2005-2007 The Kuali Foundation | |
3 | * | |
4 | * | |
5 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.opensource.org/licenses/ecl2.php | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.kuali.rice.kew.batch; | |
18 | ||
19 | import org.apache.log4j.Logger; | |
20 | import org.kuali.rice.kew.service.KEWServiceLocator; | |
21 | import org.kuali.rice.kew.util.Utilities; | |
22 | import org.kuali.rice.kew.xml.ClassLoaderEntityResolver; | |
23 | import org.kuali.rice.kew.xml.XmlLoader; | |
24 | import org.springframework.beans.factory.BeanInitializationException; | |
25 | import org.xml.sax.EntityResolver; | |
26 | import org.xml.sax.ErrorHandler; | |
27 | import org.xml.sax.SAXException; | |
28 | import org.xml.sax.SAXParseException; | |
29 | ||
30 | import javax.xml.XMLConstants; | |
31 | import javax.xml.parsers.DocumentBuilder; | |
32 | import javax.xml.parsers.DocumentBuilderFactory; | |
33 | import javax.xml.parsers.ParserConfigurationException; | |
34 | import java.io.IOException; | |
35 | import java.util.*; | |
36 | ||
37 | ||
38 | /** | |
39 | * XmlIngesterService implementation which delegates to XmlDigesterService. | |
40 | * This implementation goes through some pains to ensure that the types of | |
41 | * xml doc (determined by file name convention) are issued to the XmlDigesterService | |
42 | * in a pre-ordained order in an effort to avoid dependency problems. This implementation | |
43 | * is not responsible for knowing about the mappings between types and services, but | |
44 | * only the ordering of types, for the moment. | |
45 | * NOTE: when types are merged into a universal document, we need to decide how to handle | |
46 | * rollback if any specific type <i>in</i> that document fails, given that the current, | |
47 | * legacy implementation assumes that a given XmlDoc consists of one and only one type | |
48 | * and as such can be rolled back atomically. For instance, if universal doc now contains | |
49 | * types A, B, and C, and it invokes ServiceA, ServiceB, and ServiceC in succession on the | |
50 | * entire document, and ServiceB throws an exception attempting to parse B content... | |
51 | * is it sufficient to rollback only that entry, or do we rollback the whole document | |
52 | * and consider it "tainted"? (not to mention whether we should roll back the entire collection | |
53 | * of which the document is a part - for now we do NOT rollback a collection or workflow data doc, | |
54 | * but it is merely moved to a "problem" directory by the poller. the implementation does not yet | |
55 | * specifically note which document or type (and potentially eventually which entry) failed in the | |
56 | * collection or workflow data doc) | |
57 | * | |
58 | * NOTE: this service must be invoked only after all other services have initialized | |
59 | * this <i>should</i> be the case since the LifeCycle is kicked off after contextInitialized, | |
60 | * which <i>should</i> occur after Spring is actually done initializing. But is it, considering | |
61 | * we are asynchronously initializing Spring? There is a 30 second built-in delay before | |
62 | * XmlPoller is first run, but suffice it to say there is a possible race condition. | |
63 | * | |
64 | * @see org.kuali.rice.kew.batch.XmlIngesterService | |
65 | * @see org.kuali.rice.kew.batch.XmlDigesterServiceImpl | |
66 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
67 | */ | |
68 | 0 | public class XmlIngesterServiceImpl implements XmlIngesterService { |
69 | 0 | private static final Logger LOG = Logger.getLogger(XmlIngesterServiceImpl.class); |
70 | ||
71 | /** | |
72 | * The entity resolver to use during validation | |
73 | */ | |
74 | 0 | private EntityResolver resolver = new ClassLoaderEntityResolver(); |
75 | ||
76 | private XmlDigesterService digesterService; | |
77 | ||
78 | /** | |
79 | * Whether to validate at all | |
80 | */ | |
81 | 0 | private boolean validate = true; |
82 | ||
83 | /** | |
84 | * A list of service beans implementing XmlLoader, in the order of precedence | |
85 | * in which we should load xml doc content types. It is implicit that any | |
86 | * unspecified XmlLoader beans will be invoked after all ordered beans. In | |
87 | * the future this may be configured through the use of a special marker (such as null) | |
88 | * to indicate the "all others" set, but for now everything that is not specified | |
89 | * just gets invoked last (in arbitrary order) | |
90 | */ | |
91 | private List<String> serviceOrder; | |
92 | ||
93 | // ---- bean properties | |
94 | ||
95 | public void setXmlDigesterService(XmlDigesterService digesterService) { | |
96 | 0 | this.digesterService = digesterService; |
97 | 0 | } |
98 | ||
99 | public void setEntityResolver(EntityResolver resolver) { | |
100 | 0 | this.resolver = resolver; |
101 | 0 | } |
102 | ||
103 | public void setServiceOrder(List<String> serviceOrder) throws BeanInitializationException { | |
104 | //eat Strings for now | |
105 | // Iterator orderIt = serviceOrder.iterator(); | |
106 | // while (orderIt.hasNext()) { | |
107 | // Object o = orderIt.next(); | |
108 | // if (!(o instanceof XmlLoader)) { | |
109 | // String message = o + " does not implement XmlLoader. Only XmlLoader beans can be specified in the service order!"; | |
110 | // LOG.error(message); | |
111 | // throw new BeanInitializationException(message); | |
112 | // } | |
113 | // } | |
114 | 0 | this.serviceOrder = serviceOrder; |
115 | 0 | } |
116 | ||
117 | public void setValidate(boolean b) { | |
118 | 0 | validate = b; |
119 | 0 | } |
120 | ||
121 | // ---- implementation | |
122 | ||
123 | private static void addProcessingException(XmlDoc xmlDoc, String message, Throwable t) { | |
124 | 0 | String msg = xmlDoc.getProcessingMessage(); |
125 | 0 | if (msg == null) { |
126 | 0 | msg = ""; |
127 | } | |
128 | 0 | msg += message + "\n" + Utilities.collectStackTrace(t); |
129 | 0 | xmlDoc.setProcessingMessage(msg); |
130 | 0 | } |
131 | ||
132 | private static void validate(final XmlDoc xmlDoc, EntityResolver resolver) throws ParserConfigurationException, IOException, SAXException { | |
133 | 0 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
134 | 0 | dbf.setValidating(true); |
135 | 0 | dbf.setNamespaceAware( true ); |
136 | 0 | dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI); |
137 | 0 | DocumentBuilder db = dbf.newDocumentBuilder(); |
138 | 0 | db.setEntityResolver(resolver); |
139 | 0 | db.setErrorHandler(new ErrorHandler() { |
140 | public void warning(SAXParseException se) { | |
141 | 0 | LOG.warn("Warning parsing xml doc " + xmlDoc, se); |
142 | 0 | addProcessingException(xmlDoc, "Warning parsing xml doc " + xmlDoc, se); |
143 | 0 | } |
144 | public void error(SAXParseException se) throws SAXException { | |
145 | 0 | LOG.error("Error parsing xml doc " + xmlDoc, se); |
146 | 0 | addProcessingException(xmlDoc, "Error parsing xml doc " + xmlDoc, se); |
147 | 0 | throw se; |
148 | } | |
149 | public void fatalError(SAXParseException se) throws SAXException { | |
150 | 0 | LOG.error("Fatal error parsing xml doc " + xmlDoc, se); |
151 | 0 | addProcessingException(xmlDoc, "Fatal error parsing xml doc " + xmlDoc, se); |
152 | 0 | throw se; |
153 | } | |
154 | }); | |
155 | 0 | db.parse(xmlDoc.getStream()); |
156 | 0 | } |
157 | ||
158 | /** | |
159 | * Validates (if possible) all XmlDocs, and accumulates only those | |
160 | * which either were not possible to validate, or passed validation. | |
161 | * @param collections collection of XmlDocCollection | |
162 | * @param resolver the entity resolver to use | |
163 | * @param successful xmldoccollections in which all docs successfully validated | |
164 | * @param failed xmldoccollections in which one or more docs failed validation | |
165 | */ | |
166 | private static void validate(List<XmlDocCollection> collections, EntityResolver resolver, Set<XmlDocCollection> successful, Set<XmlDocCollection> failed) { | |
167 | // for every collection, validate all docs | |
168 | 0 | for (XmlDocCollection collection : collections) |
169 | { | |
170 | ||
171 | // for every xml doc in the collection, try to validate it | |
172 | 0 | for (XmlDoc xmlDoc : collection.getXmlDocs()) |
173 | { | |
174 | try | |
175 | { | |
176 | 0 | validate(xmlDoc, resolver); |
177 | 0 | } catch (Exception e) |
178 | { | |
179 | 0 | LOG.error("Error validating doc: " + xmlDoc, e); |
180 | 0 | addProcessingException(xmlDoc, "Error validating doc: " + xmlDoc, e); |
181 | // validation failed, so add collection to successful set | |
182 | // do not break here, so that we can attempt validation on all | |
183 | // docs in a collection; since validation has no side-effects | |
184 | // we might as well validate all the docs now instead of forcing | |
185 | // the user to continually re-submit | |
186 | 0 | failed.add(collection); |
187 | 0 | } |
188 | } | |
189 | ||
190 | // all files validated, so add collection to successful set | |
191 | 0 | successful.add(collection); |
192 | } | |
193 | 0 | } |
194 | ||
195 | private void ingest(XmlLoader xmlLoader, Collection<XmlDocCollection> xmlDocCollections, String principalId, Set<Object> successful, Set<XmlDocCollection> failed) { | |
196 | 0 | for (XmlDocCollection xmlDocCollection : xmlDocCollections) |
197 | { | |
198 | ||
199 | 0 | if (failed.contains(xmlDocCollection)) |
200 | { | |
201 | 0 | LOG.debug("Skipping " + xmlDocCollection.getFile() + "..."); |
202 | 0 | continue; |
203 | } | |
204 | ||
205 | try | |
206 | { | |
207 | //SpringServiceLocator.getXmlDigesterService().digest(xmlLoader, xmlDocCollection, user); | |
208 | 0 | digesterService.digest(xmlLoader, xmlDocCollection, principalId); |
209 | 0 | } catch (Exception e) |
210 | { | |
211 | 0 | LOG.error("Caught Exception loading xml data from " + xmlDocCollection.getFile() + ". Will move associated file to problem dir.", e); |
212 | 0 | failed.add(xmlDocCollection); |
213 | 0 | } |
214 | } | |
215 | 0 | } |
216 | ||
217 | public Collection<XmlDocCollection> ingest(List<XmlDocCollection> collections) throws Exception { | |
218 | 0 | return ingest(collections, null); |
219 | } | |
220 | ||
221 | private void ingestThroughOrderedLoaders(Collection<XmlDocCollection> xmlDocCollections, String principalId, Set<Object> successful, Set<XmlDocCollection> failed) { | |
222 | 0 | LOG.debug("Ingesting through ordered XmlLoaders"); |
223 | 0 | for (String aServiceOrder : serviceOrder) |
224 | { | |
225 | 0 | XmlLoader xmlLoader = (XmlLoader) KEWServiceLocator.getService(aServiceOrder); |
226 | 0 | LOG.debug("Ingesting through ordered XmlLoader: " + xmlLoader); |
227 | 0 | ingest(xmlLoader, xmlDocCollections, principalId, successful, failed); |
228 | 0 | } |
229 | 0 | } |
230 | ||
231 | /* FIXME commented this out because its causing problems with the default User and Workgroup services | |
232 | private void ingestThroughUnorderedLoaders(Collection xmlDocCollections, WorkflowUser user, Set successful, Set failed) { | |
233 | Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, XmlLoader.class); | |
234 | ||
235 | // ingest docs by remaining (if any) unordered services | |
236 | LOG.debug("Ingesting through un-ordered XmlLoaders"); | |
237 | Iterator entryIt = beans.entrySet().iterator(); | |
238 | while (entryIt.hasNext()) { | |
239 | Map.Entry entry = (Map.Entry) entryIt.next(); | |
240 | XmlLoader xmlLoader = (XmlLoader) entry.getValue(); | |
241 | if (serviceOrder.contains(entry.getKey())) { | |
242 | LOG.debug("Skipping ordered XmlLoader: " + entry.getKey() + " " + xmlLoader.getClass()); | |
243 | continue; | |
244 | } | |
245 | LOG.debug("Ingesting through un-ordered XmlLoader: " + entry.getKey() + " " + xmlLoader.getClass()); | |
246 | ingest(xmlLoader, xmlDocs, user); | |
247 | } | |
248 | }*/ | |
249 | ||
250 | public Collection<XmlDocCollection> ingest(List<XmlDocCollection> collections, String principalId) { | |
251 | 0 | Set<XmlDocCollection> failed = new LinkedHashSet<XmlDocCollection>(); |
252 | // validate all the docs up-front because we will be iterating over them | |
253 | // multiple times: one for each XmlLoader. If we delegated validation to | |
254 | // XmlDigesterService then the docs would re-validated over and over again, | |
255 | // for each XmlLoader | |
256 | 0 | if (validate) { |
257 | 0 | Set<XmlDocCollection> successful = new LinkedHashSet<XmlDocCollection>(); |
258 | 0 | validate(collections, resolver, successful, failed); |
259 | 0 | collections = new LinkedList<XmlDocCollection>(successful); |
260 | } | |
261 | ||
262 | 0 | Set<Object> successful = new LinkedHashSet<Object>(); |
263 | // ingest docs first by ordered services | |
264 | 0 | ingestThroughOrderedLoaders(collections, principalId, successful, failed); |
265 | // then by unordered services | |
266 | // collections = new LinkedList(successful); | |
267 | ||
268 | //ingestThroughUnorderedLoaders(collections, user, successful, failed); | |
269 | ||
270 | 0 | return failed; |
271 | } | |
272 | } |