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.transport;
038:
039: import com.sun.xml.ws.api.EndpointAddress;
040: import com.sun.xml.ws.api.message.Packet;
041: import com.sun.xml.ws.api.pipe.*;
042: import com.sun.xml.ws.api.pipe.helper.AbstractTubeImpl;
043: import com.sun.istack.NotNull;
044:
045: import javax.xml.ws.BindingProvider;
046:
047: /**
048: * Proxy transport {@link Tube} and {@link Pipe} that lazily determines the
049: * actual transport pipe by looking at {@link Packet#endpointAddress}.
050: *
051: * <p>
052: * This pseudo transport is used when there's no statically known endpoint address,
053: * and thus it's expected that the application will configure {@link BindingProvider}
054: * at runtime before making invocation.
055: *
056: * <p>
057: * Since a typical application makes multiple invocations with the same endpoint
058: * address, this class implements a simple cache strategy to avoid re-creating
059: * transport pipes excessively.
060: *
061: * @author Kohsuke Kawaguchi
062: */
063: public final class DeferredTransportPipe extends AbstractTubeImpl {
064:
065: private Tube transport;
066: private EndpointAddress address;
067:
068: // parameter to TransportPipeFactory
069: private final ClassLoader classLoader;
070: private final ClientTubeAssemblerContext context;
071:
072: public DeferredTransportPipe(ClassLoader classLoader,
073: ClientPipeAssemblerContext context) {
074: this (classLoader, new ClientTubeAssemblerContext(context
075: .getAddress(), context.getWsdlModel(), context
076: .getService(), context.getBinding(), context
077: .getContainer()));
078: }
079:
080: public DeferredTransportPipe(ClassLoader classLoader,
081: ClientTubeAssemblerContext context) {
082: this .classLoader = classLoader;
083: this .context = context;
084: }
085:
086: public NextAction processException(@NotNull
087: Throwable t) {
088: return transport.processException(t);
089: }
090:
091: public NextAction processRequest(@NotNull
092: Packet request) {
093: if (request.endpointAddress == address)
094: // cache hit
095: return transport.processRequest(request);
096:
097: // cache miss
098:
099: if (transport != null) {
100: // delete the current entry
101: transport.preDestroy();
102: transport = null;
103: address = null;
104: }
105:
106: // otherwise find out what transport will process this.
107:
108: ClientTubeAssemblerContext newContext = new ClientTubeAssemblerContext(
109: request.endpointAddress, context.getWsdlModel(),
110: context.getService(), context.getBinding(), context
111: .getContainer(), context.getCodec().copy());
112:
113: address = request.endpointAddress;
114: transport = TransportTubeFactory
115: .create(classLoader, newContext);
116: // successful return from the above method indicates a successful pipe creation
117: assert transport != null;
118:
119: return transport.processRequest(request);
120: }
121:
122: public NextAction processResponse(@NotNull
123: Packet response) {
124: return transport.processResponse(response);
125: }
126:
127: public void preDestroy() {
128: if (transport != null) {
129: transport.preDestroy();
130: transport = null;
131: address = null;
132: }
133: }
134:
135: public DeferredTransportPipe copy(TubeCloner cloner) {
136: DeferredTransportPipe copy = new DeferredTransportPipe(
137: classLoader, context);
138: cloner.add(this , copy);
139:
140: // TODO Is the following thread-safe ??
141: // copied pipeline is still likely to work with the same endpoint address,
142: // so also copy the cached transport pipe, if any
143: if (transport != null) {
144: copy.transport = ((PipeCloner) cloner).copy(this.transport);
145: copy.address = this.address;
146: }
147:
148: return copy;
149: }
150: }
|