001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package com.sun.xml.internal.ws.client;
026:
027: import com.sun.xml.internal.ws.pept.ept.MessageInfo;
028: import com.sun.xml.internal.ws.pept.presentation.MessageStruct;
029: import com.sun.xml.internal.ws.encoding.soap.internal.DelegateBase;
030: import com.sun.xml.internal.ws.model.JavaMethod;
031: import com.sun.xml.internal.ws.server.RuntimeContext;
032: import com.sun.xml.internal.ws.wsdl.WSDLContext;
033: import com.sun.xml.internal.ws.wsdl.parser.PortType;
034:
035: import javax.xml.namespace.QName;
036: import javax.xml.ws.AsyncHandler;
037: import javax.xml.ws.BindingProvider;
038: import javax.xml.ws.WebServiceException;
039: import javax.xml.ws.Service;
040: import java.lang.reflect.InvocationHandler;
041: import java.lang.reflect.InvocationTargetException;
042: import java.lang.reflect.Method;
043: import java.net.URL;
044: import java.util.concurrent.Executor;
045:
046: public class EndpointIFInvocationHandler extends EndpointIFBase
047: implements InvocationHandler,
048: com.sun.xml.internal.ws.client.BindingProviderProperties {
049:
050: Object _proxy;
051: DelegateBase _delegate;
052:
053: EndpointIFContext _endpointContext;
054:
055: Class _portInterface;
056: QName _serviceQName;
057:
058: RuntimeContext _rtcontext;
059: WSDLContext _wsdlContext;
060: boolean failure;
061: URL wsdlDocumentLocation;
062: WSServiceDelegate _service;
063:
064: /**
065: * public constructor
066: */
067:
068: public EndpointIFInvocationHandler(Class portInterface,
069: EndpointIFContext eif, WSServiceDelegate service,
070: QName serviceName) {
071:
072: if ((eif.getBindingID() == null)
073: || (eif.getRuntimeContext() == null)) {
074: failure = true;
075: return;
076: }
077: _endpointContext = eif;
078: _portInterface = portInterface;
079: _rtcontext = eif.getRuntimeContext();
080: _bindingId = eif.getBindingID();
081: _service = service;
082:
083: if (serviceName != null) {
084: if (eif.contains(serviceName))
085: _serviceQName = serviceName;
086: else
087: throw new WebServiceException("Supplied service QName "
088: + serviceName + " does not exist in this wsdl.");
089: } else
090: _serviceQName = eif.getServiceName();
091:
092: if (eif.getEndpointAddress() != null) //temp workaround for local transport kw
093: getRequestContext().put(
094: BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
095: eif.getEndpointAddress());
096:
097: ContactInfoListImpl cil = new ContactInfoListImpl();
098: //not sure I need this service argument
099: _delegate = new DelegateBase(cil, service);
100: }
101:
102: public void setModel(RuntimeContext rtcontext) {
103: _rtcontext = rtcontext;
104: }
105:
106: public void setProxy(Object p) {
107: _proxy = p;
108: }
109:
110: public Object invoke(Object proxy, Method method, Object[] args)
111: throws WebServiceException, Throwable {
112:
113: try {
114: if (isSEIMethod(method, _portInterface)) {
115: return implementSEIMethod(method, args);
116: } else {
117: return method.invoke(this , args);
118: }
119: } catch (InvocationTargetException e) {
120: throw e.getCause();
121: }
122: }
123:
124: /**
125: * Gets a new {@link com.sun.xml.internal.ws.pept.presentation.MessageStruct} from the Delegate, copies
126: * the data and metadata into the newly created MessageStruct, invokes Delegate.send, and
127: * returns the response.
128: */
129: public Object implementSEIMethod(Method method, Object[] parameters)
130: throws Throwable {
131:
132: MessageStruct messageStruct = _delegate.getMessageStruct();
133: int mmep = 0;
134: if (_rtcontext != null) {
135: JavaMethod jmethod = _rtcontext.getModel().getJavaMethod(
136: method);
137: if (jmethod != null) {
138: int mep = jmethod.getMEP();
139: mmep = (mep == MessageStruct.REQUEST_RESPONSE_MEP) ? MessageStruct.REQUEST_RESPONSE_MEP
140: : (mep == MessageStruct.ONE_WAY_MEP) ? MessageStruct.ONE_WAY_MEP
141: : ((mep == MessageStruct.ASYNC_POLL_MEP) ? MessageStruct.ASYNC_POLL_MEP
142: : MessageStruct.ASYNC_CALLBACK_MEP);
143: } else
144: throw new WebServiceException(
145: "runtime model information for java Method "
146: + method.getName() + " is not known .");
147: } //need to check if this is dispatch invocation
148:
149: if (mmep == MessageStruct.ASYNC_CALLBACK_MEP) {
150: for (Object param : parameters) {
151: if (param != null
152: && AsyncHandler.class.isAssignableFrom(param
153: .getClass())) {
154: //messageStruct.setMetaData(BindingProviderProperties.JAXWS_CLIENT_ASYNC_HANDLER, param);
155: messageStruct
156: .setMetaData(
157: BindingProviderProperties.JAXWS_CLIENT_ASYNC_HANDLER,
158: new AsyncHandlerService(
159: (AsyncHandler) param,
160: getCurrentExecutor()));
161: }
162: }
163: }
164:
165: messageStruct.setMethod(method);
166: messageStruct.setData(parameters);
167: RequestContext requestContext = (RequestContext) (java.util.Map) ((BindingProvider) _proxy)
168: .getRequestContext();
169: requestContext.put(JAXWS_CLIENT_HANDLE_PROPERTY, _proxy);
170: messageStruct.setMetaData(JAXWS_RUNTIME_CONTEXT, _rtcontext);
171: messageStruct.setMetaData(JAXWS_CONTEXT_PROPERTY,
172: requestContext);
173:
174: //set mtom threshold value to
175: Object mtomThreshold = requestContext
176: .get(MTOM_THRESHOLOD_VALUE);
177: messageStruct.setMetaData(MTOM_THRESHOLOD_VALUE, mtomThreshold);
178:
179: messageStruct.setMEP(mmep);
180:
181: // Initialize content negotiation property
182: ContentNegotiation.initialize(requestContext, messageStruct);
183:
184: // Set MTOM processing for XML requests only
185: if (_rtcontext != null && _rtcontext.getModel() != null) {
186: javax.xml.ws.soap.SOAPBinding sb = (binding instanceof javax.xml.ws.soap.SOAPBinding) ? (javax.xml.ws.soap.SOAPBinding) binding
187: : null;
188: if (sb != null) {
189: _rtcontext.getModel().enableMtom(sb.isMTOMEnabled());
190: }
191: }
192:
193: _delegate.send(messageStruct);
194: updateResponseContext((MessageInfo) messageStruct);
195: switch (messageStruct.getResponseType()) {
196: case MessageStruct.NORMAL_RESPONSE:
197: break;
198: case MessageStruct.CHECKED_EXCEPTION_RESPONSE:
199: if (_rtcontext.getModel().isCheckedException(method,
200: messageStruct.getResponse().getClass()))
201: throw (Throwable) messageStruct.getResponse();
202: throw (Exception) messageStruct.getResponse();
203: case MessageStruct.UNCHECKED_EXCEPTION_RESPONSE:
204: throw (RuntimeException) messageStruct.getResponse();
205: }
206: return messageStruct.getResponse();
207: }
208:
209: boolean isSEIMethod(Method method, Class sei) {
210: return (sei.equals(method.getDeclaringClass())) ? true : false;
211: }
212:
213: public EndpointIFContext getEndpointContext() {
214: return _endpointContext;
215: }
216:
217: public QName getServiceQName() {
218: return _serviceQName;
219: }
220:
221: public Class getPortInterface() {
222: return _portInterface;
223: }
224:
225: Executor getCurrentExecutor() {
226: return _service.getExecutor();
227: }
228:
229: public QName getWSDLPortTypeQName() {
230: return _service.getWSDLBinding(_endpointContext.getPortName())
231: .getPortTypeName();
232: }
233: }
|