View Javadoc
1   /**
2    * Copyright 2005-2015 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.kew.batch;
17  
18  import org.apache.commons.io.IOUtils;
19  import org.kuali.rice.core.api.CoreApiServiceLocator;
20  import org.kuali.rice.core.api.config.ConfigurationException;
21  import org.kuali.rice.core.api.impex.xml.FileXmlDocCollection;
22  import org.kuali.rice.core.api.impex.xml.XmlDoc;
23  import org.kuali.rice.core.api.impex.xml.XmlDocCollection;
24  import org.kuali.rice.core.api.util.ClasspathOrFileResourceLoader;
25  import org.kuali.rice.kew.api.WorkflowRuntimeException;
26  import org.springframework.core.io.Resource;
27  
28  import java.io.File;
29  import java.io.FileInputStream;
30  import java.io.FileOutputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  
37  /**
38   * This is a description of what this class does - arh14 don't forget to fill this in. 
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   *
42   */
43  public class KEWXmlDataLoader {
44      /*protected void loadXmlFile(String fileName) {
45          if (fileName.indexOf('/') < 0) {
46              this.loadXmlFile(getClass(), fileName);
47          } else {
48              loadXmlStream(getClass().getClassLoader().getResourceAsStream(fileName));
49          }
50      }*/
51  
52      /**
53       * Loads the XML specified by the resource string, which should be in Spring resource notation
54       * @param resource resource string in Spring resource notation
55       * @throws Exception 
56       */
57      public static void loadXmlResource(String resource) throws Exception {
58          Resource res = new ClasspathOrFileResourceLoader().getResource(resource);
59          InputStream xmlFile = res.getInputStream();
60          if (xmlFile == null) {
61              throw new ConfigurationException("Didn't find resource " + resource);
62          }
63          try {
64              loadXmlStream(xmlFile);
65          } finally {
66              xmlFile.close();
67          }
68  
69      }
70  
71      /**
72       * Loads the XML resource from the classloader, from the package of the specified class
73       * if the class appears relative, or from the root of the classloader if it contains a slash
74       * @param clazz the class whose package should be used to qualify the path
75       * @param path the package-relative path of the XML resource
76       * @throws Exception
77       */
78      public static void loadXmlClassLoaderResource(Class clazz, String path) throws Exception {
79          if (path.indexOf('/') < 0) {
80              loadXmlPackageResource(clazz, path);
81          } else {
82              loadXmlClassLoaderResource(clazz.getClassLoader(), path);
83          }
84      }
85  
86      /**
87       * Loads the XML resource from the classloader, from the package of the specified class.
88       * @param clazz the class whose package should be used to qualify the path
89       * @param path the package-relative path of the XML resource
90       * @throws Exception
91       */
92      public static void loadXmlPackageResource(Class clazz, String path) throws Exception {
93          InputStream xmlFile = clazz.getResourceAsStream(path);
94          if (xmlFile == null) {
95              throw new WorkflowRuntimeException("Didn't find resource " + path);
96          }
97          try {
98              loadXmlStream(xmlFile);
99          } finally {
100             xmlFile.close();
101         }
102     }
103 
104     /**
105      * Loads the XML resource from the specified classloader
106      * @param classloader the classloader from which to load the resource
107      * @param path the classloader path of the XML resource
108      * @throws Exception
109      */
110     public static void loadXmlClassLoaderResource(ClassLoader classloader, String path) throws Exception {
111         InputStream xmlFile = classloader.getResourceAsStream(path);
112         if (xmlFile == null) {
113             throw new WorkflowRuntimeException("Didn't find resource " + path);
114         }
115         try {
116             loadXmlStream(xmlFile);
117         } finally {
118             xmlFile.close();
119         }
120     }
121 
122     /**
123      * Load the XML file from the file system.
124      * 
125      * @param fileName the path to the XML file
126      * @throws Exception
127      */
128     public static void loadXmlFile(String fileName) throws Exception {
129         FileInputStream fis = new FileInputStream(fileName);
130         try {
131             loadXmlStream(fis);
132         } finally {
133             fis.close();
134         }
135     }
136 
137     /**
138      * Loads XML from a stream
139      * @param xmlStream the XML byte stream
140      * @throws Exception
141      */
142     public static void loadXmlStream(InputStream xmlStream) throws Exception {
143        List<XmlDocCollection> xmlFiles = new ArrayList<XmlDocCollection>();
144         XmlDocCollection docCollection = getFileXmlDocCollection(xmlStream, "UnitTestTemp");
145         //XmlDocCollection docCollection = new StreamXmlDocCollection(xmlStream);
146         xmlFiles.add(docCollection);
147         CoreApiServiceLocator.getXmlIngesterService().ingest(xmlFiles);
148         for (XmlDoc doc: docCollection.getXmlDocs()) {
149             if (!doc.isProcessed()) {
150                 throw new RuntimeException("Failed to ingest xml doc: " + doc.getName());
151             }
152         }
153     }
154 
155     /**
156      * Helper method that turns a stream into a FileXmlDocCollection by first making a copy on the file system.
157      * @param xmlFile
158      * @param tempFileName
159      * @return
160      * @throws IOException
161      */
162     public static FileXmlDocCollection getFileXmlDocCollection(InputStream stream, String tempFileName) throws IOException {
163         if (stream == null) {
164             throw new RuntimeException("Stream is null!");
165         }
166 
167         File temp = File.createTempFile(tempFileName, ".xml");
168         temp.deleteOnExit();
169 
170         FileOutputStream fos = new FileOutputStream(temp);
171         try {
172             IOUtils.copy(stream, fos);
173         } finally {
174             fos.close();
175         }
176 
177         return new FileXmlDocCollection(temp);
178     }
179 }