aboutsummaryrefslogtreecommitdiff
path: root/src/share/jaxws_classes/com/sun/xml/internal/ws/db/DatabindingFactoryImpl.java
blob: d4658fa0b016fddfaeb910524432f9b721114180 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.sun.xml.internal.ws.db;

import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;

import org.xml.sax.EntityResolver;

import com.oracle.webservices.internal.api.databinding.Databinding;
import com.oracle.webservices.internal.api.databinding.Databinding.Builder;
import com.oracle.webservices.internal.api.databinding.WSDLGenerator;
import com.sun.xml.internal.ws.api.BindingID;
import com.sun.xml.internal.ws.api.WSBinding;
import com.sun.xml.internal.ws.api.databinding.DatabindingConfig;
import com.sun.xml.internal.ws.api.databinding.DatabindingFactory;
import com.sun.xml.internal.ws.api.databinding.MetadataReader;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
import com.sun.xml.internal.ws.spi.db.DatabindingProvider;
import com.sun.xml.internal.ws.util.ServiceFinder;

/**
 * DatabindingFactoryImpl
 *
 * @author shih-chang.chen@oracle.com
 */
public class DatabindingFactoryImpl extends DatabindingFactory {

//      static final String WsRuntimeFactoryProperties = DatabindingProvider.class.getName() + ".properties";
        static final String WsRuntimeFactoryDefaultImpl = "com.sun.xml.internal.ws.db.DatabindingProviderImpl";

        protected Map<String, Object> properties = new HashMap<String, Object>();
        protected DatabindingProvider defaultRuntimeFactory;
//      protected Map<String, DatabindingProvider> runtimeFactories = new HashMap<String, DatabindingProvider>();
//      protected Properties wsRuntimeFactoryMap;
        protected List<DatabindingProvider> providers;

    static private List<DatabindingProvider> providers() {
        List<DatabindingProvider> factories = new java.util.ArrayList<DatabindingProvider>();
        for (DatabindingProvider p : ServiceFinder.find(DatabindingProvider.class)) {
            factories.add(p);
        }
        return factories;
    }

        public DatabindingFactoryImpl() {
        }

        public Map<String, Object> properties() {
                return properties;
        }

        <T> T property(Class<T> propType, String propName) {
                if (propName == null) propName = propType.getName();
                return propType.cast(properties.get(propName));
        }

    public DatabindingProvider provider(DatabindingConfig config) {
        String mode = databindingMode(config);
        if (providers == null)
            providers = providers();
        DatabindingProvider provider = null;
        if (providers != null) {
            for (DatabindingProvider p : providers)
                if (p.isFor(mode))
                    provider = p;
        } if (provider == null) {
            // if (defaultRuntimeFactory == null) {
            // defaultRuntimeFactory =
            // newRuntimeFactory(WsRuntimeFactoryDefaultImpl);
            // }
            // provider = defaultRuntimeFactory;
            provider = new DatabindingProviderImpl();
        }
        return provider;
    }

        public Databinding createRuntime(DatabindingConfig config) {
            DatabindingProvider provider = provider(config);
                return provider.create(config);
        }

    public WSDLGenerator createWsdlGen(DatabindingConfig config) {
        DatabindingProvider provider = provider(config);
        return provider.wsdlGen(config);
    }

//      DatabindingProvider newRuntimeFactory(String name) {
//              ClassLoader classLoader = classLoader();
//              DatabindingProvider factory = null;
//              try {
//                      Class cls = (classLoader != null) ? classLoader.loadClass(name) : Class.forName(name);
//                      factory = DatabindingProvider.class.cast(cls.newInstance());
//              } catch (Exception e) {
//                      throw new DatabindingException("Unknown DatabindingFactory: " + name, e);
//              }
//              factory.init(properties);
//              return factory;
//      }

