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;
038:
039: import com.sun.xml.ws.util.CompletedFuture;
040:
041: import javax.xml.ws.AsyncHandler;
042: import javax.xml.ws.Response;
043: import java.util.Map;
044: import java.util.concurrent.Callable;
045: import java.util.concurrent.Future;
046: import java.util.concurrent.FutureTask;
047:
048: /**
049: * {@link Response} implementation.
050: *
051: * @author Kohsuke Kawaguchi
052: * @author Kathy Walsh
053: */
054: public final class ResponseImpl<T> extends FutureTask<T> implements
055: Response<T>, ResponseContextReceiver {
056:
057: /**
058: * Optional {@link AsyncHandler} that gets invoked
059: * at the completion of the task.
060: */
061: private final AsyncHandler<T> handler;
062: private ResponseContext responseContext;
063:
064: private final Callable<T> callable;
065:
066: /**
067: *
068: * @param callable
069: * This {@link Callable} is executed asynchronously.
070: * @param handler
071: * Optional {@link AsyncHandler} to invoke at the end
072: * of the processing. Can be null.
073: */
074: public ResponseImpl(Callable<T> callable, AsyncHandler<T> handler) {
075: super (callable);
076: this .callable = callable;
077: this .handler = handler;
078: }
079:
080: @Override
081: public void run() {
082: // override so that we call set()
083: try {
084: set(callable.call(), null);
085: } catch (Throwable t) {
086: set(null, t);
087: }
088: }
089:
090: protected void set(final T v, final Throwable t) {
091: // call the handler before we mark the future as 'done'
092: if (handler != null) {
093: try {
094: /**
095: * {@link Response} object passed into the callback.
096: * We need a separate {@link Future} because we don't want {@link ResponseImpl}
097: * to be marked as 'done' before the callback finishes execution.
098: * (That would provide implicit synchronization between the application code
099: * in the main thread and the callback code, and is compatible with the JAX-RI 2.0 FCS.
100: */
101: class CallbackFuture<T> extends CompletedFuture<T>
102: implements Response<T> {
103: public CallbackFuture(T v, Throwable t) {
104: super (v, t);
105: }
106:
107: public Map<String, Object> getContext() {
108: return ResponseImpl.this .getContext();
109: }
110: }
111: handler.handleResponse(new CallbackFuture<T>(v, t));
112: } catch (Throwable e) {
113: super .setException(e);
114: return;
115: }
116: }
117: if (t != null) {
118: super .setException(t);
119: } else {
120: super .set(v);
121: }
122: }
123:
124: public ResponseContext getContext() {
125: return responseContext;
126: }
127:
128: public void setResponseContext(ResponseContext rc) {
129: responseContext = rc;
130: }
131: }
|