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