        String databindingMode(DatabindingConfig config) {
//              if ( config.getOverrideMappingInfo() != null &&
//                   config.getOverrideMappingInfo().getDatabindingMode() != null)
//                      return config.getOverrideMappingInfo().getDatabindingMode();
//              if ( config.getDefaultMappingInfo() != null &&
//                   config.getDefaultMappingInfo().getDatabindingMode() != null)
//                      return config.getDefaultMappingInfo().getDatabindingMode();

                if ( config.getMappingInfo() != null &&
                     config.getMappingInfo().getDatabindingMode() != null)
                        return config.getMappingInfo().getDatabindingMode();
        if ( config.getFeatures() != null) for (WebServiceFeature f : config.getFeatures()) {
            if (f instanceof com.oracle.webservices.internal.api.databinding.DatabindingModeFeature) {
                com.oracle.webservices.internal.api.databinding.DatabindingModeFeature dmf = (com.oracle.webservices.internal.api.databinding.DatabindingModeFeature) f;
                return dmf.getMode();
            }
        }
                return null;
        }

        ClassLoader classLoader() {
                ClassLoader classLoader = property(ClassLoader.class, null);
                if (classLoader == null) classLoader = Thread.currentThread().getContextClassLoader();
                return classLoader;
        }

        Properties loadPropertiesFile(String fileName) {
                ClassLoader classLoader = classLoader();
                Properties p = new Properties();
                try {
                        InputStream is = null;
                        if (classLoader == null) {
                                is = ClassLoader.getSystemResourceAsStream(fileName);
                        } else {
                                is = classLoader.getResourceAsStream(fileName);
                        }
                        if (is != null) {
                                p.load(is);
                        }
                } catch (Exception e) {
                        throw new WebServiceException(e);
                }
                return p;
        }

    public Builder createBuilder(Class<?> contractClass, Class<?> endpointClass) {
        return new ConfigBuilder(this, contractClass, endpointClass);
    }

    static class ConfigBuilder implements Builder {
        DatabindingConfig config;
        DatabindingFactoryImpl factory;

        ConfigBuilder(DatabindingFactoryImpl f, Class<?> contractClass, Class<?> implBeanClass) {
            factory = f;
            config = new DatabindingConfig();
            config.setContractClass(contractClass);
            config.setEndpointClass(implBeanClass);
        }
        public Builder targetNamespace(String targetNamespace) {
            config.getMappingInfo().setTargetNamespace(targetNamespace);
            return this;
        }
        public Builder serviceName(QName serviceName) {
            config.getMappingInfo().setServiceName(serviceName);
            return this;
        }
        public Builder portName(QName portName) {
            config.getMappingInfo().setPortName(portName);
            return this;
        }
        public Builder wsdlURL(URL wsdlURL) {
            config.setWsdlURL(wsdlURL);
            return this;
        }
        public Builder wsdlSource(Source wsdlSource) {
            config.setWsdlSource(wsdlSource);
            return this;
        }
        public Builder entityResolver(EntityResolver entityResolver) {
            config.setEntityResolver(entityResolver);
            return this;
        }
        public Builder classLoader(ClassLoader classLoader) {
            config.setClassLoader(classLoader);
            return this;
        }
        public Builder feature(WebServiceFeature... f) {
            config.setFeatures(f);
            return this;
        }
        public Builder property(String name, Object value) {
            config.properties().put(name, value);
            if (isfor(BindingID.class, name, value)) {
                config.getMappingInfo().setBindingID((BindingID)value);
            }
            if (isfor(WSBinding.class, name, value)) {
                config.setWSBinding((WSBinding)value);
            }
            if (isfor(WSDLPort.class, name, value)) {
                config.setWsdlPort((WSDLPort)value);
            }
            if (isfor(MetadataReader.class, name, value)) {
                config.setMetadataReader((MetadataReader)value);
            }
            return this;
        }
        boolean isfor(Class<?> type, String name, Object value) {
            return type.getName().equals(name) && type.isInstance(value);
        }

        public com.oracle.webservices.internal.api.databinding.Databinding build() {
            return factory.createRuntime(config);
        }
        public com.oracle.webservices.internal.api.databinding.WSDLGenerator createWSDLGenerator() {
            return factory.createWsdlGen(config);
        }
    }
}