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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
equal deleted inserted replaced
284:88b85470e72c 286:f50545b5e2f1
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.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
31 import java.lang.reflect.Method;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import javax.xml.namespace.QName;
36 import javax.xml.ws.WebServiceFeature;
37
38 import com.sun.xml.internal.org.jvnet.ws.message.MessageContext;
39
40 import com.sun.xml.internal.ws.api.databinding.EndpointCallBridge;
41 import com.sun.xml.internal.ws.api.databinding.JavaCallInfo;
42 import com.sun.xml.internal.ws.api.databinding.WSDLGenInfo;
43 import com.sun.xml.internal.ws.api.databinding.Databinding;
44 import com.sun.xml.internal.ws.api.databinding.DatabindingConfig;
45 import com.sun.xml.internal.ws.api.databinding.ClientCallBridge;
46 import com.sun.xml.internal.ws.api.message.Message;
47 import com.sun.xml.internal.ws.api.message.Packet;
48 import com.sun.xml.internal.ws.api.model.MEP;
49 import com.sun.xml.internal.ws.api.model.SEIModel;
50 import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
51 import com.sun.xml.internal.ws.api.pipe.Codec;
52 import com.sun.xml.internal.ws.api.pipe.ContentType;
53 import com.sun.xml.internal.ws.binding.BindingImpl;
54 import com.sun.xml.internal.ws.client.sei.StubAsyncHandler;
55 import com.sun.xml.internal.ws.client.sei.StubHandler;
56 import com.sun.xml.internal.ws.model.AbstractSEIModelImpl;
57 import com.sun.xml.internal.ws.model.JavaMethodImpl;
58 import com.sun.xml.internal.ws.model.RuntimeModeler;
59 import com.sun.xml.internal.ws.server.sei.TieHandler;
60 import com.sun.xml.internal.ws.util.QNameMap;
61 import com.sun.xml.internal.ws.wsdl.ActionBasedOperationSignature;
62 import com.sun.xml.internal.ws.wsdl.DispatchException;
63 import com.sun.xml.internal.ws.wsdl.OperationDispatcher;
64
65 /**
66 * WsRuntimeImpl is the databinding processor built on SEIModel
67 *
68 * @author shih-chang.chen@oracle.com
69 */
70 public class DatabindingImpl implements Databinding, com.sun.xml.internal.org.jvnet.ws.databinding.Databinding {
71
72 AbstractSEIModelImpl seiModel;
73 Map<Method, StubHandler> stubHandlers;
74 QNameMap<TieHandler> wsdlOpMap = new QNameMap<TieHandler>();
75 Map<Method, TieHandler> tieHandlers = new HashMap<Method, TieHandler>();
76 OperationDispatcher operationDispatcher;
77 OperationDispatcher operationDispatcherNoWsdl;
78 boolean clientConfig = false;
79 Codec codec;
80
81 public DatabindingImpl(DatabindingProviderImpl p, DatabindingConfig config) {
82 RuntimeModeler modeler = new RuntimeModeler(config);
83 modeler.setClassLoader(config.getClassLoader());
84 seiModel = modeler.buildRuntimeModel();
85 WSDLPort wsdlport = config.getWsdlPort();
86 clientConfig = isClientConfig(config);
87 if ( clientConfig ) initStubHandlers();
88 seiModel.setDatabinding(this);
89 if (wsdlport != null) freeze(wsdlport);
90 if (operationDispatcher == null) operationDispatcherNoWsdl = new OperationDispatcher(null, seiModel.getWSBinding(), seiModel);
91 // if(!clientConfig) {
92 for(JavaMethodImpl jm: seiModel.getJavaMethods()) if (!jm.isAsync()) {
93 TieHandler th = new TieHandler(jm, seiModel.getWSBinding());
94 wsdlOpMap.put(jm.getOperationQName(), th);
95 tieHandlers.put(th.getMethod(), th);
96 }
97 // }
98 }
99
100 //TODO isClientConfig
101 private boolean isClientConfig(DatabindingConfig config) {
102 if (config.getContractClass() == null) return false;
103 if (!config.getContractClass().isInterface()) return false;
104 return (config.getEndpointClass() == null || config.getEndpointClass().isInterface());
105 }
106 //TODO fix freeze
107 public synchronized void freeze(WSDLPort port) {
108 if (clientConfig) return;
109 if (operationDispatcher != null) return;
110 operationDispatcher = (port == null) ? null : new OperationDispatcher(port, seiModel.getWSBinding(), seiModel);
111 }
112
113 public SEIModel getModel() {
114 return seiModel;
115 }
116 //Refactored from SEIStub
117 private void initStubHandlers() {
118 stubHandlers = new HashMap<Method, StubHandler>();
119 Map<ActionBasedOperationSignature, JavaMethodImpl> syncs = new HashMap<ActionBasedOperationSignature, JavaMethodImpl>();
120 // fill in methodHandlers.
121 // first fill in sychronized versions
122 for (JavaMethodImpl m : seiModel.getJavaMethods()) {
123 if (!m.getMEP().isAsync) {
124 StubHandler handler = new StubHandler(m);
125 syncs.put(m.getOperationSignature(), m);
126 stubHandlers.put(m.getMethod(), handler);
127 }
128 }
129 for (JavaMethodImpl jm : seiModel.getJavaMethods()) {
130 JavaMethodImpl sync = syncs.get(jm.getOperationSignature());
131 if (jm.getMEP() == MEP.ASYNC_CALLBACK || jm.getMEP() == MEP.ASYNC_POLL) {
132 Method m = jm.getMethod();
133 StubAsyncHandler handler = new StubAsyncHandler(jm, sync);
134 stubHandlers.put(m, handler);
135 }
136 }
137 }
138
139 public QName resolveOperationQName(Packet req) throws DispatchException {
140 return (operationDispatcher != null)?
141 operationDispatcher.getWSDLOperationQName(req):
142 operationDispatcherNoWsdl.getWSDLOperationQName(req);
143 }
144
145 public JavaCallInfo deserializeRequest(Packet req) {
146 JavaCallInfo call = new JavaCallInfo();
147 try {
148 QName wsdlOp = resolveOperationQName(req);
149 TieHandler tie = wsdlOpMap.get(wsdlOp);
150 call.setMethod(tie.getMethod());
151 Object[] args = tie.readRequest(req.getMessage());
152 call.setParameters(args);
153 } catch (DispatchException e) {
154 call.setException(e);
155 }
156 return call;
157 }
158
159 public JavaCallInfo deserializeResponse(Packet res, JavaCallInfo call) {
160 StubHandler stubHandler = stubHandlers.get(call.getMethod());
161 try {
162 return stubHandler.readResponse(res, call);
163 } catch (Throwable e) {
164 call.setException(e);
165 return call;
166 }
167 }
168
169 public WebServiceFeature[] getFeatures() {
170 // TODO Auto-generated method stub
171 return null;
172 }
173
174 public Packet serializeRequest(JavaCallInfo call) {
175 StubHandler stubHandler = stubHandlers.get(call.getMethod());
176 return stubHandler.createRequestPacket(call);
177 }
178
179 public Packet serializeResponse(JavaCallInfo call) {
180 Method method = call.getMethod();
181 Message message = null;
182 if (method != null) {
183 TieHandler th = tieHandlers.get(method);
184 if (th != null) {
185 return th.serializeResponse(call);
186 }
187 }
188 if (call.getException() instanceof DispatchException) {
189 message = ((DispatchException)call.getException()).fault;
190 }
191 Packet response = new Packet();
192 response.setMessage(message);
193 return response;
194 }
195
196 public ClientCallBridge getClientBridge(Method method) {
197 return stubHandlers.get(method);
198 }
199
200
201 public void generateWSDL(WSDLGenInfo info) {
202 com.sun.xml.internal.ws.wsdl.writer.WSDLGenerator wsdlGen = new com.sun.xml.internal.ws.wsdl.writer.WSDLGenerator(
203 seiModel,
204 info.getWsdlResolver(),
205 seiModel.getWSBinding(),
206 info.getContainer(), seiModel.getEndpointClass(),
207 info.isInlineSchemas(),
208 info.getExtensions());
209 wsdlGen.doGeneration();
210 }
211
212 public EndpointCallBridge getEndpointBridge(Packet req) throws DispatchException {
213 QName wsdlOp = resolveOperationQName(req);
214 return wsdlOpMap.get(wsdlOp);
215 }
216
217
218 Codec getCodec() {
219 if (codec == null) codec = ((BindingImpl)seiModel.getWSBinding()).createCodec();
220 return codec;
221 }
222
223 public ContentType encode( Packet packet, OutputStream out ) throws IOException {
224 return getCodec().encode(packet, out);
225 }
226
227 public void decode( InputStream in, String ct, Packet p ) throws IOException{
228 getCodec().decode(in, ct, p);
229 }
230
231 public com.sun.xml.internal.org.jvnet.ws.databinding.JavaCallInfo createJavaCallInfo(Method method, Object[] args) {
232 return new JavaCallInfo(method, args);
233 }
234
235 public MessageContext serializeRequest(com.sun.xml.internal.org.jvnet.ws.databinding.JavaCallInfo call) {
236 return serializeRequest((JavaCallInfo)call);
237 }
238
239 public com.sun.xml.internal.org.jvnet.ws.databinding.JavaCallInfo deserializeResponse(
240 MessageContext message, com.sun.xml.internal.org.jvnet.ws.databinding.JavaCallInfo call) {
241 return deserializeResponse((Packet)message, (JavaCallInfo)call);
242 }
243
244 public com.sun.xml.internal.org.jvnet.ws.databinding.JavaCallInfo deserializeRequest(MessageContext message) {
245 return deserializeRequest((Packet)message);
246 }
247
248 public MessageContext serializeResponse(com.sun.xml.internal.org.jvnet.ws.databinding.JavaCallInfo call) {
249 return serializeResponse((JavaCallInfo)call);
250 }
251 }

mercurial