Fri, 22 Nov 2013 21:11:19 +0100
8010935: Better XML handling
8027378: Two closed/javax/xml/8005432 fails with jdk7u51b04
8028382: Two javax/xml/8005433 tests still fail after the fix JDK-8028147
Summary: base fix + fixes for test regressions; fix also reviewed by Maxim Soloviev, Alexander Fomin
Reviewed-by: mchung, mgrebac, mullan
ohair@286 | 1 | /* |
alanb@368 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.ws.spi.db; |
ohair@286 | 27 | |
ohair@286 | 28 | import java.util.Iterator; |
ohair@286 | 29 | import java.util.List; |
ohair@286 | 30 | import java.util.logging.Level; |
ohair@286 | 31 | import java.util.logging.Logger; |
ohair@286 | 32 | |
ohair@286 | 33 | import javax.xml.bind.JAXBContext; |
ohair@286 | 34 | import javax.xml.bind.Marshaller; |
ohair@286 | 35 | |
ohair@286 | 36 | |
alanb@368 | 37 | import com.oracle.webservices.internal.api.databinding.DatabindingModeFeature; |
ohair@286 | 38 | import com.sun.xml.internal.ws.db.glassfish.JAXBRIContextFactory; |
alanb@368 | 39 | import com.sun.xml.internal.ws.util.ServiceConfigurationError; |
ohair@286 | 40 | import com.sun.xml.internal.ws.util.ServiceFinder; |
ohair@286 | 41 | |
ohair@286 | 42 | /** |
ohair@286 | 43 | * BindingContextFactory |
ohair@286 | 44 | * |
ohair@286 | 45 | * @author shih-chang.chen@oracle.com |
ohair@286 | 46 | */ |
ohair@286 | 47 | abstract public class BindingContextFactory { |
ohair@286 | 48 | public static final String DefaultDatabindingMode = DatabindingModeFeature.GLASSFISH_JAXB; |
ohair@286 | 49 | public static final String JAXB_CONTEXT_FACTORY_PROPERTY = BindingContextFactory.class.getName(); |
ohair@286 | 50 | public static final Logger LOGGER = Logger.getLogger(BindingContextFactory.class.getName()); |
ohair@286 | 51 | |
ohair@286 | 52 | // This iterator adds exception checking for proper logging. |
ohair@286 | 53 | public static Iterator<BindingContextFactory> serviceIterator() { |
ohair@286 | 54 | final ServiceFinder<BindingContextFactory> sf = ServiceFinder |
ohair@286 | 55 | .find(BindingContextFactory.class); |
ohair@286 | 56 | final Iterator<BindingContextFactory> ibcf = sf.iterator(); |
ohair@286 | 57 | |
ohair@286 | 58 | return new Iterator<BindingContextFactory>() { |
ohair@286 | 59 | private BindingContextFactory bcf; |
ohair@286 | 60 | |
ohair@286 | 61 | public boolean hasNext() { |
ohair@286 | 62 | while (true) { |
ohair@286 | 63 | try { |
ohair@286 | 64 | if (ibcf.hasNext()) { |
ohair@286 | 65 | bcf = ibcf.next(); |
ohair@286 | 66 | return true; |
ohair@286 | 67 | } else |
ohair@286 | 68 | return false; |
ohair@286 | 69 | } catch (ServiceConfigurationError e) { |
ohair@286 | 70 | LOGGER.warning("skipping factory: ServiceConfigurationError: " |
ohair@286 | 71 | + e.getMessage()); |
ohair@286 | 72 | } catch (NoClassDefFoundError ncdfe) { |
ohair@286 | 73 | LOGGER.fine("skipping factory: NoClassDefFoundError: " |
ohair@286 | 74 | + ncdfe.getMessage()); |
ohair@286 | 75 | } |
ohair@286 | 76 | } |
ohair@286 | 77 | } |
ohair@286 | 78 | |
ohair@286 | 79 | public BindingContextFactory next() { |
ohair@286 | 80 | if (LOGGER.isLoggable(Level.FINER)) |
ohair@286 | 81 | LOGGER.finer("SPI found provider: " + |
ohair@286 | 82 | bcf.getClass().getName()); |
ohair@286 | 83 | return bcf; |
ohair@286 | 84 | } |
ohair@286 | 85 | |
ohair@286 | 86 | public void remove() { |
ohair@286 | 87 | throw new UnsupportedOperationException(); |
ohair@286 | 88 | } |
ohair@286 | 89 | }; |
ohair@286 | 90 | } |
ohair@286 | 91 | |
ohair@286 | 92 | static private List<BindingContextFactory> factories() { |
ohair@286 | 93 | List<BindingContextFactory> factories = new java.util.ArrayList<BindingContextFactory>(); |
ohair@286 | 94 | Iterator<BindingContextFactory> ibcf = serviceIterator(); |
ohair@286 | 95 | while (ibcf.hasNext()) |
ohair@286 | 96 | factories.add(ibcf.next()); |
ohair@286 | 97 | |
ohair@286 | 98 | // There should always be at least one factory available. |
ohair@286 | 99 | if (factories.isEmpty()) { |
ohair@286 | 100 | if (LOGGER.isLoggable(Level.FINER)) |
ohair@286 | 101 | LOGGER.log(Level.FINER, "No SPI providers for BindingContextFactory found, adding: " |
ohair@286 | 102 | + JAXBRIContextFactory.class.getName()); |
ohair@286 | 103 | factories.add(new JAXBRIContextFactory()); |
ohair@286 | 104 | } |
ohair@286 | 105 | return factories; |
ohair@286 | 106 | } |
ohair@286 | 107 | |
ohair@286 | 108 | abstract protected BindingContext newContext(JAXBContext context); |
ohair@286 | 109 | |
ohair@286 | 110 | abstract protected BindingContext newContext(BindingInfo bi); |
ohair@286 | 111 | |
ohair@286 | 112 | /** |
ohair@286 | 113 | * Check to see if the BindingContextFactory is for the databinding mode/flavor. The |
ohair@286 | 114 | * String parameter can be the package name of the JAXBContext implementation as well. |
ohair@286 | 115 | * @param databinding mode/flavor or the package name of the JAXBContext implementation. |
ohair@286 | 116 | * @return |
ohair@286 | 117 | */ |
alanb@368 | 118 | abstract protected boolean isFor(String databinding); |
ohair@286 | 119 | |
ohair@286 | 120 | /** |
ohair@286 | 121 | * @deprecated - Does jaxws need this? |
ohair@286 | 122 | */ |
ohair@286 | 123 | abstract protected BindingContext getContext(Marshaller m); |
ohair@286 | 124 | |
ohair@286 | 125 | static private BindingContextFactory getFactory(String mode) { |
ohair@286 | 126 | for (BindingContextFactory f: factories()) { |
ohair@286 | 127 | if (f.isFor(mode)) |
ohair@286 | 128 | return f; |
ohair@286 | 129 | } |
ohair@286 | 130 | return null; |
ohair@286 | 131 | } |
ohair@286 | 132 | |
ohair@286 | 133 | static public BindingContext create(JAXBContext context) throws DatabindingException { |
ohair@286 | 134 | return getJAXBFactory(context).newContext(context); |
ohair@286 | 135 | } |
ohair@286 | 136 | |
ohair@286 | 137 | static public BindingContext create(BindingInfo bi) { |
ohair@286 | 138 | // Any mode configured in AbstractSEIModelImpl trumps all. |
ohair@286 | 139 | // System property comes next, then SPI-located. |
ohair@286 | 140 | String mode = bi.getDatabindingMode(); |
ohair@286 | 141 | if (mode != null) { |
ohair@286 | 142 | if (LOGGER.isLoggable(Level.FINE)) |
ohair@286 | 143 | LOGGER.log(Level.FINE, "Using SEI-configured databindng mode: " |
ohair@286 | 144 | + mode); |
ohair@286 | 145 | } else if ((mode = System.getProperty("BindingContextFactory")) != null) { |
ohair@286 | 146 | // The following is left for backward compatibility and should |
ohair@286 | 147 | // eventually be removed. |
ohair@286 | 148 | bi.setDatabindingMode(mode); |
ohair@286 | 149 | if (LOGGER.isLoggable(Level.FINE)) |
ohair@286 | 150 | LOGGER.log(Level.FINE, "Using databindng: " + mode |
ohair@286 | 151 | + " based on 'BindingContextFactory' System property"); |
ohair@286 | 152 | } else if ((mode = System.getProperty(JAXB_CONTEXT_FACTORY_PROPERTY)) != null) { |
ohair@286 | 153 | bi.setDatabindingMode(mode); |
ohair@286 | 154 | if (LOGGER.isLoggable(Level.FINE)) |
ohair@286 | 155 | LOGGER.log(Level.FINE, "Using databindng: " + mode |
ohair@286 | 156 | + " based on '" + JAXB_CONTEXT_FACTORY_PROPERTY |
ohair@286 | 157 | + "' System property"); |
ohair@286 | 158 | } else { |
ohair@286 | 159 | // Find a default provider. Note we always ensure the list |
ohair@286 | 160 | // is always non-empty. |
ohair@286 | 161 | for (BindingContextFactory factory : factories()) { |
ohair@286 | 162 | if (LOGGER.isLoggable(Level.FINE)) |
ohair@286 | 163 | LOGGER.log(Level.FINE, |
ohair@286 | 164 | "Using SPI-determined databindng mode: " |
ohair@286 | 165 | + factory.getClass().getName()); |
ohair@286 | 166 | // Special case: no name lookup used. |
ohair@286 | 167 | return factory.newContext(bi); |
ohair@286 | 168 | } |
ohair@286 | 169 | |
ohair@286 | 170 | // Should never get here as the list is non-empty. |
ohair@286 | 171 | LOGGER.log(Level.SEVERE, "No Binding Context Factories found."); |
ohair@286 | 172 | throw new DatabindingException("No Binding Context Factories found."); |
ohair@286 | 173 | } |
ohair@286 | 174 | BindingContextFactory f = getFactory(mode); |
ohair@286 | 175 | if (f != null) |
ohair@286 | 176 | return f.newContext(bi); |
ohair@286 | 177 | LOGGER.severe("Unknown Databinding mode: " + mode); |
ohair@286 | 178 | throw new DatabindingException("Unknown Databinding mode: " + mode); |
ohair@286 | 179 | } |
ohair@286 | 180 | |
ohair@286 | 181 | static public boolean isContextSupported(Object o) { |
ohair@286 | 182 | if (o == null) return false; |
ohair@286 | 183 | String pkgName = o.getClass().getPackage().getName(); |
ohair@286 | 184 | for (BindingContextFactory f: factories()) if (f.isFor(pkgName)) return true; |
ohair@286 | 185 | return false; |
ohair@286 | 186 | } |
ohair@286 | 187 | |
ohair@286 | 188 | static BindingContextFactory getJAXBFactory(Object o) { |
ohair@286 | 189 | String pkgName = o.getClass().getPackage().getName(); |
ohair@286 | 190 | BindingContextFactory f = getFactory(pkgName); |
ohair@286 | 191 | if (f != null) return f; |
ohair@286 | 192 | throw new DatabindingException("Unknown JAXBContext implementation: " + o.getClass()); |
ohair@286 | 193 | |
ohair@286 | 194 | } |
ohair@286 | 195 | |
ohair@286 | 196 | /** |
ohair@286 | 197 | * @deprecated - Does jaxws need this? |
ohair@286 | 198 | */ |
ohair@286 | 199 | static public BindingContext getBindingContext(Marshaller m) { |
ohair@286 | 200 | return getJAXBFactory(m).getContext(m); |
ohair@286 | 201 | } |
ohair@286 | 202 | |
ohair@286 | 203 | /** |
ohair@286 | 204 | * Creates a new {@link BindingContext}. |
ohair@286 | 205 | * |
ohair@286 | 206 | * <p> |
ohair@286 | 207 | * {@link JAXBContext#newInstance(Class[]) JAXBContext.newInstance()} methods may |
ohair@286 | 208 | * return other JAXB providers that are not compatible with the JAX-RPC RI. |
ohair@286 | 209 | * This method guarantees that the JAX-WS RI will finds the JAXB RI. |
ohair@286 | 210 | * |
ohair@286 | 211 | * @param classes |
ohair@286 | 212 | * Classes to be bound. See {@link JAXBContext#newInstance(Class[])} for the meaning. |
ohair@286 | 213 | * @param typeRefs |
ohair@286 | 214 | * See {@link #TYPE_REFERENCES} for the meaning of this parameter. |
ohair@286 | 215 | * Can be null. |
ohair@286 | 216 | * @param subclassReplacements |
ohair@286 | 217 | * See {@link #SUBCLASS_REPLACEMENTS} for the meaning of this parameter. |
ohair@286 | 218 | * Can be null. |
ohair@286 | 219 | * @param defaultNamespaceRemap |
ohair@286 | 220 | * See {@link #DEFAULT_NAMESPACE_REMAP} for the meaning of this parameter. |
ohair@286 | 221 | * Can be null (and should be null for ordinary use of JAXB.) |
ohair@286 | 222 | * @param c14nSupport |
ohair@286 | 223 | * See {@link #CANONICALIZATION_SUPPORT} for the meaning of this parameter. |
ohair@286 | 224 | * @param ar |
ohair@286 | 225 | * See {@link #ANNOTATION_READER} for the meaning of this parameter. |
ohair@286 | 226 | * Can be null. |
ohair@286 | 227 | * @since JAXB 2.1 EA2 |
ohair@286 | 228 | */ |
ohair@286 | 229 | // public static BindingContext newInstance(@NotNull Class[] classes, |
ohair@286 | 230 | // @Nullable Collection<TypeInfo> typeRefs, |
ohair@286 | 231 | // @Nullable Map<Class,Class> subclassReplacements, |
ohair@286 | 232 | // @Nullable String defaultNamespaceRemap, boolean c14nSupport, |
ohair@286 | 233 | // @Nullable RuntimeAnnotationReader ar) throws JAXBException { |
ohair@286 | 234 | // return ContextFactory.createContext(classes, typeRefs, subclassReplacements, |
ohair@286 | 235 | // defaultNamespaceRemap, c14nSupport, ar, false, false, false); |
ohair@286 | 236 | // } |
ohair@286 | 237 | // |
ohair@286 | 238 | // /** |
ohair@286 | 239 | // * @deprecated |
ohair@286 | 240 | // * Compatibility with older versions. |
ohair@286 | 241 | // */ |
ohair@286 | 242 | // public static BindingContext newInstance(@NotNull Class[] classes, |
ohair@286 | 243 | // @Nullable Collection<TypeInfo> typeRefs, |
ohair@286 | 244 | // @Nullable String defaultNamespaceRemap, boolean c14nSupport ) throws JAXBException { |
ohair@286 | 245 | // return newInstance(classes,typeRefs, Collections.<Class,Class>emptyMap(), |
ohair@286 | 246 | // defaultNamespaceRemap,c14nSupport,null); |
ohair@286 | 247 | // } |
ohair@286 | 248 | } |