ohair@286: /*
alanb@368: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286: *
ohair@286: * This code is free software; you can redistribute it and/or modify it
ohair@286: * under the terms of the GNU General Public License version 2 only, as
ohair@286: * published by the Free Software Foundation. Oracle designates this
ohair@286: * particular file as subject to the "Classpath" exception as provided
ohair@286: * by Oracle in the LICENSE file that accompanied this code.
ohair@286: *
ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286: * version 2 for more details (a copy is included in the LICENSE file that
ohair@286: * accompanied this code).
ohair@286: *
ohair@286: * You should have received a copy of the GNU General Public License version
ohair@286: * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286: *
ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286: * or visit www.oracle.com if you need additional information or have any
ohair@286: * questions.
ohair@286: */
ohair@286:
ohair@286: package com.sun.xml.internal.bind.v2;
ohair@286:
ohair@286: import java.io.BufferedReader;
ohair@286: import java.io.IOException;
ohair@286: import java.io.InputStream;
ohair@286: import java.io.InputStreamReader;
ohair@286: import java.util.Collection;
ohair@286: import java.util.Collections;
ohair@286: import java.util.HashMap;
ohair@286: import java.util.List;
ohair@286: import java.util.Map;
ohair@286: import java.util.StringTokenizer;
ohair@286: import java.util.logging.Level;
ohair@286:
ohair@286: import javax.xml.bind.JAXBContext;
ohair@286: import javax.xml.bind.JAXBException;
ohair@286:
ohair@286: import com.sun.istack.internal.FinalArrayList;
ohair@286: import com.sun.xml.internal.bind.Util;
ohair@286: import com.sun.xml.internal.bind.api.JAXBRIContext;
ohair@286: import com.sun.xml.internal.bind.api.TypeReference;
ohair@286: import com.sun.xml.internal.bind.v2.model.annotation.RuntimeAnnotationReader;
ohair@286: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
ohair@286: import com.sun.xml.internal.bind.v2.util.TypeCast;
ohair@286:
ohair@286: /**
ohair@286: * This class is responsible for producing RI JAXBContext objects. In
ohair@286: * the RI, this is the class that the javax.xml.bind.context.factory
ohair@286: * property will point to.
ohair@286: *
ohair@286: *
ohair@286: * Used to create JAXBContext objects for v1.0.1 and forward
ohair@286: *
ohair@286: * @since 2.0
ohair@286: * @author Kohsuke Kawaguchi
ohair@286: */
ohair@286: public class ContextFactory {
alanb@368:
ohair@286: /**
ohair@286: * The API will invoke this method via reflection
ohair@286: */
alanb@368: public static JAXBContext createContext(Class[] classes, Map properties ) throws JAXBException {
ohair@286: // fool-proof check, and copy the map to make it easier to find unrecognized properties.
ohair@286: if(properties==null)
ohair@286: properties = Collections.emptyMap();
ohair@286: else
ohair@286: properties = new HashMap(properties);
ohair@286:
ohair@286: String defaultNsUri = getPropertyValue(properties,JAXBRIContext.DEFAULT_NAMESPACE_REMAP,String.class);
ohair@286:
ohair@286: Boolean c14nSupport = getPropertyValue(properties,JAXBRIContext.CANONICALIZATION_SUPPORT,Boolean.class);
ohair@286: if(c14nSupport==null)
ohair@286: c14nSupport = false;
ohair@286:
alanb@368: Boolean disablesecurityProcessing = getPropertyValue(properties, JAXBRIContext.DISABLE_XML_SECURITY, Boolean.class);
alanb@368: if (disablesecurityProcessing==null)
alanb@368: disablesecurityProcessing = false;
alanb@368:
ohair@286: Boolean allNillable = getPropertyValue(properties,JAXBRIContext.TREAT_EVERYTHING_NILLABLE,Boolean.class);
ohair@286: if(allNillable==null)
ohair@286: allNillable = false;
ohair@286:
ohair@286: Boolean retainPropertyInfo = getPropertyValue(properties, JAXBRIContext.RETAIN_REFERENCE_TO_INFO, Boolean.class);
ohair@286: if(retainPropertyInfo==null)
ohair@286: retainPropertyInfo = false;
ohair@286:
ohair@286: Boolean supressAccessorWarnings = getPropertyValue(properties, JAXBRIContext.SUPRESS_ACCESSOR_WARNINGS, Boolean.class);
ohair@286: if(supressAccessorWarnings==null)
ohair@286: supressAccessorWarnings = false;
ohair@286:
ohair@286: Boolean improvedXsiTypeHandling = getPropertyValue(properties, JAXBRIContext.IMPROVED_XSI_TYPE_HANDLING, Boolean.class);
alanb@368: if (improvedXsiTypeHandling == null) {
alanb@368: String improvedXsiSystemProperty = Util.getSystemProperty(JAXBRIContext.IMPROVED_XSI_TYPE_HANDLING);
alanb@368: if (improvedXsiSystemProperty == null) {
alanb@368: improvedXsiTypeHandling = true;
alanb@368: } else {
alanb@368: improvedXsiTypeHandling = Boolean.valueOf(improvedXsiSystemProperty);
alanb@368: }
alanb@368: }
ohair@286:
ohair@286: Boolean xmlAccessorFactorySupport = getPropertyValue(properties,
ohair@286: JAXBRIContext.XMLACCESSORFACTORY_SUPPORT,Boolean.class);
ohair@286: if(xmlAccessorFactorySupport==null){
ohair@286: xmlAccessorFactorySupport = false;
ohair@286: Util.getClassLogger().log(Level.FINE, "Property " +
ohair@286: JAXBRIContext.XMLACCESSORFACTORY_SUPPORT +
ohair@286: "is not active. Using JAXB's implementation");
ohair@286: }
ohair@286:
ohair@286: RuntimeAnnotationReader ar = getPropertyValue(properties,JAXBRIContext.ANNOTATION_READER,RuntimeAnnotationReader.class);
ohair@286:
alanb@368: Collection tr = getPropertyValue(properties, JAXBRIContext.TYPE_REFERENCES, Collection.class);
alanb@368: if (tr == null) {
alanb@368: tr = Collections.emptyList();
alanb@368: }
alanb@368:
ohair@286: Map subclassReplacements;
ohair@286: try {
ohair@286: subclassReplacements = TypeCast.checkedCast(
ohair@286: getPropertyValue(properties, JAXBRIContext.SUBCLASS_REPLACEMENTS, Map.class), Class.class, Class.class);
ohair@286: } catch (ClassCastException e) {
ohair@286: throw new JAXBException(Messages.INVALID_TYPE_IN_MAP.format(),e);
ohair@286: }
ohair@286:
ohair@286: if(!properties.isEmpty()) {
ohair@286: throw new JAXBException(Messages.UNSUPPORTED_PROPERTY.format(properties.keySet().iterator().next()));
ohair@286: }
ohair@286:
ohair@286: JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder();
ohair@286: builder.setClasses(classes);
alanb@368: builder.setTypeRefs(tr);
ohair@286: builder.setSubclassReplacements(subclassReplacements);
ohair@286: builder.setDefaultNsUri(defaultNsUri);
ohair@286: builder.setC14NSupport(c14nSupport);
ohair@286: builder.setAnnotationReader(ar);
ohair@286: builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport);
ohair@286: builder.setAllNillable(allNillable);
ohair@286: builder.setRetainPropertyInfo(retainPropertyInfo);
ohair@286: builder.setSupressAccessorWarnings(supressAccessorWarnings);
ohair@286: builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling);
alanb@368: builder.setDisableSecurityProcessing(disablesecurityProcessing);
ohair@286: return builder.build();
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * If a key is present in the map, remove the value and return it.
ohair@286: */
ohair@286: private static T getPropertyValue(Map properties, String keyName, Class type ) throws JAXBException {
ohair@286: Object o = properties.get(keyName);
ohair@286: if(o==null) return null;
ohair@286:
ohair@286: properties.remove(keyName);
ohair@286: if(!type.isInstance(o))
ohair@286: throw new JAXBException(Messages.INVALID_PROPERTY_VALUE.format(keyName,o));
ohair@286: else
ohair@286: return type.cast(o);
ohair@286: }
ohair@286:
alanb@368: /**
alanb@368: *
alanb@368: * @param classes
alanb@368: * @param typeRefs
alanb@368: * @param subclassReplacements
alanb@368: * @param defaultNsUri
alanb@368: * @param c14nSupport
alanb@368: * @param ar
alanb@368: * @param xmlAccessorFactorySupport
alanb@368: * @param allNillable
alanb@368: * @param retainPropertyInfo
alanb@368: * @return
alanb@368: * @throws JAXBException
alanb@368: * @deprecated use createContext(Class[] classes, Map properties) method instead
alanb@368: */
alanb@368: @Deprecated
ohair@286: public static JAXBRIContext createContext( Class[] classes,
ohair@286: Collection typeRefs, Map subclassReplacements,
ohair@286: String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar,
ohair@286: boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo) throws JAXBException {
ohair@286:
ohair@286: return createContext(classes, typeRefs, subclassReplacements,
ohair@286: defaultNsUri, c14nSupport, ar, xmlAccessorFactorySupport,
ohair@286: allNillable, retainPropertyInfo, false);
ohair@286: }
ohair@286:
alanb@368: /**
alanb@368: *
alanb@368: * @param classes
alanb@368: * @param typeRefs
alanb@368: * @param subclassReplacements
alanb@368: * @param defaultNsUri
alanb@368: * @param c14nSupport
alanb@368: * @param ar
alanb@368: * @param xmlAccessorFactorySupport
alanb@368: * @param allNillable
alanb@368: * @param retainPropertyInfo
alanb@368: * @param improvedXsiTypeHandling
alanb@368: * @return
alanb@368: * @throws JAXBException
alanb@368: * @deprecated use createContext( Class[] classes, Map properties) method instead
alanb@368: */
alanb@368: @Deprecated
ohair@286: public static JAXBRIContext createContext( Class[] classes,
ohair@286: Collection typeRefs, Map subclassReplacements,
ohair@286: String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar,
ohair@286: boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo, boolean improvedXsiTypeHandling) throws JAXBException {
ohair@286:
ohair@286: JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder();
ohair@286: builder.setClasses(classes);
ohair@286: builder.setTypeRefs(typeRefs);
ohair@286: builder.setSubclassReplacements(subclassReplacements);
ohair@286: builder.setDefaultNsUri(defaultNsUri);
ohair@286: builder.setC14NSupport(c14nSupport);
ohair@286: builder.setAnnotationReader(ar);
ohair@286: builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport);
ohair@286: builder.setAllNillable(allNillable);
ohair@286: builder.setRetainPropertyInfo(retainPropertyInfo);
ohair@286: builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling);
ohair@286: return builder.build();
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * The API will invoke this method via reflection.
ohair@286: */
ohair@286: public static JAXBContext createContext( String contextPath,
ohair@286: ClassLoader classLoader, Map properties ) throws JAXBException {
ohair@286: FinalArrayList classes = new FinalArrayList();
ohair@286: StringTokenizer tokens = new StringTokenizer(contextPath,":");
ohair@286: List indexedClasses;
ohair@286:
ohair@286: // at least on of these must be true per package
ohair@286: boolean foundObjectFactory;
ohair@286: boolean foundJaxbIndex;
ohair@286:
ohair@286: while(tokens.hasMoreTokens()) {
ohair@286: foundObjectFactory = foundJaxbIndex = false;
ohair@286: String pkg = tokens.nextToken();
ohair@286:
ohair@286: // look for ObjectFactory and load it
ohair@286: final Class> o;
ohair@286: try {
ohair@286: o = classLoader.loadClass(pkg+".ObjectFactory");
ohair@286: classes.add(o);
ohair@286: foundObjectFactory = true;
ohair@286: } catch (ClassNotFoundException e) {
ohair@286: // not necessarily an error
ohair@286: }
ohair@286:
ohair@286: // look for jaxb.index and load the list of classes
ohair@286: try {
ohair@286: indexedClasses = loadIndexedClasses(pkg, classLoader);
ohair@286: } catch (IOException e) {
ohair@286: //TODO: think about this more
ohair@286: throw new JAXBException(e);
ohair@286: }
ohair@286: if(indexedClasses != null) {
ohair@286: classes.addAll(indexedClasses);
ohair@286: foundJaxbIndex = true;
ohair@286: }
ohair@286:
ohair@286: if( !(foundObjectFactory || foundJaxbIndex) ) {
ohair@286: throw new JAXBException( Messages.BROKEN_CONTEXTPATH.format(pkg));
ohair@286: }
ohair@286: }
ohair@286:
ohair@286:
ohair@286: return createContext(classes.toArray(new Class[classes.size()]),properties);
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Look for jaxb.index file in the specified package and load it's contents
ohair@286: *
ohair@286: * @param pkg package name to search in
ohair@286: * @param classLoader ClassLoader to search in
ohair@286: * @return a List of Class objects to load, null if there weren't any
ohair@286: * @throws IOException if there is an error reading the index file
ohair@286: * @throws JAXBException if there are any errors in the index file
ohair@286: */
ohair@286: private static List loadIndexedClasses(String pkg, ClassLoader classLoader) throws IOException, JAXBException {
ohair@286: final String resource = pkg.replace('.', '/') + "/jaxb.index";
ohair@286: final InputStream resourceAsStream = classLoader.getResourceAsStream(resource);
ohair@286:
ohair@286: if (resourceAsStream == null) {
ohair@286: return null;
ohair@286: }
ohair@286:
ohair@286: BufferedReader in =
ohair@286: new BufferedReader(new InputStreamReader(resourceAsStream, "UTF-8"));
ohair@286: try {
ohair@286: FinalArrayList classes = new FinalArrayList();
ohair@286: String className = in.readLine();
ohair@286: while (className != null) {
ohair@286: className = className.trim();
ohair@286: if (className.startsWith("#") || (className.length() == 0)) {
ohair@286: className = in.readLine();
ohair@286: continue;
ohair@286: }
ohair@286:
ohair@286: if (className.endsWith(".class")) {
ohair@286: throw new JAXBException(Messages.ILLEGAL_ENTRY.format(className));
ohair@286: }
ohair@286:
ohair@286: try {
ohair@286: classes.add(classLoader.loadClass(pkg + '.' + className));
ohair@286: } catch (ClassNotFoundException e) {
ohair@286: throw new JAXBException(Messages.ERROR_LOADING_CLASS.format(className, resource),e);
ohair@286: }
ohair@286:
ohair@286: className = in.readLine();
ohair@286: }
ohair@286: return classes;
ohair@286: } finally {
ohair@286: in.close();
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: public static final String USE_JAXB_PROPERTIES = "_useJAXBProperties";
ohair@286: }