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

Fri, 14 Feb 2014 11:13:45 +0100

author
mkos
date
Fri, 14 Feb 2014 11:13:45 +0100
changeset 515
6cd506508147
parent 384
8f2986ff0235
child 637
9c07ef4934dd
permissions
-rw-r--r--

8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2013, 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.InputStream;
ohair@286 29 import java.net.URL;
ohair@286 30 import java.util.HashMap;
ohair@286 31 import java.util.List;
ohair@286 32 import java.util.Map;
ohair@286 33 import java.util.Properties;
ohair@286 34
ohair@286 35 import javax.xml.namespace.QName;
ohair@286 36 import javax.xml.transform.Source;
ohair@286 37 import javax.xml.ws.WebServiceException;
ohair@286 38 import javax.xml.ws.WebServiceFeature;
ohair@286 39
ohair@286 40 import org.xml.sax.EntityResolver;
ohair@286 41
alanb@368 42 import com.oracle.webservices.internal.api.databinding.Databinding;
alanb@368 43 import com.oracle.webservices.internal.api.databinding.Databinding.Builder;
alanb@368 44 import com.oracle.webservices.internal.api.databinding.WSDLGenerator;
mkos@384 45 import com.oracle.webservices.internal.api.databinding.DatabindingModeFeature;
ohair@286 46 import com.sun.xml.internal.ws.api.BindingID;
ohair@286 47 import com.sun.xml.internal.ws.api.WSBinding;
ohair@286 48 import com.sun.xml.internal.ws.api.databinding.DatabindingConfig;
ohair@286 49 import com.sun.xml.internal.ws.api.databinding.DatabindingFactory;
alanb@368 50 import com.sun.xml.internal.ws.api.databinding.MetadataReader;
ohair@286 51 import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
ohair@286 52 import com.sun.xml.internal.ws.spi.db.DatabindingProvider;
ohair@286 53 import com.sun.xml.internal.ws.util.ServiceFinder;
ohair@286 54
ohair@286 55 /**
ohair@286 56 * DatabindingFactoryImpl
ohair@286 57 *
ohair@286 58 * @author shih-chang.chen@oracle.com
ohair@286 59 */
ohair@286 60 public class DatabindingFactoryImpl extends DatabindingFactory {
ohair@286 61
ohair@286 62 static final String WsRuntimeFactoryDefaultImpl = "com.sun.xml.internal.ws.db.DatabindingProviderImpl";
ohair@286 63
ohair@286 64 protected Map<String, Object> properties = new HashMap<String, Object>();
ohair@286 65 protected DatabindingProvider defaultRuntimeFactory;
ohair@286 66 protected List<DatabindingProvider> providers;
ohair@286 67
ohair@286 68 static private List<DatabindingProvider> providers() {
ohair@286 69 List<DatabindingProvider> factories = new java.util.ArrayList<DatabindingProvider>();
ohair@286 70 for (DatabindingProvider p : ServiceFinder.find(DatabindingProvider.class)) {
ohair@286 71 factories.add(p);
ohair@286 72 }
ohair@286 73 return factories;
ohair@286 74 }
ohair@286 75
ohair@286 76 public DatabindingFactoryImpl() {
ohair@286 77 }
ohair@286 78
ohair@286 79 public Map<String, Object> properties() {
ohair@286 80 return properties;
ohair@286 81 }
ohair@286 82
ohair@286 83 <T> T property(Class<T> propType, String propName) {
ohair@286 84 if (propName == null) propName = propType.getName();
ohair@286 85 return propType.cast(properties.get(propName));
ohair@286 86 }
ohair@286 87
ohair@286 88 public DatabindingProvider provider(DatabindingConfig config) {
ohair@286 89 String mode = databindingMode(config);
ohair@286 90 if (providers == null)
ohair@286 91 providers = providers();
ohair@286 92 DatabindingProvider provider = null;
ohair@286 93 if (providers != null) {
ohair@286 94 for (DatabindingProvider p : providers)
ohair@286 95 if (p.isFor(mode))
ohair@286 96 provider = p;
ohair@286 97 } if (provider == null) {
ohair@286 98 provider = new DatabindingProviderImpl();
ohair@286 99 }
ohair@286 100 return provider;
ohair@286 101 }
ohair@286 102
ohair@286 103 public Databinding createRuntime(DatabindingConfig config) {
ohair@286 104 DatabindingProvider provider = provider(config);
ohair@286 105 return provider.create(config);
ohair@286 106 }
ohair@286 107
alanb@368 108 public WSDLGenerator createWsdlGen(DatabindingConfig config) {
ohair@286 109 DatabindingProvider provider = provider(config);
ohair@286 110 return provider.wsdlGen(config);
ohair@286 111 }
ohair@286 112
ohair@286 113 String databindingMode(DatabindingConfig config) {
ohair@286 114 if ( config.getMappingInfo() != null &&
ohair@286 115 config.getMappingInfo().getDatabindingMode() != null)
ohair@286 116 return config.getMappingInfo().getDatabindingMode();
ohair@286 117 if ( config.getFeatures() != null) for (WebServiceFeature f : config.getFeatures()) {
mkos@384 118 if (f instanceof DatabindingModeFeature) {
mkos@384 119 DatabindingModeFeature dmf = (DatabindingModeFeature) f;
mkos@384 120 config.properties().putAll(dmf.getProperties());
ohair@286 121 return dmf.getMode();
ohair@286 122 }
ohair@286 123 }
ohair@286 124 return null;
ohair@286 125 }
ohair@286 126
ohair@286 127 ClassLoader classLoader() {
ohair@286 128 ClassLoader classLoader = property(ClassLoader.class, null);
ohair@286 129 if (classLoader == null) classLoader = Thread.currentThread().getContextClassLoader();
ohair@286 130 return classLoader;
ohair@286 131 }
ohair@286 132
ohair@286 133 Properties loadPropertiesFile(String fileName) {
ohair@286 134 ClassLoader classLoader = classLoader();
ohair@286 135 Properties p = new Properties();
ohair@286 136 try {
ohair@286 137 InputStream is = null;
ohair@286 138 if (classLoader == null) {
ohair@286 139 is = ClassLoader.getSystemResourceAsStream(fileName);
ohair@286 140 } else {
ohair@286 141 is = classLoader.getResourceAsStream(fileName);
ohair@286 142 }
ohair@286 143 if (is != null) {
ohair@286 144 p.load(is);
ohair@286 145 }
ohair@286 146 } catch (Exception e) {
ohair@286 147 throw new WebServiceException(e);
ohair@286 148 }
ohair@286 149 return p;
ohair@286 150 }
ohair@286 151
ohair@286 152 public Builder createBuilder(Class<?> contractClass, Class<?> endpointClass) {
ohair@286 153 return new ConfigBuilder(this, contractClass, endpointClass);
ohair@286 154 }
ohair@286 155
ohair@286 156 static class ConfigBuilder implements Builder {
ohair@286 157 DatabindingConfig config;
ohair@286 158 DatabindingFactoryImpl factory;
ohair@286 159
ohair@286 160 ConfigBuilder(DatabindingFactoryImpl f, Class<?> contractClass, Class<?> implBeanClass) {
ohair@286 161 factory = f;
ohair@286 162 config = new DatabindingConfig();
ohair@286 163 config.setContractClass(contractClass);
ohair@286 164 config.setEndpointClass(implBeanClass);
ohair@286 165 }
ohair@286 166 public Builder targetNamespace(String targetNamespace) {
ohair@286 167 config.getMappingInfo().setTargetNamespace(targetNamespace);
ohair@286 168 return this;
ohair@286 169 }
ohair@286 170 public Builder serviceName(QName serviceName) {
ohair@286 171 config.getMappingInfo().setServiceName(serviceName);
ohair@286 172 return this;
ohair@286 173 }
ohair@286 174 public Builder portName(QName portName) {
ohair@286 175 config.getMappingInfo().setPortName(portName);
ohair@286 176 return this;
ohair@286 177 }
ohair@286 178 public Builder wsdlURL(URL wsdlURL) {
ohair@286 179 config.setWsdlURL(wsdlURL);
ohair@286 180 return this;
ohair@286 181 }
ohair@286 182 public Builder wsdlSource(Source wsdlSource) {
ohair@286 183 config.setWsdlSource(wsdlSource);
ohair@286 184 return this;
ohair@286 185 }
ohair@286 186 public Builder entityResolver(EntityResolver entityResolver) {
ohair@286 187 config.setEntityResolver(entityResolver);
ohair@286 188 return this;
ohair@286 189 }
ohair@286 190 public Builder classLoader(ClassLoader classLoader) {
ohair@286 191 config.setClassLoader(classLoader);
ohair@286 192 return this;
ohair@286 193 }
ohair@286 194 public Builder feature(WebServiceFeature... f) {
ohair@286 195 config.setFeatures(f);
ohair@286 196 return this;
ohair@286 197 }
ohair@286 198 public Builder property(String name, Object value) {
ohair@286 199 config.properties().put(name, value);
ohair@286 200 if (isfor(BindingID.class, name, value)) {
ohair@286 201 config.getMappingInfo().setBindingID((BindingID)value);
ohair@286 202 }
ohair@286 203 if (isfor(WSBinding.class, name, value)) {
ohair@286 204 config.setWSBinding((WSBinding)value);
ohair@286 205 }
ohair@286 206 if (isfor(WSDLPort.class, name, value)) {
ohair@286 207 config.setWsdlPort((WSDLPort)value);
ohair@286 208 }
alanb@368 209 if (isfor(MetadataReader.class, name, value)) {
alanb@368 210 config.setMetadataReader((MetadataReader)value);
alanb@368 211 }
ohair@286 212 return this;
ohair@286 213 }
ohair@286 214 boolean isfor(Class<?> type, String name, Object value) {
ohair@286 215 return type.getName().equals(name) && type.isInstance(value);
ohair@286 216 }
ohair@286 217
alanb@368 218 public com.oracle.webservices.internal.api.databinding.Databinding build() {
ohair@286 219 return factory.createRuntime(config);
ohair@286 220 }
alanb@368 221 public com.oracle.webservices.internal.api.databinding.WSDLGenerator createWSDLGenerator() {
ohair@286 222 return factory.createWsdlGen(config);
ohair@286 223 }
ohair@286 224 }
ohair@286 225 }

mercurial