src/share/jaxws_classes/com/sun/xml/internal/ws/api/pipe/TransportTubeFactory.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/api/pipe/TransportTubeFactory.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,160 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.api.pipe;
    1.30 +
    1.31 +import com.sun.istack.internal.NotNull;
    1.32 +import com.sun.istack.internal.Nullable;
    1.33 +import com.sun.xml.internal.ws.api.EndpointAddress;
    1.34 +import com.sun.xml.internal.ws.api.pipe.helper.PipeAdapter;
    1.35 +import com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe;
    1.36 +import com.sun.xml.internal.ws.util.ServiceFinder;
    1.37 +import com.sun.xml.internal.ws.util.pipe.StandaloneTubeAssembler;
    1.38 +import java.util.logging.Level;
    1.39 +
    1.40 +import javax.xml.ws.WebServiceException;
    1.41 +import java.util.logging.Logger;
    1.42 +
    1.43 +/**
    1.44 + * Factory for transport tubes that enables transport pluggability.
    1.45 + *
    1.46 + * <p>
    1.47 + * At runtime, on the client side, JAX-WS (more specifically the default {@link TubelineAssembler}
    1.48 + * of JAX-WS client runtime) relies on this factory to create a suitable transport {@link Tube}
    1.49 + * that can handle the given {@link EndpointAddress endpoint address}.
    1.50 + *
    1.51 + * <p>
    1.52 + * JAX-WS extensions that provide additional transport support can
    1.53 + * extend this class and implement the {@link #doCreate} method.
    1.54 + * They are expected to check the scheme of the endpoint address
    1.55 + * (and possibly some other settings from bindings), and create
    1.56 + * their transport tube implementations accordingly.
    1.57 + * For example,
    1.58 + *
    1.59 + * <pre>
    1.60 + * class MyTransportTubeFactoryImpl {
    1.61 + *   Tube doCreate(...) {
    1.62 + *     String scheme = address.getURI().getScheme();
    1.63 + *     if(scheme.equals("foo"))
    1.64 + *       return new MyTransport(...);
    1.65 + *     else
    1.66 + *       return null;
    1.67 + *   }
    1.68 + * }
    1.69 + * </pre>
    1.70 + *
    1.71 + * <p>
    1.72 + * {@link TransportTubeFactory} look-up follows the standard service
    1.73 + * discovery mechanism, so you need
    1.74 + * {@code META-INF/services/com.sun.xml.internal.ws.api.pipe.BasicTransportTubeFactory}.
    1.75 + *
    1.76 + * @author Jitendra Kotamraju
    1.77 + * @see StandaloneTubeAssembler
    1.78 + */
    1.79 +public abstract class TransportTubeFactory {
    1.80 +    /**
    1.81 +     * Creates a transport {@link Tube} for the given port, if this factory can do so,
    1.82 +     * or return null.
    1.83 +     *
    1.84 +     * @param context
    1.85 +     *      Object that captures various contextual information
    1.86 +     *      that can be used to determine the tubeline to be assembled.
    1.87 +     *
    1.88 +     * @return
    1.89 +     *      null to indicate that this factory isn't capable of creating a transport
    1.90 +     *      for this port (which causes the caller to search for other {@link TransportTubeFactory}s
    1.91 +     *      that can. Or non-null.
    1.92 +     *
    1.93 +     * @throws WebServiceException
    1.94 +     *      if this factory is capable of creating a transport tube but some fatal
    1.95 +     *      error prevented it from doing so. This exception will be propagated
    1.96 +     *      back to the user application, and no further {@link TransportTubeFactory}s
    1.97 +     *      are consulted.
    1.98 +     */
    1.99 +    public abstract Tube doCreate(@NotNull ClientTubeAssemblerContext context);
   1.100 +
   1.101 +    private static final TransportTubeFactory DEFAULT = new DefaultTransportTubeFactory();
   1.102 +    private static class DefaultTransportTubeFactory extends TransportTubeFactory {
   1.103 +
   1.104 +                @Override
   1.105 +                public Tube doCreate(ClientTubeAssemblerContext context) {
   1.106 +                        return createDefault(context);
   1.107 +                }
   1.108 +
   1.109 +    }
   1.110 +
   1.111 +    /**
   1.112 +     * Locates {@link TransportTubeFactory}s and create a suitable transport {@link Tube}.
   1.113 +     *
   1.114 +     * @param classLoader
   1.115 +     *      used to locate {@code META-INF/servces} files.
   1.116 +     * @return
   1.117 +     *      Always non-null, since we fall back to our default {@link Tube}.
   1.118 +     */
   1.119 +    public static Tube create(@Nullable ClassLoader classLoader, @NotNull ClientTubeAssemblerContext context) {
   1.120 +        for (TransportTubeFactory factory : ServiceFinder.find(TransportTubeFactory.class,classLoader, context.getContainer())) {
   1.121 +            Tube tube = factory.doCreate(context);
   1.122 +            if (tube !=null) {
   1.123 +                if (logger.isLoggable(Level.FINE)) {
   1.124 +                    TransportTubeFactory.logger.log(Level.FINE, "{0} successfully created {1}", new Object[]{factory.getClass(), tube});
   1.125 +                }
   1.126 +                return tube;
   1.127 +            }
   1.128 +        }
   1.129 +
   1.130 +        // See if there is a {@link TransportPipeFactory} out there and use it for compatibility.
   1.131 +        ClientPipeAssemblerContext ctxt = new ClientPipeAssemblerContext(
   1.132 +                context.getAddress(), context.getWsdlModel(), context.getService(),
   1.133 +                context.getBinding(), context.getContainer());
   1.134 +        ctxt.setCodec(context.getCodec());
   1.135 +        for (TransportPipeFactory factory : ServiceFinder.find(TransportPipeFactory.class,classLoader)) {
   1.136 +            Pipe pipe = factory.doCreate(ctxt);
   1.137 +            if (pipe!=null) {
   1.138 +                if (logger.isLoggable(Level.FINE)) {
   1.139 +                    logger.log(Level.FINE, "{0} successfully created {1}", new Object[]{factory.getClass(), pipe});
   1.140 +                }
   1.141 +                return PipeAdapter.adapt(pipe);
   1.142 +            }
   1.143 +        }
   1.144 +
   1.145 +        return DEFAULT.createDefault(ctxt);
   1.146 +    }
   1.147 +
   1.148 +    protected Tube createDefault(ClientTubeAssemblerContext context) {
   1.149 +        // default built-in transports
   1.150 +        String scheme = context.getAddress().getURI().getScheme();
   1.151 +        if (scheme != null) {
   1.152 +            if(scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))
   1.153 +                return createHttpTransport(context);
   1.154 +        }
   1.155 +        throw new WebServiceException("Unsupported endpoint address: "+context.getAddress());    // TODO: i18n
   1.156 +    }
   1.157 +
   1.158 +    protected Tube createHttpTransport(ClientTubeAssemblerContext context) {
   1.159 +        return new HttpTransportPipe(context.getCodec(), context.getBinding());
   1.160 +    }
   1.161 +
   1.162 +    private static final Logger logger = Logger.getLogger(TransportTubeFactory.class.getName());
   1.163 +}

mercurial