1
2
3
4
5 package org.rcfaces.core.internal.repository;
6
7 import java.io.IOException;
8 import java.util.Collection;
9 import java.util.Enumeration;
10 import java.util.Locale;
11 import java.util.StringTokenizer;
12
13 import javax.servlet.ServletConfig;
14 import javax.servlet.ServletException;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.rcfaces.core.internal.repository.IHierarchicalRepository.ISet;
19 import org.rcfaces.core.internal.repository.IRepository.IFile;
20 import org.rcfaces.core.lang.OrderedSet;
21
22
23
24
25
26 public abstract class HierarchicalRepositoryServlet extends RepositoryServlet {
27
28 private static final String REVISION = "$Revision: 1.1 $";
29
30 private static final long serialVersionUID = -8178257331664082960L;
31
32 private static final Log LOG = LogFactory
33 .getLog(HierarchicalRepositoryServlet.class);
34
35 private static final String SET_PREFIX = ".sets";
36
37 private static final String MODULES_PREFIX = ".modules";
38
39 private static final String GROUP_ALL_DEFAULT_VALUE = null;
40
41 private static final String BOOT_SET_DEFAULT_VALUE = null;
42
43 public void init(ServletConfig config) throws ServletException {
44 super.init(config);
45
46 IHierarchicalRepository hierarchicalRepository = getHierarchicalRepository();
47
48 String groupAll = config.getInitParameter(getParameterPrefix()
49 + MODULES_PREFIX + ".GROUP_ALL_FILES");
50 if (groupAll == null) {
51 groupAll = getGroupAllDefaultValue();
52 }
53 if ("false".equals(groupAll)) {
54 LOG.debug("Disabled module group.");
55
56 } else if (groupAll != null) {
57 if ("all".equalsIgnoreCase(groupAll)) {
58 LOG.debug("Concat all files for all modules.");
59
60 } else {
61 LOG.debug("Concat all files for modules: " + groupAll);
62 }
63
64 for (StringTokenizer st = new StringTokenizer(groupAll, ";, "); st
65 .hasMoreTokens();) {
66 String name = st.nextToken();
67
68 if ("all".equalsIgnoreCase(name)) {
69 IHierarchicalRepository.IModule modules[] = hierarchicalRepository
70 .listModules();
71 for (int i = 0; i < modules.length; i++) {
72 modules[i].setGroupAllFiles(true);
73 }
74
75 continue;
76 }
77
78 IHierarchicalRepository.IModule module = hierarchicalRepository
79 .getModuleByName(name);
80 if (module == null) {
81 throw new IllegalArgumentException("Can not find module '"
82 + name + "' to enable 'groupAll'.");
83 }
84
85 module.setGroupAllFiles(true);
86 }
87 }
88
89 String bootSet = config.getInitParameter(getParameterPrefix()
90 + ".BOOT_SET");
91 boolean defaultSettings = false;
92 if (bootSet == null) {
93 bootSet = getBootSetDefaultValue();
94 defaultSettings = true;
95 }
96
97 if (bootSet != null) {
98 StringTokenizer st = new StringTokenizer(bootSet, ",; \n\t\r");
99 if (st.countTokens() != 1) {
100 throw new ServletException(
101 "Only one SET can be specified as BOOT_SET !");
102 }
103
104 bootSet = st.nextToken();
105
106 String bootSetName = getParameterPrefix() + SET_PREFIX + "."
107 + bootSet;
108 String parameterValue = config.getInitParameter(bootSetName);
109
110 IHierarchicalRepository.ISet set;
111
112 if (parameterValue != null) {
113 set = initializeModuleSet(bootSet, parameterValue);
114
115 } else if (defaultSettings) {
116 set = initializeDefaultSet();
117
118 } else {
119 throw new ServletException("Set specified by " + bootSetName
120 + " is not defined !");
121 }
122
123 if (set == null) {
124 throw new IllegalArgumentException("Can not find boot set '"
125 + bootSet + "'.");
126 }
127
128 hierarchicalRepository.setBootSet(set);
129 }
130
131 Enumeration parameterNames = config.getInitParameterNames();
132 String setPrefix = getParameterPrefix() + SET_PREFIX;
133 for (; parameterNames.hasMoreElements();) {
134 String parameterName = (String) parameterNames.nextElement();
135
136 if (parameterName.startsWith(setPrefix) == false) {
137 continue;
138 }
139
140 String parameterValue = config.getInitParameter(parameterName);
141
142 parameterName = parameterName.substring(setPrefix.length() + 1);
143 if (parameterName.length() < 1) {
144 continue;
145 }
146
147 if (parameterName.equals(bootSet)) {
148 continue;
149 }
150
151 LOG.debug("Group modules '" + parameterValue + "' into the set '"
152 + parameterName + "'.");
153
154 initializeModuleSet(parameterName, parameterValue);
155 }
156 }
157
158 protected String getBootSetDefaultValue() {
159 return BOOT_SET_DEFAULT_VALUE;
160 }
161
162 protected String getGroupAllDefaultValue() {
163 return GROUP_ALL_DEFAULT_VALUE;
164 }
165
166 protected final IHierarchicalRepository getHierarchicalRepository() {
167 return (IHierarchicalRepository) getRepository();
168 }
169
170 private ISet initializeModuleSet(String setName, String moduleNames) {
171
172 Collection l = new OrderedSet();
173
174 if ("all".equals(moduleNames.trim())) {
175
176
177
178
179
180
181 if (true) {
182 throw new IllegalArgumentException("Not yet implemented !");
183 }
184 } else {
185 StringTokenizer st = new StringTokenizer(moduleNames, ",; \n\t\r");
186
187 for (; st.hasMoreTokens();) {
188 String moduleName = st.nextToken();
189 l.add(moduleName);
190 }
191 }
192
193 String uri = getSetURI(setName);
194
195 return getHierarchicalRepository().declareSet(setName, uri,
196 (String[]) l.toArray(new String[l.size()]));
197 }
198
199 protected ISet initializeDefaultSet() {
200 return null;
201 }
202
203 protected abstract String getSetURI(String setName);
204
205
206
207
208
209
210 protected abstract class HierarchicalRecord extends Record {
211 private static final String REVISION = "$Revision: 1.1 $";
212
213 public HierarchicalRecord(IFile file, Locale locale) {
214 super(file, locale);
215 }
216
217 public void verifyModifications() {
218
219 boolean modified = false;
220
221 if (file instanceof IHierarchicalRepository.ISet) {
222 modified = verifySetModifications((IHierarchicalRepository.ISet) file);
223
224 } else if (file instanceof IHierarchicalRepository.IModule) {
225 modified = verifyModuleModifications((IHierarchicalRepository.IModule) file);
226
227 } else {
228 super.verifyModifications();
229 return;
230 }
231
232 if (modified == false) {
233 return;
234 }
235
236 resetRecord();
237 }
238
239 private boolean verifyModuleModifications(
240 IHierarchicalRepository.IModule module) {
241 return verifyFilesModifications(module.listDependencies());
242 }
243
244 private boolean verifySetModifications(IHierarchicalRepository.ISet set) {
245 return verifyFilesModifications(set.listDependencies());
246 }
247
248 public byte[] getBuffer() throws IOException {
249 if (buffer != null) {
250 return buffer;
251 }
252
253 if (file instanceof IHierarchicalRepository.ISet) {
254 return getSetBuffer();
255 }
256
257 if (file instanceof IHierarchicalRepository.IModule) {
258 return getModuleBuffer();
259 }
260
261 return super.getBuffer();
262 }
263
264 private byte[] getModuleBuffer() throws IOException {
265 IHierarchicalRepository.IModule module = (IHierarchicalRepository.IModule) file;
266
267 return getFilesBuffer(module.listDependencies());
268 }
269
270 private byte[] getSetBuffer() throws IOException {
271 IHierarchicalRepository.ISet set = (IHierarchicalRepository.ISet) file;
272
273 return getFilesBuffer(set.listDependencies());
274 }
275 }
276 }