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