src/share/jaxws_classes/com/sun/xml/internal/bind/v2/ContextFactory.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/bind/v2/ContextFactory.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,322 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, 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.bind.v2;
    1.30 +
    1.31 +import java.io.BufferedReader;
    1.32 +import java.io.IOException;
    1.33 +import java.io.InputStream;
    1.34 +import java.io.InputStreamReader;
    1.35 +import java.util.Collection;
    1.36 +import java.util.Collections;
    1.37 +import java.util.HashMap;
    1.38 +import java.util.List;
    1.39 +import java.util.Map;
    1.40 +import java.util.StringTokenizer;
    1.41 +import java.util.logging.Level;
    1.42 +
    1.43 +import javax.xml.bind.JAXBContext;
    1.44 +import javax.xml.bind.JAXBException;
    1.45 +
    1.46 +import com.sun.istack.internal.FinalArrayList;
    1.47 +import com.sun.xml.internal.bind.Util;
    1.48 +import com.sun.xml.internal.bind.api.JAXBRIContext;
    1.49 +import com.sun.xml.internal.bind.api.TypeReference;
    1.50 +import com.sun.xml.internal.bind.v2.model.annotation.RuntimeAnnotationReader;
    1.51 +import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
    1.52 +import com.sun.xml.internal.bind.v2.util.TypeCast;
    1.53 +
    1.54 +/**
    1.55 + * This class is responsible for producing RI JAXBContext objects.  In
    1.56 + * the RI, this is the class that the javax.xml.bind.context.factory
    1.57 + * property will point to.
    1.58 + *
    1.59 + * <p>
    1.60 + * Used to create JAXBContext objects for v1.0.1 and forward
    1.61 + *
    1.62 + * @since 2.0
    1.63 + * @author Kohsuke Kawaguchi
    1.64 + */
    1.65 +public class ContextFactory {
    1.66 +
    1.67 +    /**
    1.68 +     * The API will invoke this method via reflection
    1.69 +     */
    1.70 +    public static JAXBContext createContext(Class[] classes, Map<String,Object> properties ) throws JAXBException {
    1.71 +        // fool-proof check, and copy the map to make it easier to find unrecognized properties.
    1.72 +        if(properties==null)
    1.73 +            properties = Collections.emptyMap();
    1.74 +        else
    1.75 +            properties = new HashMap<String,Object>(properties);
    1.76 +
    1.77 +        String defaultNsUri = getPropertyValue(properties,JAXBRIContext.DEFAULT_NAMESPACE_REMAP,String.class);
    1.78 +
    1.79 +        Boolean c14nSupport = getPropertyValue(properties,JAXBRIContext.CANONICALIZATION_SUPPORT,Boolean.class);
    1.80 +        if(c14nSupport==null)
    1.81 +            c14nSupport = false;
    1.82 +
    1.83 +        Boolean disablesecurityProcessing = getPropertyValue(properties, JAXBRIContext.DISABLE_XML_SECURITY, Boolean.class);
    1.84 +        if (disablesecurityProcessing==null)
    1.85 +            disablesecurityProcessing = false;
    1.86 +
    1.87 +        Boolean allNillable = getPropertyValue(properties,JAXBRIContext.TREAT_EVERYTHING_NILLABLE,Boolean.class);
    1.88 +        if(allNillable==null)
    1.89 +            allNillable = false;
    1.90 +
    1.91 +        Boolean retainPropertyInfo = getPropertyValue(properties, JAXBRIContext.RETAIN_REFERENCE_TO_INFO, Boolean.class);
    1.92 +        if(retainPropertyInfo==null)
    1.93 +            retainPropertyInfo = false;
    1.94 +
    1.95 +        Boolean supressAccessorWarnings = getPropertyValue(properties, JAXBRIContext.SUPRESS_ACCESSOR_WARNINGS, Boolean.class);
    1.96 +        if(supressAccessorWarnings==null)
    1.97 +            supressAccessorWarnings = false;
    1.98 +
    1.99 +        Boolean improvedXsiTypeHandling = getPropertyValue(properties, JAXBRIContext.IMPROVED_XSI_TYPE_HANDLING, Boolean.class);
   1.100 +        if (improvedXsiTypeHandling == null) {
   1.101 +            String improvedXsiSystemProperty = Util.getSystemProperty(JAXBRIContext.IMPROVED_XSI_TYPE_HANDLING);
   1.102 +            if (improvedXsiSystemProperty == null) {
   1.103 +                improvedXsiTypeHandling = true;
   1.104 +            } else {
   1.105 +                improvedXsiTypeHandling = Boolean.valueOf(improvedXsiSystemProperty);
   1.106 +            }
   1.107 +        }
   1.108 +
   1.109 +        Boolean xmlAccessorFactorySupport = getPropertyValue(properties,
   1.110 +           JAXBRIContext.XMLACCESSORFACTORY_SUPPORT,Boolean.class);
   1.111 +        if(xmlAccessorFactorySupport==null){
   1.112 +            xmlAccessorFactorySupport = false;
   1.113 +            Util.getClassLogger().log(Level.FINE, "Property " +
   1.114 +                JAXBRIContext.XMLACCESSORFACTORY_SUPPORT +
   1.115 +                "is not active.  Using JAXB's implementation");
   1.116 +        }
   1.117 +
   1.118 +        RuntimeAnnotationReader ar = getPropertyValue(properties,JAXBRIContext.ANNOTATION_READER,RuntimeAnnotationReader.class);
   1.119 +
   1.120 +        Collection<TypeReference> tr = getPropertyValue(properties, JAXBRIContext.TYPE_REFERENCES, Collection.class);
   1.121 +        if (tr == null) {
   1.122 +            tr = Collections.<TypeReference>emptyList();
   1.123 +        }
   1.124 +
   1.125 +        Map<Class,Class> subclassReplacements;
   1.126 +        try {
   1.127 +            subclassReplacements = TypeCast.checkedCast(
   1.128 +                getPropertyValue(properties, JAXBRIContext.SUBCLASS_REPLACEMENTS, Map.class), Class.class, Class.class);
   1.129 +        } catch (ClassCastException e) {
   1.130 +            throw new JAXBException(Messages.INVALID_TYPE_IN_MAP.format(),e);
   1.131 +        }
   1.132 +
   1.133 +        if(!properties.isEmpty()) {
   1.134 +            throw new JAXBException(Messages.UNSUPPORTED_PROPERTY.format(properties.keySet().iterator().next()));
   1.135 +        }
   1.136 +
   1.137 +        JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder();
   1.138 +        builder.setClasses(classes);
   1.139 +        builder.setTypeRefs(tr);
   1.140 +        builder.setSubclassReplacements(subclassReplacements);
   1.141 +        builder.setDefaultNsUri(defaultNsUri);
   1.142 +        builder.setC14NSupport(c14nSupport);
   1.143 +        builder.setAnnotationReader(ar);
   1.144 +        builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport);
   1.145 +        builder.setAllNillable(allNillable);
   1.146 +        builder.setRetainPropertyInfo(retainPropertyInfo);
   1.147 +        builder.setSupressAccessorWarnings(supressAccessorWarnings);
   1.148 +        builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling);
   1.149 +        builder.setDisableSecurityProcessing(disablesecurityProcessing);
   1.150 +        return builder.build();
   1.151 +    }
   1.152 +
   1.153 +    /**
   1.154 +     * If a key is present in the map, remove the value and return it.
   1.155 +     */
   1.156 +    private static <T> T getPropertyValue(Map<String, Object> properties, String keyName, Class<T> type ) throws JAXBException {
   1.157 +        Object o = properties.get(keyName);
   1.158 +        if(o==null)     return null;
   1.159 +
   1.160 +        properties.remove(keyName);
   1.161 +        if(!type.isInstance(o))
   1.162 +            throw new JAXBException(Messages.INVALID_PROPERTY_VALUE.format(keyName,o));
   1.163 +        else
   1.164 +            return type.cast(o);
   1.165 +    }
   1.166 +
   1.167 +    /**
   1.168 +     *
   1.169 +     * @param classes
   1.170 +     * @param typeRefs
   1.171 +     * @param subclassReplacements
   1.172 +     * @param defaultNsUri
   1.173 +     * @param c14nSupport
   1.174 +     * @param ar
   1.175 +     * @param xmlAccessorFactorySupport
   1.176 +     * @param allNillable
   1.177 +     * @param retainPropertyInfo
   1.178 +     * @return
   1.179 +     * @throws JAXBException
   1.180 +     * @deprecated use createContext(Class[] classes, Map<String,Object> properties) method instead
   1.181 +     */
   1.182 +    @Deprecated
   1.183 +    public static JAXBRIContext createContext( Class[] classes,
   1.184 +            Collection<TypeReference> typeRefs, Map<Class,Class> subclassReplacements,
   1.185 +            String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar,
   1.186 +            boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo) throws JAXBException {
   1.187 +
   1.188 +        return createContext(classes, typeRefs, subclassReplacements,
   1.189 +                defaultNsUri, c14nSupport, ar, xmlAccessorFactorySupport,
   1.190 +                allNillable, retainPropertyInfo, false);
   1.191 +    }
   1.192 +
   1.193 +    /**
   1.194 +     *
   1.195 +     * @param classes
   1.196 +     * @param typeRefs
   1.197 +     * @param subclassReplacements
   1.198 +     * @param defaultNsUri
   1.199 +     * @param c14nSupport
   1.200 +     * @param ar
   1.201 +     * @param xmlAccessorFactorySupport
   1.202 +     * @param allNillable
   1.203 +     * @param retainPropertyInfo
   1.204 +     * @param improvedXsiTypeHandling
   1.205 +     * @return
   1.206 +     * @throws JAXBException
   1.207 +     * @deprecated use createContext( Class[] classes, Map<String,Object> properties) method instead
   1.208 +     */
   1.209 +    @Deprecated
   1.210 +    public static JAXBRIContext createContext( Class[] classes,
   1.211 +            Collection<TypeReference> typeRefs, Map<Class,Class> subclassReplacements,
   1.212 +            String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar,
   1.213 +            boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo, boolean improvedXsiTypeHandling) throws JAXBException {
   1.214 +
   1.215 +        JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder();
   1.216 +        builder.setClasses(classes);
   1.217 +        builder.setTypeRefs(typeRefs);
   1.218 +        builder.setSubclassReplacements(subclassReplacements);
   1.219 +        builder.setDefaultNsUri(defaultNsUri);
   1.220 +        builder.setC14NSupport(c14nSupport);
   1.221 +        builder.setAnnotationReader(ar);
   1.222 +        builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport);
   1.223 +        builder.setAllNillable(allNillable);
   1.224 +        builder.setRetainPropertyInfo(retainPropertyInfo);
   1.225 +        builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling);
   1.226 +        return builder.build();
   1.227 +    }
   1.228 +
   1.229 +    /**
   1.230 +     * The API will invoke this method via reflection.
   1.231 +     */
   1.232 +    public static JAXBContext createContext( String contextPath,
   1.233 +                                             ClassLoader classLoader, Map<String,Object> properties ) throws JAXBException {
   1.234 +        FinalArrayList<Class> classes = new FinalArrayList<Class>();
   1.235 +        StringTokenizer tokens = new StringTokenizer(contextPath,":");
   1.236 +        List<Class> indexedClasses;
   1.237 +
   1.238 +        // at least on of these must be true per package
   1.239 +        boolean foundObjectFactory;
   1.240 +        boolean foundJaxbIndex;
   1.241 +
   1.242 +        while(tokens.hasMoreTokens()) {
   1.243 +            foundObjectFactory = foundJaxbIndex = false;
   1.244 +            String pkg = tokens.nextToken();
   1.245 +
   1.246 +            // look for ObjectFactory and load it
   1.247 +            final Class<?> o;
   1.248 +            try {
   1.249 +                o = classLoader.loadClass(pkg+".ObjectFactory");
   1.250 +                classes.add(o);
   1.251 +                foundObjectFactory = true;
   1.252 +            } catch (ClassNotFoundException e) {
   1.253 +                // not necessarily an error
   1.254 +            }
   1.255 +
   1.256 +            // look for jaxb.index and load the list of classes
   1.257 +            try {
   1.258 +                indexedClasses = loadIndexedClasses(pkg, classLoader);
   1.259 +            } catch (IOException e) {
   1.260 +                //TODO: think about this more
   1.261 +                throw new JAXBException(e);
   1.262 +            }
   1.263 +            if(indexedClasses != null) {
   1.264 +                classes.addAll(indexedClasses);
   1.265 +                foundJaxbIndex = true;
   1.266 +            }
   1.267 +
   1.268 +            if( !(foundObjectFactory || foundJaxbIndex) ) {
   1.269 +                throw new JAXBException( Messages.BROKEN_CONTEXTPATH.format(pkg));
   1.270 +            }
   1.271 +        }
   1.272 +
   1.273 +
   1.274 +        return createContext(classes.toArray(new Class[classes.size()]),properties);
   1.275 +    }
   1.276 +
   1.277 +    /**
   1.278 +     * Look for jaxb.index file in the specified package and load it's contents
   1.279 +     *
   1.280 +     * @param pkg package name to search in
   1.281 +     * @param classLoader ClassLoader to search in
   1.282 +     * @return a List of Class objects to load, null if there weren't any
   1.283 +     * @throws IOException if there is an error reading the index file
   1.284 +     * @throws JAXBException if there are any errors in the index file
   1.285 +     */
   1.286 +    private static List<Class> loadIndexedClasses(String pkg, ClassLoader classLoader) throws IOException, JAXBException {
   1.287 +        final String resource = pkg.replace('.', '/') + "/jaxb.index";
   1.288 +        final InputStream resourceAsStream = classLoader.getResourceAsStream(resource);
   1.289 +
   1.290 +        if (resourceAsStream == null) {
   1.291 +            return null;
   1.292 +        }
   1.293 +
   1.294 +        BufferedReader in =
   1.295 +                new BufferedReader(new InputStreamReader(resourceAsStream, "UTF-8"));
   1.296 +        try {
   1.297 +            FinalArrayList<Class> classes = new FinalArrayList<Class>();
   1.298 +            String className = in.readLine();
   1.299 +            while (className != null) {
   1.300 +                className = className.trim();
   1.301 +                if (className.startsWith("#") || (className.length() == 0)) {
   1.302 +                    className = in.readLine();
   1.303 +                    continue;
   1.304 +                }
   1.305 +
   1.306 +                if (className.endsWith(".class")) {
   1.307 +                    throw new JAXBException(Messages.ILLEGAL_ENTRY.format(className));
   1.308 +                }
   1.309 +
   1.310 +                try {
   1.311 +                    classes.add(classLoader.loadClass(pkg + '.' + className));
   1.312 +                } catch (ClassNotFoundException e) {
   1.313 +                    throw new JAXBException(Messages.ERROR_LOADING_CLASS.format(className, resource),e);
   1.314 +                }
   1.315 +
   1.316 +                className = in.readLine();
   1.317 +            }
   1.318 +            return classes;
   1.319 +        } finally {
   1.320 +            in.close();
   1.321 +        }
   1.322 +    }
   1.323 +
   1.324 +    public static final String USE_JAXB_PROPERTIES = "_useJAXBProperties";
   1.325 +}

mercurial