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.ws.spi.db; aoqi@0: aoqi@0: import java.util.Iterator; aoqi@0: import java.util.List; aoqi@0: import java.util.logging.Level; aoqi@0: import java.util.logging.Logger; aoqi@0: aoqi@0: import javax.xml.bind.JAXBContext; aoqi@0: import javax.xml.bind.Marshaller; aoqi@0: aoqi@0: aoqi@0: import com.oracle.webservices.internal.api.databinding.DatabindingModeFeature; aoqi@0: import com.sun.xml.internal.ws.db.glassfish.JAXBRIContextFactory; aoqi@0: import com.sun.xml.internal.ws.util.ServiceConfigurationError; aoqi@0: import com.sun.xml.internal.ws.util.ServiceFinder; aoqi@0: aoqi@0: /** aoqi@0: * BindingContextFactory aoqi@0: * aoqi@0: * @author shih-chang.chen@oracle.com aoqi@0: */ aoqi@0: abstract public class BindingContextFactory { aoqi@0: public static final String DefaultDatabindingMode = DatabindingModeFeature.GLASSFISH_JAXB; aoqi@0: public static final String JAXB_CONTEXT_FACTORY_PROPERTY = BindingContextFactory.class.getName(); aoqi@0: public static final Logger LOGGER = Logger.getLogger(BindingContextFactory.class.getName()); aoqi@0: aoqi@0: // This iterator adds exception checking for proper logging. aoqi@0: public static Iterator serviceIterator() { aoqi@0: final ServiceFinder sf = ServiceFinder aoqi@0: .find(BindingContextFactory.class); aoqi@0: final Iterator ibcf = sf.iterator(); aoqi@0: aoqi@0: return new Iterator() { aoqi@0: private BindingContextFactory bcf; aoqi@0: aoqi@0: public boolean hasNext() { aoqi@0: while (true) { aoqi@0: try { aoqi@0: if (ibcf.hasNext()) { aoqi@0: bcf = ibcf.next(); aoqi@0: return true; aoqi@0: } else aoqi@0: return false; aoqi@0: } catch (ServiceConfigurationError e) { aoqi@0: LOGGER.warning("skipping factory: ServiceConfigurationError: " aoqi@0: + e.getMessage()); aoqi@0: } catch (NoClassDefFoundError ncdfe) { aoqi@0: LOGGER.fine("skipping factory: NoClassDefFoundError: " aoqi@0: + ncdfe.getMessage()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public BindingContextFactory next() { aoqi@0: if (LOGGER.isLoggable(Level.FINER)) aoqi@0: LOGGER.finer("SPI found provider: " + aoqi@0: bcf.getClass().getName()); aoqi@0: return bcf; aoqi@0: } aoqi@0: aoqi@0: public void remove() { aoqi@0: throw new UnsupportedOperationException(); aoqi@0: } aoqi@0: }; aoqi@0: } aoqi@0: aoqi@0: static private List factories() { aoqi@0: List factories = new java.util.ArrayList(); aoqi@0: Iterator ibcf = serviceIterator(); aoqi@0: while (ibcf.hasNext()) aoqi@0: factories.add(ibcf.next()); aoqi@0: aoqi@0: // There should always be at least one factory available. aoqi@0: if (factories.isEmpty()) { aoqi@0: if (LOGGER.isLoggable(Level.FINER)) aoqi@0: LOGGER.log(Level.FINER, "No SPI providers for BindingContextFactory found, adding: " aoqi@0: + JAXBRIContextFactory.class.getName()); aoqi@0: factories.add(new JAXBRIContextFactory()); aoqi@0: } aoqi@0: return factories; aoqi@0: } aoqi@0: aoqi@0: abstract protected BindingContext newContext(JAXBContext context); aoqi@0: aoqi@0: abstract protected BindingContext newContext(BindingInfo bi); aoqi@0: aoqi@0: /** aoqi@0: * Check to see if the BindingContextFactory is for the databinding mode/flavor. The aoqi@0: * String parameter can be the package name of the JAXBContext implementation as well. aoqi@0: * @param databinding mode/flavor or the package name of the JAXBContext implementation. aoqi@0: * @return aoqi@0: */ aoqi@0: abstract protected boolean isFor(String databinding); aoqi@0: aoqi@0: /** aoqi@0: * @deprecated - Does jaxws need this? aoqi@0: */ aoqi@0: abstract protected BindingContext getContext(Marshaller m); aoqi@0: aoqi@0: static private BindingContextFactory getFactory(String mode) { aoqi@0: for (BindingContextFactory f: factories()) { aoqi@0: if (f.isFor(mode)) aoqi@0: return f; aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: static public BindingContext create(JAXBContext context) throws DatabindingException { aoqi@0: return getJAXBFactory(context).newContext(context); aoqi@0: } aoqi@0: aoqi@0: static public BindingContext create(BindingInfo bi) { aoqi@0: // Any mode configured in AbstractSEIModelImpl trumps all. aoqi@0: // System property comes next, then SPI-located. aoqi@0: String mode = bi.getDatabindingMode(); aoqi@0: if (mode != null) { aoqi@0: if (LOGGER.isLoggable(Level.FINE)) aoqi@0: LOGGER.log(Level.FINE, "Using SEI-configured databindng mode: " aoqi@0: + mode); aoqi@0: } else if ((mode = System.getProperty("BindingContextFactory")) != null) { aoqi@0: // The following is left for backward compatibility and should aoqi@0: // eventually be removed. aoqi@0: bi.setDatabindingMode(mode); aoqi@0: if (LOGGER.isLoggable(Level.FINE)) aoqi@0: LOGGER.log(Level.FINE, "Using databindng: " + mode aoqi@0: + " based on 'BindingContextFactory' System property"); aoqi@0: } else if ((mode = System.getProperty(JAXB_CONTEXT_FACTORY_PROPERTY)) != null) { aoqi@0: bi.setDatabindingMode(mode); aoqi@0: if (LOGGER.isLoggable(Level.FINE)) aoqi@0: LOGGER.log(Level.FINE, "Using databindng: " + mode aoqi@0: + " based on '" + JAXB_CONTEXT_FACTORY_PROPERTY aoqi@0: + "' System property"); aoqi@0: } else { aoqi@0: // Find a default provider. Note we always ensure the list aoqi@0: // is always non-empty. aoqi@0: for (BindingContextFactory factory : factories()) { aoqi@0: if (LOGGER.isLoggable(Level.FINE)) aoqi@0: LOGGER.log(Level.FINE, aoqi@0: "Using SPI-determined databindng mode: " aoqi@0: + factory.getClass().getName()); aoqi@0: // Special case: no name lookup used. aoqi@0: return factory.newContext(bi); aoqi@0: } aoqi@0: aoqi@0: // Should never get here as the list is non-empty. aoqi@0: LOGGER.log(Level.SEVERE, "No Binding Context Factories found."); aoqi@0: throw new DatabindingException("No Binding Context Factories found."); aoqi@0: } aoqi@0: BindingContextFactory f = getFactory(mode); aoqi@0: if (f != null) aoqi@0: return f.newContext(bi); aoqi@0: LOGGER.severe("Unknown Databinding mode: " + mode); aoqi@0: throw new DatabindingException("Unknown Databinding mode: " + mode); aoqi@0: } aoqi@0: aoqi@0: static public boolean isContextSupported(Object o) { aoqi@0: if (o == null) return false; aoqi@0: String pkgName = o.getClass().getPackage().getName(); aoqi@0: for (BindingContextFactory f: factories()) if (f.isFor(pkgName)) return true; aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: static BindingContextFactory getJAXBFactory(Object o) { aoqi@0: String pkgName = o.getClass().getPackage().getName(); aoqi@0: BindingContextFactory f = getFactory(pkgName); aoqi@0: if (f != null) return f; aoqi@0: throw new DatabindingException("Unknown JAXBContext implementation: " + o.getClass()); aoqi@0: aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * @deprecated - Does jaxws need this? aoqi@0: */ aoqi@0: static public BindingContext getBindingContext(Marshaller m) { aoqi@0: return getJAXBFactory(m).getContext(m); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Creates a new {@link BindingContext}. aoqi@0: * aoqi@0: *

aoqi@0: * {@link JAXBContext#newInstance(Class[]) JAXBContext.newInstance()} methods may aoqi@0: * return other JAXB providers that are not compatible with the JAX-RPC RI. aoqi@0: * This method guarantees that the JAX-WS RI will finds the JAXB RI. aoqi@0: * aoqi@0: * @param classes aoqi@0: * Classes to be bound. See {@link JAXBContext#newInstance(Class[])} for the meaning. aoqi@0: * @param typeRefs aoqi@0: * See {@link #TYPE_REFERENCES} for the meaning of this parameter. aoqi@0: * Can be null. aoqi@0: * @param subclassReplacements aoqi@0: * See {@link #SUBCLASS_REPLACEMENTS} for the meaning of this parameter. aoqi@0: * Can be null. aoqi@0: * @param defaultNamespaceRemap aoqi@0: * See {@link #DEFAULT_NAMESPACE_REMAP} for the meaning of this parameter. aoqi@0: * Can be null (and should be null for ordinary use of JAXB.) aoqi@0: * @param c14nSupport aoqi@0: * See {@link #CANONICALIZATION_SUPPORT} for the meaning of this parameter. aoqi@0: * @param ar aoqi@0: * See {@link #ANNOTATION_READER} for the meaning of this parameter. aoqi@0: * Can be null. aoqi@0: * @since JAXB 2.1 EA2 aoqi@0: */ aoqi@0: // public static BindingContext newInstance(@NotNull Class[] classes, aoqi@0: // @Nullable Collection typeRefs, aoqi@0: // @Nullable Map subclassReplacements, aoqi@0: // @Nullable String defaultNamespaceRemap, boolean c14nSupport, aoqi@0: // @Nullable RuntimeAnnotationReader ar) throws JAXBException { aoqi@0: // return ContextFactory.createContext(classes, typeRefs, subclassReplacements, aoqi@0: // defaultNamespaceRemap, c14nSupport, ar, false, false, false); aoqi@0: // } aoqi@0: // aoqi@0: // /** aoqi@0: // * @deprecated aoqi@0: // * Compatibility with older versions. aoqi@0: // */ aoqi@0: // public static BindingContext newInstance(@NotNull Class[] classes, aoqi@0: // @Nullable Collection typeRefs, aoqi@0: // @Nullable String defaultNamespaceRemap, boolean c14nSupport ) throws JAXBException { aoqi@0: // return newInstance(classes,typeRefs, Collections.emptyMap(), aoqi@0: // defaultNamespaceRemap,c14nSupport,null); aoqi@0: // } aoqi@0: }