1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.ashkay.strategies;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.net.URL;
21 import java.net.MalformedURLException;
22
23 import net.sf.ashkay.CacheEntry;
24 import net.sf.ashkay.CachingStrategy;
25
26 /***
27 * LastModifiedCachingStrategy attempts to locate a timestamped resource and check the last time that resource was modified.
28 * This is a tricky thing to do, and thusly, at the current time, LastModifiedCachingStrategy only works with Files and URLs.
29 * Since we can't guarantee the entry value will remain around, LastModifiedCachingStrategy only works with the entry key as
30 * well. So, LastModifiedCachingStrategy will work if you are looking up an object from the cache based on a file name or url
31 * key. It will work with keys that are of type java.io.File, java.net.URL and java.lang.String. If the key is a string,
32 * LastModifiedCachingStrategy will try to turn it first into a File and if that fails, a URL. Obviously, for URLs, your
33 * java.net stuff needs to be working (ie. the network is connected).
34 */
35 public class LastModifiedCachingStrategy implements CachingStrategy
36 {
37 private static final String PROPERTY_MODIFIED_TIME = LastModifiedCachingStrategy.class.getName() + ".fileModified";
38 /***
39 * Prepares the cache entry for caching with this strategy.<br>
40 * <b>NOTE:</b> Be Careful: a caching strategy <em>is</em> allowed to return
41 * a different CacheEntry, so make sure that you store the results of this
42 * method and don't assume the symantics of pass by reference.
43 */
44 public CacheEntry prepare(CacheEntry entry)
45 {
46 long modifiedTime = getLastModifiedTime(entry);
47 entry.addProperty(PROPERTY_MODIFIED_TIME, new Long(modifiedTime));
48 return entry;
49 }
50
51 /***
52 * Validates this cache entry for this caching strategy.
53 */
54 public boolean validate(CacheEntry entry)
55 {
56 boolean val = true;
57 Long entry_modified = (Long) entry.getProperty(PROPERTY_MODIFIED_TIME);
58 long file_modified = 0;
59
60 file_modified = getLastModifiedTime(entry);
61
62 if (entry_modified != null)
63 {
64 if (file_modified != entry_modified.longValue())
65 {
66 val = false;
67 }
68 }
69
70 return val;
71 }
72
73 private long getLastModifiedTime(CacheEntry entry)
74 {
75 long val = 0;
76
77 if (entry.getEntryKey() instanceof File)
78 {
79 val = getLastModifiedTime((File) entry.getEntryKey());
80 }
81 else if (entry.getEntryKey() instanceof URL)
82 {
83 URL url = (URL) entry.getEntryKey();
84 val = getLastModifiedTime(url);
85 }
86 else if (entry.getEntryKey() instanceof String)
87 {
88 String fileName = (String) entry.getEntryKey();
89 val = getLastModifiedTime(fileName);
90 }
91
92 return val;
93 }
94
95 private long getLastModifiedTime(String fileName)
96 {
97 File aFile = new File(fileName);
98 if (aFile.exists())
99 {
100 return getLastModifiedTime(aFile);
101 }
102 else
103 {
104 try
105 {
106 URL aUrl = new URL(fileName);
107 return getLastModifiedTime(aUrl);
108 }
109 catch (MalformedURLException e)
110 {
111
112 }
113 }
114 return 0;
115 }
116
117 private long getLastModifiedTime(URL url)
118 {
119 try
120 {
121 return url.openConnection().getLastModified();
122 }
123 catch (IOException e)
124 {
125 return 0;
126 }
127 }
128
129 private long getLastModifiedTime(File aFile)
130 {
131 return aFile.lastModified();
132 }
133 }