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