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 Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,272 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, 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 + * The API will invoke this method via reflection 1.68 + */ 1.69 + public static JAXBContext createContext( Class[] classes, Map<String,Object> properties ) throws JAXBException { 1.70 + // fool-proof check, and copy the map to make it easier to find unrecognized properties. 1.71 + if(properties==null) 1.72 + properties = Collections.emptyMap(); 1.73 + else 1.74 + properties = new HashMap<String,Object>(properties); 1.75 + 1.76 + String defaultNsUri = getPropertyValue(properties,JAXBRIContext.DEFAULT_NAMESPACE_REMAP,String.class); 1.77 + 1.78 + Boolean c14nSupport = getPropertyValue(properties,JAXBRIContext.CANONICALIZATION_SUPPORT,Boolean.class); 1.79 + if(c14nSupport==null) 1.80 + c14nSupport = false; 1.81 + 1.82 + Boolean allNillable = getPropertyValue(properties,JAXBRIContext.TREAT_EVERYTHING_NILLABLE,Boolean.class); 1.83 + if(allNillable==null) 1.84 + allNillable = false; 1.85 + 1.86 + Boolean retainPropertyInfo = getPropertyValue(properties, JAXBRIContext.RETAIN_REFERENCE_TO_INFO, Boolean.class); 1.87 + if(retainPropertyInfo==null) 1.88 + retainPropertyInfo = false; 1.89 + 1.90 + Boolean supressAccessorWarnings = getPropertyValue(properties, JAXBRIContext.SUPRESS_ACCESSOR_WARNINGS, Boolean.class); 1.91 + if(supressAccessorWarnings==null) 1.92 + supressAccessorWarnings = false; 1.93 + 1.94 + Boolean improvedXsiTypeHandling = getPropertyValue(properties, JAXBRIContext.IMPROVED_XSI_TYPE_HANDLING, Boolean.class); 1.95 + if(improvedXsiTypeHandling == null) 1.96 + improvedXsiTypeHandling = true; 1.97 + 1.98 + Boolean xmlAccessorFactorySupport = getPropertyValue(properties, 1.99 + JAXBRIContext.XMLACCESSORFACTORY_SUPPORT,Boolean.class); 1.100 + if(xmlAccessorFactorySupport==null){ 1.101 + xmlAccessorFactorySupport = false; 1.102 + Util.getClassLogger().log(Level.FINE, "Property " + 1.103 + JAXBRIContext.XMLACCESSORFACTORY_SUPPORT + 1.104 + "is not active. Using JAXB's implementation"); 1.105 + } 1.106 + 1.107 + RuntimeAnnotationReader ar = getPropertyValue(properties,JAXBRIContext.ANNOTATION_READER,RuntimeAnnotationReader.class); 1.108 + 1.109 + Map<Class,Class> subclassReplacements; 1.110 + try { 1.111 + subclassReplacements = TypeCast.checkedCast( 1.112 + getPropertyValue(properties, JAXBRIContext.SUBCLASS_REPLACEMENTS, Map.class), Class.class, Class.class); 1.113 + } catch (ClassCastException e) { 1.114 + throw new JAXBException(Messages.INVALID_TYPE_IN_MAP.format(),e); 1.115 + } 1.116 + 1.117 + if(!properties.isEmpty()) { 1.118 + throw new JAXBException(Messages.UNSUPPORTED_PROPERTY.format(properties.keySet().iterator().next())); 1.119 + } 1.120 + 1.121 + JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder(); 1.122 + builder.setClasses(classes); 1.123 + builder.setTypeRefs(Collections.<TypeReference>emptyList()); 1.124 + builder.setSubclassReplacements(subclassReplacements); 1.125 + builder.setDefaultNsUri(defaultNsUri); 1.126 + builder.setC14NSupport(c14nSupport); 1.127 + builder.setAnnotationReader(ar); 1.128 + builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport); 1.129 + builder.setAllNillable(allNillable); 1.130 + builder.setRetainPropertyInfo(retainPropertyInfo); 1.131 + builder.setSupressAccessorWarnings(supressAccessorWarnings); 1.132 + builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling); 1.133 + return builder.build(); 1.134 + } 1.135 + 1.136 + /** 1.137 + * If a key is present in the map, remove the value and return it. 1.138 + */ 1.139 + private static <T> T getPropertyValue(Map<String, Object> properties, String keyName, Class<T> type ) throws JAXBException { 1.140 + Object o = properties.get(keyName); 1.141 + if(o==null) return null; 1.142 + 1.143 + properties.remove(keyName); 1.144 + if(!type.isInstance(o)) 1.145 + throw new JAXBException(Messages.INVALID_PROPERTY_VALUE.format(keyName,o)); 1.146 + else 1.147 + return type.cast(o); 1.148 + } 1.149 + 1.150 + public static JAXBRIContext createContext( Class[] classes, 1.151 + Collection<TypeReference> typeRefs, Map<Class,Class> subclassReplacements, 1.152 + String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar, 1.153 + boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo) throws JAXBException { 1.154 + 1.155 + return createContext(classes, typeRefs, subclassReplacements, 1.156 + defaultNsUri, c14nSupport, ar, xmlAccessorFactorySupport, 1.157 + allNillable, retainPropertyInfo, false); 1.158 + } 1.159 + 1.160 + public static JAXBRIContext createContext( Class[] classes, 1.161 + Collection<TypeReference> typeRefs, Map<Class,Class> subclassReplacements, 1.162 + String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar, 1.163 + boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo, boolean improvedXsiTypeHandling) throws JAXBException { 1.164 + 1.165 + JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder(); 1.166 + builder.setClasses(classes); 1.167 + builder.setTypeRefs(typeRefs); 1.168 + builder.setSubclassReplacements(subclassReplacements); 1.169 + builder.setDefaultNsUri(defaultNsUri); 1.170 + builder.setC14NSupport(c14nSupport); 1.171 + builder.setAnnotationReader(ar); 1.172 + builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport); 1.173 + builder.setAllNillable(allNillable); 1.174 + builder.setRetainPropertyInfo(retainPropertyInfo); 1.175 + builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling); 1.176 + return builder.build(); 1.177 + } 1.178 + 1.179 + /** 1.180 + * The API will invoke this method via reflection. 1.181 + */ 1.182 + public static JAXBContext createContext( String contextPath, 1.183 + ClassLoader classLoader, Map<String,Object> properties ) throws JAXBException { 1.184 + FinalArrayList<Class> classes = new FinalArrayList<Class>(); 1.185 + StringTokenizer tokens = new StringTokenizer(contextPath,":"); 1.186 + List<Class> indexedClasses; 1.187 + 1.188 + // at least on of these must be true per package 1.189 + boolean foundObjectFactory; 1.190 + boolean foundJaxbIndex; 1.191 + 1.192 + while(tokens.hasMoreTokens()) { 1.193 + foundObjectFactory = foundJaxbIndex = false; 1.194 + String pkg = tokens.nextToken(); 1.195 + 1.196 + // look for ObjectFactory and load it 1.197 + final Class<?> o; 1.198 + try { 1.199 + o = classLoader.loadClass(pkg+".ObjectFactory"); 1.200 + classes.add(o); 1.201 + foundObjectFactory = true; 1.202 + } catch (ClassNotFoundException e) { 1.203 + // not necessarily an error 1.204 + } 1.205 + 1.206 + // look for jaxb.index and load the list of classes 1.207 + try { 1.208 + indexedClasses = loadIndexedClasses(pkg, classLoader); 1.209 + } catch (IOException e) { 1.210 + //TODO: think about this more 1.211 + throw new JAXBException(e); 1.212 + } 1.213 + if(indexedClasses != null) { 1.214 + classes.addAll(indexedClasses); 1.215 + foundJaxbIndex = true; 1.216 + } 1.217 + 1.218 + if( !(foundObjectFactory || foundJaxbIndex) ) { 1.219 + throw new JAXBException( Messages.BROKEN_CONTEXTPATH.format(pkg)); 1.220 + } 1.221 + } 1.222 + 1.223 + 1.224 + return createContext(classes.toArray(new Class[classes.size()]),properties); 1.225 + } 1.226 + 1.227 + /** 1.228 + * Look for jaxb.index file in the specified package and load it's contents 1.229 + * 1.230 + * @param pkg package name to search in 1.231 + * @param classLoader ClassLoader to search in 1.232 + * @return a List of Class objects to load, null if there weren't any 1.233 + * @throws IOException if there is an error reading the index file 1.234 + * @throws JAXBException if there are any errors in the index file 1.235 + */ 1.236 + private static List<Class> loadIndexedClasses(String pkg, ClassLoader classLoader) throws IOException, JAXBException { 1.237 + final String resource = pkg.replace('.', '/') + "/jaxb.index"; 1.238 + final InputStream resourceAsStream = classLoader.getResourceAsStream(resource); 1.239 + 1.240 + if (resourceAsStream == null) { 1.241 + return null; 1.242 + } 1.243 + 1.244 + BufferedReader in = 1.245 + new BufferedReader(new InputStreamReader(resourceAsStream, "UTF-8")); 1.246 + try { 1.247 + FinalArrayList<Class> classes = new FinalArrayList<Class>(); 1.248 + String className = in.readLine(); 1.249 + while (className != null) { 1.250 + className = className.trim(); 1.251 + if (className.startsWith("#") || (className.length() == 0)) { 1.252 + className = in.readLine(); 1.253 + continue; 1.254 + } 1.255 + 1.256 + if (className.endsWith(".class")) { 1.257 + throw new JAXBException(Messages.ILLEGAL_ENTRY.format(className)); 1.258 + } 1.259 + 1.260 + try { 1.261 + classes.add(classLoader.loadClass(pkg + '.' + className)); 1.262 + } catch (ClassNotFoundException e) { 1.263 + throw new JAXBException(Messages.ERROR_LOADING_CLASS.format(className, resource),e); 1.264 + } 1.265 + 1.266 + className = in.readLine(); 1.267 + } 1.268 + return classes; 1.269 + } finally { 1.270 + in.close(); 1.271 + } 1.272 + } 1.273 + 1.274 + public static final String USE_JAXB_PROPERTIES = "_useJAXBProperties"; 1.275 +}