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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     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	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,368 @@
     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.lang.reflect.ReflectPermission;
    1.49 +import java.net.MalformedURLException;
    1.50 +import java.net.URI;
    1.51 +import java.net.URISyntaxException;
    1.52 +import java.net.URL;
    1.53 +import java.security.*;
    1.54 +import java.util.PropertyPermission;
    1.55 +import java.util.logging.Level;
    1.56 +
    1.57 +/**
    1.58 + * This class is responsible for locating and loading Metro configuration files
    1.59 + * (both application jaxws-tubes.xml and default jaxws-tubes-default.xml).
    1.60 + * <p/>
    1.61 + * Once the configuration is loaded the class is able to resolve which tubeline
    1.62 + * configuration belongs to each endpoint or endpoint client. This information is
    1.63 + * then used in {@link TubelineAssemblyController} to construct the list of
    1.64 + * {@link TubeCreator} objects that are used in the actual tubeline construction.
    1.65 + *
    1.66 + * @author Marek Potociar <marek.potociar at sun.com>
    1.67 + */
    1.68 +// TODO Move the logic of this class directly into MetroConfig class.
    1.69 +class MetroConfigLoader {
    1.70 +
    1.71 +    private static final Logger LOGGER = Logger.getLogger(MetroConfigLoader.class);
    1.72 +
    1.73 +    private MetroConfigName defaultTubesConfigNames;
    1.74 +
    1.75 +    private static interface TubeFactoryListResolver {
    1.76 +
    1.77 +        TubeFactoryList getFactories(TubelineDefinition td);
    1.78 +    }
    1.79 +
    1.80 +    private static final TubeFactoryListResolver ENDPOINT_SIDE_RESOLVER = new TubeFactoryListResolver() {
    1.81 +
    1.82 +        public TubeFactoryList getFactories(TubelineDefinition td) {
    1.83 +            return (td != null) ? td.getEndpointSide() : null;
    1.84 +        }
    1.85 +    };
    1.86 +    private static final TubeFactoryListResolver CLIENT_SIDE_RESOLVER = new TubeFactoryListResolver() {
    1.87 +
    1.88 +        public TubeFactoryList getFactories(TubelineDefinition td) {
    1.89 +            return (td != null) ? td.getClientSide() : null;
    1.90 +        }
    1.91 +    };
    1.92 +    //
    1.93 +    private MetroConfig defaultConfig;
    1.94 +    private URL defaultConfigUrl;
    1.95 +    private MetroConfig appConfig;
    1.96 +    private URL appConfigUrl;
    1.97 +
    1.98 +    MetroConfigLoader(Container container, MetroConfigName defaultTubesConfigNames) {
    1.99 +        this.defaultTubesConfigNames = defaultTubesConfigNames;
   1.100 +        ResourceLoader spiResourceLoader = null;
   1.101 +        if (container != null) {
   1.102 +            spiResourceLoader = container.getSPI(ResourceLoader.class);
   1.103 +        }
   1.104 +        // if spi resource can't load resource, default (MetroConfigUrlLoader) is used;
   1.105 +        // it searches the classpath, so it would be most probably used
   1.106 +        // when using jaxws- or metro-defaults from jaxws libraries
   1.107 +        init(container, spiResourceLoader, new MetroConfigUrlLoader(container));
   1.108 +    }
   1.109 +
   1.110 +    private void init(Container container, ResourceLoader... loaders) {
   1.111 +
   1.112 +        String appFileName = null;
   1.113 +        String defaultFileName = null;
   1.114 +        if (container != null) {
   1.115 +            MetroConfigName mcn = container.getSPI(MetroConfigName.class);
   1.116 +            if (mcn != null) {
   1.117 +                appFileName = mcn.getAppFileName();
   1.118 +                defaultFileName = mcn.getDefaultFileName();
   1.119 +            }
   1.120 +        }
   1.121 +        if (appFileName == null) {
   1.122 +            appFileName = defaultTubesConfigNames.getAppFileName();
   1.123 +        }
   1.124 +
   1.125 +        if (defaultFileName == null) {
   1.126 +            defaultFileName = defaultTubesConfigNames.getDefaultFileName();
   1.127 +        }
   1.128 +        this.defaultConfigUrl = locateResource(defaultFileName, loaders);
   1.129 +        if (defaultConfigUrl == null) {
   1.130 +            throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0001_DEFAULT_CFG_FILE_NOT_FOUND(defaultFileName)));
   1.131 +        }
   1.132 +
   1.133 +        LOGGER.config(TubelineassemblyMessages.MASM_0002_DEFAULT_CFG_FILE_LOCATED(defaultFileName, defaultConfigUrl));
   1.134 +        this.defaultConfig = MetroConfigLoader.loadMetroConfig(defaultConfigUrl);
   1.135 +        if (defaultConfig == null) {
   1.136 +            throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0003_DEFAULT_CFG_FILE_NOT_LOADED(defaultFileName)));
   1.137 +        }
   1.138 +        if (defaultConfig.getTubelines() == null) {
   1.139 +            throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0004_NO_TUBELINES_SECTION_IN_DEFAULT_CFG_FILE(defaultFileName)));
   1.140 +        }
   1.141 +        if (defaultConfig.getTubelines().getDefault() == null) {
   1.142 +            throw LOGGER.logSevereException(new IllegalStateException(TubelineassemblyMessages.MASM_0005_NO_DEFAULT_TUBELINE_IN_DEFAULT_CFG_FILE(defaultFileName)));
   1.143 +        }
   1.144 +
   1.145 +        this.appConfigUrl = locateResource(appFileName, loaders);
   1.146 +        if (appConfigUrl != null) {
   1.147 +            LOGGER.config(TubelineassemblyMessages.MASM_0006_APP_CFG_FILE_LOCATED(appConfigUrl));
   1.148 +            this.appConfig = MetroConfigLoader.loadMetroConfig(appConfigUrl);
   1.149 +        } else {
   1.150 +            LOGGER.config(TubelineassemblyMessages.MASM_0007_APP_CFG_FILE_NOT_FOUND());
   1.151 +            this.appConfig = null;
   1.152 +        }
   1.153 +    }
   1.154 +
   1.155 +    TubeFactoryList getEndpointSideTubeFactories(URI endpointReference) {
   1.156 +        return getTubeFactories(endpointReference, ENDPOINT_SIDE_RESOLVER);
   1.157 +    }
   1.158 +
   1.159 +    TubeFactoryList getClientSideTubeFactories(URI endpointReference) {
   1.160 +        return getTubeFactories(endpointReference, CLIENT_SIDE_RESOLVER);
   1.161 +    }
   1.162 +
   1.163 +    private TubeFactoryList getTubeFactories(URI endpointReference, TubeFactoryListResolver resolver) {
   1.164 +        if (appConfig != null && appConfig.getTubelines() != null) {
   1.165 +            for (TubelineMapping mapping : appConfig.getTubelines().getTubelineMappings()) {
   1.166 +                if (mapping.getEndpointRef().equals(endpointReference.toString())) {
   1.167 +                    TubeFactoryList list = resolver.getFactories(getTubeline(appConfig, resolveReference(mapping.getTubelineRef())));
   1.168 +                    if (list != null) {
   1.169 +                        return list;
   1.170 +                    } else {
   1.171 +                        break;
   1.172 +                    }
   1.173 +                }
   1.174 +            }
   1.175 +
   1.176 +            if (appConfig.getTubelines().getDefault() != null) {
   1.177 +                TubeFactoryList list = resolver.getFactories(getTubeline(appConfig, resolveReference(appConfig.getTubelines().getDefault())));
   1.178 +                if (list != null) {
   1.179 +                    return list;
   1.180 +                }
   1.181 +            }
   1.182 +        }
   1.183 +
   1.184 +        for (TubelineMapping mapping : defaultConfig.getTubelines().getTubelineMappings()) {
   1.185 +            if (mapping.getEndpointRef().equals(endpointReference.toString())) {
   1.186 +                TubeFactoryList list = resolver.getFactories(getTubeline(defaultConfig, resolveReference(mapping.getTubelineRef())));
   1.187 +                if (list != null) {
   1.188 +                    return list;
   1.189 +                } else {
   1.190 +                    break;
   1.191 +                }
   1.192 +            }
   1.193 +        }
   1.194 +
   1.195 +        return resolver.getFactories(getTubeline(defaultConfig, resolveReference(defaultConfig.getTubelines().getDefault())));
   1.196 +    }
   1.197 +
   1.198 +    TubelineDefinition getTubeline(MetroConfig config, URI tubelineDefinitionUri) {
   1.199 +        if (config != null && config.getTubelines() != null) {
   1.200 +            for (TubelineDefinition td : config.getTubelines().getTubelineDefinitions()) {
   1.201 +                if (td.getName().equals(tubelineDefinitionUri.getFragment())) {
   1.202 +                    return td;
   1.203 +                }
   1.204 +            }
   1.205 +        }
   1.206 +
   1.207 +        return null;
   1.208 +    }
   1.209 +
   1.210 +    private static URI resolveReference(String reference) {
   1.211 +        try {
   1.212 +            return new URI(reference);
   1.213 +        } catch (URISyntaxException ex) {
   1.214 +            throw LOGGER.logSevereException(new WebServiceException(TubelineassemblyMessages.MASM_0008_INVALID_URI_REFERENCE(reference), ex));
   1.215 +        }
   1.216 +    }
   1.217 +
   1.218 +
   1.219 +    private static URL locateResource(String resource, ResourceLoader loader) {
   1.220 +        if (loader == null) return null;
   1.221 +
   1.222 +        try {
   1.223 +            return loader.getResource(resource);
   1.224 +        } catch (MalformedURLException ex) {
   1.225 +            LOGGER.severe(TubelineassemblyMessages.MASM_0009_CANNOT_FORM_VALID_URL(resource), ex);
   1.226 +        }
   1.227 +        return null;
   1.228 +    }
   1.229 +
   1.230 +    private static URL locateResource(String resource, ResourceLoader[] loaders) {
   1.231 +
   1.232 +        for (ResourceLoader loader : loaders) {
   1.233 +            URL url = locateResource(resource, loader);
   1.234 +            if (url != null) {
   1.235 +                return url;
   1.236 +            }
   1.237 +        }
   1.238 +        return null;
   1.239 +    }
   1.240 +
   1.241 +    private static MetroConfig loadMetroConfig(@NotNull URL resourceUrl) {
   1.242 +        MetroConfig result = null;
   1.243 +        try {
   1.244 +            JAXBContext jaxbContext = createJAXBContext();
   1.245 +            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
   1.246 +            XMLInputFactory factory = XmlUtil.newXMLInputFactory(true);
   1.247 +            final JAXBElement<MetroConfig> configElement = unmarshaller.unmarshal(factory.createXMLStreamReader(resourceUrl.openStream()), MetroConfig.class);
   1.248 +            result = configElement.getValue();
   1.249 +        } catch (Exception e) {
   1.250 +            LOGGER.warning(TubelineassemblyMessages.MASM_0010_ERROR_READING_CFG_FILE_FROM_LOCATION(resourceUrl.toString()), e);
   1.251 +        }
   1.252 +        return result;
   1.253 +    }
   1.254 +
   1.255 +    private static JAXBContext createJAXBContext() throws Exception {
   1.256 +        if (isJDKInternal()) {
   1.257 +            // since jdk classes are repackaged, extra privilege is necessary to create JAXBContext
   1.258 +            return AccessController.doPrivileged(
   1.259 +                    new PrivilegedExceptionAction<JAXBContext>() {
   1.260 +                        @Override
   1.261 +                        public JAXBContext run() throws Exception {
   1.262 +                            return JAXBContext.newInstance(MetroConfig.class.getPackage().getName());
   1.263 +                        }
   1.264 +                    }, createSecurityContext()
   1.265 +            );
   1.266 +        } else {
   1.267 +            // usage from JAX-WS/Metro/Glassfish
   1.268 +            return JAXBContext.newInstance(MetroConfig.class.getPackage().getName());
   1.269 +        }
   1.270 +    }
   1.271 +
   1.272 +    private static AccessControlContext createSecurityContext() {
   1.273 +        PermissionCollection perms = new Permissions();
   1.274 +        perms.add(new RuntimePermission("accessClassInPackage.com" + ".sun.xml.internal.ws.runtime.config")); // avoid repackaging
   1.275 +        perms.add(new ReflectPermission("suppressAccessChecks"));
   1.276 +        return new AccessControlContext(
   1.277 +                new ProtectionDomain[]{
   1.278 +                        new ProtectionDomain(null, perms),
   1.279 +                });
   1.280 +    }
   1.281 +
   1.282 +    private static boolean isJDKInternal() {
   1.283 +        // avoid "string repackaging"
   1.284 +        return MetroConfigLoader.class.getName().startsWith("com." + "sun.xml.internal.ws");
   1.285 +    }
   1.286 +
   1.287 +    private static class MetroConfigUrlLoader extends ResourceLoader {
   1.288 +
   1.289 +        Container container; // TODO remove the field together with the code path using it (see below)
   1.290 +        ResourceLoader parentLoader;
   1.291 +
   1.292 +        MetroConfigUrlLoader(ResourceLoader parentLoader) {
   1.293 +            this.parentLoader = parentLoader;
   1.294 +        }
   1.295 +
   1.296 +        MetroConfigUrlLoader(Container container) {
   1.297 +            this((container != null) ? container.getSPI(ResourceLoader.class) : null);
   1.298 +            this.container = container;
   1.299 +        }
   1.300 +
   1.301 +        @Override
   1.302 +        public URL getResource(String resource) throws MalformedURLException {
   1.303 +            LOGGER.entering(resource);
   1.304 +            URL resourceUrl = null;
   1.305 +            try {
   1.306 +                if (parentLoader != null) {
   1.307 +                    if (LOGGER.isLoggable(Level.FINE)) {
   1.308 +                        LOGGER.fine(TubelineassemblyMessages.MASM_0011_LOADING_RESOURCE(resource, parentLoader));
   1.309 +                    }
   1.310 +
   1.311 +                    resourceUrl = parentLoader.getResource(resource);
   1.312 +                }
   1.313 +
   1.314 +                if (resourceUrl == null) {
   1.315 +                    resourceUrl = loadViaClassLoaders("com/sun/xml/internal/ws/assembler/" + resource);
   1.316 +                }
   1.317 +
   1.318 +                if (resourceUrl == null && container != null) {
   1.319 +                    // TODO: we should remove this code path, the config file should be loaded using ResourceLoader only
   1.320 +                    resourceUrl = loadFromServletContext(resource);
   1.321 +                }
   1.322 +
   1.323 +                return resourceUrl;
   1.324 +            } finally {
   1.325 +                LOGGER.exiting(resourceUrl);
   1.326 +            }
   1.327 +        }
   1.328 +
   1.329 +        private static URL loadViaClassLoaders(final String resource) {
   1.330 +            URL resourceUrl = tryLoadFromClassLoader(resource, Thread.currentThread().getContextClassLoader());
   1.331 +            if (resourceUrl == null) {
   1.332 +                resourceUrl = tryLoadFromClassLoader(resource, MetroConfigLoader.class.getClassLoader());
   1.333 +                if (resourceUrl == null) {
   1.334 +                    return ClassLoader.getSystemResource(resource);
   1.335 +                }
   1.336 +            }
   1.337 +
   1.338 +            return resourceUrl;
   1.339 +        }
   1.340 +
   1.341 +        private static URL tryLoadFromClassLoader(final String resource, final ClassLoader loader) {
   1.342 +            return (loader != null) ? loader.getResource(resource) : null;
   1.343 +        }
   1.344 +
   1.345 +        private URL loadFromServletContext(String resource) throws RuntimeException {
   1.346 +            Object context = null;
   1.347 +            try {
   1.348 +                final Class<?> contextClass = Class.forName("javax.servlet.ServletContext");
   1.349 +                context = container.getSPI(contextClass);
   1.350 +                if (context != null) {
   1.351 +                    if (LOGGER.isLoggable(Level.FINE)) {
   1.352 +                        LOGGER.fine(TubelineassemblyMessages.MASM_0012_LOADING_VIA_SERVLET_CONTEXT(resource, context));
   1.353 +                    }
   1.354 +                    try {
   1.355 +                        final Method method = context.getClass().getMethod("getResource", String.class);
   1.356 +                        final Object result = method.invoke(context, "/WEB-INF/" + resource);
   1.357 +                        return URL.class.cast(result);
   1.358 +                    } catch (Exception e) {
   1.359 +                        throw LOGGER.logSevereException(new RuntimeException(TubelineassemblyMessages.MASM_0013_ERROR_INVOKING_SERVLET_CONTEXT_METHOD("getResource()")), e);
   1.360 +                    }
   1.361 +                }
   1.362 +            } catch (ClassNotFoundException e) {
   1.363 +                if (LOGGER.isLoggable(Level.FINE)) {
   1.364 +                    LOGGER.fine(TubelineassemblyMessages.MASM_0014_UNABLE_TO_LOAD_CLASS("javax.servlet.ServletContext"));
   1.365 +                }
   1.366 +            }
   1.367 +            return null;
   1.368 +        }
   1.369 +    }
   1.370 +
   1.371 +}

mercurial