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