Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
XmlDigesterServiceImpl |
|
| 5.5;5.5 |
1 | /** | |
2 | * Copyright 2005-2011 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl2.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.kuali.rice.core.impl.impex.xml; | |
17 | ||
18 | import java.io.BufferedInputStream; | |
19 | import java.io.IOException; | |
20 | import java.io.InputStream; | |
21 | ||
22 | import org.apache.commons.lang.exception.ExceptionUtils; | |
23 | import org.apache.log4j.Logger; | |
24 | import org.kuali.rice.core.api.impex.xml.XmlDoc; | |
25 | import org.kuali.rice.core.api.impex.xml.XmlDocCollection; | |
26 | import org.kuali.rice.core.framework.impex.xml.XmlLoader; | |
27 | ||
28 | ||
29 | /** | |
30 | * XmlDigesterService implementation. This class simply loads the specified xml doc | |
31 | * with the specified XmlLoader. | |
32 | * @see org.kuali.rice.core.impl.impex.xml.batch.XmlDigesterService | |
33 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
34 | */ | |
35 | 0 | public class XmlDigesterServiceImpl implements XmlDigesterService { |
36 | 0 | private static final Logger LOG = Logger.getLogger(XmlDigesterServiceImpl.class); |
37 | ||
38 | private static void addProcessingException(XmlDoc xmlDoc, String message, Throwable t) { | |
39 | 0 | String msg = xmlDoc.getProcessingMessage(); |
40 | 0 | if (msg == null) { |
41 | 0 | msg = ""; |
42 | } | |
43 | 0 | msg += message + "\n" + ExceptionUtils.getFullStackTrace(t); |
44 | 0 | xmlDoc.setProcessingMessage(msg); |
45 | 0 | } |
46 | ||
47 | public void digest(XmlLoader xmlLoader, XmlDocCollection xmlDocCollection, String principalId) throws IOException { | |
48 | 0 | for (XmlDoc xmlDoc : xmlDocCollection.getXmlDocs()) |
49 | { | |
50 | 0 | InputStream inputStream = null; |
51 | try | |
52 | { | |
53 | 0 | inputStream = new BufferedInputStream(xmlDoc.getStream()); |
54 | // NOTE: do we need XmlLoader to return a boolean to indicate | |
55 | // whether the document was handled at all, now that we are handing | |
56 | // all docs to all services? | |
57 | // e.g. if (xmlLoader.loadXml(inputstream)) xmlDoc.setProcessed(true); | |
58 | // it's ok if the xml doc is processed multiple times however | |
59 | // because it could have multiple types of content | |
60 | // but we need to know if it was not processed ANY times | |
61 | // (so just don't setProcessed to false) | |
62 | 0 | xmlLoader.loadXml(inputStream, principalId); |
63 | // it would be nice to know if the xmlLoader actually handled the doc | |
64 | // so we could print out a log entry ONLY if the doc was handled, not | |
65 | // if it was just (successfully) ignored | |
66 | ||
67 | // should only be set on successful loading | |
68 | 0 | xmlDoc.setProcessed(true); |
69 | 0 | } catch (Exception e) |
70 | { | |
71 | 0 | xmlDoc.setProcessed(false); |
72 | 0 | addProcessingException(xmlDoc, "Caught Exception loading xml data from " + xmlDoc + ". Will move associated file to problem dir.", e); |
73 | 0 | LOG.error("Caught Exception loading xml data from " + xmlDoc + ". Will move associated file to problem dir.", e); |
74 | 0 | if (e instanceof RuntimeException) |
75 | { | |
76 | 0 | throw (RuntimeException) e; |
77 | 0 | } else if (e instanceof IOException) |
78 | { | |
79 | 0 | throw (IOException) e; |
80 | } | |
81 | } finally | |
82 | { | |
83 | 0 | if (inputStream != null) try |
84 | { | |
85 | 0 | inputStream.close(); |
86 | 0 | } catch (IOException ioe) |
87 | { | |
88 | 0 | LOG.warn("Error closing stream for xml doc: " + xmlDoc, ioe); |
89 | 0 | } |
90 | } | |
91 | 0 | } |
92 | 0 | } |
93 | } |