001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.client.sei;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.xml.ws.api.SOAPVersion;
041: import com.sun.xml.ws.api.addressing.WSEndpointReference;
042: import com.sun.xml.ws.api.message.Header;
043: import com.sun.xml.ws.api.message.Headers;
044: import com.sun.xml.ws.api.message.Packet;
045: import com.sun.xml.ws.api.model.MEP;
046: import com.sun.xml.ws.api.model.wsdl.WSDLBoundOperation;
047: import com.sun.xml.ws.api.pipe.Tube;
048: import com.sun.xml.ws.api.pipe.Fiber;
049: import com.sun.xml.ws.binding.BindingImpl;
050: import com.sun.xml.ws.client.RequestContext;
051: import com.sun.xml.ws.client.ResponseContextReceiver;
052: import com.sun.xml.ws.client.Stub;
053: import com.sun.xml.ws.client.WSServiceDelegate;
054: import com.sun.xml.ws.model.JavaMethodImpl;
055: import com.sun.xml.ws.model.SOAPSEIModel;
056:
057: import javax.xml.namespace.QName;
058: import java.lang.reflect.InvocationHandler;
059: import java.lang.reflect.InvocationTargetException;
060: import java.lang.reflect.Method;
061: import java.util.HashMap;
062: import java.util.Map;
063:
064: /**
065: * {@link Stub} that handles method invocations
066: * through a strongly-typed endpoint interface.
067: *
068: * @author Kohsuke Kawaguchi
069: */
070: public final class SEIStub extends Stub implements InvocationHandler {
071: public SEIStub(WSServiceDelegate owner, BindingImpl binding,
072: SOAPSEIModel seiModel, Tube master, WSEndpointReference epr) {
073: super (owner, master, binding, seiModel.getPort(), seiModel
074: .getPort().getAddress(), epr);
075: this .seiModel = seiModel;
076: this .soapVersion = binding.getSOAPVersion();
077:
078: Map<WSDLBoundOperation, JavaMethodImpl> syncs = new HashMap<WSDLBoundOperation, JavaMethodImpl>();
079:
080: // fill in methodHandlers.
081: // first fill in sychronized versions
082: for (JavaMethodImpl m : seiModel.getJavaMethods()) {
083: if (!m.getMEP().isAsync) {
084: SyncMethodHandler handler = new SyncMethodHandler(this ,
085: m);
086: syncs.put(m.getOperation(), m);
087: methodHandlers.put(m.getMethod(), handler);
088: }
089: }
090:
091: for (JavaMethodImpl jm : seiModel.getJavaMethods()) {
092: JavaMethodImpl sync = syncs.get(jm.getOperation());
093: if (jm.getMEP() == MEP.ASYNC_CALLBACK) {
094: Method m = jm.getMethod();
095: CallbackMethodHandler handler = new CallbackMethodHandler(
096: this , jm, sync,
097: m.getParameterTypes().length - 1);
098: methodHandlers.put(m, handler);
099: }
100: if (jm.getMEP() == MEP.ASYNC_POLL) {
101: Method m = jm.getMethod();
102: PollingMethodHandler handler = new PollingMethodHandler(
103: this , jm, sync);
104: methodHandlers.put(m, handler);
105: }
106: }
107: }
108:
109: public final SOAPSEIModel seiModel;
110:
111: public final SOAPVersion soapVersion;
112:
113: /**
114: * For each method on the port interface we have
115: * a {@link MethodHandler} that processes it.
116: */
117: private final Map<Method, MethodHandler> methodHandlers = new HashMap<Method, MethodHandler>();
118:
119: public Object invoke(Object proxy, Method method, Object[] args)
120: throws Throwable {
121: MethodHandler handler = methodHandlers.get(method);
122: if (handler != null) {
123: return handler.invoke(proxy, args);
124: } else {
125: // we handle the other method invocations by ourselves
126: try {
127: return method.invoke(this , args);
128: } catch (IllegalAccessException e) {
129: // impossible
130: throw new AssertionError(e);
131: } catch (IllegalArgumentException e) {
132: throw new AssertionError(e);
133: } catch (InvocationTargetException e) {
134: throw e.getCause();
135: }
136: }
137: }
138:
139: public final Packet doProcess(Packet request, RequestContext rc,
140: ResponseContextReceiver receiver) {
141: return super .process(request, rc, receiver);
142: }
143:
144: public final void doProcessAsync(Packet request, RequestContext rc,
145: Fiber.CompletionCallback callback) {
146: super .processAsync(request, rc, callback);
147: }
148:
149: protected final @NotNull
150: QName getPortName() {
151: return wsdlPort.getName();
152: }
153:
154: public void setOutboundHeaders(Object... headers) {
155: if (headers == null)
156: throw new IllegalArgumentException();
157: Header[] hl = new Header[headers.length];
158: for (int i = 0; i < hl.length; i++) {
159: if (headers[i] == null)
160: throw new IllegalArgumentException();
161: hl[i] = Headers.create(seiModel.getJAXBContext(),
162: headers[i]);
163: }
164: super.setOutboundHeaders(hl);
165: }
166: }
|