1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/spi/db/BindingContextFactory.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,248 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.spi.db; 1.30 + 1.31 +import java.util.Iterator; 1.32 +import java.util.List; 1.33 +import java.util.logging.Level; 1.34 +import java.util.logging.Logger; 1.35 + 1.36 +import javax.xml.bind.JAXBContext; 1.37 +import javax.xml.bind.Marshaller; 1.38 + 1.39 + 1.40 +import com.oracle.webservices.internal.api.databinding.DatabindingModeFeature; 1.41 +import com.sun.xml.internal.ws.db.glassfish.JAXBRIContextFactory; 1.42 +import com.sun.xml.internal.ws.util.ServiceConfigurationError; 1.43 +import com.sun.xml.internal.ws.util.ServiceFinder; 1.44 + 1.45 +/** 1.46 + * BindingContextFactory 1.47 + * 1.48 + * @author shih-chang.chen@oracle.com 1.49 + */ 1.50 +abstract public class BindingContextFactory { 1.51 + public static final String DefaultDatabindingMode = DatabindingModeFeature.GLASSFISH_JAXB; 1.52 + public static final String JAXB_CONTEXT_FACTORY_PROPERTY = BindingContextFactory.class.getName(); 1.53 + public static final Logger LOGGER = Logger.getLogger(BindingContextFactory.class.getName()); 1.54 + 1.55 + // This iterator adds exception checking for proper logging. 1.56 + public static Iterator<BindingContextFactory> serviceIterator() { 1.57 + final ServiceFinder<BindingContextFactory> sf = ServiceFinder 1.58 + .find(BindingContextFactory.class); 1.59 + final Iterator<BindingContextFactory> ibcf = sf.iterator(); 1.60 + 1.61 + return new Iterator<BindingContextFactory>() { 1.62 + private BindingContextFactory bcf; 1.63 + 1.64 + public boolean hasNext() { 1.65 + while (true) { 1.66 + try { 1.67 + if (ibcf.hasNext()) { 1.68 + bcf = ibcf.next(); 1.69 + return true; 1.70 + } else 1.71 + return false; 1.72 + } catch (ServiceConfigurationError e) { 1.73 + LOGGER.warning("skipping factory: ServiceConfigurationError: " 1.74 + + e.getMessage()); 1.75 + } catch (NoClassDefFoundError ncdfe) { 1.76 + LOGGER.fine("skipping factory: NoClassDefFoundError: " 1.77 + + ncdfe.getMessage()); 1.78 + } 1.79 + } 1.80 + } 1.81 + 1.82 + public BindingContextFactory next() { 1.83 + if (LOGGER.isLoggable(Level.FINER)) 1.84 + LOGGER.finer("SPI found provider: " + 1.85 + bcf.getClass().getName()); 1.86 + return bcf; 1.87 + } 1.88 + 1.89 + public void remove() { 1.90 + throw new UnsupportedOperationException(); 1.91 + } 1.92 + }; 1.93 + } 1.94 + 1.95 + static private List<BindingContextFactory> factories() { 1.96 + List<BindingContextFactory> factories = new java.util.ArrayList<BindingContextFactory>(); 1.97 + Iterator<BindingContextFactory> ibcf = serviceIterator(); 1.98 + while (ibcf.hasNext()) 1.99 + factories.add(ibcf.next()); 1.100 + 1.101 + // There should always be at least one factory available. 1.102 + if (factories.isEmpty()) { 1.103 + if (LOGGER.isLoggable(Level.FINER)) 1.104 + LOGGER.log(Level.FINER, "No SPI providers for BindingContextFactory found, adding: " 1.105 + + JAXBRIContextFactory.class.getName()); 1.106 + factories.add(new JAXBRIContextFactory()); 1.107 + } 1.108 + return factories; 1.109 + } 1.110 + 1.111 + abstract protected BindingContext newContext(JAXBContext context); 1.112 + 1.113 + abstract protected BindingContext newContext(BindingInfo bi); 1.114 + 1.115 + /** 1.116 + * Check to see if the BindingContextFactory is for the databinding mode/flavor. The 1.117 + * String parameter can be the package name of the JAXBContext implementation as well. 1.118 + * @param databinding mode/flavor or the package name of the JAXBContext implementation. 1.119 + * @return 1.120 + */ 1.121 + abstract protected boolean isFor(String databinding); 1.122 + 1.123 + /** 1.124 + * @deprecated - Does jaxws need this? 1.125 + */ 1.126 + abstract protected BindingContext getContext(Marshaller m); 1.127 + 1.128 + static private BindingContextFactory getFactory(String mode) { 1.129 + for (BindingContextFactory f: factories()) { 1.130 + if (f.isFor(mode)) 1.131 + return f; 1.132 + } 1.133 + return null; 1.134 + } 1.135 + 1.136 + static public BindingContext create(JAXBContext context) throws DatabindingException { 1.137 + return getJAXBFactory(context).newContext(context); 1.138 + } 1.139 + 1.140 + static public BindingContext create(BindingInfo bi) { 1.141 + // Any mode configured in AbstractSEIModelImpl trumps all. 1.142 + // System property comes next, then SPI-located. 1.143 + String mode = bi.getDatabindingMode(); 1.144 + if (mode != null) { 1.145 + if (LOGGER.isLoggable(Level.FINE)) 1.146 + LOGGER.log(Level.FINE, "Using SEI-configured databindng mode: " 1.147 + + mode); 1.148 + } else if ((mode = System.getProperty("BindingContextFactory")) != null) { 1.149 + // The following is left for backward compatibility and should 1.150 + // eventually be removed. 1.151 + bi.setDatabindingMode(mode); 1.152 + if (LOGGER.isLoggable(Level.FINE)) 1.153 + LOGGER.log(Level.FINE, "Using databindng: " + mode 1.154 + + " based on 'BindingContextFactory' System property"); 1.155 + } else if ((mode = System.getProperty(JAXB_CONTEXT_FACTORY_PROPERTY)) != null) { 1.156 + bi.setDatabindingMode(mode); 1.157 + if (LOGGER.isLoggable(Level.FINE)) 1.158 + LOGGER.log(Level.FINE, "Using databindng: " + mode 1.159 + + " based on '" + JAXB_CONTEXT_FACTORY_PROPERTY 1.160 + + "' System property"); 1.161 + } else { 1.162 + // Find a default provider. Note we always ensure the list 1.163 + // is always non-empty. 1.164 + for (BindingContextFactory factory : factories()) { 1.165 + if (LOGGER.isLoggable(Level.FINE)) 1.166 + LOGGER.log(Level.FINE, 1.167 + "Using SPI-determined databindng mode: " 1.168 + + factory.getClass().getName()); 1.169 + // Special case: no name lookup used. 1.170 + return factory.newContext(bi); 1.171 + } 1.172 + 1.173 + // Should never get here as the list is non-empty. 1.174 + LOGGER.log(Level.SEVERE, "No Binding Context Factories found."); 1.175 + throw new DatabindingException("No Binding Context Factories found."); 1.176 + } 1.177 + BindingContextFactory f = getFactory(mode); 1.178 + if (f != null) 1.179 + return f.newContext(bi); 1.180 + LOGGER.severe("Unknown Databinding mode: " + mode); 1.181 + throw new DatabindingException("Unknown Databinding mode: " + mode); 1.182 + } 1.183 + 1.184 + static public boolean isContextSupported(Object o) { 1.185 + if (o == null) return false; 1.186 + String pkgName = o.getClass().getPackage().getName(); 1.187 + for (BindingContextFactory f: factories()) if (f.isFor(pkgName)) return true; 1.188 + return false; 1.189 + } 1.190 + 1.191 + static BindingContextFactory getJAXBFactory(Object o) { 1.192 + String pkgName = o.getClass().getPackage().getName(); 1.193 + BindingContextFactory f = getFactory(pkgName); 1.194 + if (f != null) return f; 1.195 + throw new DatabindingException("Unknown JAXBContext implementation: " + o.getClass()); 1.196 + 1.197 + } 1.198 + 1.199 + /** 1.200 + * @deprecated - Does jaxws need this? 1.201 + */ 1.202 + static public BindingContext getBindingContext(Marshaller m) { 1.203 + return getJAXBFactory(m).getContext(m); 1.204 + } 1.205 + 1.206 + /** 1.207 + * Creates a new {@link BindingContext}. 1.208 + * 1.209 + * <p> 1.210 + * {@link JAXBContext#newInstance(Class[]) JAXBContext.newInstance()} methods may 1.211 + * return other JAXB providers that are not compatible with the JAX-RPC RI. 1.212 + * This method guarantees that the JAX-WS RI will finds the JAXB RI. 1.213 + * 1.214 + * @param classes 1.215 + * Classes to be bound. See {@link JAXBContext#newInstance(Class[])} for the meaning. 1.216 + * @param typeRefs 1.217 + * See {@link #TYPE_REFERENCES} for the meaning of this parameter. 1.218 + * Can be null. 1.219 + * @param subclassReplacements 1.220 + * See {@link #SUBCLASS_REPLACEMENTS} for the meaning of this parameter. 1.221 + * Can be null. 1.222 + * @param defaultNamespaceRemap 1.223 + * See {@link #DEFAULT_NAMESPACE_REMAP} for the meaning of this parameter. 1.224 + * Can be null (and should be null for ordinary use of JAXB.) 1.225 + * @param c14nSupport 1.226 + * See {@link #CANONICALIZATION_SUPPORT} for the meaning of this parameter. 1.227 + * @param ar 1.228 + * See {@link #ANNOTATION_READER} for the meaning of this parameter. 1.229 + * Can be null. 1.230 + * @since JAXB 2.1 EA2 1.231 + */ 1.232 +// public static BindingContext newInstance(@NotNull Class[] classes, 1.233 +// @Nullable Collection<TypeInfo> typeRefs, 1.234 +// @Nullable Map<Class,Class> subclassReplacements, 1.235 +// @Nullable String defaultNamespaceRemap, boolean c14nSupport, 1.236 +// @Nullable RuntimeAnnotationReader ar) throws JAXBException { 1.237 +// return ContextFactory.createContext(classes, typeRefs, subclassReplacements, 1.238 +// defaultNamespaceRemap, c14nSupport, ar, false, false, false); 1.239 +// } 1.240 +// 1.241 +// /** 1.242 +// * @deprecated 1.243 +// * Compatibility with older versions. 1.244 +// */ 1.245 +// public static BindingContext newInstance(@NotNull Class[] classes, 1.246 +// @Nullable Collection<TypeInfo> typeRefs, 1.247 +// @Nullable String defaultNamespaceRemap, boolean c14nSupport ) throws JAXBException { 1.248 +// return newInstance(classes,typeRefs, Collections.<Class,Class>emptyMap(), 1.249 +// defaultNamespaceRemap,c14nSupport,null); 1.250 +// } 1.251 +}