Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
ohair@286 | 1 | /* |
ohair@286 | 2 | * Copyright (c) 1997, 2010, 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 { |
ohair@286 | 63 | /** |
ohair@286 | 64 | * The API will invoke this method via reflection |
ohair@286 | 65 | */ |
ohair@286 | 66 | public static JAXBContext createContext( Class[] classes, Map<String,Object> properties ) throws JAXBException { |
ohair@286 | 67 | // fool-proof check, and copy the map to make it easier to find unrecognized properties. |
ohair@286 | 68 | if(properties==null) |
ohair@286 | 69 | properties = Collections.emptyMap(); |
ohair@286 | 70 | else |
ohair@286 | 71 | properties = new HashMap<String,Object>(properties); |
ohair@286 | 72 | |
ohair@286 | 73 | String defaultNsUri = getPropertyValue(properties,JAXBRIContext.DEFAULT_NAMESPACE_REMAP,String.class); |
ohair@286 | 74 | |
ohair@286 | 75 | Boolean c14nSupport = getPropertyValue(properties,JAXBRIContext.CANONICALIZATION_SUPPORT,Boolean.class); |
ohair@286 | 76 | if(c14nSupport==null) |
ohair@286 | 77 | c14nSupport = false; |
ohair@286 | 78 | |
ohair@286 | 79 | Boolean allNillable = getPropertyValue(properties,JAXBRIContext.TREAT_EVERYTHING_NILLABLE,Boolean.class); |
ohair@286 | 80 | if(allNillable==null) |
ohair@286 | 81 | allNillable = false; |
ohair@286 | 82 | |
ohair@286 | 83 | Boolean retainPropertyInfo = getPropertyValue(properties, JAXBRIContext.RETAIN_REFERENCE_TO_INFO, Boolean.class); |
ohair@286 | 84 | if(retainPropertyInfo==null) |
ohair@286 | 85 | retainPropertyInfo = false; |
ohair@286 | 86 | |
ohair@286 | 87 | Boolean supressAccessorWarnings = getPropertyValue(properties, JAXBRIContext.SUPRESS_ACCESSOR_WARNINGS, Boolean.class); |
ohair@286 | 88 | if(supressAccessorWarnings==null) |
ohair@286 | 89 | supressAccessorWarnings = false; |
ohair@286 | 90 | |
ohair@286 | 91 | Boolean improvedXsiTypeHandling = getPropertyValue(properties, JAXBRIContext.IMPROVED_XSI_TYPE_HANDLING, Boolean.class); |
ohair@286 | 92 | if(improvedXsiTypeHandling == null) |
ohair@286 | 93 | improvedXsiTypeHandling = true; |
ohair@286 | 94 | |
ohair@286 | 95 | Boolean xmlAccessorFactorySupport = getPropertyValue(properties, |
ohair@286 | 96 | JAXBRIContext.XMLACCESSORFACTORY_SUPPORT,Boolean.class); |
ohair@286 | 97 | if(xmlAccessorFactorySupport==null){ |
ohair@286 | 98 | xmlAccessorFactorySupport = false; |
ohair@286 | 99 | Util.getClassLogger().log(Level.FINE, "Property " + |
ohair@286 | 100 | JAXBRIContext.XMLACCESSORFACTORY_SUPPORT + |
ohair@286 | 101 | "is not active. Using JAXB's implementation"); |
ohair@286 | 102 | } |
ohair@286 | 103 | |
ohair@286 | 104 | RuntimeAnnotationReader ar = getPropertyValue(properties,JAXBRIContext.ANNOTATION_READER,RuntimeAnnotationReader.class); |
ohair@286 | 105 | |
ohair@286 | 106 | Map<Class,Class> subclassReplacements; |
ohair@286 | 107 | try { |
ohair@286 | 108 | subclassReplacements = TypeCast.checkedCast( |
ohair@286 | 109 | getPropertyValue(properties, JAXBRIContext.SUBCLASS_REPLACEMENTS, Map.class), Class.class, Class.class); |
ohair@286 | 110 | } catch (ClassCastException e) { |
ohair@286 | 111 | throw new JAXBException(Messages.INVALID_TYPE_IN_MAP.format(),e); |
ohair@286 | 112 | } |
ohair@286 | 113 | |
ohair@286 | 114 | if(!properties.isEmpty()) { |
ohair@286 | 115 | throw new JAXBException(Messages.UNSUPPORTED_PROPERTY.format(properties.keySet().iterator().next())); |
ohair@286 | 116 | } |
ohair@286 | 117 | |
ohair@286 | 118 | JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder(); |
ohair@286 | 119 | builder.setClasses(classes); |
ohair@286 | 120 | builder.setTypeRefs(Collections.<TypeReference>emptyList()); |
ohair@286 | 121 | builder.setSubclassReplacements(subclassReplacements); |
ohair@286 | 122 | builder.setDefaultNsUri(defaultNsUri); |
ohair@286 | 123 | builder.setC14NSupport(c14nSupport); |
ohair@286 | 124 | builder.setAnnotationReader(ar); |
ohair@286 | 125 | builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport); |
ohair@286 | 126 | builder.setAllNillable(allNillable); |
ohair@286 | 127 | builder.setRetainPropertyInfo(retainPropertyInfo); |
ohair@286 | 128 | builder.setSupressAccessorWarnings(supressAccessorWarnings); |
ohair@286 | 129 | builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling); |
ohair@286 | 130 | return builder.build(); |
ohair@286 | 131 | } |
ohair@286 | 132 | |
ohair@286 | 133 | /** |
ohair@286 | 134 | * If a key is present in the map, remove the value and return it. |
ohair@286 | 135 | */ |
ohair@286 | 136 | private static <T> T getPropertyValue(Map<String, Object> properties, String keyName, Class<T> type ) throws JAXBException { |
ohair@286 | 137 | Object o = properties.get(keyName); |
ohair@286 | 138 | if(o==null) return null; |
ohair@286 | 139 | |
ohair@286 | 140 | properties.remove(keyName); |
ohair@286 | 141 | if(!type.isInstance(o)) |
ohair@286 | 142 | throw new JAXBException(Messages.INVALID_PROPERTY_VALUE.format(keyName,o)); |
ohair@286 | 143 | else |
ohair@286 | 144 | return type.cast(o); |
ohair@286 | 145 | } |
ohair@286 | 146 | |
ohair@286 | 147 | public static JAXBRIContext createContext( Class[] classes, |
ohair@286 | 148 | Collection<TypeReference> typeRefs, Map<Class,Class> subclassReplacements, |
ohair@286 | 149 | String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar, |
ohair@286 | 150 | boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo) throws JAXBException { |
ohair@286 | 151 | |
ohair@286 | 152 | return createContext(classes, typeRefs, subclassReplacements, |
ohair@286 | 153 | defaultNsUri, c14nSupport, ar, xmlAccessorFactorySupport, |
ohair@286 | 154 | allNillable, retainPropertyInfo, false); |
ohair@286 | 155 | } |
ohair@286 | 156 | |
ohair@286 | 157 | public static JAXBRIContext createContext( Class[] classes, |
ohair@286 | 158 | Collection<TypeReference> typeRefs, Map<Class,Class> subclassReplacements, |
ohair@286 | 159 | String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar, |
ohair@286 | 160 | boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo, boolean improvedXsiTypeHandling) throws JAXBException { |
ohair@286 | 161 | |
ohair@286 | 162 | JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder(); |
ohair@286 | 163 | builder.setClasses(classes); |
ohair@286 | 164 | builder.setTypeRefs(typeRefs); |
ohair@286 | 165 | builder.setSubclassReplacements(subclassReplacements); |
ohair@286 | 166 | builder.setDefaultNsUri(defaultNsUri); |
ohair@286 | 167 | builder.setC14NSupport(c14nSupport); |
ohair@286 | 168 | builder.setAnnotationReader(ar); |
ohair@286 | 169 | builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport); |
ohair@286 | 170 | builder.setAllNillable(allNillable); |
ohair@286 | 171 | builder.setRetainPropertyInfo(retainPropertyInfo); |
ohair@286 | 172 | builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling); |
ohair@286 | 173 | return builder.build(); |
ohair@286 | 174 | } |
ohair@286 | 175 | |
ohair@286 | 176 | /** |
ohair@286 | 177 | * The API will invoke this method via reflection. |
ohair@286 | 178 | */ |
ohair@286 | 179 | public static JAXBContext createContext( String contextPath, |
ohair@286 | 180 | ClassLoader classLoader, Map<String,Object> properties ) throws JAXBException { |
ohair@286 | 181 | FinalArrayList<Class> classes = new FinalArrayList<Class>(); |
ohair@286 | 182 | StringTokenizer tokens = new StringTokenizer(contextPath,":"); |
ohair@286 | 183 | List<Class> indexedClasses; |
ohair@286 | 184 | |
ohair@286 | 185 | // at least on of these must be true per package |
ohair@286 | 186 | boolean foundObjectFactory; |
ohair@286 | 187 | boolean foundJaxbIndex; |
ohair@286 | 188 | |
ohair@286 | 189 | while(tokens.hasMoreTokens()) { |
ohair@286 | 190 | foundObjectFactory = foundJaxbIndex = false; |
ohair@286 | 191 | String pkg = tokens.nextToken(); |
ohair@286 | 192 | |
ohair@286 | 193 | // look for ObjectFactory and load it |
ohair@286 | 194 | final Class<?> o; |
ohair@286 | 195 | try { |
ohair@286 | 196 | o = classLoader.loadClass(pkg+".ObjectFactory"); |
ohair@286 | 197 | classes.add(o); |
ohair@286 | 198 | foundObjectFactory = true; |
ohair@286 | 199 | } catch (ClassNotFoundException e) { |
ohair@286 | 200 | // not necessarily an error |
ohair@286 | 201 | } |
ohair@286 | 202 | |
ohair@286 | 203 | // look for jaxb.index and load the list of classes |
ohair@286 | 204 | try { |
ohair@286 | 205 | indexedClasses = loadIndexedClasses(pkg, classLoader); |
ohair@286 | 206 | } catch (IOException e) { |
ohair@286 | 207 | //TODO: think about this more |
ohair@286 | 208 | throw new JAXBException(e); |
ohair@286 | 209 | } |
ohair@286 | 210 | if(indexedClasses != null) { |
ohair@286 | 211 | classes.addAll(indexedClasses); |
ohair@286 | 212 | foundJaxbIndex = true; |
ohair@286 | 213 | } |
ohair@286 | 214 | |
ohair@286 | 215 | if( !(foundObjectFactory || foundJaxbIndex) ) { |
ohair@286 | 216 | throw new JAXBException( Messages.BROKEN_CONTEXTPATH.format(pkg)); |
ohair@286 | 217 | } |
ohair@286 | 218 | } |
ohair@286 | 219 | |
ohair@286 | 220 | |
ohair@286 | 221 | return createContext(classes.toArray(new Class[classes.size()]),properties); |
ohair@286 | 222 | } |
ohair@286 | 223 | |
ohair@286 | 224 | /** |
ohair@286 | 225 | * Look for jaxb.index file in the specified package and load it's contents |
ohair@286 | 226 | * |
ohair@286 | 227 | * @param pkg package name to search in |
ohair@286 | 228 | * @param classLoader ClassLoader to search in |
ohair@286 | 229 | * @return a List of Class objects to load, null if there weren't any |
ohair@286 | 230 | * @throws IOException if there is an error reading the index file |
ohair@286 | 231 | * @throws JAXBException if there are any errors in the index file |
ohair@286 | 232 | */ |
ohair@286 | 233 | private static List<Class> loadIndexedClasses(String pkg, ClassLoader classLoader) throws IOException, JAXBException { |
ohair@286 | 234 | final String resource = pkg.replace('.', '/') + "/jaxb.index"; |
ohair@286 | 235 | final InputStream resourceAsStream = classLoader.getResourceAsStream(resource); |
ohair@286 | 236 | |
ohair@286 | 237 | if (resourceAsStream == null) { |
ohair@286 | 238 | return null; |
ohair@286 | 239 | } |
ohair@286 | 240 | |
ohair@286 | 241 | BufferedReader in = |
ohair@286 | 242 | new BufferedReader(new InputStreamReader(resourceAsStream, "UTF-8")); |
ohair@286 | 243 | try { |
ohair@286 | 244 | FinalArrayList<Class> classes = new FinalArrayList<Class>(); |
ohair@286 | 245 | String className = in.readLine(); |
ohair@286 | 246 | while (className != null) { |
ohair@286 | 247 | className = className.trim(); |
ohair@286 | 248 | if (className.startsWith("#") || (className.length() == 0)) { |
ohair@286 | 249 | className = in.readLine(); |
ohair@286 | 250 | continue; |
ohair@286 | 251 | } |
ohair@286 | 252 | |
ohair@286 | 253 | if (className.endsWith(".class")) { |
ohair@286 | 254 | throw new JAXBException(Messages.ILLEGAL_ENTRY.format(className)); |
ohair@286 | 255 | } |
ohair@286 | 256 | |
ohair@286 | 257 | try { |
ohair@286 | 258 | classes.add(classLoader.loadClass(pkg + '.' + className)); |
ohair@286 | 259 | } catch (ClassNotFoundException e) { |
ohair@286 | 260 | throw new JAXBException(Messages.ERROR_LOADING_CLASS.format(className, resource),e); |
ohair@286 | 261 | } |
ohair@286 | 262 | |
ohair@286 | 263 | className = in.readLine(); |
ohair@286 | 264 | } |
ohair@286 | 265 | return classes; |
ohair@286 | 266 | } finally { |
ohair@286 | 267 | in.close(); |
ohair@286 | 268 | } |
ohair@286 | 269 | } |
ohair@286 | 270 | |
ohair@286 | 271 | public static final String USE_JAXB_PROPERTIES = "_useJAXBProperties"; |
ohair@286 | 272 | } |