Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
XmlDigesterServiceImpl |
|
| 5.5;5.5 |
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.core.impl.impex.xml; | |
18 | ||
19 | import java.io.BufferedInputStream; | |
20 | import java.io.IOException; | |
21 | import java.io.InputStream; | |
22 | ||
23 | import org.apache.commons.lang.exception.ExceptionUtils; | |
24 | import org.apache.log4j.Logger; | |
25 | import org.kuali.rice.core.api.impex.xml.XmlDoc; | |
26 | import org.kuali.rice.core.api.impex.xml.XmlDocCollection; | |
27 | import org.kuali.rice.core.framework.impex.xml.XmlLoader; | |
28 | ||
29 | ||
30 | /** | |
31 | * XmlDigesterService implementation. This class simply loads the specified xml doc | |
32 | * with the specified XmlLoader. | |
33 | * @see org.kuali.rice.core.impl.impex.xml.batch.XmlDigesterService | |
34 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
35 | */ | |
36 | 0 | public class XmlDigesterServiceImpl implements XmlDigesterService { |
37 | 0 | private static final Logger LOG = Logger.getLogger(XmlDigesterServiceImpl.class); |
38 | ||
39 | private static void addProcessingException(XmlDoc xmlDoc, String message, Throwable t) { | |
40 | 0 | String msg = xmlDoc.getProcessingMessage(); |
41 | 0 | if (msg == null) { |
42 | 0 | msg = ""; |
43 | } | |
44 | 0 | msg += message + "\n" + ExceptionUtils.getFullStackTrace(t); |
45 | 0 | xmlDoc.setProcessingMessage(msg); |
46 | 0 | } |
47 | ||
48 | public void digest(XmlLoader xmlLoader, XmlDocCollection xmlDocCollection, String principalId) throws IOException { | |
49 | 0 | for (XmlDoc xmlDoc : xmlDocCollection.getXmlDocs()) |
50 | { | |
51 | 0 | InputStream inputStream = null; |
52 | try | |
53 | { | |
54 | 0 | inputStream = new BufferedInputStream(xmlDoc.getStream()); |
55 | // NOTE: do we need XmlLoader to return a boolean to indicate | |
56 | // whether the document was handled at all, now that we are handing | |
57 | // all docs to all services? | |
58 | // e.g. if (xmlLoader.loadXml(inputstream)) xmlDoc.setProcessed(true); | |
59 | // it's ok if the xml doc is processed multiple times however | |
60 | // because it could have multiple types of content | |
61 | // but we need to know if it was not processed ANY times | |
62 | // (so just don't setProcessed to false) | |
63 | 0 | xmlLoader.loadXml(inputStream, principalId); |
64 | // it would be nice to know if the xmlLoader actually handled the doc | |
65 | // so we could print out a log entry ONLY if the doc was handled, not | |
66 | // if it was just (successfully) ignored | |
67 | ||
68 | // should only be set on successful loading | |
69 | 0 | xmlDoc.setProcessed(true); |
70 | 0 | } catch (Exception e) |
71 | { | |
72 | 0 | xmlDoc.setProcessed(false); |
73 | 0 | addProcessingException(xmlDoc, "Caught Exception loading xml data from " + xmlDoc + ". Will move associated file to problem dir.", e); |
74 | 0 | LOG.error("Caught Exception loading xml data from " + xmlDoc + ". Will move associated file to problem dir.", e); |
75 | 0 | if (e instanceof RuntimeException) |
76 | { | |
77 | 0 | throw (RuntimeException) e; |
78 | 0 | } else if (e instanceof IOException) |
79 | { | |
80 | 0 | throw (IOException) e; |
81 | } | |
82 | } finally | |
83 | { | |
84 | 0 | if (inputStream != null) try |
85 | { | |
86 | 0 | inputStream.close(); |
87 | 0 | } catch (IOException ioe) |
88 | { | |
89 | 0 | LOG.warn("Error closing stream for xml doc: " + xmlDoc, ioe); |
90 | 0 | } |
91 | } | |
92 | 0 | } |
93 | 0 | } |
94 | } |