1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
package org.apache.tiles.definition.dao; |
23 | |
|
24 | |
import java.io.FileNotFoundException; |
25 | |
import java.io.IOException; |
26 | |
import java.io.InputStream; |
27 | |
import java.util.ArrayList; |
28 | |
import java.util.HashMap; |
29 | |
import java.util.List; |
30 | |
import java.util.Locale; |
31 | |
import java.util.Map; |
32 | |
import java.util.Set; |
33 | |
|
34 | |
import org.apache.tiles.Definition; |
35 | |
import org.apache.tiles.definition.DefinitionsFactoryException; |
36 | |
import org.apache.tiles.definition.DefinitionsReader; |
37 | |
import org.apache.tiles.definition.RefreshMonitor; |
38 | |
import org.apache.tiles.request.ApplicationContext; |
39 | |
import org.apache.tiles.request.ApplicationResource; |
40 | |
import org.slf4j.Logger; |
41 | |
import org.slf4j.LoggerFactory; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public abstract class BaseLocaleUrlDefinitionDAO implements |
51 | |
DefinitionDAO<Locale>, RefreshMonitor { |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 29 | private final Logger log = LoggerFactory |
57 | |
.getLogger(BaseLocaleUrlDefinitionDAO.class); |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
protected List<ApplicationResource> sources; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
protected Map<String, Long> lastModifiedDates; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
protected DefinitionsReader reader; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
protected ApplicationContext applicationContext; |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | 29 | public BaseLocaleUrlDefinitionDAO(ApplicationContext applicationContext) { |
91 | 29 | this.applicationContext = applicationContext; |
92 | 29 | lastModifiedDates = new HashMap<String, Long>(); |
93 | 29 | } |
94 | |
|
95 | |
public void setSources(List<ApplicationResource> sources) { |
96 | |
|
97 | 27 | ArrayList<ApplicationResource> defaultSources = new ArrayList<ApplicationResource>(); |
98 | 27 | for(ApplicationResource source: sources) { |
99 | 51 | if(Locale.ROOT.equals(source.getLocale())) { |
100 | 51 | defaultSources.add(source); |
101 | |
} |
102 | 51 | } |
103 | 27 | this.sources = defaultSources; |
104 | 27 | } |
105 | |
|
106 | |
public void setReader(DefinitionsReader reader) { |
107 | 28 | this.reader = reader; |
108 | 28 | } |
109 | |
|
110 | |
|
111 | |
public boolean refreshRequired() { |
112 | 2 | boolean status = false; |
113 | |
|
114 | 2 | Set<String> paths = lastModifiedDates.keySet(); |
115 | |
|
116 | |
try { |
117 | 2 | for (String path : paths) { |
118 | 2 | Long lastModifiedDate = lastModifiedDates.get(path); |
119 | 2 | ApplicationResource resource = applicationContext.getResource(path); |
120 | 2 | long newModDate = resource.getLastModified(); |
121 | 2 | if (newModDate != lastModifiedDate) { |
122 | 1 | status = true; |
123 | 1 | break; |
124 | |
} |
125 | 1 | } |
126 | 0 | } catch (IOException e) { |
127 | 0 | log.warn("Exception while monitoring update times.", e); |
128 | 0 | return true; |
129 | 2 | } |
130 | 2 | return status; |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
protected Map<String, Definition> loadDefinitionsFromResource(ApplicationResource resource) { |
140 | 177 | Map<String, Definition> defsMap = null; |
141 | |
|
142 | 177 | InputStream stream = null; |
143 | |
try { |
144 | 177 | lastModifiedDates.put(resource.getLocalePath(), resource |
145 | |
.getLastModified()); |
146 | |
|
147 | |
|
148 | |
|
149 | 177 | stream = resource.getInputStream(); |
150 | 176 | defsMap = reader.read(stream); |
151 | 1 | } catch (FileNotFoundException e) { |
152 | |
|
153 | 1 | if (log.isDebugEnabled()) { |
154 | 0 | log.debug("File " + resource.toString() + " not found, continue"); |
155 | |
} |
156 | 0 | } catch (IOException e) { |
157 | 0 | throw new DefinitionsFactoryException( |
158 | |
"I/O error processing configuration.", e); |
159 | |
} finally { |
160 | 0 | try { |
161 | 177 | if (stream != null) { |
162 | 176 | stream.close(); |
163 | |
} |
164 | 0 | } catch (IOException e) { |
165 | 0 | throw new DefinitionsFactoryException( |
166 | |
"I/O error closing " + resource.toString(), e); |
167 | 177 | } |
168 | |
} |
169 | |
|
170 | 177 | return defsMap; |
171 | |
} |
172 | |
} |