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.api.pipe.helper;
038:
039: import com.sun.istack.NotNull;
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.Pipe;
044: import com.sun.xml.ws.api.pipe.PipeCloner;
045: import com.sun.xml.ws.api.pipe.Tube;
046: import com.sun.xml.ws.api.pipe.TubeCloner;
047:
048: /**
049: * {@link Tube} that invokes {@link Pipe}.
050: *
051: * <p>
052: * This can be used to make a {@link Pipe} look like a {@link Tube}.
053: *
054: * @author Kohsuke Kawaguchi
055: * @author Jitendra Kotamraju
056: */
057: public class PipeAdapter extends AbstractTubeImpl {
058: private final Pipe next;
059:
060: public static Tube adapt(Pipe p) {
061: if (p instanceof Tube) {
062: return (Tube) p;
063: } else {
064: return new PipeAdapter(p);
065: }
066: }
067:
068: public static Pipe adapt(Tube p) {
069: if (p instanceof Pipe) {
070: return (Pipe) p;
071: } else {
072: class TubeAdapter extends AbstractPipeImpl {
073: private final Tube t;
074:
075: public TubeAdapter(Tube t) {
076: this .t = t;
077: }
078:
079: private TubeAdapter(TubeAdapter that, PipeCloner cloner) {
080: super (that, cloner);
081: this .t = cloner.copy(that.t);
082: }
083:
084: public Packet process(Packet request) {
085: return Fiber.current().runSync(t, request);
086: }
087:
088: public Pipe copy(PipeCloner cloner) {
089: return new TubeAdapter(this , cloner);
090: }
091: }
092:
093: return new TubeAdapter(p);
094: }
095: }
096:
097: private PipeAdapter(Pipe next) {
098: this .next = next;
099: }
100:
101: /**
102: * Copy constructor
103: */
104: private PipeAdapter(PipeAdapter that, TubeCloner cloner) {
105: super (that, cloner);
106: this .next = ((PipeCloner) cloner).copy(that.next);
107: }
108:
109: /**
110: * Uses the current fiber and runs the whole pipe to the completion
111: * (meaning everything from now on will run synchronously.)
112: */
113: public @NotNull
114: NextAction processRequest(@NotNull
115: Packet p) {
116: return doReturnWith(next.process(p));
117: }
118:
119: public @NotNull
120: NextAction processResponse(@NotNull
121: Packet p) {
122: throw new IllegalStateException();
123: }
124:
125: @NotNull
126: public NextAction processException(@NotNull
127: Throwable t) {
128: throw new IllegalStateException();
129: }
130:
131: public void preDestroy() {
132: next.preDestroy();
133: }
134:
135: public PipeAdapter copy(TubeCloner cloner) {
136: return new PipeAdapter(this , cloner);
137: }
138:
139: public String toString() {
140: return super .toString() + "[" + next.toString() + "]";
141: }
142: }
|