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