1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portal.expire;
18
19
20 import org.apache.jetspeed.cache.disk.DiskCacheEntry;
21 import org.apache.jetspeed.cache.disk.DiskCacheUtils;
22 import org.apache.jetspeed.cache.disk.JetspeedDiskCache;
23 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
24 import org.apache.jetspeed.services.logging.JetspeedLogger;
25
26
27 import java.io.IOException;
28 import java.io.Serializable;
29
30
31 /***
32 A generic class for watching a file and determining if it has changed.
33
34 @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
35 @version $Id: FileWatcher.java,v 1.16 2004/02/23 03:24:40 jford Exp $
36 */
37 public class FileWatcher implements Serializable
38 {
39
40 /***
41 * Static initialization of the logger for this class
42 */
43 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(FileWatcher.class.getName());
44
45
46
47 public static final String EXCEPTION_URL_NOT_NULL =
48 "URL can NOT be null here.";
49
50 public static final String EXCEPTION_URL_NOT_IN_CACHE =
51 "The URL you specified within the disk cache does not exist: ";
52
53
54
55
56 /***
57 The URL on which this FileWatchExpire is based.
58 */
59 private String url = "";
60
61 /***
62 The last time this files URL has been modified on disk.
63 */
64 private long lastModified = 0;
65
66 /***
67 The object that relies on this FileWatcher
68 */
69 private String parent = "";
70
71 /***
72 Create a FileWatcher with no parent info.
73
74 @see #FileWatcher( String, String )
75
76 @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
77 @version $Id: FileWatcher.java,v 1.16 2004/02/23 03:24:40 jford Exp $
78 */
79 public FileWatcher( String url ) throws IOException {
80 this( url, null );
81 }
82
83 /***
84 Create a new FileWatcher to watch the given URL.
85
86 @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
87 @version $Id: FileWatcher.java,v 1.16 2004/02/23 03:24:40 jford Exp $
88 */
89 public FileWatcher( String url,
90 String parent ) throws IOException {
91
92 if ( url == null ) {
93 throw new IOException( EXCEPTION_URL_NOT_NULL );
94 }
95
96 if ( DiskCacheUtils.isRemote( url ) &&
97 DiskCacheUtils.isCached( url ) == false ) {
98
99 throw new IOException( EXCEPTION_URL_NOT_IN_CACHE + url );
100 }
101
102
103 try {
104 this.lastModified = JetspeedDiskCache.getInstance().getEntry( url ).
105 getLastModified();
106 } catch (Throwable e)
107 {
108 logger.error( "Unable to set last modified on url " + url, e );
109 }
110
111 this.url = url;
112 this.parent = parent;
113 }
114
115 /***
116 Return true if the URL on which this is based has changed.
117
118 @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
119 @version $Id: FileWatcher.java,v 1.16 2004/02/23 03:24:40 jford Exp $
120 */
121 public boolean hasChanged() {
122
123 try {
124
125
126 if ( this.lastModified == 0 ) {
127
128 DiskCacheEntry entry = JetspeedDiskCache.getInstance().getEntry( url );
129
130 this.lastModified = entry.getLastModified();
131
132 return false;
133
134 }
135
136
137
138 long recent = JetspeedDiskCache.getInstance()
139 .getEntry( url ).getLastModified();
140
141
142 if ( recent == 0 ||
143 this.lastModified < recent ) {
144
145 if ( logger.isInfoEnabled() )
146 {
147 String message = "";
148
149 if ( this.parent != null ) {
150 message = this.parent + ": ";
151 }
152
153 message += "REFRESH: Expiring Portlet because it's URL has been modified on disk -> " + url;
154
155 logger.info( message );
156 }
157 return true;
158 }
159
160 } catch ( IOException e ) {
161 logger.error("Exception", e );
162 return false;
163 }
164
165
166 return false;
167
168 }
169
170 }