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: package com.sun.xml.ws.server.provider;
037:
038: import com.sun.istack.NotNull;
039: import com.sun.istack.Nullable;
040: import com.sun.xml.ws.api.message.Packet;
041: import com.sun.xml.ws.api.pipe.Fiber;
042: import com.sun.xml.ws.api.pipe.NextAction;
043: import com.sun.xml.ws.api.pipe.Tube;
044: import com.sun.xml.ws.api.server.AsyncProvider;
045: import com.sun.xml.ws.api.server.AsyncProviderCallback;
046: import com.sun.xml.ws.api.server.Invoker;
047: import com.sun.xml.ws.api.server.WSEndpoint;
048: import com.sun.xml.ws.server.AbstractWebServiceContext;
049:
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052:
053: /**
054: * This {@link Tube} is used to invoke the {@link AsyncProvider} endpoints.
055: *
056: * @author Jitendra Kotamraju
057: */
058: class AsyncProviderInvokerTube<T> extends ProviderInvokerTube<T> {
059:
060: private static final Logger LOGGER = Logger
061: .getLogger(com.sun.xml.ws.util.Constants.LoggingDomain
062: + ".server.AsyncProviderInvokerTube");
063:
064: public AsyncProviderInvokerTube(Invoker invoker,
065: ProviderArgumentsBuilder<T> argsBuilder) {
066: super (invoker, argsBuilder);
067: }
068:
069: /*
070: * This binds the parameter for Provider endpoints and invokes the
071: * invoke() method of {@linke Provider} endpoint. The return value from
072: * invoke() is used to create a new {@link Message} that traverses
073: * through the Pipeline to transport.
074: */
075: public @NotNull
076: NextAction processRequest(@NotNull
077: Packet request) {
078: T param = argsBuilder.getParameter(request);
079: AsyncProviderCallback callback = new AsyncProviderInvokerTube.AsyncProviderCallbackImpl(
080: request);
081: AsyncWebServiceContext ctxt = new AsyncWebServiceContext(
082: getEndpoint(), request);
083:
084: AsyncProviderInvokerTube.LOGGER
085: .fine("Invoking AsyncProvider Endpoint");
086: try {
087: getInvoker(request).invokeAsyncProvider(request, param,
088: callback, ctxt);
089: } catch (Exception e) {
090: LOGGER.log(Level.SEVERE, e.getMessage(), e);
091: return doThrow(e);
092: }
093: // Suspend the Fiber. AsyncProviderCallback will resume the Fiber after
094: // it receives response.
095: return doSuspend();
096: }
097:
098: private class AsyncProviderCallbackImpl implements
099: AsyncProviderCallback<T> {
100: private final Packet request;
101: private final Fiber fiber;
102:
103: public AsyncProviderCallbackImpl(Packet request) {
104: this .request = request;
105: this .fiber = Fiber.current();
106: }
107:
108: public void send(@Nullable
109: T param) {
110: if (param == null) {
111: if (request.transportBackChannel != null) {
112: request.transportBackChannel.close();
113: }
114: }
115: Packet packet = argsBuilder
116: .getResponse(request, param, getEndpoint()
117: .getPort(), getEndpoint().getBinding());
118: fiber.resume(packet);
119: }
120:
121: public void sendError(@NotNull
122: Throwable t) {
123: Exception e;
124: if (t instanceof RuntimeException) {
125: e = (RuntimeException) t;
126: } else {
127: e = new RuntimeException(t);
128: }
129: Packet packet = argsBuilder
130: .getResponse(request, e, getEndpoint().getPort(),
131: getEndpoint().getBinding());
132: fiber.resume(packet);
133: }
134: }
135:
136: /**
137: * The single {@link javax.xml.ws.WebServiceContext} instance injected into application.
138: */
139: private static final class AsyncWebServiceContext extends
140: AbstractWebServiceContext {
141: final Packet packet;
142:
143: AsyncWebServiceContext(WSEndpoint endpoint, Packet packet) {
144: super (endpoint);
145: this .packet = packet;
146: }
147:
148: public @NotNull
149: Packet getRequestPacket() {
150: return packet;
151: }
152: }
153:
154: public @NotNull
155: NextAction processResponse(@NotNull
156: Packet response) {
157: return doReturnWith(response);
158: }
159:
160: public @NotNull
161: NextAction processException(@NotNull
162: Throwable t) {
163: throw new IllegalStateException(
164: "AsyncProviderInvokerTube's processException shouldn't be called.");
165: }
166:
167: }
|