Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.ws.api.pipe; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 29 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 30 | import com.sun.xml.internal.ws.api.EndpointAddress; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.api.pipe.helper.PipeAdapter; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.util.ServiceFinder; |
aoqi@0 | 34 | import com.sun.xml.internal.ws.util.pipe.StandaloneTubeAssembler; |
aoqi@0 | 35 | import java.util.logging.Level; |
aoqi@0 | 36 | |
aoqi@0 | 37 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 38 | import java.util.logging.Logger; |
aoqi@0 | 39 | |
aoqi@0 | 40 | /** |
aoqi@0 | 41 | * Factory for transport tubes that enables transport pluggability. |
aoqi@0 | 42 | * |
aoqi@0 | 43 | * <p> |
aoqi@0 | 44 | * At runtime, on the client side, JAX-WS (more specifically the default {@link TubelineAssembler} |
aoqi@0 | 45 | * of JAX-WS client runtime) relies on this factory to create a suitable transport {@link Tube} |
aoqi@0 | 46 | * that can handle the given {@link EndpointAddress endpoint address}. |
aoqi@0 | 47 | * |
aoqi@0 | 48 | * <p> |
aoqi@0 | 49 | * JAX-WS extensions that provide additional transport support can |
aoqi@0 | 50 | * extend this class and implement the {@link #doCreate} method. |
aoqi@0 | 51 | * They are expected to check the scheme of the endpoint address |
aoqi@0 | 52 | * (and possibly some other settings from bindings), and create |
aoqi@0 | 53 | * their transport tube implementations accordingly. |
aoqi@0 | 54 | * For example, |
aoqi@0 | 55 | * |
aoqi@0 | 56 | * <pre> |
aoqi@0 | 57 | * class MyTransportTubeFactoryImpl { |
aoqi@0 | 58 | * Tube doCreate(...) { |
aoqi@0 | 59 | * String scheme = address.getURI().getScheme(); |
aoqi@0 | 60 | * if(scheme.equals("foo")) |
aoqi@0 | 61 | * return new MyTransport(...); |
aoqi@0 | 62 | * else |
aoqi@0 | 63 | * return null; |
aoqi@0 | 64 | * } |
aoqi@0 | 65 | * } |
aoqi@0 | 66 | * </pre> |
aoqi@0 | 67 | * |
aoqi@0 | 68 | * <p> |
aoqi@0 | 69 | * {@link TransportTubeFactory} look-up follows the standard service |
aoqi@0 | 70 | * discovery mechanism, so you need |
aoqi@0 | 71 | * {@code META-INF/services/com.sun.xml.internal.ws.api.pipe.BasicTransportTubeFactory}. |
aoqi@0 | 72 | * |
aoqi@0 | 73 | * @author Jitendra Kotamraju |
aoqi@0 | 74 | * @see StandaloneTubeAssembler |
aoqi@0 | 75 | */ |
aoqi@0 | 76 | public abstract class TransportTubeFactory { |
aoqi@0 | 77 | /** |
aoqi@0 | 78 | * Creates a transport {@link Tube} for the given port, if this factory can do so, |
aoqi@0 | 79 | * or return null. |
aoqi@0 | 80 | * |
aoqi@0 | 81 | * @param context |
aoqi@0 | 82 | * Object that captures various contextual information |
aoqi@0 | 83 | * that can be used to determine the tubeline to be assembled. |
aoqi@0 | 84 | * |
aoqi@0 | 85 | * @return |
aoqi@0 | 86 | * null to indicate that this factory isn't capable of creating a transport |
aoqi@0 | 87 | * for this port (which causes the caller to search for other {@link TransportTubeFactory}s |
aoqi@0 | 88 | * that can. Or non-null. |
aoqi@0 | 89 | * |
aoqi@0 | 90 | * @throws WebServiceException |
aoqi@0 | 91 | * if this factory is capable of creating a transport tube but some fatal |
aoqi@0 | 92 | * error prevented it from doing so. This exception will be propagated |
aoqi@0 | 93 | * back to the user application, and no further {@link TransportTubeFactory}s |
aoqi@0 | 94 | * are consulted. |
aoqi@0 | 95 | */ |
aoqi@0 | 96 | public abstract Tube doCreate(@NotNull ClientTubeAssemblerContext context); |
aoqi@0 | 97 | |
aoqi@0 | 98 | private static final TransportTubeFactory DEFAULT = new DefaultTransportTubeFactory(); |
aoqi@0 | 99 | private static class DefaultTransportTubeFactory extends TransportTubeFactory { |
aoqi@0 | 100 | |
aoqi@0 | 101 | @Override |
aoqi@0 | 102 | public Tube doCreate(ClientTubeAssemblerContext context) { |
aoqi@0 | 103 | return createDefault(context); |
aoqi@0 | 104 | } |
aoqi@0 | 105 | |
aoqi@0 | 106 | } |
aoqi@0 | 107 | |
aoqi@0 | 108 | /** |
aoqi@0 | 109 | * Locates {@link TransportTubeFactory}s and create a suitable transport {@link Tube}. |
aoqi@0 | 110 | * |
aoqi@0 | 111 | * @param classLoader |
aoqi@0 | 112 | * used to locate {@code META-INF/servces} files. |
aoqi@0 | 113 | * @return |
aoqi@0 | 114 | * Always non-null, since we fall back to our default {@link Tube}. |
aoqi@0 | 115 | */ |
aoqi@0 | 116 | public static Tube create(@Nullable ClassLoader classLoader, @NotNull ClientTubeAssemblerContext context) { |
aoqi@0 | 117 | for (TransportTubeFactory factory : ServiceFinder.find(TransportTubeFactory.class,classLoader, context.getContainer())) { |
aoqi@0 | 118 | Tube tube = factory.doCreate(context); |
aoqi@0 | 119 | if (tube !=null) { |
aoqi@0 | 120 | if (logger.isLoggable(Level.FINE)) { |
aoqi@0 | 121 | TransportTubeFactory.logger.log(Level.FINE, "{0} successfully created {1}", new Object[]{factory.getClass(), tube}); |
aoqi@0 | 122 | } |
aoqi@0 | 123 | return tube; |
aoqi@0 | 124 | } |
aoqi@0 | 125 | } |
aoqi@0 | 126 | |
aoqi@0 | 127 | // See if there is a {@link TransportPipeFactory} out there and use it for compatibility. |
aoqi@0 | 128 | ClientPipeAssemblerContext ctxt = new ClientPipeAssemblerContext( |
aoqi@0 | 129 | context.getAddress(), context.getWsdlModel(), context.getService(), |
aoqi@0 | 130 | context.getBinding(), context.getContainer()); |
aoqi@0 | 131 | ctxt.setCodec(context.getCodec()); |
aoqi@0 | 132 | for (TransportPipeFactory factory : ServiceFinder.find(TransportPipeFactory.class,classLoader)) { |
aoqi@0 | 133 | Pipe pipe = factory.doCreate(ctxt); |
aoqi@0 | 134 | if (pipe!=null) { |
aoqi@0 | 135 | if (logger.isLoggable(Level.FINE)) { |
aoqi@0 | 136 | logger.log(Level.FINE, "{0} successfully created {1}", new Object[]{factory.getClass(), pipe}); |
aoqi@0 | 137 | } |
aoqi@0 | 138 | return PipeAdapter.adapt(pipe); |
aoqi@0 | 139 | } |
aoqi@0 | 140 | } |
aoqi@0 | 141 | |
aoqi@0 | 142 | return DEFAULT.createDefault(ctxt); |
aoqi@0 | 143 | } |
aoqi@0 | 144 | |
aoqi@0 | 145 | protected Tube createDefault(ClientTubeAssemblerContext context) { |
aoqi@0 | 146 | // default built-in transports |
aoqi@0 | 147 | String scheme = context.getAddress().getURI().getScheme(); |
aoqi@0 | 148 | if (scheme != null) { |
aoqi@0 | 149 | if(scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https")) |
aoqi@0 | 150 | return createHttpTransport(context); |
aoqi@0 | 151 | } |
aoqi@0 | 152 | throw new WebServiceException("Unsupported endpoint address: "+context.getAddress()); // TODO: i18n |
aoqi@0 | 153 | } |
aoqi@0 | 154 | |
aoqi@0 | 155 | protected Tube createHttpTransport(ClientTubeAssemblerContext context) { |
aoqi@0 | 156 | return new HttpTransportPipe(context.getCodec(), context.getBinding()); |
aoqi@0 | 157 | } |
aoqi@0 | 158 | |
aoqi@0 | 159 | private static final Logger logger = Logger.getLogger(TransportTubeFactory.class.getName()); |
aoqi@0 | 160 | } |