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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 384
8f2986ff0235
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial