src/share/jaxws_classes/com/sun/xml/internal/ws/db/DatabindingFactoryImpl.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.ws.db;
ohair@286 27
ohair@286 28 import java.io.File;
ohair@286 29 import java.io.InputStream;
ohair@286 30 import java.net.URL;
ohair@286 31 import java.util.HashMap;
ohair@286 32 import java.util.List;
ohair@286 33 import java.util.Map;
ohair@286 34 import java.util.Properties;
ohair@286 35
ohair@286 36 import javax.xml.namespace.QName;
ohair@286 37 import javax.xml.transform.Source;
ohair@286 38 import javax.xml.ws.WebServiceException;
ohair@286 39 import javax.xml.ws.WebServiceFeature;
ohair@286 40
ohair@286 41 import com.sun.xml.internal.org.jvnet.ws.databinding.Databinding;
ohair@286 42 import com.sun.xml.internal.org.jvnet.ws.databinding.DatabindingModeFeature;
ohair@286 43 import com.sun.xml.internal.org.jvnet.ws.databinding.Databinding.Builder;
ohair@286 44 import com.sun.xml.internal.org.jvnet.ws.databinding.Databinding.WSDLGenerator;
ohair@286 45 import org.xml.sax.EntityResolver;
ohair@286 46
ohair@286 47 import com.sun.xml.internal.ws.api.BindingID;
ohair@286 48 import com.sun.xml.internal.ws.api.WSBinding;
ohair@286 49 import com.sun.xml.internal.ws.api.databinding.DatabindingConfig;
ohair@286 50 import com.sun.xml.internal.ws.api.databinding.DatabindingFactory;
ohair@286 51 import com.sun.xml.internal.ws.api.databinding.WSDLGenInfo;
ohair@286 52 import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
ohair@286 53 import com.sun.xml.internal.ws.spi.db.DatabindingException;
ohair@286 54 import com.sun.xml.internal.ws.spi.db.DatabindingProvider;
ohair@286 55 import com.sun.xml.internal.ws.util.ServiceFinder;
ohair@286 56
ohair@286 57 /**
ohair@286 58 * DatabindingFactoryImpl
ohair@286 59 *
ohair@286 60 * @author shih-chang.chen@oracle.com
ohair@286 61 */
ohair@286 62 public class DatabindingFactoryImpl extends DatabindingFactory {
ohair@286 63
ohair@286 64 // static final String WsRuntimeFactoryProperties = DatabindingProvider.class.getName() + ".properties";
ohair@286 65 static final String WsRuntimeFactoryDefaultImpl = "com.sun.xml.internal.ws.db.DatabindingProviderImpl";
ohair@286 66
ohair@286 67 protected Map<String, Object> properties = new HashMap<String, Object>();
ohair@286 68 protected DatabindingProvider defaultRuntimeFactory;
ohair@286 69 // protected Map<String, DatabindingProvider> runtimeFactories = new HashMap<String, DatabindingProvider>();
ohair@286 70 // protected Properties wsRuntimeFactoryMap;
ohair@286 71 protected List<DatabindingProvider> providers;
ohair@286 72
ohair@286 73 static private List<DatabindingProvider> providers() {
ohair@286 74 List<DatabindingProvider> factories = new java.util.ArrayList<DatabindingProvider>();
ohair@286 75 for (DatabindingProvider p : ServiceFinder.find(DatabindingProvider.class)) {
ohair@286 76 factories.add(p);
ohair@286 77 }
ohair@286 78 return factories;
ohair@286 79 }
ohair@286 80
ohair@286 81 public DatabindingFactoryImpl() {
ohair@286 82 }
ohair@286 83
ohair@286 84 public Map<String, Object> properties() {
ohair@286 85 return properties;
ohair@286 86 }
ohair@286 87
ohair@286 88 <T> T property(Class<T> propType, String propName) {
ohair@286 89 if (propName == null) propName = propType.getName();
ohair@286 90 return propType.cast(properties.get(propName));
ohair@286 91 }
ohair@286 92
ohair@286 93 public DatabindingProvider provider(DatabindingConfig config) {
ohair@286 94 String mode = databindingMode(config);
ohair@286 95 if (providers == null)
ohair@286 96 providers = providers();
ohair@286 97 DatabindingProvider provider = null;
ohair@286 98 if (providers != null) {
ohair@286 99 for (DatabindingProvider p : providers)
ohair@286 100 if (p.isFor(mode))
ohair@286 101 provider = p;
ohair@286 102 } if (provider == null) {
ohair@286 103 // if (defaultRuntimeFactory == null) {
ohair@286 104 // defaultRuntimeFactory =
ohair@286 105 // newRuntimeFactory(WsRuntimeFactoryDefaultImpl);
ohair@286 106 // }
ohair@286 107 // provider = defaultRuntimeFactory;
ohair@286 108 provider = new DatabindingProviderImpl();
ohair@286 109 }
ohair@286 110 return provider;
ohair@286 111 }
ohair@286 112
ohair@286 113 public Databinding createRuntime(DatabindingConfig config) {
ohair@286 114 DatabindingProvider provider = provider(config);
ohair@286 115 return provider.create(config);
ohair@286 116 }
ohair@286 117
ohair@286 118 public Databinding.WSDLGenerator createWsdlGen(DatabindingConfig config) {
ohair@286 119 DatabindingProvider provider = provider(config);
ohair@286 120 return provider.wsdlGen(config);
ohair@286 121 }
ohair@286 122
ohair@286 123 // DatabindingProvider newRuntimeFactory(String name) {
ohair@286 124 // ClassLoader classLoader = classLoader();
ohair@286 125 // DatabindingProvider factory = null;
ohair@286 126 // try {
ohair@286 127 // Class cls = (classLoader != null) ? classLoader.loadClass(name) : Class.forName(name);
ohair@286 128 // factory = DatabindingProvider.class.cast(cls.newInstance());
ohair@286 129 // } catch (Exception e) {
ohair@286 130 // throw new DatabindingException("Unknown DatabindingFactory: " + name, e);
ohair@286 131 // }
ohair@286 132 // factory.init(properties);
ohair@286 133 // return factory;
ohair@286 134 // }
ohair@286 135
ohair@286 136 String databindingMode(DatabindingConfig config) {
ohair@286 137 // if ( config.getOverrideMappingInfo() != null &&
ohair@286 138 // config.getOverrideMappingInfo().getDatabindingMode() != null)
ohair@286 139 // return config.getOverrideMappingInfo().getDatabindingMode();
ohair@286 140 // if ( config.getDefaultMappingInfo() != null &&
ohair@286 141 // config.getDefaultMappingInfo().getDatabindingMode() != null)
ohair@286 142 // return config.getDefaultMappingInfo().getDatabindingMode();
ohair@286 143
ohair@286 144 if ( config.getMappingInfo() != null &&
ohair@286 145 config.getMappingInfo().getDatabindingMode() != null)
ohair@286 146 return config.getMappingInfo().getDatabindingMode();
ohair@286 147 if ( config.getFeatures() != null) for (WebServiceFeature f : config.getFeatures()) {
ohair@286 148 if (f instanceof DatabindingModeFeature) {
ohair@286 149 DatabindingModeFeature dmf = (DatabindingModeFeature) f;
ohair@286 150 return dmf.getMode();
ohair@286 151 }
ohair@286 152 }
ohair@286 153 return null;
ohair@286 154 }
ohair@286 155
ohair@286 156 ClassLoader classLoader() {
ohair@286 157 ClassLoader classLoader = property(ClassLoader.class, null);
ohair@286 158 if (classLoader == null) classLoader = Thread.currentThread().getContextClassLoader();
ohair@286 159 return classLoader;
ohair@286 160 }
ohair@286 161
ohair@286 162 Properties loadPropertiesFile(String fileName) {
ohair@286 163 ClassLoader classLoader = classLoader();
ohair@286 164 Properties p = new Properties();
ohair@286 165 try {
ohair@286 166 InputStream is = null;
ohair@286 167 if (classLoader == null) {
ohair@286 168 is = ClassLoader.getSystemResourceAsStream(fileName);
ohair@286 169 } else {
ohair@286 170 is = classLoader.getResourceAsStream(fileName);
ohair@286 171 }
ohair@286 172 if (is != null) {
ohair@286 173 p.load(is);
ohair@286 174 }
ohair@286 175 } catch (Exception e) {
ohair@286 176 throw new WebServiceException(e);
ohair@286 177 }
ohair@286 178 return p;
ohair@286 179 }
ohair@286 180
ohair@286 181 public Builder createBuilder(Class<?> contractClass, Class<?> endpointClass) {
ohair@286 182 return new ConfigBuilder(this, contractClass, endpointClass);
ohair@286 183 }
ohair@286 184
ohair@286 185 static class ConfigBuilder implements Builder {
ohair@286 186 DatabindingConfig config;
ohair@286 187 DatabindingFactoryImpl factory;
ohair@286 188
ohair@286 189 ConfigBuilder(DatabindingFactoryImpl f, Class<?> contractClass, Class<?> implBeanClass) {
ohair@286 190 factory = f;
ohair@286 191 config = new DatabindingConfig();
ohair@286 192 config.setContractClass(contractClass);
ohair@286 193 config.setEndpointClass(implBeanClass);
ohair@286 194 }
ohair@286 195 public Builder targetNamespace(String targetNamespace) {
ohair@286 196 config.getMappingInfo().setTargetNamespace(targetNamespace);
ohair@286 197 return this;
ohair@286 198 }
ohair@286 199 public Builder serviceName(QName serviceName) {
ohair@286 200 config.getMappingInfo().setServiceName(serviceName);
ohair@286 201 return this;
ohair@286 202 }
ohair@286 203 public Builder portName(QName portName) {
ohair@286 204 config.getMappingInfo().setPortName(portName);
ohair@286 205 return this;
ohair@286 206 }
ohair@286 207 public Builder wsdlURL(URL wsdlURL) {
ohair@286 208 config.setWsdlURL(wsdlURL);
ohair@286 209 return this;
ohair@286 210 }
ohair@286 211 public Builder wsdlSource(Source wsdlSource) {
ohair@286 212 config.setWsdlSource(wsdlSource);
ohair@286 213 return this;
ohair@286 214 }
ohair@286 215 public Builder entityResolver(EntityResolver entityResolver) {
ohair@286 216 config.setEntityResolver(entityResolver);
ohair@286 217 return this;
ohair@286 218 }
ohair@286 219 public Builder classLoader(ClassLoader classLoader) {
ohair@286 220 config.setClassLoader(classLoader);
ohair@286 221 return this;
ohair@286 222 }
ohair@286 223 public Builder feature(WebServiceFeature... f) {
ohair@286 224 config.setFeatures(f);
ohair@286 225 return this;
ohair@286 226 }
ohair@286 227 public Builder property(String name, Object value) {
ohair@286 228 config.properties().put(name, value);
ohair@286 229 if (isfor(BindingID.class, name, value)) {
ohair@286 230 config.getMappingInfo().setBindingID((BindingID)value);
ohair@286 231 }
ohair@286 232 if (isfor(WSBinding.class, name, value)) {
ohair@286 233 config.setWSBinding((WSBinding)value);
ohair@286 234 }
ohair@286 235 if (isfor(WSDLPort.class, name, value)) {
ohair@286 236 config.setWsdlPort((WSDLPort)value);
ohair@286 237 }
ohair@286 238 return this;
ohair@286 239 }
ohair@286 240 boolean isfor(Class<?> type, String name, Object value) {
ohair@286 241 return type.getName().equals(name) && type.isInstance(value);
ohair@286 242 }
ohair@286 243
ohair@286 244 public com.sun.xml.internal.org.jvnet.ws.databinding.Databinding build() {
ohair@286 245 return factory.createRuntime(config);
ohair@286 246 }
ohair@286 247 public WSDLGenerator createWSDLGenerator() {
ohair@286 248 return factory.createWsdlGen(config);
ohair@286 249 }
ohair@286 250 }
ohair@286 251 }

mercurial