aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2; aoqi@0: aoqi@0: import java.io.BufferedReader; aoqi@0: import java.io.IOException; aoqi@0: import java.io.InputStream; aoqi@0: import java.io.InputStreamReader; aoqi@0: import java.util.Collection; aoqi@0: import java.util.Collections; aoqi@0: import java.util.HashMap; aoqi@0: import java.util.List; aoqi@0: import java.util.Map; aoqi@0: import java.util.StringTokenizer; aoqi@0: import java.util.logging.Level; aoqi@0: aoqi@0: import javax.xml.bind.JAXBContext; aoqi@0: import javax.xml.bind.JAXBException; aoqi@0: aoqi@0: import com.sun.istack.internal.FinalArrayList; aoqi@0: import com.sun.xml.internal.bind.Util; aoqi@0: import com.sun.xml.internal.bind.api.JAXBRIContext; aoqi@0: import com.sun.xml.internal.bind.api.TypeReference; aoqi@0: import com.sun.xml.internal.bind.v2.model.annotation.RuntimeAnnotationReader; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; aoqi@0: import com.sun.xml.internal.bind.v2.util.TypeCast; aoqi@0: aoqi@0: /** aoqi@0: * This class is responsible for producing RI JAXBContext objects. In aoqi@0: * the RI, this is the class that the javax.xml.bind.context.factory aoqi@0: * property will point to. aoqi@0: * aoqi@0: *

aoqi@0: * Used to create JAXBContext objects for v1.0.1 and forward aoqi@0: * aoqi@0: * @since 2.0 aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public class ContextFactory { aoqi@0: aoqi@0: /** aoqi@0: * The API will invoke this method via reflection aoqi@0: */ aoqi@0: public static JAXBContext createContext(Class[] classes, Map properties ) throws JAXBException { aoqi@0: // fool-proof check, and copy the map to make it easier to find unrecognized properties. aoqi@0: if(properties==null) aoqi@0: properties = Collections.emptyMap(); aoqi@0: else aoqi@0: properties = new HashMap(properties); aoqi@0: aoqi@0: String defaultNsUri = getPropertyValue(properties,JAXBRIContext.DEFAULT_NAMESPACE_REMAP,String.class); aoqi@0: aoqi@0: Boolean c14nSupport = getPropertyValue(properties,JAXBRIContext.CANONICALIZATION_SUPPORT,Boolean.class); aoqi@0: if(c14nSupport==null) aoqi@0: c14nSupport = false; aoqi@0: aoqi@0: Boolean disablesecurityProcessing = getPropertyValue(properties, JAXBRIContext.DISABLE_XML_SECURITY, Boolean.class); aoqi@0: if (disablesecurityProcessing==null) aoqi@0: disablesecurityProcessing = false; aoqi@0: aoqi@0: Boolean allNillable = getPropertyValue(properties,JAXBRIContext.TREAT_EVERYTHING_NILLABLE,Boolean.class); aoqi@0: if(allNillable==null) aoqi@0: allNillable = false; aoqi@0: aoqi@0: Boolean retainPropertyInfo = getPropertyValue(properties, JAXBRIContext.RETAIN_REFERENCE_TO_INFO, Boolean.class); aoqi@0: if(retainPropertyInfo==null) aoqi@0: retainPropertyInfo = false; aoqi@0: aoqi@0: Boolean supressAccessorWarnings = getPropertyValue(properties, JAXBRIContext.SUPRESS_ACCESSOR_WARNINGS, Boolean.class); aoqi@0: if(supressAccessorWarnings==null) aoqi@0: supressAccessorWarnings = false; aoqi@0: aoqi@0: Boolean improvedXsiTypeHandling = getPropertyValue(properties, JAXBRIContext.IMPROVED_XSI_TYPE_HANDLING, Boolean.class); aoqi@0: if (improvedXsiTypeHandling == null) { aoqi@0: String improvedXsiSystemProperty = Util.getSystemProperty(JAXBRIContext.IMPROVED_XSI_TYPE_HANDLING); aoqi@0: if (improvedXsiSystemProperty == null) { aoqi@0: improvedXsiTypeHandling = true; aoqi@0: } else { aoqi@0: improvedXsiTypeHandling = Boolean.valueOf(improvedXsiSystemProperty); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: Boolean xmlAccessorFactorySupport = getPropertyValue(properties, aoqi@0: JAXBRIContext.XMLACCESSORFACTORY_SUPPORT,Boolean.class); aoqi@0: if(xmlAccessorFactorySupport==null){ aoqi@0: xmlAccessorFactorySupport = false; aoqi@0: Util.getClassLogger().log(Level.FINE, "Property " + aoqi@0: JAXBRIContext.XMLACCESSORFACTORY_SUPPORT + aoqi@0: "is not active. Using JAXB's implementation"); aoqi@0: } aoqi@0: aoqi@0: RuntimeAnnotationReader ar = getPropertyValue(properties,JAXBRIContext.ANNOTATION_READER,RuntimeAnnotationReader.class); aoqi@0: aoqi@0: Collection tr = getPropertyValue(properties, JAXBRIContext.TYPE_REFERENCES, Collection.class); aoqi@0: if (tr == null) { aoqi@0: tr = Collections.emptyList(); aoqi@0: } aoqi@0: aoqi@0: Map subclassReplacements; aoqi@0: try { aoqi@0: subclassReplacements = TypeCast.checkedCast( aoqi@0: getPropertyValue(properties, JAXBRIContext.SUBCLASS_REPLACEMENTS, Map.class), Class.class, Class.class); aoqi@0: } catch (ClassCastException e) { aoqi@0: throw new JAXBException(Messages.INVALID_TYPE_IN_MAP.format(),e); aoqi@0: } aoqi@0: aoqi@0: if(!properties.isEmpty()) { aoqi@0: throw new JAXBException(Messages.UNSUPPORTED_PROPERTY.format(properties.keySet().iterator().next())); aoqi@0: } aoqi@0: aoqi@0: JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder(); aoqi@0: builder.setClasses(classes); aoqi@0: builder.setTypeRefs(tr); aoqi@0: builder.setSubclassReplacements(subclassReplacements); aoqi@0: builder.setDefaultNsUri(defaultNsUri); aoqi@0: builder.setC14NSupport(c14nSupport); aoqi@0: builder.setAnnotationReader(ar); aoqi@0: builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport); aoqi@0: builder.setAllNillable(allNillable); aoqi@0: builder.setRetainPropertyInfo(retainPropertyInfo); aoqi@0: builder.setSupressAccessorWarnings(supressAccessorWarnings); aoqi@0: builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling); aoqi@0: builder.setDisableSecurityProcessing(disablesecurityProcessing); aoqi@0: return builder.build(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * If a key is present in the map, remove the value and return it. aoqi@0: */ aoqi@0: private static T getPropertyValue(Map properties, String keyName, Class type ) throws JAXBException { aoqi@0: Object o = properties.get(keyName); aoqi@0: if(o==null) return null; aoqi@0: aoqi@0: properties.remove(keyName); aoqi@0: if(!type.isInstance(o)) aoqi@0: throw new JAXBException(Messages.INVALID_PROPERTY_VALUE.format(keyName,o)); aoqi@0: else aoqi@0: return type.cast(o); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * aoqi@0: * @param classes aoqi@0: * @param typeRefs aoqi@0: * @param subclassReplacements aoqi@0: * @param defaultNsUri aoqi@0: * @param c14nSupport aoqi@0: * @param ar aoqi@0: * @param xmlAccessorFactorySupport aoqi@0: * @param allNillable aoqi@0: * @param retainPropertyInfo aoqi@0: * @return aoqi@0: * @throws JAXBException aoqi@0: * @deprecated use createContext(Class[] classes, Map properties) method instead aoqi@0: */ aoqi@0: @Deprecated aoqi@0: public static JAXBRIContext createContext( Class[] classes, aoqi@0: Collection typeRefs, Map subclassReplacements, aoqi@0: String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar, aoqi@0: boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo) throws JAXBException { aoqi@0: aoqi@0: return createContext(classes, typeRefs, subclassReplacements, aoqi@0: defaultNsUri, c14nSupport, ar, xmlAccessorFactorySupport, aoqi@0: allNillable, retainPropertyInfo, false); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * aoqi@0: * @param classes aoqi@0: * @param typeRefs aoqi@0: * @param subclassReplacements aoqi@0: * @param defaultNsUri aoqi@0: * @param c14nSupport aoqi@0: * @param ar aoqi@0: * @param xmlAccessorFactorySupport aoqi@0: * @param allNillable aoqi@0: * @param retainPropertyInfo aoqi@0: * @param improvedXsiTypeHandling aoqi@0: * @return aoqi@0: * @throws JAXBException aoqi@0: * @deprecated use createContext( Class[] classes, Map properties) method instead aoqi@0: */ aoqi@0: @Deprecated aoqi@0: public static JAXBRIContext createContext( Class[] classes, aoqi@0: Collection typeRefs, Map subclassReplacements, aoqi@0: String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar, aoqi@0: boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo, boolean improvedXsiTypeHandling) throws JAXBException { aoqi@0: aoqi@0: JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder(); aoqi@0: builder.setClasses(classes); aoqi@0: builder.setTypeRefs(typeRefs); aoqi@0: builder.setSubclassReplacements(subclassReplacements); aoqi@0: builder.setDefaultNsUri(defaultNsUri); aoqi@0: builder.setC14NSupport(c14nSupport); aoqi@0: builder.setAnnotationReader(ar); aoqi@0: builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport); aoqi@0: builder.setAllNillable(allNillable); aoqi@0: builder.setRetainPropertyInfo(retainPropertyInfo); aoqi@0: builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling); aoqi@0: return builder.build(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * The API will invoke this method via reflection. aoqi@0: */ aoqi@0: public static JAXBContext createContext( String contextPath, aoqi@0: ClassLoader classLoader, Map properties ) throws JAXBException { aoqi@0: FinalArrayList classes = new FinalArrayList(); aoqi@0: StringTokenizer tokens = new StringTokenizer(contextPath,":"); aoqi@0: List indexedClasses; aoqi@0: aoqi@0: // at least on of these must be true per package aoqi@0: boolean foundObjectFactory; aoqi@0: boolean foundJaxbIndex; aoqi@0: aoqi@0: while(tokens.hasMoreTokens()) { aoqi@0: foundObjectFactory = foundJaxbIndex = false; aoqi@0: String pkg = tokens.nextToken(); aoqi@0: aoqi@0: // look for ObjectFactory and load it aoqi@0: final Class o; aoqi@0: try { aoqi@0: o = classLoader.loadClass(pkg+".ObjectFactory"); aoqi@0: classes.add(o); aoqi@0: foundObjectFactory = true; aoqi@0: } catch (ClassNotFoundException e) { aoqi@0: // not necessarily an error aoqi@0: } aoqi@0: aoqi@0: // look for jaxb.index and load the list of classes aoqi@0: try { aoqi@0: indexedClasses = loadIndexedClasses(pkg, classLoader); aoqi@0: } catch (IOException e) { aoqi@0: //TODO: think about this more aoqi@0: throw new JAXBException(e); aoqi@0: } aoqi@0: if(indexedClasses != null) { aoqi@0: classes.addAll(indexedClasses); aoqi@0: foundJaxbIndex = true; aoqi@0: } aoqi@0: aoqi@0: if( !(foundObjectFactory || foundJaxbIndex) ) { aoqi@0: throw new JAXBException( Messages.BROKEN_CONTEXTPATH.format(pkg)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: return createContext(classes.toArray(new Class[classes.size()]),properties); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Look for jaxb.index file in the specified package and load it's contents aoqi@0: * aoqi@0: * @param pkg package name to search in aoqi@0: * @param classLoader ClassLoader to search in aoqi@0: * @return a List of Class objects to load, null if there weren't any aoqi@0: * @throws IOException if there is an error reading the index file aoqi@0: * @throws JAXBException if there are any errors in the index file aoqi@0: */ aoqi@0: private static List loadIndexedClasses(String pkg, ClassLoader classLoader) throws IOException, JAXBException { aoqi@0: final String resource = pkg.replace('.', '/') + "/jaxb.index"; aoqi@0: final InputStream resourceAsStream = classLoader.getResourceAsStream(resource); aoqi@0: aoqi@0: if (resourceAsStream == null) { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: BufferedReader in = aoqi@0: new BufferedReader(new InputStreamReader(resourceAsStream, "UTF-8")); aoqi@0: try { aoqi@0: FinalArrayList classes = new FinalArrayList(); aoqi@0: String className = in.readLine(); aoqi@0: while (className != null) { aoqi@0: className = className.trim(); aoqi@0: if (className.startsWith("#") || (className.length() == 0)) { aoqi@0: className = in.readLine(); aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: if (className.endsWith(".class")) { aoqi@0: throw new JAXBException(Messages.ILLEGAL_ENTRY.format(className)); aoqi@0: } aoqi@0: aoqi@0: try { aoqi@0: classes.add(classLoader.loadClass(pkg + '.' + className)); aoqi@0: } catch (ClassNotFoundException e) { aoqi@0: throw new JAXBException(Messages.ERROR_LOADING_CLASS.format(className, resource),e); aoqi@0: } aoqi@0: aoqi@0: className = in.readLine(); aoqi@0: } aoqi@0: return classes; aoqi@0: } finally { aoqi@0: in.close(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static final String USE_JAXB_PROPERTIES = "_useJAXBProperties"; aoqi@0: }