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