1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.page.document.psml; |
18 |
|
|
19 |
|
import java.io.File; |
20 |
|
import java.io.FileInputStream; |
21 |
|
import java.io.FileNotFoundException; |
22 |
|
import java.io.FileOutputStream; |
23 |
|
import java.io.IOException; |
24 |
|
import java.io.InputStream; |
25 |
|
import java.io.InputStreamReader; |
26 |
|
import java.io.OutputStreamWriter; |
27 |
|
import java.io.Writer; |
28 |
|
|
29 |
|
import javax.xml.parsers.ParserConfigurationException; |
30 |
|
import javax.xml.parsers.SAXParser; |
31 |
|
import javax.xml.parsers.SAXParserFactory; |
32 |
|
|
33 |
|
import org.apache.commons.logging.Log; |
34 |
|
import org.apache.commons.logging.LogFactory; |
35 |
|
import org.apache.jetspeed.cache.file.FileCache; |
36 |
|
import org.apache.jetspeed.cache.file.FileCacheEntry; |
37 |
|
import org.apache.jetspeed.cache.file.FileCacheEventListener; |
38 |
|
import org.apache.jetspeed.om.common.SecurityConstraints; |
39 |
|
import org.apache.jetspeed.om.folder.psml.FolderImpl; |
40 |
|
import org.apache.jetspeed.om.page.Document; |
41 |
|
import org.apache.jetspeed.om.page.psml.AbstractBaseElement; |
42 |
|
import org.apache.jetspeed.page.PageNotFoundException; |
43 |
|
import org.apache.jetspeed.page.document.DocumentException; |
44 |
|
import org.apache.jetspeed.page.document.DocumentHandlerFactory; |
45 |
|
import org.apache.jetspeed.page.document.DocumentNotFoundException; |
46 |
|
import org.apache.jetspeed.page.document.FailedToDeleteDocumentException; |
47 |
|
import org.apache.jetspeed.page.document.FailedToUpdateDocumentException; |
48 |
|
import org.apache.jetspeed.page.document.Node; |
49 |
|
import org.apache.jetspeed.page.document.NodeException; |
50 |
|
import org.apache.xml.serialize.OutputFormat; |
51 |
|
import org.apache.xml.serialize.Serializer; |
52 |
|
import org.apache.xml.serialize.XMLSerializer; |
53 |
|
import org.castor.mapping.BindingType; |
54 |
|
import org.castor.mapping.MappingUnmarshaller; |
55 |
|
import org.exolab.castor.mapping.Mapping; |
56 |
|
import org.exolab.castor.mapping.MappingException; |
57 |
|
import org.exolab.castor.mapping.MappingLoader; |
58 |
|
import org.exolab.castor.xml.ClassDescriptorResolver; |
59 |
|
import org.exolab.castor.xml.ClassDescriptorResolverFactory; |
60 |
|
import org.exolab.castor.xml.MarshalException; |
61 |
|
import org.exolab.castor.xml.Marshaller; |
62 |
|
import org.exolab.castor.xml.SAX2EventProducer; |
63 |
|
import org.exolab.castor.xml.Unmarshaller; |
64 |
|
import org.exolab.castor.xml.ValidationException; |
65 |
|
import org.exolab.castor.xml.XMLClassDescriptorResolver; |
66 |
|
import org.xml.sax.Attributes; |
67 |
|
import org.xml.sax.ContentHandler; |
68 |
|
import org.xml.sax.InputSource; |
69 |
|
import org.xml.sax.Locator; |
70 |
|
import org.xml.sax.SAXException; |
71 |
|
import org.xml.sax.XMLReader; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
0 |
public class CastorFileSystemDocumentHandler implements org.apache.jetspeed.page.document.DocumentHandler, FileCacheEventListener |
86 |
|
{ |
87 |
0 |
private final static Log log = LogFactory.getLog(CastorFileSystemDocumentHandler.class); |
88 |
|
|
89 |
|
private final static String PSML_DOCUMENT_ENCODING = "UTF-8"; |
90 |
|
|
91 |
|
protected String documentType; |
92 |
|
protected Class expectedReturnType; |
93 |
|
protected String documentRoot; |
94 |
|
protected File documentRootDir; |
95 |
|
protected FileCache fileCache; |
96 |
|
|
97 |
|
private OutputFormat format; |
98 |
|
private final XMLReader xmlReader; |
99 |
|
private DocumentHandlerFactory handlerFactory; |
100 |
|
private ClassDescriptorResolver classDescriptorResolver; |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
public CastorFileSystemDocumentHandler( String mappingFile, String documentType, Class expectedReturnType, |
112 |
|
String documentRoot, FileCache fileCache ) throws FileNotFoundException,SAXException,ParserConfigurationException, MappingException |
113 |
|
{ |
114 |
0 |
super(); |
115 |
0 |
this.documentType = documentType; |
116 |
0 |
this.expectedReturnType = expectedReturnType; |
117 |
0 |
this.documentRoot = documentRoot; |
118 |
0 |
this.documentRootDir = new File(documentRoot); |
119 |
0 |
verifyPath(documentRootDir); |
120 |
0 |
this.fileCache = fileCache; |
121 |
0 |
this.fileCache.addListener(class="keyword">this); |
122 |
0 |
this.format = new OutputFormat(); |
123 |
0 |
format.setIndenting(true); |
124 |
0 |
format.setIndent(4); |
125 |
0 |
format.setEncoding(PSML_DOCUMENT_ENCODING); |
126 |
|
|
127 |
0 |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
128 |
0 |
SAXParser parser = factory.newSAXParser(); |
129 |
|
|
130 |
0 |
xmlReader = parser.getXMLReader(); |
131 |
0 |
xmlReader.setFeature("http://xml.org/sax/features/namespaces", false); |
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
0 |
createCastorClassDescriptorResolver(mappingFile); |
138 |
0 |
} |
139 |
|
|
140 |
|
public CastorFileSystemDocumentHandler( String mappingFile, String documentType, String expectedReturnType, |
141 |
|
String documentRoot, FileCache fileCache ) throws FileNotFoundException, ClassNotFoundException,SAXException,ParserConfigurationException, MappingException |
142 |
|
{ |
143 |
0 |
this(mappingFile, documentType, Class.forName(expectedReturnType), documentRoot, fileCache); |
144 |
0 |
} |
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
public Document getDocument( String name ) throws NodeException, DocumentNotFoundException |
159 |
|
{ |
160 |
0 |
return getDocument(name, true); |
161 |
|
} |
162 |
|
|
163 |
|
public void updateDocument( Document document ) throws FailedToUpdateDocumentException |
164 |
|
{ |
165 |
0 |
updateDocument(document, false); |
166 |
0 |
} |
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
protected void updateDocument( Document document, boolean systemUpdate) throws FailedToUpdateDocumentException |
178 |
|
{ |
179 |
|
|
180 |
0 |
if (document == null) |
181 |
|
{ |
182 |
0 |
log.warn("Recieved null Document to update"); |
183 |
0 |
return; |
184 |
|
} |
185 |
0 |
String path = document.getPath(); |
186 |
0 |
if (path == null) |
187 |
|
{ |
188 |
0 |
path = document.getId(); |
189 |
0 |
if (path == null) |
190 |
|
{ |
191 |
0 |
log.warn("Recieved Document with null path/id to update"); |
192 |
0 |
return; |
193 |
|
} |
194 |
0 |
document.setPath(path); |
195 |
|
} |
196 |
0 |
AbstractBaseElement documentImpl = (AbstractBaseElement)document; |
197 |
0 |
documentImpl.setHandlerFactory(handlerFactory); |
198 |
0 |
if (systemUpdate){ |
199 |
|
|
200 |
0 |
documentImpl.setPermissionsEnabled(false); |
201 |
0 |
documentImpl.setConstraintsEnabled(false); |
202 |
|
} else { |
203 |
0 |
documentImpl.setPermissionsEnabled(handlerFactory.getPermissionsEnabled()); |
204 |
0 |
documentImpl.setConstraintsEnabled(handlerFactory.getConstraintsEnabled()); |
205 |
|
} |
206 |
0 |
documentImpl.marshalling(); |
207 |
|
|
208 |
|
|
209 |
0 |
String fileName = path; |
210 |
0 |
if (!fileName.endsWith(this.documentType)) |
211 |
|
{ |
212 |
0 |
fileName = path + this.documentType; |
213 |
|
} |
214 |
0 |
File f = new File(this.documentRootDir, fileName); |
215 |
0 |
Writer writer = null; |
216 |
|
|
217 |
|
try |
218 |
|
{ |
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
0 |
writer = new OutputStreamWriter(class="keyword">new FileOutputStream(f), PSML_DOCUMENT_ENCODING); |
224 |
0 |
Serializer serializer = new XMLSerializer(writer, this.format); |
225 |
0 |
final ContentHandler handler = serializer.asContentHandler(); |
226 |
|
|
227 |
0 |
Marshaller marshaller = new Marshaller(class="keyword">new ContentHandler() |
228 |
|
{ |
229 |
|
private int menuDepth = 0; |
230 |
|
|
231 |
|
public void characters(char[] ch, int start, class="keyword">int length) throws SAXException |
232 |
|
{ |
233 |
|
handler.characters(ch, start, length); |
234 |
|
} |
235 |
|
|
236 |
|
public void endDocument() throws SAXException |
237 |
|
{ |
238 |
|
handler.endDocument(); |
239 |
|
} |
240 |
|
|
241 |
|
public void ignorableWhitespace(char[] ch, int start, class="keyword">int length) throws SAXException |
242 |
|
{ |
243 |
|
handler.ignorableWhitespace(ch, start, length); |
244 |
|
} |
245 |
|
|
246 |
|
public void processingInstruction(String target, String data) throws SAXException |
247 |
|
{ |
248 |
|
handler.processingInstruction(target, data); |
249 |
|
} |
250 |
|
|
251 |
|
public void setDocumentLocator(Locator locator) |
252 |
|
{ |
253 |
|
handler.setDocumentLocator(locator); |
254 |
|
} |
255 |
|
|
256 |
|
public void startDocument() throws SAXException |
257 |
|
{ |
258 |
|
handler.startDocument(); |
259 |
|
} |
260 |
|
|
261 |
|
public void endElement(String uri, String localName, String qName) throws SAXException { |
262 |
|
|
263 |
|
if (qName.equals("menu")) |
264 |
|
{ |
265 |
|
menuDepth--; |
266 |
|
} |
267 |
|
|
268 |
|
|
269 |
|
if ((menuDepth == 0) || !qName.equals("menu-element")) |
270 |
|
{ |
271 |
|
handler.endElement(uri, localName, qName); |
272 |
|
} |
273 |
|
} |
274 |
|
|
275 |
|
public void endPrefixMapping(String prefix) throws SAXException { |
276 |
|
} |
277 |
|
|
278 |
|
public void skippedEntity(String name) throws SAXException { |
279 |
|
handler.skippedEntity(name); |
280 |
|
} |
281 |
|
|
282 |
|
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { |
283 |
|
|
284 |
|
if ((menuDepth == 0) || !qName.equals("menu-element")) |
285 |
|
{ |
286 |
|
handler.startElement(uri,localName, qName, atts); |
287 |
|
} |
288 |
|
|
289 |
|
|
290 |
|
if (qName.equals("menu")) |
291 |
|
{ |
292 |
|
menuDepth++; |
293 |
|
} |
294 |
|
} |
295 |
|
|
296 |
|
public void startPrefixMapping(String prefix, String uri) throws SAXException { |
297 |
|
} |
298 |
|
}); |
299 |
0 |
marshaller.setResolver((XMLClassDescriptorResolver) classDescriptorResolver); |
300 |
|
|
301 |
0 |
marshaller.setValidation(false); |
302 |
0 |
marshaller.marshal(document); |
303 |
|
} |
304 |
0 |
catch (MarshalException e) |
305 |
|
{ |
306 |
0 |
log.error("Could not marshal the file " + f.getAbsolutePath(), e); |
307 |
0 |
throw new FailedToUpdateDocumentException(e); |
308 |
|
} |
309 |
0 |
catch (ValidationException e) |
310 |
|
{ |
311 |
0 |
log.error("Document " + f.getAbsolutePath() + " is not valid", e); |
312 |
0 |
throw new FailedToUpdateDocumentException(e); |
313 |
|
} |
314 |
0 |
catch (IOException e) |
315 |
|
{ |
316 |
0 |
log.error("Could not save the file " + f.getAbsolutePath(), e); |
317 |
0 |
throw new FailedToUpdateDocumentException(e); |
318 |
|
} |
319 |
0 |
catch (Exception e) |
320 |
|
{ |
321 |
0 |
log.error("Error while saving " + f.getAbsolutePath(), e); |
322 |
0 |
throw new FailedToUpdateDocumentException(e); |
323 |
|
} |
324 |
|
finally |
325 |
|
{ |
326 |
0 |
if (systemUpdate){ |
327 |
|
|
328 |
0 |
documentImpl.setPermissionsEnabled(handlerFactory.getPermissionsEnabled()); |
329 |
0 |
documentImpl.setConstraintsEnabled(handlerFactory.getConstraintsEnabled()); |
330 |
|
} |
331 |
|
try |
332 |
|
{ |
333 |
0 |
writer.close(); |
334 |
|
} |
335 |
0 |
catch (IOException e) |
336 |
|
{ |
337 |
0 |
} |
338 |
0 |
} |
339 |
|
|
340 |
0 |
} |
341 |
|
|
342 |
|
protected void createCastorClassDescriptorResolver(String mappingFile) throws MappingException |
343 |
|
{ |
344 |
0 |
Mapping mapping=null; |
345 |
|
try |
346 |
|
{ |
347 |
0 |
InputStream stream = getClass().getResourceAsStream(mappingFile); |
348 |
|
|
349 |
0 |
if (log.isDebugEnabled()) |
350 |
|
{ |
351 |
0 |
log.debug("Loading psml mapping file " + mappingFile); |
352 |
|
} |
353 |
|
|
354 |
0 |
mapping = new Mapping(); |
355 |
|
|
356 |
0 |
InputSource is = new InputSource(stream); |
357 |
|
|
358 |
0 |
is.setSystemId(mappingFile); |
359 |
0 |
mapping.loadMapping(is); |
360 |
|
} |
361 |
0 |
catch (Exception e) |
362 |
|
{ |
363 |
0 |
IllegalStateException ise = new IllegalStateException("Error in psml mapping creation"); |
364 |
0 |
ise.initCause(e); |
365 |
0 |
throw ise; |
366 |
0 |
} |
367 |
0 |
this.classDescriptorResolver = |
368 |
|
ClassDescriptorResolverFactory.createClassDescriptorResolver(BindingType.XML); |
369 |
0 |
MappingUnmarshaller mappingUnmarshaller = new MappingUnmarshaller(); |
370 |
0 |
MappingLoader mappingLoader = mappingUnmarshaller.getMappingLoader(mapping, BindingType.XML); |
371 |
0 |
classDescriptorResolver.setMappingLoader(mappingLoader); |
372 |
0 |
} |
373 |
|
|
374 |
|
protected Object unmarshallDocument( Class clazz, String path, String extension ) throws DocumentNotFoundException, |
375 |
|
DocumentException |
376 |
|
{ |
377 |
0 |
Document document = null; |
378 |
0 |
File f = null; |
379 |
0 |
if (path.endsWith(extension)) |
380 |
|
{ |
381 |
0 |
f = new File(this.documentRootDir, path); |
382 |
|
} |
383 |
|
else |
384 |
|
{ |
385 |
0 |
f = new File(this.documentRootDir, path + extension); |
386 |
|
} |
387 |
|
|
388 |
0 |
if (!f.exists()) |
389 |
|
{ |
390 |
0 |
throw new PageNotFoundException("Document not found: " + path); |
391 |
|
} |
392 |
|
|
393 |
|
try |
394 |
|
{ |
395 |
|
|
396 |
|
|
397 |
|
|
398 |
|
|
399 |
|
|
400 |
0 |
final InputSource readerInput = new InputSource(class="keyword">new InputStreamReader(class="keyword">new FileInputStream(f), PSML_DOCUMENT_ENCODING)); |
401 |
0 |
Unmarshaller unmarshaller = new Unmarshaller(); |
402 |
0 |
unmarshaller.setResolver((XMLClassDescriptorResolver) classDescriptorResolver); |
403 |
0 |
unmarshaller.setValidation(false); |
404 |
|
|
405 |
0 |
synchronized (xmlReader) |
406 |
|
{ |
407 |
0 |
document = (Document) unmarshaller.unmarshal(new SAX2EventProducer() |
408 |
|
{ |
409 |
|
public void setContentHandler(final ContentHandler handler) |
410 |
|
{ |
411 |
|
xmlReader.setContentHandler(new ContentHandler() |
412 |
|
{ |
413 |
|
private int menuDepth = 0; |
414 |
|
|
415 |
|
public void characters(char[] ch, int start, class="keyword">int length) throws SAXException |
416 |
|
{ |
417 |
|
handler.characters(ch, start, length); |
418 |
|
} |
419 |
|
|
420 |
|
public void endDocument() throws SAXException |
421 |
|
{ |
422 |
|
handler.endDocument(); |
423 |
|
} |
424 |
|
|
425 |
|
public void ignorableWhitespace(char[] ch, int start, class="keyword">int length) throws SAXException |
426 |
|
{ |
427 |
|
handler.ignorableWhitespace(ch, start, length); |
428 |
|
} |
429 |
|
|
430 |
|
public void processingInstruction(String target, String data) throws SAXException |
431 |
|
{ |
432 |
|
handler.processingInstruction(target, data); |
433 |
|
} |
434 |
|
|
435 |
|
public void setDocumentLocator(Locator locator) |
436 |
|
{ |
437 |
|
handler.setDocumentLocator(locator); |
438 |
|
} |
439 |
|
|
440 |
|
public void startDocument() throws SAXException |
441 |
|
{ |
442 |
|
handler.startDocument(); |
443 |
|
} |
444 |
|
|
445 |
|
public void endElement(String uri, String localName, String qName) throws SAXException { |
446 |
|
|
447 |
|
handler.endElement(uri,localName,qName); |
448 |
|
|
449 |
|
|
450 |
|
|
451 |
|
if (qName.equals("menu")) |
452 |
|
{ |
453 |
|
menuDepth--; |
454 |
|
if (menuDepth > 0) |
455 |
|
{ |
456 |
|
handler.endElement(null,class="keyword">null,"menu-element"); |
457 |
|
} |
458 |
|
} |
459 |
|
else if ((menuDepth > 0) && |
460 |
|
(qName.equals("options") || qName.equals("separator") || |
461 |
|
qName.equals("include") || qName.equals("exclude"))) |
462 |
|
{ |
463 |
|
handler.endElement(null,class="keyword">null,"menu-element"); |
464 |
|
} |
465 |
|
} |
466 |
|
|
467 |
|
public void endPrefixMapping(String prefix) throws SAXException { |
468 |
|
} |
469 |
|
|
470 |
|
public void skippedEntity(String name) throws SAXException { |
471 |
|
handler.skippedEntity(name); |
472 |
|
} |
473 |
|
|
474 |
|
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { |
475 |
|
|
476 |
|
|
477 |
|
|
478 |
|
|
479 |
|
if (qName.equals("menu")) |
480 |
|
{ |
481 |
|
if (menuDepth > 0) |
482 |
|
{ |
483 |
|
handler.startElement(null,class="keyword">null,"menu-element", null); |
484 |
|
} |
485 |
|
menuDepth++; |
486 |
|
} |
487 |
|
else if ((menuDepth > 0) && |
488 |
|
(qName.equals("options") || qName.equals("separator") || |
489 |
|
qName.equals("include") || qName.equals("exclude"))) |
490 |
|
{ |
491 |
|
handler.startElement(null,class="keyword">null,"menu-element", null); |
492 |
|
} |
493 |
|
|
494 |
|
|
495 |
|
handler.startElement(null,class="keyword">null, qName, atts); |
496 |
|
} |
497 |
|
|
498 |
|
public void startPrefixMapping(String prefix, String uri) throws SAXException { |
499 |
|
} |
500 |
|
}); |
501 |
|
} |
502 |
|
public void start() throws SAXException |
503 |
|
{ |
504 |
|
try |
505 |
|
{ |
506 |
|
xmlReader.parse(readerInput); |
507 |
|
} |
508 |
|
catch (IOException ioe) |
509 |
|
{ |
510 |
|
throw new SAXException(ioe); |
511 |
|
} |
512 |
|
} |
513 |
|
}); |
514 |
0 |
} |
515 |
|
|
516 |
0 |
document.setPath(path); |
517 |
0 |
AbstractBaseElement documentImpl = (AbstractBaseElement)document; |
518 |
0 |
documentImpl.setHandlerFactory(handlerFactory); |
519 |
0 |
documentImpl.setPermissionsEnabled(handlerFactory.getPermissionsEnabled()); |
520 |
0 |
documentImpl.setConstraintsEnabled(handlerFactory.getConstraintsEnabled()); |
521 |
0 |
documentImpl.unmarshalled(); |
522 |
0 |
if (document.isDirty()){ |
523 |
0 |
updateDocument(document, true); |
524 |
0 |
document.setDirty(false); |
525 |
|
} |
526 |
|
} |
527 |
0 |
catch (IOException e) |
528 |
|
{ |
529 |
0 |
log.error("Could not load the file " + f.getAbsolutePath(), e); |
530 |
0 |
throw new PageNotFoundException("Could not load the file " + f.getAbsolutePath(), e); |
531 |
|
} |
532 |
0 |
catch (MarshalException e) |
533 |
|
{ |
534 |
0 |
log.error("Could not unmarshal the file " + f.getAbsolutePath(), e); |
535 |
0 |
throw new PageNotFoundException("Could not unmarshal the file " + f.getAbsolutePath(), e); |
536 |
|
} |
537 |
0 |
catch (ValidationException e) |
538 |
|
{ |
539 |
0 |
log.error("Document " + f.getAbsolutePath() + " is not valid", e); |
540 |
0 |
throw new DocumentNotFoundException("Document " + f.getAbsolutePath() + " is not valid", e); |
541 |
0 |
} |
542 |
|
|
543 |
|
|
544 |
0 |
if (document == null) |
545 |
|
{ |
546 |
0 |
throw new DocumentNotFoundException("Document not found: " + path); |
547 |
|
} |
548 |
|
else |
549 |
|
{ |
550 |
0 |
if (!clazz.isAssignableFrom(document.getClass())) |
551 |
|
{ |
552 |
0 |
throw new ClassCastException(document.getClass().getName() + " must implement or extend " |
553 |
|
+ clazz.getName()); |
554 |
|
} |
555 |
0 |
return document; |
556 |
|
} |
557 |
|
} |
558 |
|
|
559 |
|
protected void verifyPath( File path ) throws FileNotFoundException |
560 |
|
{ |
561 |
0 |
if (path == null) |
562 |
|
{ |
563 |
0 |
throw new IllegalArgumentException("Page root cannot be null"); |
564 |
|
} |
565 |
|
|
566 |
0 |
if (!path.exists()) |
567 |
|
{ |
568 |
0 |
throw new FileNotFoundException("Could not locate root pages path " + path.getAbsolutePath()); |
569 |
|
} |
570 |
0 |
} |
571 |
|
|
572 |
|
|
573 |
|
|
574 |
|
|
575 |
|
|
576 |
|
|
577 |
|
|
578 |
|
|
579 |
|
|
580 |
|
|
581 |
|
|
582 |
|
public void removeDocument( Document document ) throws DocumentNotFoundException, FailedToDeleteDocumentException |
583 |
|
{ |
584 |
|
|
585 |
0 |
if (document == null) |
586 |
|
{ |
587 |
0 |
log.warn("Recieved null Document to remove"); |
588 |
0 |
return; |
589 |
|
} |
590 |
0 |
String path = document.getPath(); |
591 |
0 |
if (path == null) |
592 |
|
{ |
593 |
0 |
path = document.getId(); |
594 |
0 |
if (path == null) |
595 |
|
{ |
596 |
0 |
log.warn("Recieved Document with null path/id to remove"); |
597 |
0 |
return; |
598 |
|
} |
599 |
|
} |
600 |
|
|
601 |
|
|
602 |
0 |
String fileName = path; |
603 |
0 |
if (!fileName.endsWith(this.documentType)) |
604 |
|
{ |
605 |
0 |
fileName = path + this.documentType; |
606 |
|
} |
607 |
0 |
File file = new File(this.documentRootDir, fileName); |
608 |
0 |
if (!file.delete()) |
609 |
|
{ |
610 |
0 |
throw new FailedToDeleteDocumentException(file.getAbsolutePath()+" document cannot be deleted."); |
611 |
|
} |
612 |
|
|
613 |
|
|
614 |
0 |
fileCache.remove(path); |
615 |
|
|
616 |
|
|
617 |
0 |
AbstractNode documentImpl = (AbstractNode)document; |
618 |
0 |
documentImpl.setParent(null); |
619 |
0 |
} |
620 |
|
|
621 |
|
|
622 |
|
|
623 |
|
|
624 |
|
|
625 |
|
|
626 |
|
|
627 |
|
|
628 |
|
|
629 |
|
|
630 |
|
|
631 |
|
|
632 |
|
|
633 |
|
|
634 |
|
public Document getDocument( String name, boolean fromCache ) throws DocumentNotFoundException, NodeException |
635 |
|
{ |
636 |
0 |
Document document = null; |
637 |
0 |
if (fromCache) |
638 |
|
{ |
639 |
0 |
Object obj = fileCache.getDocument(name); |
640 |
0 |
document = (Document) obj; |
641 |
0 |
if (document == null) |
642 |
|
{ |
643 |
0 |
document = (Document) unmarshallDocument(expectedReturnType, name, documentType); |
644 |
0 |
addToCache(name, document); |
645 |
|
} |
646 |
0 |
} |
647 |
|
else |
648 |
|
{ |
649 |
0 |
document = (Document) unmarshallDocument(expectedReturnType, name, documentType); |
650 |
|
} |
651 |
|
|
652 |
0 |
return document; |
653 |
|
} |
654 |
|
|
655 |
|
|
656 |
|
|
657 |
|
|
658 |
|
|
659 |
|
|
660 |
|
|
661 |
|
|
662 |
|
|
663 |
|
protected void addToCache( String path, Object objectToCache ) |
664 |
|
{ |
665 |
0 |
synchronized (fileCache) |
666 |
|
{ |
667 |
|
|
668 |
|
|
669 |
|
try |
670 |
|
{ |
671 |
0 |
fileCache.put(path, objectToCache, this.documentRootDir); |
672 |
|
|
673 |
|
} |
674 |
0 |
catch (java.io.IOException e) |
675 |
|
{ |
676 |
0 |
log.error("Error putting document: " + e); |
677 |
0 |
IllegalStateException ise = new IllegalStateException("Error storing Document in the FileCache: " |
678 |
|
+ e.toString()); |
679 |
0 |
ise.initCause(e); |
680 |
0 |
} |
681 |
0 |
} |
682 |
0 |
} |
683 |
|
|
684 |
|
|
685 |
|
|
686 |
|
|
687 |
|
|
688 |
|
|
689 |
|
|
690 |
|
|
691 |
|
|
692 |
|
|
693 |
|
public void refresh( FileCacheEntry entry ) throws Exception |
694 |
|
{ |
695 |
0 |
log.debug("Entry is refreshing: " + entry.getFile().getName()); |
696 |
|
|
697 |
0 |
if (entry.getDocument() instanceof Document && ((Document) entry.getDocument()).getPath().endsWith(documentType)) |
698 |
|
{ |
699 |
0 |
Document document = (Document) entry.getDocument(); |
700 |
0 |
Document freshDoc = getDocument(document.getPath(), false); |
701 |
0 |
Node parent = ((AbstractNode)document).getParent(false); |
702 |
|
|
703 |
0 |
freshDoc.setParent(parent); |
704 |
0 |
if(parent instanceof FolderImpl) |
705 |
|
{ |
706 |
0 |
FolderImpl folder = (FolderImpl) parent; |
707 |
0 |
folder.getAllNodes().add(freshDoc); |
708 |
|
} |
709 |
|
|
710 |
0 |
freshDoc.setPath(document.getPath()); |
711 |
0 |
entry.setDocument(freshDoc); |
712 |
|
} |
713 |
|
|
714 |
0 |
} |
715 |
|
|
716 |
|
|
717 |
|
|
718 |
|
|
719 |
|
|
720 |
|
|
721 |
|
|
722 |
|
|
723 |
|
|
724 |
|
|
725 |
|
public void evict( FileCacheEntry entry ) throws Exception |
726 |
|
{ |
727 |
|
|
728 |
|
|
729 |
0 |
} |
730 |
|
|
731 |
|
|
732 |
|
|
733 |
|
|
734 |
|
|
735 |
|
|
736 |
|
|
737 |
|
|
738 |
|
|
739 |
|
public String getType() |
740 |
|
{ |
741 |
0 |
return documentType; |
742 |
|
} |
743 |
|
|
744 |
|
|
745 |
|
|
746 |
|
|
747 |
|
|
748 |
|
|
749 |
|
|
750 |
|
|
751 |
|
|
752 |
|
public DocumentHandlerFactory getHandlerFactory() |
753 |
|
{ |
754 |
0 |
return handlerFactory; |
755 |
|
} |
756 |
|
|
757 |
|
|
758 |
|
|
759 |
|
|
760 |
|
|
761 |
|
|
762 |
|
|
763 |
|
|
764 |
|
|
765 |
|
public void setHandlerFactory(DocumentHandlerFactory factory) |
766 |
|
{ |
767 |
0 |
this.handlerFactory = factory; |
768 |
0 |
} |
769 |
|
|
770 |
|
} |