1
2
3
4 package org.rcfaces.core.internal.util;
5
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.Enumeration;
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.StringTokenizer;
18
19 import javax.faces.context.ExternalContext;
20
21 import org.apache.commons.digester.Digester;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.rcfaces.core.internal.RcfacesContext;
25
26
27
28
29
30
31 public class ConfigurationLoader {
32
33 public static final Log LOG = LogFactory.getLog(ConfigurationLoader.class);
34
35 public static final String RCFACES_CONFIG_FILENAME = RcfacesContext.RCFACES_CONFIG_FILENAME;
36
37 public static final String RCFACES_CONFIG_INIT_PARAMETER = RcfacesContext.RCFACES_CONFIG_FILES_PARAMETER;
38
39 private static final String FACES_CONFIG_RESOURCE_NAME = "faces-config.xml";
40
41 private static final String FACES_CONFIG_INIT_PARAMETER = "javax.faces.CONFIG_FILES";
42
43 private final List urls;
44
45 private ConfigurationLoader(List urls) {
46 this.urls = urls;
47 }
48
49 public void parse(Digester digester) {
50
51 if (urls.isEmpty()) {
52 return;
53 }
54
55 for (Iterator it = urls.iterator(); it.hasNext();) {
56 URL url = (URL) it.next();
57
58 InputStream inputStream;
59 try {
60 inputStream = url.openStream();
61
62 } catch (IOException e) {
63 LOG.error("Can not open url '" + url + "'.", e);
64 continue;
65 }
66
67 if (inputStream == null) {
68 LOG.debug("Configuration file '" + url + "' does not exist.");
69 continue;
70 }
71
72 parse(digester, inputStream, url.toString());
73 }
74 }
75
76 private void parse(Digester digester, InputStream inputStream,
77 String resourceName) {
78
79 LOG.debug("Loading config file '" + resourceName + "' ...");
80
81 try {
82 digester.parse(inputStream);
83
84 } catch (Throwable th) {
85 LOG.error("Can not parse config file '" + resourceName + "'.", th);
86 return;
87
88 } finally {
89 try {
90 inputStream.close();
91
92 } catch (IOException e) {
93 LOG.error("Can not close config file '" + resourceName + "'.",
94 e);
95 }
96 }
97
98 LOG.debug("Config file '" + resourceName + "' loaded !");
99 }
100
101 public static ConfigurationLoader scanConfigurationURLs(
102 ExternalContext externalContext, String resourceName,
103 String resourceNameInitParameter, List urls) {
104
105 if (urls == null) {
106 urls = Collections.emptyList();
107 }
108
109 urls = new ArrayList(urls);
110
111 ClassLoader contextClassLoader = Thread.currentThread()
112 .getContextClassLoader();
113 if (contextClassLoader != null) {
114 String metaInfResourceName = "META-INF/" + resourceName;
115
116 Enumeration enumeration = null;
117 try {
118 enumeration = contextClassLoader
119 .getResources(metaInfResourceName);
120
121 } catch (IOException e) {
122 LOG.error("Can not scan resources '" + metaInfResourceName
123 + "' into context classloader.");
124 }
125
126 if (enumeration != null) {
127 for (; enumeration.hasMoreElements();) {
128 URL url = (URL) enumeration.nextElement();
129
130 if (urls.contains(url)) {
131 continue;
132 }
133
134 LOG.debug("Resource '" + metaInfResourceName
135 + "' detected : " + url);
136
137 urls.add(url);
138 }
139 }
140 }
141
142 String configFilenames = externalContext
143 .getInitParameter(resourceNameInitParameter);
144
145 if (configFilenames == null) {
146 configFilenames = "";
147 }
148
149 configFilenames = "WEB-INF/" + resourceName + ", " + configFilenames;
150
151
152
153
154
155 StringTokenizer st = new StringTokenizer(configFilenames, ",;\t \r\n");
156
157 Set alreadyLoaded = new HashSet();
158
159 for (; st.hasMoreTokens();) {
160 String filename = st.nextToken();
161
162 if (alreadyLoaded.add(filename) == false) {
163 continue;
164 }
165
166 LOG.debug("An item of value of parameter '"
167 + resourceNameInitParameter + "' detected : '" + filename
168 + "'.");
169
170 URL url = contextClassLoader.getResource(filename);
171
172 if (url == null) {
173 try {
174 url = externalContext.getResource(filename);
175
176 } catch (MalformedURLException ex) {
177 LOG.error("Malformed url for filename '" + filename + "'.",
178 ex);
179 }
180 }
181
182 if (url == null) {
183 LOG.debug("Configuration file '" + filename
184 + "' does not exist.");
185 continue;
186 }
187
188 if (urls.contains(url)) {
189 continue;
190 }
191
192 LOG.debug("Configuration file '" + filename + "' registred.");
193
194 urls.add(url);
195 }
196
197 return new ConfigurationLoader(urls);
198 }
199
200 public static ConfigurationLoader scanFacesConfig(
201 ExternalContext externalContext, List urls) {
202
203 return scanConfigurationURLs(externalContext,
204 FACES_CONFIG_RESOURCE_NAME, FACES_CONFIG_INIT_PARAMETER, urls);
205 }
206
207 public static ConfigurationLoader scanRCFacesConfig(
208 ExternalContext externalContext, List urls) {
209
210 return scanConfigurationURLs(externalContext, RCFACES_CONFIG_FILENAME,
211 RCFACES_CONFIG_INIT_PARAMETER, urls);
212 }
213 }