< prev index next >

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

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -23,10 +23,11 @@
  * questions.
  */
 
 package com.sun.xml.internal.ws.spi.db;
 
+import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 

@@ -154,21 +155,13 @@
             if (LOGGER.isLoggable(Level.FINE))
                 LOGGER.log(Level.FINE, "Using databindng: " + mode
                         + " based on '" + JAXB_CONTEXT_FACTORY_PROPERTY
                         + "' System property");
         } else {
-            // Find a default provider.  Note we always ensure the list
-            // is always non-empty.
-            for (BindingContextFactory factory : factories()) {
-                if (LOGGER.isLoggable(Level.FINE))
-                    LOGGER.log(Level.FINE,
-                            "Using SPI-determined databindng mode: "
-                                    + factory.getClass().getName());
-                // Special case: no name lookup used.
-                return factory.newContext(bi);
-            }
-
+            // Find a default provider.  Note we always ensure the list is always non-empty.
+            BindingContext factory = getBindingContextFromSpi(factories(), bi);
+            if (factory != null) return factory;
             // Should never get here as the list is non-empty.
             LOGGER.log(Level.SEVERE, "No Binding Context Factories found.");
             throw new DatabindingException("No Binding Context Factories found.");
         }
         BindingContextFactory f = getFactory(mode);

@@ -176,10 +169,66 @@
             return f.newContext(bi);
         LOGGER.severe("Unknown Databinding mode: " + mode);
         throw new DatabindingException("Unknown Databinding mode: " + mode);
     }
 
+    /**
+     * Creates JAXB bindingContext with one of the provided factories.
+     * To filter appropriate factory {@link BindingContextFactory#isFor(String)} method is used.
+     * Currently known 2 appropriate factories: JAXB RI and MOXY.
+     * In case no suitable factory is found we are trying to create context with any given factory.
+     *
+     * @param factories given collection of factories.
+     * @param bindingInfo will be used to create bindingContext.
+     * @return Created context or null. Null will be returned if we were not able to create context with any given factory.
+     */
+    private static BindingContext getBindingContextFromSpi(List<BindingContextFactory> factories, BindingInfo bindingInfo) {
+        List<BindingContextFactory> fallback = new ArrayList<BindingContextFactory>();
+        BindingContext result;
+        for (BindingContextFactory factory : factories) {
+            if (LOGGER.isLoggable(Level.FINE)) {
+                LOGGER.log(Level.FINE, "Found SPI-determined databindng mode: " + factory.getClass().getName());
+            }
+            if (factory.isFor("org.eclipse.persistence.jaxb") || factory.isFor("com.sun.xml.internal.bind.v2.runtime")) { // filter (JAXB RI || MOXy) implementation
+                result = factory.newContext(bindingInfo);
+                if (result != null) {
+                    return result;
+                }
+            } else {
+                if (LOGGER.isLoggable(Level.FINE)) {
+                    LOGGER.log(Level.FINE, "Skipped -> not JAXB.");
+                }
+                fallback.add(factory);
+            }
+        }
+        for (BindingContextFactory factory : fallback) {
+            if (LOGGER.isLoggable(Level.FINE)) {
+                LOGGER.log(Level.FINE, "Fallback. Creating from: " + factory.getClass().getName());
+            }
+            result = getContextOrNullIfError(factory, bindingInfo);
+            if (result != null) {
+                return result;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Factory creates new context bases on provided bindingInfo.
+     * @param factory given factory.
+     * @param bindingInfo to be used to create context.
+     * @return Created context or null. Null will be returned if an error happened during the creation process.
+     */
+    private static BindingContext getContextOrNullIfError(BindingContextFactory factory, BindingInfo bindingInfo) {
+        try {
+            return factory.newContext(bindingInfo);
+        } catch (Exception e) {
+            LOGGER.log(Level.WARNING, e.getMessage(), e);
+            return null;
+        }
+    }
+
         static public boolean isContextSupported(Object o) {
             if (o == null) return false;
                 String pkgName = o.getClass().getPackage().getName();
                 for (BindingContextFactory f: factories()) if (f.isFor(pkgName)) return true;
                 return false;

@@ -197,52 +246,6 @@
          * @deprecated - Does jaxws need this?
          */
         static public BindingContext getBindingContext(Marshaller m) {
                 return getJAXBFactory(m).getContext(m);
         }
-
-    /**
-     * Creates a new {@link BindingContext}.
-     *
-     * <p>
-     * {@link JAXBContext#newInstance(Class[]) JAXBContext.newInstance()} methods may
-     * return other JAXB providers that are not compatible with the JAX-RPC RI.
-     * This method guarantees that the JAX-WS RI will finds the JAXB RI.
-     *
-     * @param classes
-     *      Classes to be bound. See {@link JAXBContext#newInstance(Class[])} for the meaning.
-     * @param typeRefs
-     *      See {@link #TYPE_REFERENCES} for the meaning of this parameter.
-     *      Can be null.
-     * @param subclassReplacements
-     *      See {@link #SUBCLASS_REPLACEMENTS} for the meaning of this parameter.
-     *      Can be null.
-     * @param defaultNamespaceRemap
-     *      See {@link #DEFAULT_NAMESPACE_REMAP} for the meaning of this parameter.
-     *      Can be null (and should be null for ordinary use of JAXB.)
-     * @param c14nSupport
-     *      See {@link #CANONICALIZATION_SUPPORT} for the meaning of this parameter.
-     * @param ar
-     *      See {@link #ANNOTATION_READER} for the meaning of this parameter.
-     *      Can be null.
-     * @since JAXB 2.1 EA2
-     */
-//    public static BindingContext newInstance(@NotNull Class[] classes,
-//       @Nullable Collection<TypeInfo> typeRefs,
-//       @Nullable Map<Class,Class> subclassReplacements,
-//       @Nullable String defaultNamespaceRemap, boolean c14nSupport,
-//       @Nullable RuntimeAnnotationReader ar) throws JAXBException {
-//        return ContextFactory.createContext(classes, typeRefs, subclassReplacements,
-//                defaultNamespaceRemap, c14nSupport, ar, false, false, false);
-//    }
-//
-//    /**
-//     * @deprecated
-//     *      Compatibility with older versions.
-//     */
-//    public static BindingContext newInstance(@NotNull Class[] classes,
-//        @Nullable Collection<TypeInfo> typeRefs,
-//        @Nullable String defaultNamespaceRemap, boolean c14nSupport ) throws JAXBException {
-//        return newInstance(classes,typeRefs, Collections.<Class,Class>emptyMap(),
-//                defaultNamespaceRemap,c14nSupport,null);
-//    }
 }
< prev index next >