ohair@286: /* alanb@368: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.db; ohair@286: ohair@286: import java.io.InputStream; ohair@286: import java.net.URL; ohair@286: import java.util.HashMap; ohair@286: import java.util.List; ohair@286: import java.util.Map; ohair@286: import java.util.Properties; ohair@286: ohair@286: import javax.xml.namespace.QName; ohair@286: import javax.xml.transform.Source; ohair@286: import javax.xml.ws.WebServiceException; ohair@286: import javax.xml.ws.WebServiceFeature; ohair@286: ohair@286: import org.xml.sax.EntityResolver; ohair@286: alanb@368: import com.oracle.webservices.internal.api.databinding.Databinding; alanb@368: import com.oracle.webservices.internal.api.databinding.Databinding.Builder; alanb@368: import com.oracle.webservices.internal.api.databinding.WSDLGenerator; ohair@286: import com.sun.xml.internal.ws.api.BindingID; ohair@286: import com.sun.xml.internal.ws.api.WSBinding; ohair@286: import com.sun.xml.internal.ws.api.databinding.DatabindingConfig; ohair@286: import com.sun.xml.internal.ws.api.databinding.DatabindingFactory; alanb@368: import com.sun.xml.internal.ws.api.databinding.MetadataReader; ohair@286: import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort; ohair@286: import com.sun.xml.internal.ws.spi.db.DatabindingProvider; ohair@286: import com.sun.xml.internal.ws.util.ServiceFinder; ohair@286: ohair@286: /** ohair@286: * DatabindingFactoryImpl ohair@286: * ohair@286: * @author shih-chang.chen@oracle.com ohair@286: */ ohair@286: public class DatabindingFactoryImpl extends DatabindingFactory { ohair@286: ohair@286: // static final String WsRuntimeFactoryProperties = DatabindingProvider.class.getName() + ".properties"; ohair@286: static final String WsRuntimeFactoryDefaultImpl = "com.sun.xml.internal.ws.db.DatabindingProviderImpl"; ohair@286: ohair@286: protected Map properties = new HashMap(); ohair@286: protected DatabindingProvider defaultRuntimeFactory; ohair@286: // protected Map runtimeFactories = new HashMap(); ohair@286: // protected Properties wsRuntimeFactoryMap; ohair@286: protected List providers; ohair@286: ohair@286: static private List providers() { ohair@286: List factories = new java.util.ArrayList(); ohair@286: for (DatabindingProvider p : ServiceFinder.find(DatabindingProvider.class)) { ohair@286: factories.add(p); ohair@286: } ohair@286: return factories; ohair@286: } ohair@286: ohair@286: public DatabindingFactoryImpl() { ohair@286: } ohair@286: ohair@286: public Map properties() { ohair@286: return properties; ohair@286: } ohair@286: ohair@286: T property(Class propType, String propName) { ohair@286: if (propName == null) propName = propType.getName(); ohair@286: return propType.cast(properties.get(propName)); ohair@286: } ohair@286: ohair@286: public DatabindingProvider provider(DatabindingConfig config) { ohair@286: String mode = databindingMode(config); ohair@286: if (providers == null) ohair@286: providers = providers(); ohair@286: DatabindingProvider provider = null; ohair@286: if (providers != null) { ohair@286: for (DatabindingProvider p : providers) ohair@286: if (p.isFor(mode)) ohair@286: provider = p; ohair@286: } if (provider == null) { ohair@286: // if (defaultRuntimeFactory == null) { ohair@286: // defaultRuntimeFactory = ohair@286: // newRuntimeFactory(WsRuntimeFactoryDefaultImpl); ohair@286: // } ohair@286: // provider = defaultRuntimeFactory; ohair@286: provider = new DatabindingProviderImpl(); ohair@286: } ohair@286: return provider; ohair@286: } ohair@286: ohair@286: public Databinding createRuntime(DatabindingConfig config) { ohair@286: DatabindingProvider provider = provider(config); ohair@286: return provider.create(config); ohair@286: } ohair@286: alanb@368: public WSDLGenerator createWsdlGen(DatabindingConfig config) { ohair@286: DatabindingProvider provider = provider(config); ohair@286: return provider.wsdlGen(config); ohair@286: } ohair@286: ohair@286: // DatabindingProvider newRuntimeFactory(String name) { ohair@286: // ClassLoader classLoader = classLoader(); ohair@286: // DatabindingProvider factory = null; ohair@286: // try { ohair@286: // Class cls = (classLoader != null) ? classLoader.loadClass(name) : Class.forName(name); ohair@286: // factory = DatabindingProvider.class.cast(cls.newInstance()); ohair@286: // } catch (Exception e) { ohair@286: // throw new DatabindingException("Unknown DatabindingFactory: " + name, e); ohair@286: // } ohair@286: // factory.init(properties); ohair@286: // return factory; ohair@286: // } ohair@286: ohair@286: String databindingMode(DatabindingConfig config) { ohair@286: // if ( config.getOverrideMappingInfo() != null && ohair@286: // config.getOverrideMappingInfo().getDatabindingMode() != null) ohair@286: // return config.getOverrideMappingInfo().getDatabindingMode(); ohair@286: // if ( config.getDefaultMappingInfo() != null && ohair@286: // config.getDefaultMappingInfo().getDatabindingMode() != null) ohair@286: // return config.getDefaultMappingInfo().getDatabindingMode(); ohair@286: ohair@286: if ( config.getMappingInfo() != null && ohair@286: config.getMappingInfo().getDatabindingMode() != null) ohair@286: return config.getMappingInfo().getDatabindingMode(); ohair@286: if ( config.getFeatures() != null) for (WebServiceFeature f : config.getFeatures()) { alanb@368: if (f instanceof com.oracle.webservices.internal.api.databinding.DatabindingModeFeature) { alanb@368: com.oracle.webservices.internal.api.databinding.DatabindingModeFeature dmf = (com.oracle.webservices.internal.api.databinding.DatabindingModeFeature) f; ohair@286: return dmf.getMode(); ohair@286: } ohair@286: } ohair@286: return null; ohair@286: } ohair@286: ohair@286: ClassLoader classLoader() { ohair@286: ClassLoader classLoader = property(ClassLoader.class, null); ohair@286: if (classLoader == null) classLoader = Thread.currentThread().getContextClassLoader(); ohair@286: return classLoader; ohair@286: } ohair@286: ohair@286: Properties loadPropertiesFile(String fileName) { ohair@286: ClassLoader classLoader = classLoader(); ohair@286: Properties p = new Properties(); ohair@286: try { ohair@286: InputStream is = null; ohair@286: if (classLoader == null) { ohair@286: is = ClassLoader.getSystemResourceAsStream(fileName); ohair@286: } else { ohair@286: is = classLoader.getResourceAsStream(fileName); ohair@286: } ohair@286: if (is != null) { ohair@286: p.load(is); ohair@286: } ohair@286: } catch (Exception e) { ohair@286: throw new WebServiceException(e); ohair@286: } ohair@286: return p; ohair@286: } ohair@286: ohair@286: public Builder createBuilder(Class contractClass, Class endpointClass) { ohair@286: return new ConfigBuilder(this, contractClass, endpointClass); ohair@286: } ohair@286: ohair@286: static class ConfigBuilder implements Builder { ohair@286: DatabindingConfig config; ohair@286: DatabindingFactoryImpl factory; ohair@286: ohair@286: ConfigBuilder(DatabindingFactoryImpl f, Class contractClass, Class implBeanClass) { ohair@286: factory = f; ohair@286: config = new DatabindingConfig(); ohair@286: config.setContractClass(contractClass); ohair@286: config.setEndpointClass(implBeanClass); ohair@286: } ohair@286: public Builder targetNamespace(String targetNamespace) { ohair@286: config.getMappingInfo().setTargetNamespace(targetNamespace); ohair@286: return this; ohair@286: } ohair@286: public Builder serviceName(QName serviceName) { ohair@286: config.getMappingInfo().setServiceName(serviceName); ohair@286: return this; ohair@286: } ohair@286: public Builder portName(QName portName) { ohair@286: config.getMappingInfo().setPortName(portName); ohair@286: return this; ohair@286: } ohair@286: public Builder wsdlURL(URL wsdlURL) { ohair@286: config.setWsdlURL(wsdlURL); ohair@286: return this; ohair@286: } ohair@286: public Builder wsdlSource(Source wsdlSource) { ohair@286: config.setWsdlSource(wsdlSource); ohair@286: return this; ohair@286: } ohair@286: public Builder entityResolver(EntityResolver entityResolver) { ohair@286: config.setEntityResolver(entityResolver); ohair@286: return this; ohair@286: } ohair@286: public Builder classLoader(ClassLoader classLoader) { ohair@286: config.setClassLoader(classLoader); ohair@286: return this; ohair@286: } ohair@286: public Builder feature(WebServiceFeature... f) { ohair@286: config.setFeatures(f); ohair@286: return this; ohair@286: } ohair@286: public Builder property(String name, Object value) { ohair@286: config.properties().put(name, value); ohair@286: if (isfor(BindingID.class, name, value)) { ohair@286: config.getMappingInfo().setBindingID((BindingID)value); ohair@286: } ohair@286: if (isfor(WSBinding.class, name, value)) { ohair@286: config.setWSBinding((WSBinding)value); ohair@286: } ohair@286: if (isfor(WSDLPort.class, name, value)) { ohair@286: config.setWsdlPort((WSDLPort)value); ohair@286: } alanb@368: if (isfor(MetadataReader.class, name, value)) { alanb@368: config.setMetadataReader((MetadataReader)value); alanb@368: } ohair@286: return this; ohair@286: } ohair@286: boolean isfor(Class type, String name, Object value) { ohair@286: return type.getName().equals(name) && type.isInstance(value); ohair@286: } ohair@286: alanb@368: public com.oracle.webservices.internal.api.databinding.Databinding build() { ohair@286: return factory.createRuntime(config); ohair@286: } alanb@368: public com.oracle.webservices.internal.api.databinding.WSDLGenerator createWSDLGenerator() { ohair@286: return factory.createWsdlGen(config); ohair@286: } ohair@286: } ohair@286: }