src/share/jaxws_classes/com/sun/xml/internal/ws/assembler/MetroConfigLoader.java

changeset 368
0989ad8c0860
child 384
8f2986ff0235
equal deleted inserted replaced
366:8c0b6bccfe47 368:0989ad8c0860
1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.xml.internal.ws.assembler;
27
28 import com.sun.istack.internal.NotNull;
29 import com.sun.istack.internal.logging.Logger;
30 import com.sun.xml.internal.ws.api.ResourceLoader;
31 import com.sun.xml.internal.ws.api.server.Container;
32 import com.sun.xml.internal.ws.resources.TubelineassemblyMessages;
33 import com.sun.xml.internal.ws.runtime.config.MetroConfig;
34 import com.sun.xml.internal.ws.runtime.config.TubeFactoryList;
35 import com.sun.xml.internal.ws.runtime.config.TubelineDefinition;
36 import com.sun.xml.internal.ws.runtime.config.TubelineMapping;
37 import com.sun.xml.internal.ws.util.xml.XmlUtil;
38
39 import javax.xml.bind.JAXBContext;
40 import javax.xml.bind.JAXBElement;
41 import javax.xml.bind.Unmarshaller;
42 import javax.xml.stream.XMLInputFactory;
43 import javax.xml.ws.WebServiceException;
44 import java.lang.reflect.Method;
45 import java.net.MalformedURLException;
46 import java.net.URI;
47 import java.net.URISyntaxException;
48 import java.net.URL;
49 import java.util.logging.Level;
50
51 /**
52 * This class is responsible for locating and loading Metro configuration files
53 * (both application jaxws-tubes.xml and default jaxws-tubes-default.xml).
54 * <p/>
55 * Once the configuration is loaded the class is able to resolve which tubeline
56 * configuration belongs to each endpoint or endpoint client. This information is
57 * then used in {@link TubelineAssemblyController} to construct the list of
58 * {@link TubeCreator} objects that are used in the actual tubeline construction.
59 *
60 * @author Marek Potociar <marek.potociar at sun.com>
61 */
62 // TODO Move the logic of this class directly into MetroConfig class.
63 class MetroConfigLoader {
64
65 private static final Logger LOGGER = Logger.getLogger(MetroConfigLoader.class);
66
67 private MetroConfigName defaultTubesConfigNames;
68
69 private static interface TubeFactoryListResolver {
70
71 TubeFactoryList getFactories(TubelineDefinition td);
72 }
73
74 private static final TubeFactoryListResolver ENDPOINT_SIDE_RESOLVER = new TubeFactoryListResolver() {
75
76 public TubeFactoryList getFactories(TubelineDefinition td) {
77 return (td != null) ? td.getEndpointSide() : null;
78 }
79 };
80 private static final TubeFactoryListResolver CLIENT_SIDE_RESOLVER = new TubeFactoryListResolver() {
81
82 public TubeFactoryList getFactories(TubelineDefinition td) {
83 return (td != null) ? td.getClientSide() : null;
84 }
85 };
86 //
87 private MetroConfig defaultConfig;
88 private URL defaultConfigUrl;
89 private MetroConfig appConfig;
90 private URL appConfigUrl;
91
92 MetroConfigLoader(Container container, MetroConfigName defaultTubesConfigNames) {
93 this.defaultTubesConfigNames = defaultTubesConfigNames;
94 ResourceLoader spiResourceLoader = null;
95 if (container != null) {
96 spiResourceLoader = container.getSPI(ResourceLoader.class);
97 }
98 // if spi resource can't load resource, default (MetroConfigUrlLoader) is used;
99 // it searches the classpath, so it would be most probably used
100 // when using jaxws- or metro-defaults from jaxws libraries
101 init(container, spiResourceLoader, new MetroConfigUrlLoader(container));
102 }
103
104 private void init(Container container, ResourceLoader... loaders) {
105
106 String appFileName = null;
107 String defaultFileName = null;
108 if (container != null) {
109 MetroConfigName mcn = container.getSPI(MetroConfigName.class);
110 if (mcn != null) {
111 appFileName = mcn.getAppFileName();
112 defaultFileName = mcn.getDefaultFileName();
113 }
114 }
115 if (appFileName == null) {
116 appFileName = defaultTubesConfigNames.getAppFileName();
117 }
118
119 if (defaultFileName == null) {
120 defaultFileName = defaultTubesConfigNames.getDefaultFileName();
121 }
122 this.defaultConfigUrl = locateResource(defaultFileName, loaders);
123 if (defaultConfigUrl == null) {
124 throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0001_DEFAULT_CFG_FILE_NOT_FOUND(defaultFileName)));
125 }
126
127 LOGGER.config(TubelineassemblyMessages.MASM_0002_DEFAULT_CFG_FILE_LOCATED(defaultFileName, defaultConfigUrl));
128 this.defaultConfig = MetroConfigLoader.loadMetroConfig(defaultConfigUrl);
129 if (defaultConfig == null) {
130 throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0003_DEFAULT_CFG_FILE_NOT_LOADED(defaultFileName)));
131 }
132 if (defaultConfig.getTubelines() == null) {
133 throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0004_NO_TUBELINES_SECTION_IN_DEFAULT_CFG_FILE(defaultFileName)));
134 }
135 if (defaultConfig.getTubelines().getDefault() == null) {
136 throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0005_NO_DEFAULT_TUBELINE_IN_DEFAULT_CFG_FILE(defaultFileName)));
137 }
138
139 this.appConfigUrl = locateResource(appFileName, loaders);
140 if (appConfigUrl != null) {
141 LOGGER.config(TubelineassemblyMessages.MASM_0006_APP_CFG_FILE_LOCATED(appConfigUrl));
142 this.appConfig = MetroConfigLoader.loadMetroConfig(appConfigUrl);
143 } else {
144 LOGGER.config(TubelineassemblyMessages.MASM_0007_APP_CFG_FILE_NOT_FOUND());
145 this.appConfig = null;
146 }
147 }
148
149 TubeFactoryList getEndpointSideTubeFactories(URI endpointReference) {
150 return getTubeFactories(endpointReference, ENDPOINT_SIDE_RESOLVER);
151 }
152
153 TubeFactoryList getClientSideTubeFactories(URI endpointReference) {
154 return getTubeFactories(endpointReference, CLIENT_SIDE_RESOLVER);
155 }
156
157 private TubeFactoryList getTubeFactories(URI endpointReference, TubeFactoryListResolver resolver) {
158 if (appConfig != null && appConfig.getTubelines() != null) {
159 for (TubelineMapping mapping : appConfig.getTubelines().getTubelineMappings()) {
160 if (mapping.getEndpointRef().equals(endpointReference.toString())) {
161 TubeFactoryList list = resolver.getFactories(getTubeline(appConfig, resolveReference(mapping.getTubelineRef())));
162 if (list != null) {
163 return list;
164 } else {
165 break;
166 }
167 }
168 }
169
170 if (appConfig.getTubelines().getDefault() != null) {
171 TubeFactoryList list = resolver.getFactories(getTubeline(appConfig, resolveReference(appConfig.getTubelines().getDefault())));
172 if (list != null) {
173 return list;
174 }
175 }
176 }
177
178 for (TubelineMapping mapping : defaultConfig.getTubelines().getTubelineMappings()) {
179 if (mapping.getEndpointRef().equals(endpointReference.toString())) {
180 TubeFactoryList list = resolver.getFactories(getTubeline(defaultConfig, resolveReference(mapping.getTubelineRef())));
181 if (list != null) {
182 return list;
183 } else {
184 break;
185 }
186 }
187 }
188
189 return resolver.getFactories(getTubeline(defaultConfig, resolveReference(defaultConfig.getTubelines().getDefault())));
190 }
191
192 TubelineDefinition getTubeline(MetroConfig config, URI tubelineDefinitionUri) {
193 if (config != null && config.getTubelines() != null) {
194 for (TubelineDefinition td : config.getTubelines().getTubelineDefinitions()) {
195 if (td.getName().equals(tubelineDefinitionUri.getFragment())) {
196 return td;
197 }
198 }
199 }
200
201 return null;
202 }
203
204 private static URI resolveReference(String reference) {
205 try {
206 return new URI(reference);
207 } catch (URISyntaxException ex) {
208 throw LOGGER.logSevereException(new WebServiceException(TubelineassemblyMessages.MASM_0008_INVALID_URI_REFERENCE(reference), ex));
209 }
210 }
211
212
213 private static URL locateResource(String resource, ResourceLoader loader) {
214 if (loader == null) return null;
215
216 try {
217 return loader.getResource(resource);
218 } catch (MalformedURLException ex) {
219 LOGGER.severe(TubelineassemblyMessages.MASM_0009_CANNOT_FORM_VALID_URL(resource), ex);
220 }
221 return null;
222 }
223
224 private static URL locateResource(String resource, ResourceLoader[] loaders) {
225
226 for (ResourceLoader loader : loaders) {
227 URL url = locateResource(resource, loader);
228 if (url != null) {
229 return url;
230 }
231 }
232 return null;
233 }
234
235 private static MetroConfig loadMetroConfig(@NotNull URL resourceUrl) {
236 MetroConfig result = null;
237 try {
238 JAXBContext jaxbContext = JAXBContext.newInstance(MetroConfig.class.getPackage().getName());
239 Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
240 XMLInputFactory factory = XmlUtil.newXMLInputFactory(true);
241 final JAXBElement<MetroConfig> configElement = unmarshaller.unmarshal(factory.createXMLStreamReader(resourceUrl.openStream()), MetroConfig.class);
242 result = configElement.getValue();
243 } catch (Exception e) {
244 LOGGER.warning(TubelineassemblyMessages.MASM_0010_ERROR_READING_CFG_FILE_FROM_LOCATION(resourceUrl.toString()), e);
245 }
246 return result;
247 }
248
249 private static class MetroConfigUrlLoader extends ResourceLoader {
250
251 Container container; // TODO remove the field together with the code path using it (see below)
252 ResourceLoader parentLoader;
253
254 MetroConfigUrlLoader(ResourceLoader parentLoader) {
255 this.parentLoader = parentLoader;
256 }
257
258 MetroConfigUrlLoader(Container container) {
259 this((container != null) ? container.getSPI(ResourceLoader.class) : null);
260 this.container = container;
261 }
262
263 @Override
264 public URL getResource(String resource) throws MalformedURLException {
265 LOGGER.entering(resource);
266 URL resourceUrl = null;
267 try {
268 if (parentLoader != null) {
269 if (LOGGER.isLoggable(Level.FINE)) {
270 LOGGER.fine(TubelineassemblyMessages.MASM_0011_LOADING_RESOURCE(resource, parentLoader));
271 }
272
273 resourceUrl = parentLoader.getResource(resource);
274 }
275
276 if (resourceUrl == null) {
277 resourceUrl = loadViaClassLoaders("com/sun/xml/internal/ws/assembler/" + resource);
278 }
279
280 if (resourceUrl == null && container != null) {
281 // TODO: we should remove this code path, the config file should be loaded using ResourceLoader only
282 resourceUrl = loadFromServletContext(resource);
283 }
284
285 return resourceUrl;
286 } finally {
287 LOGGER.exiting(resourceUrl);
288 }
289 }
290
291 private static URL loadViaClassLoaders(final String resource) {
292 URL resourceUrl = tryLoadFromClassLoader(resource, Thread.currentThread().getContextClassLoader());
293 if (resourceUrl == null) {
294 resourceUrl = tryLoadFromClassLoader(resource, MetroConfigLoader.class.getClassLoader());
295 if (resourceUrl == null) {
296 return ClassLoader.getSystemResource(resource);
297 }
298 }
299
300 return resourceUrl;
301 }
302
303 private static URL tryLoadFromClassLoader(final String resource, final ClassLoader loader) {
304 return (loader != null) ? loader.getResource(resource) : null;
305 }
306
307 private URL loadFromServletContext(String resource) throws RuntimeException {
308 Object context = null;
309 try {
310 final Class<?> contextClass = Class.forName("javax.servlet.ServletContext");
311 context = container.getSPI(contextClass);
312 if (context != null) {
313 if (LOGGER.isLoggable(Level.FINE)) {
314 LOGGER.fine(TubelineassemblyMessages.MASM_0012_LOADING_VIA_SERVLET_CONTEXT(resource, context));
315 }
316 try {
317 final Method method = context.getClass().getMethod("getResource", String.class);
318 final Object result = method.invoke(context, "/WEB-INF/" + resource);
319 return URL.class.cast(result);
320 } catch (Exception e) {
321 throw LOGGER.logSevereException(new RuntimeException(TubelineassemblyMessages.MASM_0013_ERROR_INVOKING_SERVLET_CONTEXT_METHOD("getResource()")), e);
322 }
323 }
324 } catch (ClassNotFoundException e) {
325 if (LOGGER.isLoggable(Level.FINE)) {
326 LOGGER.fine(TubelineassemblyMessages.MASM_0014_UNABLE_TO_LOAD_CLASS("javax.servlet.ServletContext"));
327 }
328 }
329 return null;
330 }
331 }
332
333 }

mercurial