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