001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.batch;
018:
019: import java.io.BufferedInputStream;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.Iterator;
023:
024: import org.apache.log4j.Logger;
025:
026: import edu.iu.uis.eden.XmlLoader;
027: import edu.iu.uis.eden.user.WorkflowUser;
028: import edu.iu.uis.eden.util.Utilities;
029:
030: /**
031: * XmlDigesterService implementation. This class simply loads the specified xml doc
032: * with the specified XmlLoader.
033: * @see edu.iu.uis.eden.batch.XmlDigesterService
034: * @author Aaron Hamid (arh14 at cornell dot edu)
035: */
036: public class XmlDigesterServiceImpl implements XmlDigesterService {
037: private static final Logger LOG = Logger
038: .getLogger(XmlDigesterServiceImpl.class);
039:
040: private static void addProcessingException(XmlDoc xmlDoc,
041: String message, Throwable t) {
042: String msg = xmlDoc.getProcessingMessage();
043: if (msg == null) {
044: msg = "";
045: }
046: msg += message + "\n" + Utilities.collectStackTrace(t);
047: xmlDoc.setProcessingMessage(msg);
048: }
049:
050: public void digest(XmlLoader xmlLoader,
051: XmlDocCollection xmlDocCollection, WorkflowUser user)
052: throws IOException {
053: Iterator it = xmlDocCollection.getXmlDocs().iterator();
054: while (it.hasNext()) {
055: XmlDoc xmlDoc = (XmlDoc) it.next();
056: InputStream inputStream = null;
057: try {
058: inputStream = new BufferedInputStream(xmlDoc
059: .getStream());
060: // NOTE: do we need XmlLoader to return a boolean to indicate
061: // whether the document was handled at all, now that we are handing
062: // all docs to all services?
063: // e.g. if (xmlLoader.loadXml(inputstream)) xmlDoc.setProcessed(true);
064: // it's ok if the xml doc is processed multiple times however
065: // because it could have multiple types of content
066: // but we need to know if it was not processed ANY times
067: // (so just don't setProcessed to false)
068: xmlLoader.loadXml(inputStream, user);
069: // it would be nice to know if the xmlLoader actually handled the doc
070: // so we could print out a log entry ONLY if the doc was handled, not
071: // if it was just (successfully) ignored
072:
073: // should only be set on successful loading
074: xmlDoc.setProcessed(true);
075: } catch (Exception e) {
076: addProcessingException(
077: xmlDoc,
078: "Caught Exception loading xml data from "
079: + xmlDoc
080: + ". Will move associated file to problem dir.",
081: e);
082: LOG
083: .error(
084: "Caught Exception loading xml data from "
085: + xmlDoc
086: + ". Will move associated file to problem dir.",
087: e);
088: if (e instanceof RuntimeException) {
089: throw (RuntimeException) e;
090: } else if (e instanceof IOException) {
091: throw (IOException) e;
092: }
093: } finally {
094: if (inputStream != null)
095: try {
096: inputStream.close();
097: } catch (IOException ioe) {
098: LOG.warn("Error closing stream for xml doc: "
099: + xmlDoc, ioe);
100: }
101: }
102: }
103: }
104: }
|