< prev index next >

src/java.xml.ws/share/classes/com/sun/xml/internal/ws/spi/db/BindingContextFactory.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.ws.spi.db;
  27 

  28 import java.util.Iterator;
  29 import java.util.List;
  30 import java.util.logging.Level;
  31 import java.util.logging.Logger;
  32 
  33 import javax.xml.bind.JAXBContext;
  34 import javax.xml.bind.Marshaller;
  35 
  36 
  37 import com.oracle.webservices.internal.api.databinding.DatabindingModeFeature;
  38 import com.sun.xml.internal.ws.db.glassfish.JAXBRIContextFactory;
  39 import com.sun.xml.internal.ws.util.ServiceConfigurationError;
  40 import com.sun.xml.internal.ws.util.ServiceFinder;
  41 
  42 /**
  43  * BindingContextFactory
  44  *
  45  * @author shih-chang.chen@oracle.com
  46  */
  47 abstract public class BindingContextFactory {


 139         // System property comes next, then SPI-located.
 140         String mode = bi.getDatabindingMode();
 141         if (mode != null) {
 142             if (LOGGER.isLoggable(Level.FINE))
 143                 LOGGER.log(Level.FINE, "Using SEI-configured databindng mode: "
 144                         + mode);
 145         } else if ((mode = System.getProperty("BindingContextFactory")) != null) {
 146             // The following is left for backward compatibility and should
 147             // eventually be removed.
 148             bi.setDatabindingMode(mode);
 149             if (LOGGER.isLoggable(Level.FINE))
 150                 LOGGER.log(Level.FINE, "Using databindng: " + mode
 151                         + " based on 'BindingContextFactory' System property");
 152         } else if ((mode = System.getProperty(JAXB_CONTEXT_FACTORY_PROPERTY)) != null) {
 153             bi.setDatabindingMode(mode);
 154             if (LOGGER.isLoggable(Level.FINE))
 155                 LOGGER.log(Level.FINE, "Using databindng: " + mode
 156                         + " based on '" + JAXB_CONTEXT_FACTORY_PROPERTY
 157                         + "' System property");
 158         } else {
 159             // Find a default provider.  Note we always ensure the list
 160             // is always non-empty.
 161             for (BindingContextFactory factory : factories()) {
 162                 if (LOGGER.isLoggable(Level.FINE))
 163                     LOGGER.log(Level.FINE,
 164                             "Using SPI-determined databindng mode: "
 165                                     + factory.getClass().getName());
 166                 // Special case: no name lookup used.
 167                 return factory.newContext(bi);
 168             }
 169 
 170             // Should never get here as the list is non-empty.
 171             LOGGER.log(Level.SEVERE, "No Binding Context Factories found.");
 172             throw new DatabindingException("No Binding Context Factories found.");
 173         }
 174         BindingContextFactory f = getFactory(mode);
 175         if (f != null)
 176             return f.newContext(bi);
 177         LOGGER.severe("Unknown Databinding mode: " + mode);
 178         throw new DatabindingException("Unknown Databinding mode: " + mode);
 179     }
 180 
























































 181         static public boolean isContextSupported(Object o) {
 182             if (o == null) return false;
 183                 String pkgName = o.getClass().getPackage().getName();
 184                 for (BindingContextFactory f: factories()) if (f.isFor(pkgName)) return true;
 185                 return false;
 186         }
 187 
 188         static BindingContextFactory getJAXBFactory(Object o) {
 189                 String pkgName = o.getClass().getPackage().getName();
 190                 BindingContextFactory f = getFactory(pkgName);
 191                 if (f != null) return f;
 192                 throw new DatabindingException("Unknown JAXBContext implementation: " + o.getClass());
 193 
 194         }
 195 
 196         /**
 197          * @deprecated - Does jaxws need this?
 198          */
 199         static public BindingContext getBindingContext(Marshaller m) {
 200                 return getJAXBFactory(m).getContext(m);
 201         }
 202 
 203     /**
 204      * Creates a new {@link BindingContext}.
 205      *
 206      * <p>
 207      * {@link JAXBContext#newInstance(Class[]) JAXBContext.newInstance()} methods may
 208      * return other JAXB providers that are not compatible with the JAX-RPC RI.
 209      * This method guarantees that the JAX-WS RI will finds the JAXB RI.
 210      *
 211      * @param classes
 212      *      Classes to be bound. See {@link JAXBContext#newInstance(Class[])} for the meaning.
 213      * @param typeRefs
 214      *      See {@link #TYPE_REFERENCES} for the meaning of this parameter.
 215      *      Can be null.
 216      * @param subclassReplacements
 217      *      See {@link #SUBCLASS_REPLACEMENTS} for the meaning of this parameter.
 218      *      Can be null.
 219      * @param defaultNamespaceRemap
 220      *      See {@link #DEFAULT_NAMESPACE_REMAP} for the meaning of this parameter.
 221      *      Can be null (and should be null for ordinary use of JAXB.)
 222      * @param c14nSupport
 223      *      See {@link #CANONICALIZATION_SUPPORT} for the meaning of this parameter.
 224      * @param ar
 225      *      See {@link #ANNOTATION_READER} for the meaning of this parameter.
 226      *      Can be null.
 227      * @since JAXB 2.1 EA2
 228      */
 229 //    public static BindingContext newInstance(@NotNull Class[] classes,
 230 //       @Nullable Collection<TypeInfo> typeRefs,
 231 //       @Nullable Map<Class,Class> subclassReplacements,
 232 //       @Nullable String defaultNamespaceRemap, boolean c14nSupport,
 233 //       @Nullable RuntimeAnnotationReader ar) throws JAXBException {
 234 //        return ContextFactory.createContext(classes, typeRefs, subclassReplacements,
 235 //                defaultNamespaceRemap, c14nSupport, ar, false, false, false);
 236 //    }
 237 //
 238 //    /**
 239 //     * @deprecated
 240 //     *      Compatibility with older versions.
 241 //     */
 242 //    public static BindingContext newInstance(@NotNull Class[] classes,
 243 //        @Nullable Collection<TypeInfo> typeRefs,
 244 //        @Nullable String defaultNamespaceRemap, boolean c14nSupport ) throws JAXBException {
 245 //        return newInstance(classes,typeRefs, Collections.<Class,Class>emptyMap(),
 246 //                defaultNamespaceRemap,c14nSupport,null);
 247 //    }
 248 }
   1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.ws.spi.db;
  27 
  28 import java.util.ArrayList;
  29 import java.util.Iterator;
  30 import java.util.List;
  31 import java.util.logging.Level;
  32 import java.util.logging.Logger;
  33 
  34 import javax.xml.bind.JAXBContext;
  35 import javax.xml.bind.Marshaller;
  36 
  37 
  38 import com.oracle.webservices.internal.api.databinding.DatabindingModeFeature;
  39 import com.sun.xml.internal.ws.db.glassfish.JAXBRIContextFactory;
  40 import com.sun.xml.internal.ws.util.ServiceConfigurationError;
  41 import com.sun.xml.internal.ws.util.ServiceFinder;
  42 
  43 /**
  44  * BindingContextFactory
  45  *
  46  * @author shih-chang.chen@oracle.com
  47  */
  48 abstract public class BindingContextFactory {


 140         // System property comes next, then SPI-located.
 141         String mode = bi.getDatabindingMode();
 142         if (mode != null) {
 143             if (LOGGER.isLoggable(Level.FINE))
 144                 LOGGER.log(Level.FINE, "Using SEI-configured databindng mode: "
 145                         + mode);
 146         } else if ((mode = System.getProperty("BindingContextFactory")) != null) {
 147             // The following is left for backward compatibility and should
 148             // eventually be removed.
 149             bi.setDatabindingMode(mode);
 150             if (LOGGER.isLoggable(Level.FINE))
 151                 LOGGER.log(Level.FINE, "Using databindng: " + mode
 152                         + " based on 'BindingContextFactory' System property");
 153         } else if ((mode = System.getProperty(JAXB_CONTEXT_FACTORY_PROPERTY)) != null) {
 154             bi.setDatabindingMode(mode);
 155             if (LOGGER.isLoggable(Level.FINE))
 156                 LOGGER.log(Level.FINE, "Using databindng: " + mode
 157                         + " based on '" + JAXB_CONTEXT_FACTORY_PROPERTY
 158                         + "' System property");
 159         } else {
 160             // Find a default provider.  Note we always ensure the list is always non-empty.
 161             BindingContext factory = getBindingContextFromSpi(factories(), bi);
 162             if (factory != null) return factory;








 163             // Should never get here as the list is non-empty.
 164             LOGGER.log(Level.SEVERE, "No Binding Context Factories found.");
 165             throw new DatabindingException("No Binding Context Factories found.");
 166         }
 167         BindingContextFactory f = getFactory(mode);
 168         if (f != null)
 169             return f.newContext(bi);
 170         LOGGER.severe("Unknown Databinding mode: " + mode);
 171         throw new DatabindingException("Unknown Databinding mode: " + mode);
 172     }
 173 
 174     /**
 175      * Creates JAXB bindingContext with one of the provided factories.
 176      * To filter appropriate factory {@link BindingContextFactory#isFor(String)} method is used.
 177      * Currently known 2 appropriate factories: JAXB RI and MOXY.
 178      * In case no suitable factory is found we are trying to create context with any given factory.
 179      *
 180      * @param factories given collection of factories.
 181      * @param bindingInfo will be used to create bindingContext.
 182      * @return Created context or null. Null will be returned if we were not able to create context with any given factory.
 183      */
 184     private static BindingContext getBindingContextFromSpi(List<BindingContextFactory> factories, BindingInfo bindingInfo) {
 185         List<BindingContextFactory> fallback = new ArrayList<BindingContextFactory>();
 186         BindingContext result;
 187         for (BindingContextFactory factory : factories) {
 188             if (LOGGER.isLoggable(Level.FINE)) {
 189                 LOGGER.log(Level.FINE, "Found SPI-determined databindng mode: " + factory.getClass().getName());
 190             }
 191             if (factory.isFor("org.eclipse.persistence.jaxb") || factory.isFor("com.sun.xml.internal.bind.v2.runtime")) { // filter (JAXB RI || MOXy) implementation
 192                 result = factory.newContext(bindingInfo);
 193                 if (result != null) {
 194                     return result;
 195                 }
 196             } else {
 197                 if (LOGGER.isLoggable(Level.FINE)) {
 198                     LOGGER.log(Level.FINE, "Skipped -> not JAXB.");
 199                 }
 200                 fallback.add(factory);
 201             }
 202         }
 203         for (BindingContextFactory factory : fallback) {
 204             if (LOGGER.isLoggable(Level.FINE)) {
 205                 LOGGER.log(Level.FINE, "Fallback. Creating from: " + factory.getClass().getName());
 206             }
 207             result = getContextOrNullIfError(factory, bindingInfo);
 208             if (result != null) {
 209                 return result;
 210             }
 211         }
 212         return null;
 213     }
 214 
 215     /**
 216      * Factory creates new context bases on provided bindingInfo.
 217      * @param factory given factory.
 218      * @param bindingInfo to be used to create context.
 219      * @return Created context or null. Null will be returned if an error happened during the creation process.
 220      */
 221     private static BindingContext getContextOrNullIfError(BindingContextFactory factory, BindingInfo bindingInfo) {
 222         try {
 223             return factory.newContext(bindingInfo);
 224         } catch (Exception e) {
 225             LOGGER.log(Level.WARNING, e.getMessage(), e);
 226             return null;
 227         }
 228     }
 229 
 230     static public boolean isContextSupported(Object o) {
 231             if (o == null) return false;
 232                 String pkgName = o.getClass().getPackage().getName();
 233                 for (BindingContextFactory f: factories()) if (f.isFor(pkgName)) return true;
 234                 return false;
 235         }
 236 
 237         static BindingContextFactory getJAXBFactory(Object o) {
 238                 String pkgName = o.getClass().getPackage().getName();
 239                 BindingContextFactory f = getFactory(pkgName);
 240                 if (f != null) return f;
 241                 throw new DatabindingException("Unknown JAXBContext implementation: " + o.getClass());
 242 
 243         }
 244 
 245         /**
 246          * @deprecated - Does jaxws need this?
 247          */
 248         static public BindingContext getBindingContext(Marshaller m) {
 249                 return getJAXBFactory(m).getContext(m);
 250         }














































 251 }
< prev index next >