1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/assembler/MetroConfigLoader.java Tue Apr 09 14:51:13 2013 +0100 1.3 @@ -0,0 +1,333 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.assembler; 1.30 + 1.31 +import com.sun.istack.internal.NotNull; 1.32 +import com.sun.istack.internal.logging.Logger; 1.33 +import com.sun.xml.internal.ws.api.ResourceLoader; 1.34 +import com.sun.xml.internal.ws.api.server.Container; 1.35 +import com.sun.xml.internal.ws.resources.TubelineassemblyMessages; 1.36 +import com.sun.xml.internal.ws.runtime.config.MetroConfig; 1.37 +import com.sun.xml.internal.ws.runtime.config.TubeFactoryList; 1.38 +import com.sun.xml.internal.ws.runtime.config.TubelineDefinition; 1.39 +import com.sun.xml.internal.ws.runtime.config.TubelineMapping; 1.40 +import com.sun.xml.internal.ws.util.xml.XmlUtil; 1.41 + 1.42 +import javax.xml.bind.JAXBContext; 1.43 +import javax.xml.bind.JAXBElement; 1.44 +import javax.xml.bind.Unmarshaller; 1.45 +import javax.xml.stream.XMLInputFactory; 1.46 +import javax.xml.ws.WebServiceException; 1.47 +import java.lang.reflect.Method; 1.48 +import java.net.MalformedURLException; 1.49 +import java.net.URI; 1.50 +import java.net.URISyntaxException; 1.51 +import java.net.URL; 1.52 +import java.util.logging.Level; 1.53 + 1.54 +/** 1.55 + * This class is responsible for locating and loading Metro configuration files 1.56 + * (both application jaxws-tubes.xml and default jaxws-tubes-default.xml). 1.57 + * <p/> 1.58 + * Once the configuration is loaded the class is able to resolve which tubeline 1.59 + * configuration belongs to each endpoint or endpoint client. This information is 1.60 + * then used in {@link TubelineAssemblyController} to construct the list of 1.61 + * {@link TubeCreator} objects that are used in the actual tubeline construction. 1.62 + * 1.63 + * @author Marek Potociar <marek.potociar at sun.com> 1.64 + */ 1.65 +// TODO Move the logic of this class directly into MetroConfig class. 1.66 +class MetroConfigLoader { 1.67 + 1.68 + private static final Logger LOGGER = Logger.getLogger(MetroConfigLoader.class); 1.69 + 1.70 + private MetroConfigName defaultTubesConfigNames; 1.71 + 1.72 + private static interface TubeFactoryListResolver { 1.73 + 1.74 + TubeFactoryList getFactories(TubelineDefinition td); 1.75 + } 1.76 + 1.77 + private static final TubeFactoryListResolver ENDPOINT_SIDE_RESOLVER = new TubeFactoryListResolver() { 1.78 + 1.79 + public TubeFactoryList getFactories(TubelineDefinition td) { 1.80 + return (td != null) ? td.getEndpointSide() : null; 1.81 + } 1.82 + }; 1.83 + private static final TubeFactoryListResolver CLIENT_SIDE_RESOLVER = new TubeFactoryListResolver() { 1.84 + 1.85 + public TubeFactoryList getFactories(TubelineDefinition td) { 1.86 + return (td != null) ? td.getClientSide() : null; 1.87 + } 1.88 + }; 1.89 + // 1.90 + private MetroConfig defaultConfig; 1.91 + private URL defaultConfigUrl; 1.92 + private MetroConfig appConfig; 1.93 + private URL appConfigUrl; 1.94 + 1.95 + MetroConfigLoader(Container container, MetroConfigName defaultTubesConfigNames) { 1.96 + this.defaultTubesConfigNames = defaultTubesConfigNames; 1.97 + ResourceLoader spiResourceLoader = null; 1.98 + if (container != null) { 1.99 + spiResourceLoader = container.getSPI(ResourceLoader.class); 1.100 + } 1.101 + // if spi resource can't load resource, default (MetroConfigUrlLoader) is used; 1.102 + // it searches the classpath, so it would be most probably used 1.103 + // when using jaxws- or metro-defaults from jaxws libraries 1.104 + init(container, spiResourceLoader, new MetroConfigUrlLoader(container)); 1.105 + } 1.106 + 1.107 + private void init(Container container, ResourceLoader... loaders) { 1.108 + 1.109 + String appFileName = null; 1.110 + String defaultFileName = null; 1.111 + if (container != null) { 1.112 + MetroConfigName mcn = container.getSPI(MetroConfigName.class); 1.113 + if (mcn != null) { 1.114 + appFileName = mcn.getAppFileName(); 1.115 + defaultFileName = mcn.getDefaultFileName(); 1.116 + } 1.117 + } 1.118 + if (appFileName == null) { 1.119 + appFileName = defaultTubesConfigNames.getAppFileName(); 1.120 + } 1.121 + 1.122 + if (defaultFileName == null) { 1.123 + defaultFileName = defaultTubesConfigNames.getDefaultFileName(); 1.124 + } 1.125 + this.defaultConfigUrl = locateResource(defaultFileName, loaders); 1.126 + if (defaultConfigUrl == null) { 1.127 + throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0001_DEFAULT_CFG_FILE_NOT_FOUND(defaultFileName))); 1.128 + } 1.129 + 1.130 + LOGGER.config(TubelineassemblyMessages.MASM_0002_DEFAULT_CFG_FILE_LOCATED(defaultFileName, defaultConfigUrl)); 1.131 + this.defaultConfig = MetroConfigLoader.loadMetroConfig(defaultConfigUrl); 1.132 + if (defaultConfig == null) { 1.133 + throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0003_DEFAULT_CFG_FILE_NOT_LOADED(defaultFileName))); 1.134 + } 1.135 + if (defaultConfig.getTubelines() == null) { 1.136 + throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0004_NO_TUBELINES_SECTION_IN_DEFAULT_CFG_FILE(defaultFileName))); 1.137 + } 1.138 + if (defaultConfig.getTubelines().getDefault() == null) { 1.139 + throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0005_NO_DEFAULT_TUBELINE_IN_DEFAULT_CFG_FILE(defaultFileName))); 1.140 + } 1.141 + 1.142 + this.appConfigUrl = locateResource(appFileName, loaders); 1.143 + if (appConfigUrl != null) { 1.144 + LOGGER.config(TubelineassemblyMessages.MASM_0006_APP_CFG_FILE_LOCATED(appConfigUrl)); 1.145 + this.appConfig = MetroConfigLoader.loadMetroConfig(appConfigUrl); 1.146 + } else { 1.147 + LOGGER.config(TubelineassemblyMessages.MASM_0007_APP_CFG_FILE_NOT_FOUND()); 1.148 + this.appConfig = null; 1.149 + } 1.150 + } 1.151 + 1.152 + TubeFactoryList getEndpointSideTubeFactories(URI endpointReference) { 1.153 + return getTubeFactories(endpointReference, ENDPOINT_SIDE_RESOLVER); 1.154 + } 1.155 + 1.156 + TubeFactoryList getClientSideTubeFactories(URI endpointReference) { 1.157 + return getTubeFactories(endpointReference, CLIENT_SIDE_RESOLVER); 1.158 + } 1.159 + 1.160 + private TubeFactoryList getTubeFactories(URI endpointReference, TubeFactoryListResolver resolver) { 1.161 + if (appConfig != null && appConfig.getTubelines() != null) { 1.162 + for (TubelineMapping mapping : appConfig.getTubelines().getTubelineMappings()) { 1.163 + if (mapping.getEndpointRef().equals(endpointReference.toString())) { 1.164 + TubeFactoryList list = resolver.getFactories(getTubeline(appConfig, resolveReference(mapping.getTubelineRef()))); 1.165 + if (list != null) { 1.166 + return list; 1.167 + } else { 1.168 + break; 1.169 + } 1.170 + } 1.171 + } 1.172 + 1.173 + if (appConfig.getTubelines().getDefault() != null) { 1.174 + TubeFactoryList list = resolver.getFactories(getTubeline(appConfig, resolveReference(appConfig.getTubelines().getDefault()))); 1.175 + if (list != null) { 1.176 + return list; 1.177 + } 1.178 + } 1.179 + } 1.180 + 1.181 + for (TubelineMapping mapping : defaultConfig.getTubelines().getTubelineMappings()) { 1.182 + if (mapping.getEndpointRef().equals(endpointReference.toString())) { 1.183 + TubeFactoryList list = resolver.getFactories(getTubeline(defaultConfig, resolveReference(mapping.getTubelineRef()))); 1.184 + if (list != null) { 1.185 + return list; 1.186 + } else { 1.187 + break; 1.188 + } 1.189 + } 1.190 + } 1.191 + 1.192 + return resolver.getFactories(getTubeline(defaultConfig, resolveReference(defaultConfig.getTubelines().getDefault()))); 1.193 + } 1.194 + 1.195 + TubelineDefinition getTubeline(MetroConfig config, URI tubelineDefinitionUri) { 1.196 + if (config != null && config.getTubelines() != null) { 1.197 + for (TubelineDefinition td : config.getTubelines().getTubelineDefinitions()) { 1.198 + if (td.getName().equals(tubelineDefinitionUri.getFragment())) { 1.199 + return td; 1.200 + } 1.201 + } 1.202 + } 1.203 + 1.204 + return null; 1.205 + } 1.206 + 1.207 + private static URI resolveReference(String reference) { 1.208 + try { 1.209 + return new URI(reference); 1.210 + } catch (URISyntaxException ex) { 1.211 + throw LOGGER.logSevereException(new WebServiceException(TubelineassemblyMessages.MASM_0008_INVALID_URI_REFERENCE(reference), ex)); 1.212 + } 1.213 + } 1.214 + 1.215 + 1.216 + private static URL locateResource(String resource, ResourceLoader loader) { 1.217 + if (loader == null) return null; 1.218 + 1.219 + try { 1.220 + return loader.getResource(resource); 1.221 + } catch (MalformedURLException ex) { 1.222 + LOGGER.severe(TubelineassemblyMessages.MASM_0009_CANNOT_FORM_VALID_URL(resource), ex); 1.223 + } 1.224 + return null; 1.225 + } 1.226 + 1.227 + private static URL locateResource(String resource, ResourceLoader[] loaders) { 1.228 + 1.229 + for (ResourceLoader loader : loaders) { 1.230 + URL url = locateResource(resource, loader); 1.231 + if (url != null) { 1.232 + return url; 1.233 + } 1.234 + } 1.235 + return null; 1.236 + } 1.237 + 1.238 + private static MetroConfig loadMetroConfig(@NotNull URL resourceUrl) { 1.239 + MetroConfig result = null; 1.240 + try { 1.241 + JAXBContext jaxbContext = JAXBContext.newInstance(MetroConfig.class.getPackage().getName()); 1.242 + Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); 1.243 + XMLInputFactory factory = XmlUtil.newXMLInputFactory(true); 1.244 + final JAXBElement<MetroConfig> configElement = unmarshaller.unmarshal(factory.createXMLStreamReader(resourceUrl.openStream()), MetroConfig.class); 1.245 + result = configElement.getValue(); 1.246 + } catch (Exception e) { 1.247 + LOGGER.warning(TubelineassemblyMessages.MASM_0010_ERROR_READING_CFG_FILE_FROM_LOCATION(resourceUrl.toString()), e); 1.248 + } 1.249 + return result; 1.250 + } 1.251 + 1.252 + private static class MetroConfigUrlLoader extends ResourceLoader { 1.253 + 1.254 + Container container; // TODO remove the field together with the code path using it (see below) 1.255 + ResourceLoader parentLoader; 1.256 + 1.257 + MetroConfigUrlLoader(ResourceLoader parentLoader) { 1.258 + this.parentLoader = parentLoader; 1.259 + } 1.260 + 1.261 + MetroConfigUrlLoader(Container container) { 1.262 + this((container != null) ? container.getSPI(ResourceLoader.class) : null); 1.263 + this.container = container; 1.264 + } 1.265 + 1.266 + @Override 1.267 + public URL getResource(String resource) throws MalformedURLException { 1.268 + LOGGER.entering(resource); 1.269 + URL resourceUrl = null; 1.270 + try { 1.271 + if (parentLoader != null) { 1.272 + if (LOGGER.isLoggable(Level.FINE)) { 1.273 + LOGGER.fine(TubelineassemblyMessages.MASM_0011_LOADING_RESOURCE(resource, parentLoader)); 1.274 + } 1.275 + 1.276 + resourceUrl = parentLoader.getResource(resource); 1.277 + } 1.278 + 1.279 + if (resourceUrl == null) { 1.280 + resourceUrl = loadViaClassLoaders("com/sun/xml/internal/ws/assembler/" + resource); 1.281 + } 1.282 + 1.283 + if (resourceUrl == null && container != null) { 1.284 + // TODO: we should remove this code path, the config file should be loaded using ResourceLoader only 1.285 + resourceUrl = loadFromServletContext(resource); 1.286 + } 1.287 + 1.288 + return resourceUrl; 1.289 + } finally { 1.290 + LOGGER.exiting(resourceUrl); 1.291 + } 1.292 + } 1.293 + 1.294 + private static URL loadViaClassLoaders(final String resource) { 1.295 + URL resourceUrl = tryLoadFromClassLoader(resource, Thread.currentThread().getContextClassLoader()); 1.296 + if (resourceUrl == null) { 1.297 + resourceUrl = tryLoadFromClassLoader(resource, MetroConfigLoader.class.getClassLoader()); 1.298 + if (resourceUrl == null) { 1.299 + return ClassLoader.getSystemResource(resource); 1.300 + } 1.301 + } 1.302 + 1.303 + return resourceUrl; 1.304 + } 1.305 + 1.306 + private static URL tryLoadFromClassLoader(final String resource, final ClassLoader loader) { 1.307 + return (loader != null) ? loader.getResource(resource) : null; 1.308 + } 1.309 + 1.310 + private URL loadFromServletContext(String resource) throws RuntimeException { 1.311 + Object context = null; 1.312 + try { 1.313 + final Class<?> contextClass = Class.forName("javax.servlet.ServletContext"); 1.314 + context = container.getSPI(contextClass); 1.315 + if (context != null) { 1.316 + if (LOGGER.isLoggable(Level.FINE)) { 1.317 + LOGGER.fine(TubelineassemblyMessages.MASM_0012_LOADING_VIA_SERVLET_CONTEXT(resource, context)); 1.318 + } 1.319 + try { 1.320 + final Method method = context.getClass().getMethod("getResource", String.class); 1.321 + final Object result = method.invoke(context, "/WEB-INF/" + resource); 1.322 + return URL.class.cast(result); 1.323 + } catch (Exception e) { 1.324 + throw LOGGER.logSevereException(new RuntimeException(TubelineassemblyMessages.MASM_0013_ERROR_INVOKING_SERVLET_CONTEXT_METHOD("getResource()")), e); 1.325 + } 1.326 + } 1.327 + } catch (ClassNotFoundException e) { 1.328 + if (LOGGER.isLoggable(Level.FINE)) { 1.329 + LOGGER.fine(TubelineassemblyMessages.MASM_0014_UNABLE_TO_LOAD_CLASS("javax.servlet.ServletContext")); 1.330 + } 1.331 + } 1.332 + return null; 1.333 + } 1.334 + } 1.335 + 1.336 +}