1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/db/DatabindingFactoryImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,225 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, 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.db; 1.30 + 1.31 +import java.io.InputStream; 1.32 +import java.net.URL; 1.33 +import java.util.HashMap; 1.34 +import java.util.List; 1.35 +import java.util.Map; 1.36 +import java.util.Properties; 1.37 + 1.38 +import javax.xml.namespace.QName; 1.39 +import javax.xml.transform.Source; 1.40 +import javax.xml.ws.WebServiceException; 1.41 +import javax.xml.ws.WebServiceFeature; 1.42 + 1.43 +import org.xml.sax.EntityResolver; 1.44 + 1.45 +import com.oracle.webservices.internal.api.databinding.Databinding; 1.46 +import com.oracle.webservices.internal.api.databinding.Databinding.Builder; 1.47 +import com.oracle.webservices.internal.api.databinding.WSDLGenerator; 1.48 +import com.oracle.webservices.internal.api.databinding.DatabindingModeFeature; 1.49 +import com.sun.xml.internal.ws.api.BindingID; 1.50 +import com.sun.xml.internal.ws.api.WSBinding; 1.51 +import com.sun.xml.internal.ws.api.databinding.DatabindingConfig; 1.52 +import com.sun.xml.internal.ws.api.databinding.DatabindingFactory; 1.53 +import com.sun.xml.internal.ws.api.databinding.MetadataReader; 1.54 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort; 1.55 +import com.sun.xml.internal.ws.spi.db.DatabindingProvider; 1.56 +import com.sun.xml.internal.ws.util.ServiceFinder; 1.57 + 1.58 +/** 1.59 + * DatabindingFactoryImpl 1.60 + * 1.61 + * @author shih-chang.chen@oracle.com 1.62 + */ 1.63 +public class DatabindingFactoryImpl extends DatabindingFactory { 1.64 + 1.65 + static final String WsRuntimeFactoryDefaultImpl = "com.sun.xml.internal.ws.db.DatabindingProviderImpl"; 1.66 + 1.67 + protected Map<String, Object> properties = new HashMap<String, Object>(); 1.68 + protected DatabindingProvider defaultRuntimeFactory; 1.69 + protected List<DatabindingProvider> providers; 1.70 + 1.71 + static private List<DatabindingProvider> providers() { 1.72 + List<DatabindingProvider> factories = new java.util.ArrayList<DatabindingProvider>(); 1.73 + for (DatabindingProvider p : ServiceFinder.find(DatabindingProvider.class)) { 1.74 + factories.add(p); 1.75 + } 1.76 + return factories; 1.77 + } 1.78 + 1.79 + public DatabindingFactoryImpl() { 1.80 + } 1.81 + 1.82 + public Map<String, Object> properties() { 1.83 + return properties; 1.84 + } 1.85 + 1.86 + <T> T property(Class<T> propType, String propName) { 1.87 + if (propName == null) propName = propType.getName(); 1.88 + return propType.cast(properties.get(propName)); 1.89 + } 1.90 + 1.91 + public DatabindingProvider provider(DatabindingConfig config) { 1.92 + String mode = databindingMode(config); 1.93 + if (providers == null) 1.94 + providers = providers(); 1.95 + DatabindingProvider provider = null; 1.96 + if (providers != null) { 1.97 + for (DatabindingProvider p : providers) 1.98 + if (p.isFor(mode)) 1.99 + provider = p; 1.100 + } if (provider == null) { 1.101 + provider = new DatabindingProviderImpl(); 1.102 + } 1.103 + return provider; 1.104 + } 1.105 + 1.106 + public Databinding createRuntime(DatabindingConfig config) { 1.107 + DatabindingProvider provider = provider(config); 1.108 + return provider.create(config); 1.109 + } 1.110 + 1.111 + public WSDLGenerator createWsdlGen(DatabindingConfig config) { 1.112 + DatabindingProvider provider = provider(config); 1.113 + return provider.wsdlGen(config); 1.114 + } 1.115 + 1.116 + String databindingMode(DatabindingConfig config) { 1.117 + if ( config.getMappingInfo() != null && 1.118 + config.getMappingInfo().getDatabindingMode() != null) 1.119 + return config.getMappingInfo().getDatabindingMode(); 1.120 + if ( config.getFeatures() != null) for (WebServiceFeature f : config.getFeatures()) { 1.121 + if (f instanceof DatabindingModeFeature) { 1.122 + DatabindingModeFeature dmf = (DatabindingModeFeature) f; 1.123 + config.properties().putAll(dmf.getProperties()); 1.124 + return dmf.getMode(); 1.125 + } 1.126 + } 1.127 + return null; 1.128 + } 1.129 + 1.130 + ClassLoader classLoader() { 1.131 + ClassLoader classLoader = property(ClassLoader.class, null); 1.132 + if (classLoader == null) classLoader = Thread.currentThread().getContextClassLoader(); 1.133 + return classLoader; 1.134 + } 1.135 + 1.136 + Properties loadPropertiesFile(String fileName) { 1.137 + ClassLoader classLoader = classLoader(); 1.138 + Properties p = new Properties(); 1.139 + try { 1.140 + InputStream is = null; 1.141 + if (classLoader == null) { 1.142 + is = ClassLoader.getSystemResourceAsStream(fileName); 1.143 + } else { 1.144 + is = classLoader.getResourceAsStream(fileName); 1.145 + } 1.146 + if (is != null) { 1.147 + p.load(is); 1.148 + } 1.149 + } catch (Exception e) { 1.150 + throw new WebServiceException(e); 1.151 + } 1.152 + return p; 1.153 + } 1.154 + 1.155 + public Builder createBuilder(Class<?> contractClass, Class<?> endpointClass) { 1.156 + return new ConfigBuilder(this, contractClass, endpointClass); 1.157 + } 1.158 + 1.159 + static class ConfigBuilder implements Builder { 1.160 + DatabindingConfig config; 1.161 + DatabindingFactoryImpl factory; 1.162 + 1.163 + ConfigBuilder(DatabindingFactoryImpl f, Class<?> contractClass, Class<?> implBeanClass) { 1.164 + factory = f; 1.165 + config = new DatabindingConfig(); 1.166 + config.setContractClass(contractClass); 1.167 + config.setEndpointClass(implBeanClass); 1.168 + } 1.169 + public Builder targetNamespace(String targetNamespace) { 1.170 + config.getMappingInfo().setTargetNamespace(targetNamespace); 1.171 + return this; 1.172 + } 1.173 + public Builder serviceName(QName serviceName) { 1.174 + config.getMappingInfo().setServiceName(serviceName); 1.175 + return this; 1.176 + } 1.177 + public Builder portName(QName portName) { 1.178 + config.getMappingInfo().setPortName(portName); 1.179 + return this; 1.180 + } 1.181 + public Builder wsdlURL(URL wsdlURL) { 1.182 + config.setWsdlURL(wsdlURL); 1.183 + return this; 1.184 + } 1.185 + public Builder wsdlSource(Source wsdlSource) { 1.186 + config.setWsdlSource(wsdlSource); 1.187 + return this; 1.188 + } 1.189 + public Builder entityResolver(EntityResolver entityResolver) { 1.190 + config.setEntityResolver(entityResolver); 1.191 + return this; 1.192 + } 1.193 + public Builder classLoader(ClassLoader classLoader) { 1.194 + config.setClassLoader(classLoader); 1.195 + return this; 1.196 + } 1.197 + public Builder feature(WebServiceFeature... f) { 1.198 + config.setFeatures(f); 1.199 + return this; 1.200 + } 1.201 + public Builder property(String name, Object value) { 1.202 + config.properties().put(name, value); 1.203 + if (isfor(BindingID.class, name, value)) { 1.204 + config.getMappingInfo().setBindingID((BindingID)value); 1.205 + } 1.206 + if (isfor(WSBinding.class, name, value)) { 1.207 + config.setWSBinding((WSBinding)value); 1.208 + } 1.209 + if (isfor(WSDLPort.class, name, value)) { 1.210 + config.setWsdlPort((WSDLPort)value); 1.211 + } 1.212 + if (isfor(MetadataReader.class, name, value)) { 1.213 + config.setMetadataReader((MetadataReader)value); 1.214 + } 1.215 + return this; 1.216 + } 1.217 + boolean isfor(Class<?> type, String name, Object value) { 1.218 + return type.getName().equals(name) && type.isInstance(value); 1.219 + } 1.220 + 1.221 + public com.oracle.webservices.internal.api.databinding.Databinding build() { 1.222 + return factory.createRuntime(config); 1.223 + } 1.224 + public com.oracle.webservices.internal.api.databinding.WSDLGenerator createWSDLGenerator() { 1.225 + return factory.createWsdlGen(config); 1.226 + } 1.227 + } 1.228 +}