Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.bind.v2; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.BufferedReader; |
aoqi@0 | 29 | import java.io.IOException; |
aoqi@0 | 30 | import java.io.InputStream; |
aoqi@0 | 31 | import java.io.InputStreamReader; |
aoqi@0 | 32 | import java.util.Collection; |
aoqi@0 | 33 | import java.util.Collections; |
aoqi@0 | 34 | import java.util.HashMap; |
aoqi@0 | 35 | import java.util.List; |
aoqi@0 | 36 | import java.util.Map; |
aoqi@0 | 37 | import java.util.StringTokenizer; |
aoqi@0 | 38 | import java.util.logging.Level; |
aoqi@0 | 39 | |
aoqi@0 | 40 | import javax.xml.bind.JAXBContext; |
aoqi@0 | 41 | import javax.xml.bind.JAXBException; |
aoqi@0 | 42 | |
aoqi@0 | 43 | import com.sun.istack.internal.FinalArrayList; |
aoqi@0 | 44 | import com.sun.xml.internal.bind.Util; |
aoqi@0 | 45 | import com.sun.xml.internal.bind.api.JAXBRIContext; |
aoqi@0 | 46 | import com.sun.xml.internal.bind.api.TypeReference; |
aoqi@0 | 47 | import com.sun.xml.internal.bind.v2.model.annotation.RuntimeAnnotationReader; |
aoqi@0 | 48 | import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; |
aoqi@0 | 49 | import com.sun.xml.internal.bind.v2.util.TypeCast; |
aoqi@0 | 50 | |
aoqi@0 | 51 | /** |
aoqi@0 | 52 | * This class is responsible for producing RI JAXBContext objects. In |
aoqi@0 | 53 | * the RI, this is the class that the javax.xml.bind.context.factory |
aoqi@0 | 54 | * property will point to. |
aoqi@0 | 55 | * |
aoqi@0 | 56 | * <p> |
aoqi@0 | 57 | * Used to create JAXBContext objects for v1.0.1 and forward |
aoqi@0 | 58 | * |
aoqi@0 | 59 | * @since 2.0 |
aoqi@0 | 60 | * @author Kohsuke Kawaguchi |
aoqi@0 | 61 | */ |
aoqi@0 | 62 | public class ContextFactory { |
aoqi@0 | 63 | |
aoqi@0 | 64 | /** |
aoqi@0 | 65 | * The API will invoke this method via reflection |
aoqi@0 | 66 | */ |
aoqi@0 | 67 | public static JAXBContext createContext(Class[] classes, Map<String,Object> properties ) throws JAXBException { |
aoqi@0 | 68 | // fool-proof check, and copy the map to make it easier to find unrecognized properties. |
aoqi@0 | 69 | if(properties==null) |
aoqi@0 | 70 | properties = Collections.emptyMap(); |
aoqi@0 | 71 | else |
aoqi@0 | 72 | properties = new HashMap<String,Object>(properties); |
aoqi@0 | 73 | |
aoqi@0 | 74 | String defaultNsUri = getPropertyValue(properties,JAXBRIContext.DEFAULT_NAMESPACE_REMAP,String.class); |
aoqi@0 | 75 | |
aoqi@0 | 76 | Boolean c14nSupport = getPropertyValue(properties,JAXBRIContext.CANONICALIZATION_SUPPORT,Boolean.class); |
aoqi@0 | 77 | if(c14nSupport==null) |
aoqi@0 | 78 | c14nSupport = false; |
aoqi@0 | 79 | |
aoqi@0 | 80 | Boolean disablesecurityProcessing = getPropertyValue(properties, JAXBRIContext.DISABLE_XML_SECURITY, Boolean.class); |
aoqi@0 | 81 | if (disablesecurityProcessing==null) |
aoqi@0 | 82 | disablesecurityProcessing = false; |
aoqi@0 | 83 | |
aoqi@0 | 84 | Boolean allNillable = getPropertyValue(properties,JAXBRIContext.TREAT_EVERYTHING_NILLABLE,Boolean.class); |
aoqi@0 | 85 | if(allNillable==null) |
aoqi@0 | 86 | allNillable = false; |
aoqi@0 | 87 | |
aoqi@0 | 88 | Boolean retainPropertyInfo = getPropertyValue(properties, JAXBRIContext.RETAIN_REFERENCE_TO_INFO, Boolean.class); |
aoqi@0 | 89 | if(retainPropertyInfo==null) |
aoqi@0 | 90 | retainPropertyInfo = false; |
aoqi@0 | 91 | |
aoqi@0 | 92 | Boolean supressAccessorWarnings = getPropertyValue(properties, JAXBRIContext.SUPRESS_ACCESSOR_WARNINGS, Boolean.class); |
aoqi@0 | 93 | if(supressAccessorWarnings==null) |
aoqi@0 | 94 | supressAccessorWarnings = false; |
aoqi@0 | 95 | |
aoqi@0 | 96 | Boolean improvedXsiTypeHandling = getPropertyValue(properties, JAXBRIContext.IMPROVED_XSI_TYPE_HANDLING, Boolean.class); |
aoqi@0 | 97 | if (improvedXsiTypeHandling == null) { |
aoqi@0 | 98 | String improvedXsiSystemProperty = Util.getSystemProperty(JAXBRIContext.IMPROVED_XSI_TYPE_HANDLING); |
aoqi@0 | 99 | if (improvedXsiSystemProperty == null) { |
aoqi@0 | 100 | improvedXsiTypeHandling = true; |
aoqi@0 | 101 | } else { |
aoqi@0 | 102 | improvedXsiTypeHandling = Boolean.valueOf(improvedXsiSystemProperty); |
aoqi@0 | 103 | } |
aoqi@0 | 104 | } |
aoqi@0 | 105 | |
aoqi@0 | 106 | Boolean xmlAccessorFactorySupport = getPropertyValue(properties, |
aoqi@0 | 107 | JAXBRIContext.XMLACCESSORFACTORY_SUPPORT,Boolean.class); |
aoqi@0 | 108 | if(xmlAccessorFactorySupport==null){ |
aoqi@0 | 109 | xmlAccessorFactorySupport = false; |
aoqi@0 | 110 | Util.getClassLogger().log(Level.FINE, "Property " + |
aoqi@0 | 111 | JAXBRIContext.XMLACCESSORFACTORY_SUPPORT + |
aoqi@0 | 112 | "is not active. Using JAXB's implementation"); |
aoqi@0 | 113 | } |
aoqi@0 | 114 | |
aoqi@0 | 115 | RuntimeAnnotationReader ar = getPropertyValue(properties,JAXBRIContext.ANNOTATION_READER,RuntimeAnnotationReader.class); |
aoqi@0 | 116 | |
aoqi@0 | 117 | Collection<TypeReference> tr = getPropertyValue(properties, JAXBRIContext.TYPE_REFERENCES, Collection.class); |
aoqi@0 | 118 | if (tr == null) { |
aoqi@0 | 119 | tr = Collections.<TypeReference>emptyList(); |
aoqi@0 | 120 | } |
aoqi@0 | 121 | |
aoqi@0 | 122 | Map<Class,Class> subclassReplacements; |
aoqi@0 | 123 | try { |
aoqi@0 | 124 | subclassReplacements = TypeCast.checkedCast( |
aoqi@0 | 125 | getPropertyValue(properties, JAXBRIContext.SUBCLASS_REPLACEMENTS, Map.class), Class.class, Class.class); |
aoqi@0 | 126 | } catch (ClassCastException e) { |
aoqi@0 | 127 | throw new JAXBException(Messages.INVALID_TYPE_IN_MAP.format(),e); |
aoqi@0 | 128 | } |
aoqi@0 | 129 | |
aoqi@0 | 130 | if(!properties.isEmpty()) { |
aoqi@0 | 131 | throw new JAXBException(Messages.UNSUPPORTED_PROPERTY.format(properties.keySet().iterator().next())); |
aoqi@0 | 132 | } |
aoqi@0 | 133 | |
aoqi@0 | 134 | JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder(); |
aoqi@0 | 135 | builder.setClasses(classes); |
aoqi@0 | 136 | builder.setTypeRefs(tr); |
aoqi@0 | 137 | builder.setSubclassReplacements(subclassReplacements); |
aoqi@0 | 138 | builder.setDefaultNsUri(defaultNsUri); |
aoqi@0 | 139 | builder.setC14NSupport(c14nSupport); |
aoqi@0 | 140 | builder.setAnnotationReader(ar); |
aoqi@0 | 141 | builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport); |
aoqi@0 | 142 | builder.setAllNillable(allNillable); |
aoqi@0 | 143 | builder.setRetainPropertyInfo(retainPropertyInfo); |
aoqi@0 | 144 | builder.setSupressAccessorWarnings(supressAccessorWarnings); |
aoqi@0 | 145 | builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling); |
aoqi@0 | 146 | builder.setDisableSecurityProcessing(disablesecurityProcessing); |
aoqi@0 | 147 | return builder.build(); |
aoqi@0 | 148 | } |
aoqi@0 | 149 | |
aoqi@0 | 150 | /** |
aoqi@0 | 151 | * If a key is present in the map, remove the value and return it. |
aoqi@0 | 152 | */ |
aoqi@0 | 153 | private static <T> T getPropertyValue(Map<String, Object> properties, String keyName, Class<T> type ) throws JAXBException { |
aoqi@0 | 154 | Object o = properties.get(keyName); |
aoqi@0 | 155 | if(o==null) return null; |
aoqi@0 | 156 | |
aoqi@0 | 157 | properties.remove(keyName); |
aoqi@0 | 158 | if(!type.isInstance(o)) |
aoqi@0 | 159 | throw new JAXBException(Messages.INVALID_PROPERTY_VALUE.format(keyName,o)); |
aoqi@0 | 160 | else |
aoqi@0 | 161 | return type.cast(o); |
aoqi@0 | 162 | } |
aoqi@0 | 163 | |
aoqi@0 | 164 | /** |
aoqi@0 | 165 | * |
aoqi@0 | 166 | * @param classes |
aoqi@0 | 167 | * @param typeRefs |
aoqi@0 | 168 | * @param subclassReplacements |
aoqi@0 | 169 | * @param defaultNsUri |
aoqi@0 | 170 | * @param c14nSupport |
aoqi@0 | 171 | * @param ar |
aoqi@0 | 172 | * @param xmlAccessorFactorySupport |
aoqi@0 | 173 | * @param allNillable |
aoqi@0 | 174 | * @param retainPropertyInfo |
aoqi@0 | 175 | * @return |
aoqi@0 | 176 | * @throws JAXBException |
aoqi@0 | 177 | * @deprecated use createContext(Class[] classes, Map<String,Object> properties) method instead |
aoqi@0 | 178 | */ |
aoqi@0 | 179 | @Deprecated |
aoqi@0 | 180 | public static JAXBRIContext createContext( Class[] classes, |
aoqi@0 | 181 | Collection<TypeReference> typeRefs, Map<Class,Class> subclassReplacements, |
aoqi@0 | 182 | String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar, |
aoqi@0 | 183 | boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo) throws JAXBException { |
aoqi@0 | 184 | |
aoqi@0 | 185 | return createContext(classes, typeRefs, subclassReplacements, |
aoqi@0 | 186 | defaultNsUri, c14nSupport, ar, xmlAccessorFactorySupport, |
aoqi@0 | 187 | allNillable, retainPropertyInfo, false); |
aoqi@0 | 188 | } |
aoqi@0 | 189 | |
aoqi@0 | 190 | /** |
aoqi@0 | 191 | * |
aoqi@0 | 192 | * @param classes |
aoqi@0 | 193 | * @param typeRefs |
aoqi@0 | 194 | * @param subclassReplacements |
aoqi@0 | 195 | * @param defaultNsUri |
aoqi@0 | 196 | * @param c14nSupport |
aoqi@0 | 197 | * @param ar |
aoqi@0 | 198 | * @param xmlAccessorFactorySupport |
aoqi@0 | 199 | * @param allNillable |
aoqi@0 | 200 | * @param retainPropertyInfo |
aoqi@0 | 201 | * @param improvedXsiTypeHandling |
aoqi@0 | 202 | * @return |
aoqi@0 | 203 | * @throws JAXBException |
aoqi@0 | 204 | * @deprecated use createContext( Class[] classes, Map<String,Object> properties) method instead |
aoqi@0 | 205 | */ |
aoqi@0 | 206 | @Deprecated |
aoqi@0 | 207 | public static JAXBRIContext createContext( Class[] classes, |
aoqi@0 | 208 | Collection<TypeReference> typeRefs, Map<Class,Class> subclassReplacements, |
aoqi@0 | 209 | String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar, |
aoqi@0 | 210 | boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo, boolean improvedXsiTypeHandling) throws JAXBException { |
aoqi@0 | 211 | |
aoqi@0 | 212 | JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder(); |
aoqi@0 | 213 | builder.setClasses(classes); |
aoqi@0 | 214 | builder.setTypeRefs(typeRefs); |
aoqi@0 | 215 | builder.setSubclassReplacements(subclassReplacements); |
aoqi@0 | 216 | builder.setDefaultNsUri(defaultNsUri); |
aoqi@0 | 217 | builder.setC14NSupport(c14nSupport); |
aoqi@0 | 218 | builder.setAnnotationReader(ar); |
aoqi@0 | 219 | builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport); |
aoqi@0 | 220 | builder.setAllNillable(allNillable); |
aoqi@0 | 221 | builder.setRetainPropertyInfo(retainPropertyInfo); |
aoqi@0 | 222 | builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling); |
aoqi@0 | 223 | return builder.build(); |
aoqi@0 | 224 | } |
aoqi@0 | 225 | |
aoqi@0 | 226 | /** |
aoqi@0 | 227 | * The API will invoke this method via reflection. |
aoqi@0 | 228 | */ |
aoqi@0 | 229 | public static JAXBContext createContext( String contextPath, |
aoqi@0 | 230 | ClassLoader classLoader, Map<String,Object> properties ) throws JAXBException { |
aoqi@0 | 231 | FinalArrayList<Class> classes = new FinalArrayList<Class>(); |
aoqi@0 | 232 | StringTokenizer tokens = new StringTokenizer(contextPath,":"); |
aoqi@0 | 233 | List<Class> indexedClasses; |
aoqi@0 | 234 | |
aoqi@0 | 235 | // at least on of these must be true per package |
aoqi@0 | 236 | boolean foundObjectFactory; |
aoqi@0 | 237 | boolean foundJaxbIndex; |
aoqi@0 | 238 | |
aoqi@0 | 239 | while(tokens.hasMoreTokens()) { |
aoqi@0 | 240 | foundObjectFactory = foundJaxbIndex = false; |
aoqi@0 | 241 | String pkg = tokens.nextToken(); |
aoqi@0 | 242 | |
aoqi@0 | 243 | // look for ObjectFactory and load it |
aoqi@0 | 244 | final Class<?> o; |
aoqi@0 | 245 | try { |
aoqi@0 | 246 | o = classLoader.loadClass(pkg+".ObjectFactory"); |
aoqi@0 | 247 | classes.add(o); |
aoqi@0 | 248 | foundObjectFactory = true; |
aoqi@0 | 249 | } catch (ClassNotFoundException e) { |
aoqi@0 | 250 | // not necessarily an error |
aoqi@0 | 251 | } |
aoqi@0 | 252 | |
aoqi@0 | 253 | // look for jaxb.index and load the list of classes |
aoqi@0 | 254 | try { |
aoqi@0 | 255 | indexedClasses = loadIndexedClasses(pkg, classLoader); |
aoqi@0 | 256 | } catch (IOException e) { |
aoqi@0 | 257 | //TODO: think about this more |
aoqi@0 | 258 | throw new JAXBException(e); |
aoqi@0 | 259 | } |
aoqi@0 | 260 | if(indexedClasses != null) { |
aoqi@0 | 261 | classes.addAll(indexedClasses); |
aoqi@0 | 262 | foundJaxbIndex = true; |
aoqi@0 | 263 | } |
aoqi@0 | 264 | |
aoqi@0 | 265 | if( !(foundObjectFactory || foundJaxbIndex) ) { |
aoqi@0 | 266 | throw new JAXBException( Messages.BROKEN_CONTEXTPATH.format(pkg)); |
aoqi@0 | 267 | } |
aoqi@0 | 268 | } |
aoqi@0 | 269 | |
aoqi@0 | 270 | |
aoqi@0 | 271 | return createContext(classes.toArray(new Class[classes.size()]),properties); |
aoqi@0 | 272 | } |
aoqi@0 | 273 | |
aoqi@0 | 274 | /** |
aoqi@0 | 275 | * Look for jaxb.index file in the specified package and load it's contents |
aoqi@0 | 276 | * |
aoqi@0 | 277 | * @param pkg package name to search in |
aoqi@0 | 278 | * @param classLoader ClassLoader to search in |
aoqi@0 | 279 | * @return a List of Class objects to load, null if there weren't any |
aoqi@0 | 280 | * @throws IOException if there is an error reading the index file |
aoqi@0 | 281 | * @throws JAXBException if there are any errors in the index file |
aoqi@0 | 282 | */ |
aoqi@0 | 283 | private static List<Class> loadIndexedClasses(String pkg, ClassLoader classLoader) throws IOException, JAXBException { |
aoqi@0 | 284 | final String resource = pkg.replace('.', '/') + "/jaxb.index"; |
aoqi@0 | 285 | final InputStream resourceAsStream = classLoader.getResourceAsStream(resource); |
aoqi@0 | 286 | |
aoqi@0 | 287 | if (resourceAsStream == null) { |
aoqi@0 | 288 | return null; |
aoqi@0 | 289 | } |
aoqi@0 | 290 | |
aoqi@0 | 291 | BufferedReader in = |
aoqi@0 | 292 | new BufferedReader(new InputStreamReader(resourceAsStream, "UTF-8")); |
aoqi@0 | 293 | try { |
aoqi@0 | 294 | FinalArrayList<Class> classes = new FinalArrayList<Class>(); |
aoqi@0 | 295 | String className = in.readLine(); |
aoqi@0 | 296 | while (className != null) { |
aoqi@0 | 297 | className = className.trim(); |
aoqi@0 | 298 | if (className.startsWith("#") || (className.length() == 0)) { |
aoqi@0 | 299 | className = in.readLine(); |
aoqi@0 | 300 | continue; |
aoqi@0 | 301 | } |
aoqi@0 | 302 | |
aoqi@0 | 303 | if (className.endsWith(".class")) { |
aoqi@0 | 304 | throw new JAXBException(Messages.ILLEGAL_ENTRY.format(className)); |
aoqi@0 | 305 | } |
aoqi@0 | 306 | |
aoqi@0 | 307 | try { |
aoqi@0 | 308 | classes.add(classLoader.loadClass(pkg + '.' + className)); |
aoqi@0 | 309 | } catch (ClassNotFoundException e) { |
aoqi@0 | 310 | throw new JAXBException(Messages.ERROR_LOADING_CLASS.format(className, resource),e); |
aoqi@0 | 311 | } |
aoqi@0 | 312 | |
aoqi@0 | 313 | className = in.readLine(); |
aoqi@0 | 314 | } |
aoqi@0 | 315 | return classes; |
aoqi@0 | 316 | } finally { |
aoqi@0 | 317 | in.close(); |
aoqi@0 | 318 | } |
aoqi@0 | 319 | } |
aoqi@0 | 320 | |
aoqi@0 | 321 | public static final String USE_JAXB_PROPERTIES = "_useJAXBProperties"; |
aoqi@0 | 322 | } |