src/share/jaxws_classes/com/sun/xml/internal/bind/v2/ContextFactory.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial