Coverage Report - org.kuali.rice.core.impl.impex.xml.XmlIngesterServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlIngesterServiceImpl
0%
0/62
0%
0/14
1.786
XmlIngesterServiceImpl$1
0%
0/10
N/A
1.786
 
 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.IOException;
 19  
 import java.util.Collection;
 20  
 import java.util.LinkedHashSet;
 21  
 import java.util.LinkedList;
 22  
 import java.util.List;
 23  
 import java.util.Set;
 24  
 
 25  
 import javax.xml.XMLConstants;
 26  
 import javax.xml.parsers.DocumentBuilder;
 27  
 import javax.xml.parsers.DocumentBuilderFactory;
 28  
 import javax.xml.parsers.ParserConfigurationException;
 29  
 
 30  
 import org.apache.commons.lang.exception.ExceptionUtils;
 31  
 import org.apache.log4j.Logger;
 32  
 import org.kuali.rice.core.api.impex.xml.XmlDoc;
 33  
 import org.kuali.rice.core.api.impex.xml.XmlDocCollection;
 34  
 import org.kuali.rice.core.api.impex.xml.XmlIngesterService;
 35  
 import org.kuali.rice.core.framework.impex.xml.XmlImpexRegistry;
 36  
 import org.kuali.rice.core.framework.impex.xml.XmlLoader;
 37  
 import org.xml.sax.EntityResolver;
 38  
 import org.xml.sax.ErrorHandler;
 39  
 import org.xml.sax.SAXException;
 40  
 import org.xml.sax.SAXParseException;
 41  
 
 42  
 
 43  
 /**
 44  
  * XmlIngesterService implementation which delegates to XmlDigesterService.
 45  
  * This implementation goes through some pains to ensure that the types of
 46  
  * xml doc (determined by file name convention) are issued to the XmlDigesterService
 47  
  * in a pre-ordained order in an effort to avoid dependency problems.  This implementation
 48  
  * is not responsible for knowing about the mappings between types and services, but
 49  
  * only the ordering of types, for the moment.
 50  
  * NOTE: when types are merged into a universal document, we need to decide how to handle
 51  
  * rollback if any specific type <i>in</i> that document fails, given that the current,
 52  
  * legacy implementation assumes that a given XmlDoc consists of one and only one type
 53  
  * and as such can be rolled back atomically.  For instance, if universal doc now contains
 54  
  * types A, B, and C, and it invokes ServiceA, ServiceB, and ServiceC in succession on the
 55  
  * entire document, and ServiceB throws an exception attempting to parse B content...
 56  
  * is it sufficient to rollback only that entry, or do we rollback the whole document
 57  
  * and consider it "tainted"? (not to mention whether we should roll back the entire collection
 58  
  * of which the document is a part - for now we do NOT rollback a collection or workflow data doc,
 59  
  * but it is merely moved to a "problem" directory by the poller.  the implementation does not yet
 60  
  * specifically note which document or type (and potentially eventually which entry) failed in the
 61  
  * collection or workflow data doc)
 62  
  *
 63  
  * NOTE: this service must be invoked only after all other services have initialized
 64  
  * this <i>should</i> be the case since the LifeCycle is kicked off after contextInitialized,
 65  
  * which <i>should</i> occur after Spring is actually done initializing.  But is it, considering
 66  
  * we are asynchronously initializing Spring?  There is a 30 second built-in delay before
 67  
  * XmlPoller is first run, but suffice it to say there is a possible race condition.
 68  
  *
 69  
  * @see org.kuali.rice.core.api.impex.xml.batch.XmlIngesterService
 70  
  * @see org.kuali.rice.core.impl.impex.xml.batch.XmlDigesterServiceImpl
 71  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 72  
  */
 73  0
 public class XmlIngesterServiceImpl implements XmlIngesterService {
 74  
         
 75  0
     private static final Logger LOG = Logger.getLogger(XmlIngesterServiceImpl.class);
 76  
 
 77  
     /**
 78  
      * The entity resolver to use during validation
 79  
      */
 80  0
     private EntityResolver resolver = new ClassLoaderEntityResolver();
 81  
 
 82  
     private XmlDigesterService digesterService;
 83  
     
 84  
     private XmlImpexRegistry xmlImpexRegistry;
 85  
 
 86  
     /**
 87  
      * Whether to validate at all
 88  
      */
 89  0
     private boolean validate = true;
 90  
 
 91  
     // ---- bean properties
 92  
 
 93  
     public void setXmlDigesterService(XmlDigesterService digesterService) {
 94  0
         this.digesterService = digesterService;
 95  0
     }
 96  
     
 97  
     public void setXmlImpexRegistry(XmlImpexRegistry xmlImpexRegistry) {
 98  0
             this.xmlImpexRegistry = xmlImpexRegistry;
 99  0
     }
 100  
 
 101  
     public void setEntityResolver(EntityResolver resolver) {
 102  0
         this.resolver = resolver;
 103  0
     }
 104  
 
 105  
     public void setValidate(boolean b) {
 106  0
         validate = b;
 107  0
     }
 108  
 
 109  
     // ---- implementation
 110  
 
 111  
     private static void addProcessingException(XmlDoc xmlDoc, String message, Throwable t) {
 112  0
         String msg = xmlDoc.getProcessingMessage();
 113  0
         if (msg == null) {
 114  0
             msg = "";
 115  
         }
 116  0
         msg += message + "\n" + ExceptionUtils.getFullStackTrace(t);
 117  0
         xmlDoc.setProcessingMessage(msg);
 118  0
     }
 119  
 
 120  
     private static void validate(final XmlDoc xmlDoc, EntityResolver resolver) throws ParserConfigurationException, IOException, SAXException {
 121  0
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 122  0
         dbf.setValidating(true);
 123  0
         dbf.setNamespaceAware( true );
 124  0
         dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI);
 125  0
         DocumentBuilder db = dbf.newDocumentBuilder();
 126  0
         db.setEntityResolver(resolver);
 127  0
         db.setErrorHandler(new ErrorHandler() {
 128  
             public void warning(SAXParseException se) {
 129  0
                 LOG.warn("Warning parsing xml doc " + xmlDoc, se);
 130  0
                 addProcessingException(xmlDoc, "Warning parsing xml doc " + xmlDoc, se);
 131  0
             }
 132  
             public void error(SAXParseException se) throws SAXException {
 133  0
                 LOG.error("Error parsing xml doc " + xmlDoc, se);
 134  0
                 addProcessingException(xmlDoc, "Error parsing xml doc " + xmlDoc, se);
 135  0
                 throw se;
 136  
             }
 137  
             public void fatalError(SAXParseException se) throws SAXException {
 138  0
                 LOG.error("Fatal error parsing xml doc " + xmlDoc, se);
 139  0
                 addProcessingException(xmlDoc, "Fatal error parsing xml doc " + xmlDoc, se);
 140  0
                 throw se;
 141  
             }
 142  
         });
 143  0
         db.parse(xmlDoc.getStream());
 144  0
     }
 145  
 
 146  
     /**
 147  
      * Validates (if possible) all XmlDocs, and accumulates only those
 148  
      * which either were not possible to validate, or passed validation.
 149  
      * @param collections collection of XmlDocCollection
 150  
      * @param resolver the entity resolver to use
 151  
      * @param successful xmldoccollections in which all docs successfully validated
 152  
      * @param failed xmldoccollections in which one or more docs failed validation
 153  
      */
 154  
     private static void validate(List<XmlDocCollection> collections, EntityResolver resolver, Set<XmlDocCollection> successful, Set<XmlDocCollection> failed) {
 155  
         // for every collection, validate all docs
 156  0
         for (XmlDocCollection collection : collections)
 157  
         {
 158  
 
 159  
             // for every xml doc in the collection, try to validate it
 160  0
             for (XmlDoc xmlDoc : collection.getXmlDocs())
 161  
             {
 162  
                 try
 163  
                 {
 164  0
                     validate(xmlDoc, resolver);
 165  0
                 } catch (Exception e)
 166  
                 {
 167  0
                     LOG.error("Error validating doc: " + xmlDoc, e);
 168  0
                     addProcessingException(xmlDoc, "Error validating doc: " + xmlDoc, e);
 169  
                     // validation failed, so add collection to successful set
 170  
                     // do not break here, so that we can attempt validation on all
 171  
                     // docs in a collection; since validation has no side-effects
 172  
                     // we might as well validate all the docs now instead of forcing
 173  
                     // the user to continually re-submit
 174  0
                     failed.add(collection);
 175  0
                 }
 176  
             }
 177  
 
 178  
             // all files validated, so add collection to successful set
 179  0
             successful.add(collection);
 180  
         }
 181  0
     }
 182  
 
 183  
     private void ingest(XmlLoader xmlLoader, Collection<XmlDocCollection> xmlDocCollections, String principalId, Set<Object> successful, Set<XmlDocCollection> failed) {
 184  0
         for (XmlDocCollection xmlDocCollection : xmlDocCollections)
 185  
         {
 186  
 
 187  0
             if (failed.contains(xmlDocCollection))
 188  
             {
 189  0
                 LOG.debug("Skipping " + xmlDocCollection.getFile() + "...");
 190  0
                 continue;
 191  
             }
 192  
 
 193  
             try
 194  
             {
 195  0
                 digesterService.digest(xmlLoader, xmlDocCollection, principalId);
 196  0
             } catch (Exception e)
 197  
             {
 198  0
                 LOG.error("Caught Exception loading xml data from " + xmlDocCollection.getFile() + ".  Will move associated file to problem dir.", e);
 199  0
                 failed.add(xmlDocCollection);
 200  0
             }
 201  
         }
 202  0
     }
 203  
 
 204  
     public Collection<XmlDocCollection> ingest(List<XmlDocCollection> collections) throws Exception {
 205  0
         return ingest(collections, null);
 206  
     }
 207  
 
 208  
     private void ingestThroughOrderedLoaders(Collection<XmlDocCollection> xmlDocCollections, String principalId, Set<Object> successful, Set<XmlDocCollection> failed) {
 209  0
         LOG.debug("Ingesting through ordered XmlLoaders");
 210  0
         List<XmlLoader> xmlLoaders = xmlImpexRegistry.getLoaders();
 211  0
         for (XmlLoader xmlLoader : xmlLoaders) {
 212  0
                 LOG.debug("Ingesting through ordered XmlLoader: " + xmlLoader);
 213  0
                 ingest(xmlLoader, xmlDocCollections, principalId, successful, failed);
 214  
         }
 215  0
     }
 216  
 
 217  
     public Collection<XmlDocCollection> ingest(List<XmlDocCollection> collections, String principalId) {
 218  0
         Set<XmlDocCollection> failed = new LinkedHashSet<XmlDocCollection>();
 219  
         // validate all the docs up-front because we will be iterating over them
 220  
         // multiple times: one for each XmlLoader.  If we delegated validation to
 221  
         // XmlDigesterService then the docs would re-validated over and over again,
 222  
         // for each XmlLoader
 223  0
         if (validate) {
 224  0
             Set<XmlDocCollection> successful = new LinkedHashSet<XmlDocCollection>();
 225  0
             validate(collections, resolver, successful, failed);
 226  0
             collections = new LinkedList<XmlDocCollection>(successful);
 227  
         }
 228  
 
 229  0
         Set<Object> successful = new LinkedHashSet<Object>();
 230  
         // ingest docs first by ordered services
 231  0
         ingestThroughOrderedLoaders(collections, principalId, successful, failed);
 232  
         // then by unordered services
 233  
 //        collections = new LinkedList(successful);
 234  
 
 235  
         //ingestThroughUnorderedLoaders(collections, user, successful, failed);
 236  
 
 237  0
         return failed;
 238  
     }
 239  
 }