1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration2.reloading;
19
20 import org.apache.commons.configuration2.ex.ConfigurationRuntimeException;
21 import org.apache.commons.configuration2.io.FileHandler;
22 import org.apache.commons.configuration2.io.FileSystem;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.commons.vfs2.FileObject;
26 import org.apache.commons.vfs2.FileSystemException;
27 import org.apache.commons.vfs2.FileSystemManager;
28 import org.apache.commons.vfs2.VFS;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class VFSFileHandlerReloadingDetector extends FileHandlerReloadingDetector {
46
47 private final Log log = LogFactory.getLog(getClass());
48
49
50
51
52
53 public VFSFileHandlerReloadingDetector() {
54 }
55
56
57
58
59
60
61
62
63 public VFSFileHandlerReloadingDetector(final FileHandler handler, final long refreshDelay) {
64 super(handler, refreshDelay);
65 }
66
67
68
69
70
71
72
73 public VFSFileHandlerReloadingDetector(final FileHandler handler) {
74 super(handler);
75 }
76
77
78
79
80
81 @Override
82 protected long getLastModificationDate() {
83 final FileObject file = getFileObject();
84 try {
85 if (file == null || !file.exists()) {
86 return 0;
87 }
88
89 return file.getContent().getLastModifiedTime();
90 } catch (final FileSystemException ex) {
91 log.error("Unable to get last modified time for" + file.getName().getURI(), ex);
92 return 0;
93 }
94 }
95
96
97
98
99
100
101
102 protected FileObject getFileObject() {
103 if (!getFileHandler().isLocationDefined()) {
104 return null;
105 }
106
107 try {
108 final FileSystemManager fsManager = VFS.getManager();
109 final String uri = resolveFileURI();
110 if (uri == null) {
111 throw new ConfigurationRuntimeException("Unable to determine file to monitor");
112 }
113 return fsManager.resolveFile(uri);
114 } catch (final FileSystemException fse) {
115 final String msg = "Unable to monitor " + getFileHandler().getURL().toString();
116 log.error(msg);
117 throw new ConfigurationRuntimeException(msg, fse);
118 }
119 }
120
121
122
123
124
125
126 protected String resolveFileURI() {
127 final FileSystem fs = getFileHandler().getFileSystem();
128 return fs.getPath(null, getFileHandler().getURL(), getFileHandler().getBasePath(), getFileHandler().getFileName());
129 }
130 }