This project has retired. For details please refer to its
Attic page.
BaseLocaleUrlDefinitionDAO xref
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 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 public BaseLocaleUrlDefinitionDAO(ApplicationContext applicationContext) {
91 this.applicationContext = applicationContext;
92 lastModifiedDates = new HashMap<String, Long>();
93 }
94
95 public void setSources(List<ApplicationResource> sources) {
96
97 ArrayList<ApplicationResource> defaultSources = new ArrayList<ApplicationResource>();
98 for(ApplicationResource source: sources) {
99 if(Locale.ROOT.equals(source.getLocale())) {
100 defaultSources.add(source);
101 }
102 }
103 this.sources = defaultSources;
104 }
105
106 public void setReader(DefinitionsReader reader) {
107 this.reader = reader;
108 }
109
110
111 public boolean refreshRequired() {
112 boolean status = false;
113
114 Set<String> paths = lastModifiedDates.keySet();
115
116 try {
117 for (String path : paths) {
118 Long lastModifiedDate = lastModifiedDates.get(path);
119 ApplicationResource resource = applicationContext.getResource(path);
120 long newModDate = resource.getLastModified();
121 if (newModDate != lastModifiedDate) {
122 status = true;
123 break;
124 }
125 }
126 } catch (IOException e) {
127 log.warn("Exception while monitoring update times.", e);
128 return true;
129 }
130 return status;
131 }
132
133
134
135
136
137
138
139 protected Map<String, Definition> loadDefinitionsFromResource(ApplicationResource resource) {
140 Map<String, Definition> defsMap = null;
141
142 InputStream stream = null;
143 try {
144 lastModifiedDates.put(resource.getLocalePath(), resource
145 .getLastModified());
146
147
148
149 stream = resource.getInputStream();
150 defsMap = reader.read(stream);
151 } catch (FileNotFoundException e) {
152
153 if (log.isDebugEnabled()) {
154 log.debug("File " + resource.toString() + " not found, continue");
155 }
156 } catch (IOException e) {
157 throw new DefinitionsFactoryException(
158 "I/O error processing configuration.", e);
159 } finally {
160 try {
161 if (stream != null) {
162 stream.close();
163 }
164 } catch (IOException e) {
165 throw new DefinitionsFactoryException(
166 "I/O error closing " + resource.toString(), e);
167 }
168 }
169
170 return defsMap;
171 }
172 }