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