alanb@368: /*
alanb@368: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
alanb@368: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
alanb@368: *
alanb@368: * This code is free software; you can redistribute it and/or modify it
alanb@368: * under the terms of the GNU General Public License version 2 only, as
alanb@368: * published by the Free Software Foundation. Oracle designates this
alanb@368: * particular file as subject to the "Classpath" exception as provided
alanb@368: * by Oracle in the LICENSE file that accompanied this code.
alanb@368: *
alanb@368: * This code is distributed in the hope that it will be useful, but WITHOUT
alanb@368: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
alanb@368: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
alanb@368: * version 2 for more details (a copy is included in the LICENSE file that
alanb@368: * accompanied this code).
alanb@368: *
alanb@368: * You should have received a copy of the GNU General Public License version
alanb@368: * 2 along with this work; if not, write to the Free Software Foundation,
alanb@368: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
alanb@368: *
alanb@368: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
alanb@368: * or visit www.oracle.com if you need additional information or have any
alanb@368: * questions.
alanb@368: */
alanb@368:
alanb@368: package com.sun.xml.internal.ws.assembler;
alanb@368:
alanb@368: import com.sun.istack.internal.NotNull;
alanb@368: import com.sun.istack.internal.logging.Logger;
alanb@368: import com.sun.xml.internal.ws.api.ResourceLoader;
alanb@368: import com.sun.xml.internal.ws.api.server.Container;
alanb@368: import com.sun.xml.internal.ws.resources.TubelineassemblyMessages;
alanb@368: import com.sun.xml.internal.ws.runtime.config.MetroConfig;
alanb@368: import com.sun.xml.internal.ws.runtime.config.TubeFactoryList;
alanb@368: import com.sun.xml.internal.ws.runtime.config.TubelineDefinition;
alanb@368: import com.sun.xml.internal.ws.runtime.config.TubelineMapping;
alanb@368: import com.sun.xml.internal.ws.util.xml.XmlUtil;
alanb@368:
alanb@368: import javax.xml.bind.JAXBContext;
alanb@368: import javax.xml.bind.JAXBElement;
alanb@368: import javax.xml.bind.Unmarshaller;
alanb@368: import javax.xml.stream.XMLInputFactory;
alanb@368: import javax.xml.ws.WebServiceException;
alanb@368: import java.lang.reflect.Method;
alanb@368: import java.net.MalformedURLException;
alanb@368: import java.net.URI;
alanb@368: import java.net.URISyntaxException;
alanb@368: import java.net.URL;
alanb@368: import java.util.logging.Level;
alanb@368:
alanb@368: /**
alanb@368: * This class is responsible for locating and loading Metro configuration files
alanb@368: * (both application jaxws-tubes.xml and default jaxws-tubes-default.xml).
alanb@368: *
alanb@368: * Once the configuration is loaded the class is able to resolve which tubeline
alanb@368: * configuration belongs to each endpoint or endpoint client. This information is
alanb@368: * then used in {@link TubelineAssemblyController} to construct the list of
alanb@368: * {@link TubeCreator} objects that are used in the actual tubeline construction.
alanb@368: *
alanb@368: * @author Marek Potociar
alanb@368: */
alanb@368: // TODO Move the logic of this class directly into MetroConfig class.
alanb@368: class MetroConfigLoader {
alanb@368:
alanb@368: private static final Logger LOGGER = Logger.getLogger(MetroConfigLoader.class);
alanb@368:
alanb@368: private MetroConfigName defaultTubesConfigNames;
alanb@368:
alanb@368: private static interface TubeFactoryListResolver {
alanb@368:
alanb@368: TubeFactoryList getFactories(TubelineDefinition td);
alanb@368: }
alanb@368:
alanb@368: private static final TubeFactoryListResolver ENDPOINT_SIDE_RESOLVER = new TubeFactoryListResolver() {
alanb@368:
alanb@368: public TubeFactoryList getFactories(TubelineDefinition td) {
alanb@368: return (td != null) ? td.getEndpointSide() : null;
alanb@368: }
alanb@368: };
alanb@368: private static final TubeFactoryListResolver CLIENT_SIDE_RESOLVER = new TubeFactoryListResolver() {
alanb@368:
alanb@368: public TubeFactoryList getFactories(TubelineDefinition td) {
alanb@368: return (td != null) ? td.getClientSide() : null;
alanb@368: }
alanb@368: };
alanb@368: //
alanb@368: private MetroConfig defaultConfig;
alanb@368: private URL defaultConfigUrl;
alanb@368: private MetroConfig appConfig;
alanb@368: private URL appConfigUrl;
alanb@368:
alanb@368: MetroConfigLoader(Container container, MetroConfigName defaultTubesConfigNames) {
alanb@368: this.defaultTubesConfigNames = defaultTubesConfigNames;
alanb@368: ResourceLoader spiResourceLoader = null;
alanb@368: if (container != null) {
alanb@368: spiResourceLoader = container.getSPI(ResourceLoader.class);
alanb@368: }
alanb@368: // if spi resource can't load resource, default (MetroConfigUrlLoader) is used;
alanb@368: // it searches the classpath, so it would be most probably used
alanb@368: // when using jaxws- or metro-defaults from jaxws libraries
alanb@368: init(container, spiResourceLoader, new MetroConfigUrlLoader(container));
alanb@368: }
alanb@368:
alanb@368: private void init(Container container, ResourceLoader... loaders) {
alanb@368:
alanb@368: String appFileName = null;
alanb@368: String defaultFileName = null;
alanb@368: if (container != null) {
alanb@368: MetroConfigName mcn = container.getSPI(MetroConfigName.class);
alanb@368: if (mcn != null) {
alanb@368: appFileName = mcn.getAppFileName();
alanb@368: defaultFileName = mcn.getDefaultFileName();
alanb@368: }
alanb@368: }
alanb@368: if (appFileName == null) {
alanb@368: appFileName = defaultTubesConfigNames.getAppFileName();
alanb@368: }
alanb@368:
alanb@368: if (defaultFileName == null) {
alanb@368: defaultFileName = defaultTubesConfigNames.getDefaultFileName();
alanb@368: }
alanb@368: this.defaultConfigUrl = locateResource(defaultFileName, loaders);
alanb@368: if (defaultConfigUrl == null) {
alanb@368: throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0001_DEFAULT_CFG_FILE_NOT_FOUND(defaultFileName)));
alanb@368: }
alanb@368:
alanb@368: LOGGER.config(TubelineassemblyMessages.MASM_0002_DEFAULT_CFG_FILE_LOCATED(defaultFileName, defaultConfigUrl));
alanb@368: this.defaultConfig = MetroConfigLoader.loadMetroConfig(defaultConfigUrl);
alanb@368: if (defaultConfig == null) {
alanb@368: throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0003_DEFAULT_CFG_FILE_NOT_LOADED(defaultFileName)));
alanb@368: }
alanb@368: if (defaultConfig.getTubelines() == null) {
alanb@368: throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0004_NO_TUBELINES_SECTION_IN_DEFAULT_CFG_FILE(defaultFileName)));
alanb@368: }
alanb@368: if (defaultConfig.getTubelines().getDefault() == null) {
alanb@368: throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0005_NO_DEFAULT_TUBELINE_IN_DEFAULT_CFG_FILE(defaultFileName)));
alanb@368: }
alanb@368:
alanb@368: this.appConfigUrl = locateResource(appFileName, loaders);
alanb@368: if (appConfigUrl != null) {
alanb@368: LOGGER.config(TubelineassemblyMessages.MASM_0006_APP_CFG_FILE_LOCATED(appConfigUrl));
alanb@368: this.appConfig = MetroConfigLoader.loadMetroConfig(appConfigUrl);
alanb@368: } else {
alanb@368: LOGGER.config(TubelineassemblyMessages.MASM_0007_APP_CFG_FILE_NOT_FOUND());
alanb@368: this.appConfig = null;
alanb@368: }
alanb@368: }
alanb@368:
alanb@368: TubeFactoryList getEndpointSideTubeFactories(URI endpointReference) {
alanb@368: return getTubeFactories(endpointReference, ENDPOINT_SIDE_RESOLVER);
alanb@368: }
alanb@368:
alanb@368: TubeFactoryList getClientSideTubeFactories(URI endpointReference) {
alanb@368: return getTubeFactories(endpointReference, CLIENT_SIDE_RESOLVER);
alanb@368: }
alanb@368:
alanb@368: private TubeFactoryList getTubeFactories(URI endpointReference, TubeFactoryListResolver resolver) {
alanb@368: if (appConfig != null && appConfig.getTubelines() != null) {
alanb@368: for (TubelineMapping mapping : appConfig.getTubelines().getTubelineMappings()) {
alanb@368: if (mapping.getEndpointRef().equals(endpointReference.toString())) {
alanb@368: TubeFactoryList list = resolver.getFactories(getTubeline(appConfig, resolveReference(mapping.getTubelineRef())));
alanb@368: if (list != null) {
alanb@368: return list;
alanb@368: } else {
alanb@368: break;
alanb@368: }
alanb@368: }
alanb@368: }
alanb@368:
alanb@368: if (appConfig.getTubelines().getDefault() != null) {
alanb@368: TubeFactoryList list = resolver.getFactories(getTubeline(appConfig, resolveReference(appConfig.getTubelines().getDefault())));
alanb@368: if (list != null) {
alanb@368: return list;
alanb@368: }
alanb@368: }
alanb@368: }
alanb@368:
alanb@368: for (TubelineMapping mapping : defaultConfig.getTubelines().getTubelineMappings()) {
alanb@368: if (mapping.getEndpointRef().equals(endpointReference.toString())) {
alanb@368: TubeFactoryList list = resolver.getFactories(getTubeline(defaultConfig, resolveReference(mapping.getTubelineRef())));
alanb@368: if (list != null) {
alanb@368: return list;
alanb@368: } else {
alanb@368: break;
alanb@368: }
alanb@368: }
alanb@368: }
alanb@368:
alanb@368: return resolver.getFactories(getTubeline(defaultConfig, resolveReference(defaultConfig.getTubelines().getDefault())));
alanb@368: }
alanb@368:
alanb@368: TubelineDefinition getTubeline(MetroConfig config, URI tubelineDefinitionUri) {
alanb@368: if (config != null && config.getTubelines() != null) {
alanb@368: for (TubelineDefinition td : config.getTubelines().getTubelineDefinitions()) {
alanb@368: if (td.getName().equals(tubelineDefinitionUri.getFragment())) {
alanb@368: return td;
alanb@368: }
alanb@368: }
alanb@368: }
alanb@368:
alanb@368: return null;
alanb@368: }
alanb@368:
alanb@368: private static URI resolveReference(String reference) {
alanb@368: try {
alanb@368: return new URI(reference);
alanb@368: } catch (URISyntaxException ex) {
alanb@368: throw LOGGER.logSevereException(new WebServiceException(TubelineassemblyMessages.MASM_0008_INVALID_URI_REFERENCE(reference), ex));
alanb@368: }
alanb@368: }
alanb@368:
alanb@368:
alanb@368: private static URL locateResource(String resource, ResourceLoader loader) {
alanb@368: if (loader == null) return null;
alanb@368:
alanb@368: try {
alanb@368: return loader.getResource(resource);
alanb@368: } catch (MalformedURLException ex) {
alanb@368: LOGGER.severe(TubelineassemblyMessages.MASM_0009_CANNOT_FORM_VALID_URL(resource), ex);
alanb@368: }
alanb@368: return null;
alanb@368: }
alanb@368:
alanb@368: private static URL locateResource(String resource, ResourceLoader[] loaders) {
alanb@368:
alanb@368: for (ResourceLoader loader : loaders) {
alanb@368: URL url = locateResource(resource, loader);
alanb@368: if (url != null) {
alanb@368: return url;
alanb@368: }
alanb@368: }
alanb@368: return null;
alanb@368: }
alanb@368:
alanb@368: private static MetroConfig loadMetroConfig(@NotNull URL resourceUrl) {
alanb@368: MetroConfig result = null;
alanb@368: try {
alanb@368: JAXBContext jaxbContext = JAXBContext.newInstance(MetroConfig.class.getPackage().getName());
alanb@368: Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
alanb@368: XMLInputFactory factory = XmlUtil.newXMLInputFactory(true);
alanb@368: final JAXBElement configElement = unmarshaller.unmarshal(factory.createXMLStreamReader(resourceUrl.openStream()), MetroConfig.class);
alanb@368: result = configElement.getValue();
alanb@368: } catch (Exception e) {
alanb@368: LOGGER.warning(TubelineassemblyMessages.MASM_0010_ERROR_READING_CFG_FILE_FROM_LOCATION(resourceUrl.toString()), e);
alanb@368: }
alanb@368: return result;
alanb@368: }
alanb@368:
alanb@368: private static class MetroConfigUrlLoader extends ResourceLoader {
alanb@368:
alanb@368: Container container; // TODO remove the field together with the code path using it (see below)
alanb@368: ResourceLoader parentLoader;
alanb@368:
alanb@368: MetroConfigUrlLoader(ResourceLoader parentLoader) {
alanb@368: this.parentLoader = parentLoader;
alanb@368: }
alanb@368:
alanb@368: MetroConfigUrlLoader(Container container) {
alanb@368: this((container != null) ? container.getSPI(ResourceLoader.class) : null);
alanb@368: this.container = container;
alanb@368: }
alanb@368:
alanb@368: @Override
alanb@368: public URL getResource(String resource) throws MalformedURLException {
alanb@368: LOGGER.entering(resource);
alanb@368: URL resourceUrl = null;
alanb@368: try {
alanb@368: if (parentLoader != null) {
alanb@368: if (LOGGER.isLoggable(Level.FINE)) {
alanb@368: LOGGER.fine(TubelineassemblyMessages.MASM_0011_LOADING_RESOURCE(resource, parentLoader));
alanb@368: }
alanb@368:
alanb@368: resourceUrl = parentLoader.getResource(resource);
alanb@368: }
alanb@368:
alanb@368: if (resourceUrl == null) {
alanb@368: resourceUrl = loadViaClassLoaders("com/sun/xml/internal/ws/assembler/" + resource);
alanb@368: }
alanb@368:
alanb@368: if (resourceUrl == null && container != null) {
alanb@368: // TODO: we should remove this code path, the config file should be loaded using ResourceLoader only
alanb@368: resourceUrl = loadFromServletContext(resource);
alanb@368: }
alanb@368:
alanb@368: return resourceUrl;
alanb@368: } finally {
alanb@368: LOGGER.exiting(resourceUrl);
alanb@368: }
alanb@368: }
alanb@368:
alanb@368: private static URL loadViaClassLoaders(final String resource) {
alanb@368: URL resourceUrl = tryLoadFromClassLoader(resource, Thread.currentThread().getContextClassLoader());
alanb@368: if (resourceUrl == null) {
alanb@368: resourceUrl = tryLoadFromClassLoader(resource, MetroConfigLoader.class.getClassLoader());
alanb@368: if (resourceUrl == null) {
alanb@368: return ClassLoader.getSystemResource(resource);
alanb@368: }
alanb@368: }
alanb@368:
alanb@368: return resourceUrl;
alanb@368: }
alanb@368:
alanb@368: private static URL tryLoadFromClassLoader(final String resource, final ClassLoader loader) {
alanb@368: return (loader != null) ? loader.getResource(resource) : null;
alanb@368: }
alanb@368:
alanb@368: private URL loadFromServletContext(String resource) throws RuntimeException {
alanb@368: Object context = null;
alanb@368: try {
alanb@368: final Class> contextClass = Class.forName("javax.servlet.ServletContext");
alanb@368: context = container.getSPI(contextClass);
alanb@368: if (context != null) {
alanb@368: if (LOGGER.isLoggable(Level.FINE)) {
alanb@368: LOGGER.fine(TubelineassemblyMessages.MASM_0012_LOADING_VIA_SERVLET_CONTEXT(resource, context));
alanb@368: }
alanb@368: try {
alanb@368: final Method method = context.getClass().getMethod("getResource", String.class);
alanb@368: final Object result = method.invoke(context, "/WEB-INF/" + resource);
alanb@368: return URL.class.cast(result);
alanb@368: } catch (Exception e) {
alanb@368: throw LOGGER.logSevereException(new RuntimeException(TubelineassemblyMessages.MASM_0013_ERROR_INVOKING_SERVLET_CONTEXT_METHOD("getResource()")), e);
alanb@368: }
alanb@368: }
alanb@368: } catch (ClassNotFoundException e) {
alanb@368: if (LOGGER.isLoggable(Level.FINE)) {
alanb@368: LOGGER.fine(TubelineassemblyMessages.MASM_0014_UNABLE_TO_LOAD_CLASS("javax.servlet.ServletContext"));
alanb@368: }
alanb@368: }
alanb@368: return null;
alanb@368: }
alanb@368: }
alanb@368:
alanb@368: }