KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > action > VFSXmlConfigurationProvider


1 package org.jpublish.action;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5
6 import com.anthonyeden.lib.util.MessageUtilities;
7 import com.opensymphony.xwork.config.providers.XmlConfigurationProvider;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.apache.commons.vfs.FileContent;
11 import org.apache.commons.vfs.FileObject;
12 import org.apache.commons.vfs.FileSystemException;
13 import org.jpublish.JPublishEngine;
14
15 /**
16  * VFS implementation of the XWork XmlConfigurationProvider.
17  *
18  * @author Anthony Eden
19  */

20
21 public class VFSXmlConfigurationProvider extends XmlConfigurationProvider {
22
23     private static final Log LOG = LogFactory.getLog(VFSXmlConfigurationProvider.class);
24
25     private FileObject file;
26     private long lastModified = -1;
27
28     /**
29      * Construct a new VFSXmlConfigurationProvider.
30      *
31      * @param file The VFS FileObject
32      */

33
34     public VFSXmlConfigurationProvider(FileObject file) {
35         super(file.getName().getBaseName());
36         this.file = file;
37     }
38
39     /**
40      * Tells whether the ConfigurationProvider should reload its configuration. This method should only be called if
41      * ConfigurationManager.isReloadingConfigs() is true.
42      *
43      * @return true if the file has been changed since the last time we read it
44      */

45
46     public boolean needsReload() {
47         try {
48             FileContent content = file.getContent();
49             long fileLastModified = content.getLastModifiedTime();
50             if (fileLastModified != lastModified) {
51                 lastModified = fileLastModified;
52                 return true;
53             } else {
54                 return false;
55             }
56         } catch (FileSystemException e) {
57             LOG.warn("Could not determine last modified", e);
58             return true;
59         }
60     }
61
62     /**
63      * Get the InputStream for the given file name.
64      *
65      * @param fileName The file name
66      * @return The InputStream
67      */

68
69     protected InputStream JavaDoc getInputStream(String JavaDoc fileName) {
70         try {
71             if (LOG.isDebugEnabled()) {
72                 LOG.debug("Loading file: " + fileName);
73             }
74             FileObject fileObject = file.getParent().resolveFile(fileName);
75             FileContent content = fileObject.getContent();
76             return content.getInputStream();
77         } catch (IOException JavaDoc e) {
78             Object JavaDoc[] args = {fileName, e.getMessage()};
79             String JavaDoc msg = MessageUtilities.getMessage(getClass(),
80                     JPublishEngine.MESSAGE_PACKAGE, "ioError", args);
81             throw new RuntimeException JavaDoc(msg);
82         }
83     }
84
85 }
86
Popular Tags