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