1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.batch; |
18 | |
|
19 | |
import java.io.BufferedReader; |
20 | |
import java.io.File; |
21 | |
import java.io.FileReader; |
22 | |
import java.io.FileWriter; |
23 | |
import java.io.IOException; |
24 | |
import java.text.Format; |
25 | |
import java.text.SimpleDateFormat; |
26 | |
import java.util.ArrayList; |
27 | |
import java.util.Calendar; |
28 | |
import java.util.Collection; |
29 | |
import java.util.Date; |
30 | |
import java.util.Iterator; |
31 | |
import java.util.List; |
32 | |
|
33 | |
import org.kuali.rice.core.api.impex.xml.DirectoryXmlDocCollection; |
34 | |
import org.kuali.rice.core.api.impex.xml.FileXmlDocCollection; |
35 | |
import org.kuali.rice.core.api.impex.xml.XmlDocCollection; |
36 | |
import org.kuali.rice.core.api.impex.xml.XmlIngesterService; |
37 | |
import org.kuali.rice.core.api.impex.xml.ZipXmlDocCollection; |
38 | |
import org.kuali.rice.core.api.services.CoreApiServiceLocator; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 0 | public class XmlPollerServiceImpl implements XmlPollerService { |
60 | |
|
61 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger |
62 | |
.getLogger(XmlPollerServiceImpl.class); |
63 | 0 | private static final Format DIR_FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-SSS"); |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | 0 | private int pollIntervalSecs = 5 * 60; |
69 | |
|
70 | |
|
71 | |
|
72 | 0 | private int initialDelaySecs = 30; |
73 | |
|
74 | |
|
75 | |
|
76 | |
private String xmlPendingLocation; |
77 | |
|
78 | |
|
79 | |
|
80 | |
private String xmlCompletedLocation; |
81 | |
|
82 | |
|
83 | |
|
84 | |
private String xmlProblemLocation; |
85 | |
|
86 | |
private String xmlParentDirectory; |
87 | |
private static final String PENDING_MOVE_FAILED_ARCHIVE_FILE = "movesfailed"; |
88 | |
private static final String NEW_LINE = "\n"; |
89 | |
|
90 | |
public void run() { |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | 0 | LOG.debug("checking for xml data files..."); |
96 | 0 | File[] files = getXmlPendingDir().listFiles(); |
97 | 0 | if (files == null || files.length == 0) { |
98 | 0 | return; |
99 | |
} |
100 | 0 | LOG.info("Found " + files.length + " files to ingest."); |
101 | 0 | List<XmlDocCollection> collections = new ArrayList<XmlDocCollection>(); |
102 | 0 | for (File file : files) |
103 | |
{ |
104 | 0 | if (file.isDirectory()) |
105 | |
{ |
106 | 0 | collections.add(new DirectoryXmlDocCollection(file)); |
107 | 0 | } else if (file.getName().equals(PENDING_MOVE_FAILED_ARCHIVE_FILE)) |
108 | |
{ |
109 | |
|
110 | 0 | continue; |
111 | 0 | } else if (file.getName().toLowerCase().endsWith(".zip")) |
112 | |
{ |
113 | |
try |
114 | |
{ |
115 | 0 | collections.add(new ZipXmlDocCollection(file)); |
116 | 0 | } catch (IOException ioe) |
117 | |
{ |
118 | 0 | LOG.error("Unable to load file: " + file); |
119 | 0 | } |
120 | 0 | } else if (file.getName().endsWith(".xml")) |
121 | |
{ |
122 | 0 | collections.add(new FileXmlDocCollection(file)); |
123 | |
} else |
124 | |
{ |
125 | 0 | LOG.warn("Ignoring extraneous file in xml pending directory: " + file); |
126 | |
} |
127 | |
} |
128 | |
|
129 | |
|
130 | 0 | Iterator collectionsIt = collections.iterator(); |
131 | 0 | Collection<XmlDocCollection> culled = new ArrayList<XmlDocCollection>(); |
132 | 0 | while (collectionsIt.hasNext()) { |
133 | 0 | XmlDocCollection container = (XmlDocCollection) collectionsIt.next(); |
134 | |
|
135 | 0 | if (inPendingMoveFailedArchive(container.getFile())) { |
136 | 0 | LOG.info("Ignoring previously processed resource: " + container); |
137 | 0 | culled.add(container); |
138 | |
} |
139 | 0 | } |
140 | 0 | collections.removeAll(culled); |
141 | |
|
142 | 0 | if (collections.size() == 0) { |
143 | 0 | LOG.debug("No valid new resources found to ingest"); |
144 | 0 | return; |
145 | |
} |
146 | |
|
147 | 0 | Date LOAD_TIME = Calendar.getInstance().getTime(); |
148 | |
|
149 | 0 | File completeDir = new File(getXmlCompleteDir(), DIR_FORMAT.format(LOAD_TIME)); |
150 | 0 | File failedDir = new File(getXmlProblemDir(), DIR_FORMAT.format(LOAD_TIME)); |
151 | |
|
152 | |
|
153 | 0 | Collection failed = null; |
154 | |
try { |
155 | 0 | failed = CoreApiServiceLocator.getXmlIngesterService().ingest(collections); |
156 | 0 | } catch (Exception e) { |
157 | 0 | LOG.error("Error ingesting data", e); |
158 | |
|
159 | 0 | } |
160 | |
|
161 | |
|
162 | 0 | LOG.info("Moving files..."); |
163 | 0 | collectionsIt = collections.iterator(); |
164 | 0 | while (collectionsIt.hasNext()) { |
165 | 0 | XmlDocCollection container = (XmlDocCollection) collectionsIt.next(); |
166 | 0 | LOG.debug("container: " + container); |
167 | |
try { |
168 | |
|
169 | |
|
170 | 0 | container.close(); |
171 | 0 | } catch (IOException ioe) { |
172 | 0 | LOG.warn("Error closing " + container, ioe); |
173 | 0 | } |
174 | 0 | if (failed.contains(container)) { |
175 | |
|
176 | |
|
177 | 0 | if (container.getFile() != null) { |
178 | 0 | LOG.error("Moving " + container.getFile() + " to problem dir."); |
179 | 0 | if ((!failedDir.isDirectory() && !failedDir.mkdirs()) |
180 | |
|| !moveFile(failedDir, container.getFile())) { |
181 | 0 | LOG.error("Could not move: " + container.getFile()); |
182 | 0 | recordUnmovablePendingFile(container.getFile(), LOAD_TIME); |
183 | |
} |
184 | |
} |
185 | |
} else { |
186 | 0 | if (container.getFile() != null) { |
187 | 0 | LOG.info("Moving " + container.getFile() + " to loaded dir."); |
188 | 0 | if((!completeDir.isDirectory() && !completeDir.mkdirs()) |
189 | |
|| !moveFile(completeDir, container.getFile())){ |
190 | 0 | LOG.error("Could not move: " + container.getFile()); |
191 | 0 | recordUnmovablePendingFile(container.getFile(), LOAD_TIME); |
192 | |
} |
193 | |
} |
194 | |
} |
195 | 0 | } |
196 | 0 | } |
197 | |
|
198 | |
private boolean inPendingMoveFailedArchive(File xmlDataFile){ |
199 | 0 | if (xmlDataFile == null) return false; |
200 | 0 | BufferedReader inFile = null; |
201 | 0 | File movesFailedFile = new File(getXmlPendingDir(), PENDING_MOVE_FAILED_ARCHIVE_FILE); |
202 | 0 | if (!movesFailedFile.isFile()) return false; |
203 | |
try { |
204 | 0 | inFile = new BufferedReader(new FileReader(movesFailedFile)); |
205 | |
String line; |
206 | |
|
207 | 0 | while((line = inFile.readLine()) != null){ |
208 | 0 | String trimmedLine = line.trim(); |
209 | 0 | if(trimmedLine.equals(xmlDataFile.getName()) || |
210 | |
trimmedLine.startsWith(xmlDataFile.getName() + "=")) { |
211 | 0 | return true; |
212 | |
} |
213 | 0 | } |
214 | 0 | } catch (IOException e){ |
215 | 0 | LOG.warn("Error reading file " + movesFailedFile); |
216 | |
|
217 | |
} finally { |
218 | 0 | if (inFile != null) try { |
219 | 0 | inFile.close(); |
220 | 0 | } catch (Exception e) { |
221 | 0 | LOG.warn("Error closing buffered reader for " + movesFailedFile); |
222 | 0 | } |
223 | |
} |
224 | |
|
225 | 0 | return false; |
226 | |
} |
227 | |
|
228 | |
private boolean recordUnmovablePendingFile(File unMovablePendingFile, Date dateLoaded){ |
229 | 0 | boolean recorded = false; |
230 | 0 | FileWriter archiveFile = null; |
231 | |
try{ |
232 | 0 | archiveFile = new FileWriter(new File(getXmlPendingDir(), PENDING_MOVE_FAILED_ARCHIVE_FILE), true); |
233 | 0 | archiveFile.write(unMovablePendingFile.getName() + "=" + dateLoaded.getTime() + NEW_LINE); |
234 | 0 | recorded = true; |
235 | 0 | } catch (IOException e){ |
236 | 0 | LOG.error("Unable to record unmovable pending file " + unMovablePendingFile.getName() + "in the archive file " + PENDING_MOVE_FAILED_ARCHIVE_FILE); |
237 | |
} finally { |
238 | 0 | if (archiveFile != null) { |
239 | |
try { |
240 | 0 | archiveFile.close(); |
241 | 0 | } catch (IOException ioe) { |
242 | 0 | LOG.error("Error closing unmovable pending file", ioe); |
243 | 0 | } |
244 | |
} |
245 | |
} |
246 | 0 | return recorded; |
247 | |
} |
248 | |
|
249 | |
private boolean moveFile(File toDirectory, File fileToMove){ |
250 | 0 | boolean moved = true; |
251 | 0 | if (!fileToMove.renameTo(new File(toDirectory.getPath(), fileToMove.getName()))){ |
252 | 0 | LOG.error("Unable to move file " + fileToMove.getName() + " to directory " + toDirectory.getPath()); |
253 | 0 | moved = false; |
254 | |
} |
255 | 0 | return moved; |
256 | |
} |
257 | |
|
258 | |
private File getXmlPendingDir() { |
259 | 0 | return new File(getXmlPendingLocation()); |
260 | |
} |
261 | |
|
262 | |
private File getXmlCompleteDir() { |
263 | 0 | return new File(getXmlCompletedLocation()); |
264 | |
} |
265 | |
|
266 | |
private File getXmlProblemDir() { |
267 | 0 | return new File(getXmlProblemLocation()); |
268 | |
} |
269 | |
|
270 | |
public String getXmlCompletedLocation() { |
271 | 0 | return xmlCompletedLocation; |
272 | |
} |
273 | |
|
274 | |
public void setXmlCompletedLocation(String xmlCompletedLocation) { |
275 | 0 | this.xmlCompletedLocation = xmlCompletedLocation; |
276 | 0 | } |
277 | |
|
278 | |
public String getXmlPendingLocation() { |
279 | 0 | return xmlPendingLocation; |
280 | |
} |
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
public void setXmlPendingLocation(String xmlPendingLocation) { |
288 | 0 | this.xmlPendingLocation = xmlPendingLocation; |
289 | 0 | } |
290 | |
|
291 | |
public String getXmlProblemLocation() { |
292 | 0 | return xmlProblemLocation; |
293 | |
} |
294 | |
|
295 | |
public void setXmlProblemLocation(String xmlProblemLocation) { |
296 | 0 | this.xmlProblemLocation = xmlProblemLocation; |
297 | 0 | } |
298 | |
public String getXmlParentDirectory() { |
299 | 0 | return xmlParentDirectory; |
300 | |
} |
301 | |
public void setXmlParentDirectory(String xmlDataParentDirectory) { |
302 | 0 | this.xmlParentDirectory = xmlDataParentDirectory; |
303 | 0 | } |
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
public void setPollIntervalSecs(int seconds) { |
310 | 0 | this.pollIntervalSecs = seconds; |
311 | 0 | } |
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
public int getPollIntervalSecs() { |
318 | 0 | return this.pollIntervalSecs; |
319 | |
} |
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
public void setInitialDelaySecs(int seconds) { |
326 | 0 | this.initialDelaySecs = seconds; |
327 | 0 | } |
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
public int getInitialDelaySecs() { |
334 | 0 | return this.initialDelaySecs; |
335 | |
} |
336 | |
} |