1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.view.facelets.impl; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.io.InputStream; |
23 | |
import java.net.URL; |
24 | |
import java.net.URLConnection; |
25 | |
import java.util.HashMap; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
import javax.faces.view.facelets.FaceletCache; |
29 | |
import javax.faces.view.facelets.FaceletException; |
30 | |
|
31 | |
import org.apache.myfaces.shared.resource.ResourceLoaderUtils; |
32 | |
import org.apache.myfaces.view.facelets.util.ParameterCheck; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 0 | class FaceletCacheImpl extends FaceletCache<DefaultFacelet> |
52 | |
{ |
53 | |
|
54 | |
private static final long INFINITE_DELAY = -1; |
55 | |
private static final long NO_CACHE_DELAY = 0; |
56 | |
|
57 | |
private Map<String, DefaultFacelet> _facelets; |
58 | |
|
59 | |
private Map<String, DefaultFacelet> _viewMetadataFacelets; |
60 | |
|
61 | |
private long _refreshPeriod; |
62 | |
|
63 | |
FaceletCacheImpl(long refreshPeriod) |
64 | 0 | { |
65 | 0 | _refreshPeriod = refreshPeriod < 0 ? INFINITE_DELAY : refreshPeriod * 1000; |
66 | |
|
67 | 0 | _facelets = new HashMap<String, DefaultFacelet>(); |
68 | |
|
69 | 0 | _viewMetadataFacelets = new HashMap<String, DefaultFacelet>(); |
70 | 0 | } |
71 | |
|
72 | |
@Override |
73 | |
public DefaultFacelet getFacelet(URL url) throws IOException |
74 | |
{ |
75 | 0 | ParameterCheck.notNull("url", url); |
76 | |
|
77 | 0 | String key = url.toString(); |
78 | |
|
79 | 0 | DefaultFacelet f = _facelets.get(key); |
80 | |
|
81 | 0 | if (f == null || this.needsToBeRefreshed(f)) |
82 | |
{ |
83 | |
|
84 | 0 | f = getMemberFactory().newInstance(url); |
85 | 0 | if (_refreshPeriod != NO_CACHE_DELAY) |
86 | |
{ |
87 | 0 | Map<String, DefaultFacelet> newLoc = new HashMap<String, DefaultFacelet>(_facelets); |
88 | 0 | newLoc.put(key, f); |
89 | 0 | _facelets = newLoc; |
90 | |
} |
91 | |
} |
92 | |
|
93 | 0 | return f; |
94 | |
} |
95 | |
|
96 | |
@Override |
97 | |
public boolean isFaceletCached(URL url) |
98 | |
{ |
99 | 0 | return _facelets.containsKey(url.toString()); |
100 | |
} |
101 | |
|
102 | |
@Override |
103 | |
public DefaultFacelet getViewMetadataFacelet(URL url) throws IOException |
104 | |
{ |
105 | 0 | ParameterCheck.notNull("url", url); |
106 | |
|
107 | 0 | String key = url.toString(); |
108 | |
|
109 | 0 | DefaultFacelet f = _viewMetadataFacelets.get(key); |
110 | |
|
111 | 0 | if (f == null || this.needsToBeRefreshed(f)) |
112 | |
{ |
113 | |
|
114 | 0 | f = getMetadataMemberFactory().newInstance(url); |
115 | 0 | if (_refreshPeriod != NO_CACHE_DELAY) |
116 | |
{ |
117 | 0 | Map<String, DefaultFacelet> newLoc = new HashMap<String, DefaultFacelet>(_viewMetadataFacelets); |
118 | 0 | newLoc.put(key, f); |
119 | 0 | _viewMetadataFacelets = newLoc; |
120 | |
} |
121 | |
} |
122 | |
|
123 | 0 | return f; |
124 | |
} |
125 | |
|
126 | |
@Override |
127 | |
public boolean isViewMetadataFaceletCached(URL url) |
128 | |
{ |
129 | 0 | return _viewMetadataFacelets.containsKey(url.toString()); |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
protected boolean needsToBeRefreshed(DefaultFacelet facelet) |
140 | |
{ |
141 | |
|
142 | 0 | if (_refreshPeriod == NO_CACHE_DELAY) |
143 | |
{ |
144 | 0 | return true; |
145 | |
} |
146 | |
|
147 | |
|
148 | 0 | if (_refreshPeriod == INFINITE_DELAY) |
149 | |
{ |
150 | 0 | return false; |
151 | |
} |
152 | |
|
153 | 0 | long target = facelet.getCreateTime() + _refreshPeriod; |
154 | 0 | if (System.currentTimeMillis() > target) |
155 | |
{ |
156 | |
|
157 | |
|
158 | 0 | URLConnection conn = null; |
159 | |
try |
160 | |
{ |
161 | 0 | conn = facelet.getSource().openConnection(); |
162 | 0 | long lastModified = ResourceLoaderUtils.getResourceLastModified(conn); |
163 | |
|
164 | 0 | return lastModified == 0 || lastModified > target; |
165 | |
} |
166 | 0 | catch (IOException e) |
167 | |
{ |
168 | 0 | throw new FaceletException("Error Checking Last Modified for " + facelet.getAlias(), e); |
169 | |
} |
170 | |
finally |
171 | |
{ |
172 | |
|
173 | 0 | if (conn != null) |
174 | |
{ |
175 | |
try |
176 | |
{ |
177 | 0 | InputStream is = conn.getInputStream(); |
178 | 0 | if (is != null) |
179 | |
{ |
180 | 0 | is.close(); |
181 | |
} |
182 | |
} |
183 | 0 | catch (IOException e) |
184 | |
{ |
185 | |
|
186 | 0 | } |
187 | |
} |
188 | |
} |
189 | |
} |
190 | |
|
191 | 0 | return false; |
192 | |
} |
193 | |
} |