Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, 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; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 31 | import java.io.IOException; |
aoqi@0 | 32 | import java.net.MalformedURLException; |
aoqi@0 | 33 | import java.net.Proxy; |
aoqi@0 | 34 | import java.net.ProxySelector; |
aoqi@0 | 35 | import java.net.URI; |
aoqi@0 | 36 | import java.net.URISyntaxException; |
aoqi@0 | 37 | import java.net.URL; |
aoqi@0 | 38 | import java.net.URLConnection; |
aoqi@0 | 39 | import java.net.URLStreamHandler; |
aoqi@0 | 40 | import java.util.Iterator; |
aoqi@0 | 41 | |
aoqi@0 | 42 | /** |
aoqi@0 | 43 | * Represents the endpoint address URI. |
aoqi@0 | 44 | * |
aoqi@0 | 45 | * <p> |
aoqi@0 | 46 | * Conceptually this can be really thought of as an {@link URI}, |
aoqi@0 | 47 | * but it hides some of the details that improve the performance. |
aoqi@0 | 48 | * |
aoqi@0 | 49 | * <p> |
aoqi@0 | 50 | * Being an {@link URI} allows this class to represent custom made-up URIs |
aoqi@0 | 51 | * (like "jms" for example.) Whenever possible, this object |
aoqi@0 | 52 | * also creates an {@link URL} (this is only possible when the address |
aoqi@0 | 53 | * has a registered {@link URLStreamHandler}), so that if the clients |
aoqi@0 | 54 | * of this code wants to use it, it can do so. |
aoqi@0 | 55 | * |
aoqi@0 | 56 | * |
aoqi@0 | 57 | * <h3>How it improves the performance</h3> |
aoqi@0 | 58 | * <ol> |
aoqi@0 | 59 | * <li> |
aoqi@0 | 60 | * Endpoint address is often eventually turned into an {@link URLConnection}, |
aoqi@0 | 61 | * and given that generally this value is read more often than being set, |
aoqi@0 | 62 | * it makes sense to eagerly turn it into an {@link URL}, |
aoqi@0 | 63 | * thereby avoiding a repeated conversion. |
aoqi@0 | 64 | * |
aoqi@0 | 65 | * <li> |
aoqi@0 | 66 | * JDK spends a lot of time choosing a list of {@link Proxy} |
aoqi@0 | 67 | * to connect to an {@link URL}. Since the default proxy selector |
aoqi@0 | 68 | * implementation always return the same proxy for the same URL, |
aoqi@0 | 69 | * we can determine the proxy by ourselves to let JDK skip its |
aoqi@0 | 70 | * proxy-discovery step. |
aoqi@0 | 71 | * |
aoqi@0 | 72 | * (That said, user-defined proxy selector can do a lot of interesting things |
aoqi@0 | 73 | * --- like doing a round-robin, or pick one from a proxy farm randomly, |
aoqi@0 | 74 | * and so it's dangerous to stick to one proxy. For this case, |
aoqi@0 | 75 | * we still let JDK decide the proxy. This shouldn't be that much of an |
aoqi@0 | 76 | * disappointment, since most people only mess with system properties, |
aoqi@0 | 77 | * and never with {@link ProxySelector}. Also, avoiding optimization |
aoqi@0 | 78 | * with non-standard proxy selector allows people to effectively disable |
aoqi@0 | 79 | * this optimization, which may come in handy for a trouble-shooting.) |
aoqi@0 | 80 | * </ol> |
aoqi@0 | 81 | * |
aoqi@0 | 82 | * @author Kohsuke Kawaguchi |
aoqi@0 | 83 | */ |
aoqi@0 | 84 | public final class EndpointAddress { |
aoqi@0 | 85 | @Nullable |
aoqi@0 | 86 | private URL url; |
aoqi@0 | 87 | private final URI uri; |
aoqi@0 | 88 | private final String stringForm; |
aoqi@0 | 89 | private volatile boolean dontUseProxyMethod; |
aoqi@0 | 90 | /** |
aoqi@0 | 91 | * Pre-selected proxy. |
aoqi@0 | 92 | * |
aoqi@0 | 93 | * If {@link #url} is null, this field is null. |
aoqi@0 | 94 | * Otherwise, this field could still be null if the proxy couldn't be chosen |
aoqi@0 | 95 | * upfront. |
aoqi@0 | 96 | */ |
aoqi@0 | 97 | private Proxy proxy; |
aoqi@0 | 98 | |
aoqi@0 | 99 | public EndpointAddress(URI uri) { |
aoqi@0 | 100 | this.uri = uri; |
aoqi@0 | 101 | this.stringForm = uri.toString(); |
aoqi@0 | 102 | try { |
aoqi@0 | 103 | initURL(); |
aoqi@0 | 104 | proxy = chooseProxy(); |
aoqi@0 | 105 | } catch (MalformedURLException e) { |
aoqi@0 | 106 | // ignore |
aoqi@0 | 107 | } |
aoqi@0 | 108 | } |
aoqi@0 | 109 | |
aoqi@0 | 110 | /** |
aoqi@0 | 111 | * |
aoqi@0 | 112 | * @see #create(String) |
aoqi@0 | 113 | */ |
aoqi@0 | 114 | public EndpointAddress(String url) throws URISyntaxException { |
aoqi@0 | 115 | this.uri = new URI(url); |
aoqi@0 | 116 | this.stringForm = url; |
aoqi@0 | 117 | try { |
aoqi@0 | 118 | initURL(); |
aoqi@0 | 119 | proxy = chooseProxy(); |
aoqi@0 | 120 | } catch (MalformedURLException e) { |
aoqi@0 | 121 | // ignore |
aoqi@0 | 122 | } |
aoqi@0 | 123 | } |
aoqi@0 | 124 | |
aoqi@0 | 125 | |
aoqi@0 | 126 | private void initURL() throws MalformedURLException { |
aoqi@0 | 127 | String scheme = uri.getScheme(); |
aoqi@0 | 128 | //URI.toURL() only works when scheme is not null. |
aoqi@0 | 129 | if (scheme == null) { |
aoqi@0 | 130 | this.url = new URL(uri.toString()); |
aoqi@0 | 131 | return; |
aoqi@0 | 132 | } |
aoqi@0 | 133 | scheme =scheme.toLowerCase(); |
aoqi@0 | 134 | if ("http".equals(scheme) || "https".equals(scheme)) { |
aoqi@0 | 135 | url = new URL(uri.toASCIIString()); |
aoqi@0 | 136 | } else { |
aoqi@0 | 137 | this.url = uri.toURL(); |
aoqi@0 | 138 | } |
aoqi@0 | 139 | } |
aoqi@0 | 140 | |
aoqi@0 | 141 | /** |
aoqi@0 | 142 | * Creates a new {@link EndpointAddress} with a reasonably |
aoqi@0 | 143 | * generic error handling. |
aoqi@0 | 144 | */ |
aoqi@0 | 145 | public static EndpointAddress create(String url) { |
aoqi@0 | 146 | try { |
aoqi@0 | 147 | return new EndpointAddress(url); |
aoqi@0 | 148 | } catch(URISyntaxException e) { |
aoqi@0 | 149 | throw new WebServiceException("Illegal endpoint address: "+url,e); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | } |
aoqi@0 | 152 | |
aoqi@0 | 153 | private Proxy chooseProxy() { |
aoqi@0 | 154 | ProxySelector sel = |
aoqi@0 | 155 | java.security.AccessController.doPrivileged( |
aoqi@0 | 156 | new java.security.PrivilegedAction<ProxySelector>() { |
aoqi@0 | 157 | @Override |
aoqi@0 | 158 | public ProxySelector run() { |
aoqi@0 | 159 | return ProxySelector.getDefault(); |
aoqi@0 | 160 | } |
aoqi@0 | 161 | }); |
aoqi@0 | 162 | |
aoqi@0 | 163 | if(sel==null) |
aoqi@0 | 164 | return Proxy.NO_PROXY; |
aoqi@0 | 165 | |
aoqi@0 | 166 | |
aoqi@0 | 167 | if(!sel.getClass().getName().equals("sun.net.spi.DefaultProxySelector")) |
aoqi@0 | 168 | // user-defined proxy. may return a different proxy for each invocation |
aoqi@0 | 169 | return null; |
aoqi@0 | 170 | |
aoqi@0 | 171 | Iterator<Proxy> it = sel.select(uri).iterator(); |
aoqi@0 | 172 | if(it.hasNext()) |
aoqi@0 | 173 | return it.next(); |
aoqi@0 | 174 | |
aoqi@0 | 175 | return Proxy.NO_PROXY; |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | /** |
aoqi@0 | 179 | * Returns an URL of this endpoint adress. |
aoqi@0 | 180 | * |
aoqi@0 | 181 | * @return |
aoqi@0 | 182 | * null if this endpoint address doesn't have a registered {@link URLStreamHandler}. |
aoqi@0 | 183 | */ |
aoqi@0 | 184 | public URL getURL() { |
aoqi@0 | 185 | return url; |
aoqi@0 | 186 | } |
aoqi@0 | 187 | |
aoqi@0 | 188 | /** |
aoqi@0 | 189 | * Returns an URI of the endpoint address. |
aoqi@0 | 190 | * |
aoqi@0 | 191 | * @return |
aoqi@0 | 192 | * always non-null. |
aoqi@0 | 193 | */ |
aoqi@0 | 194 | public URI getURI() { |
aoqi@0 | 195 | return uri; |
aoqi@0 | 196 | } |
aoqi@0 | 197 | |
aoqi@0 | 198 | /** |
aoqi@0 | 199 | * Tries to open {@link URLConnection} for this endpoint. |
aoqi@0 | 200 | * |
aoqi@0 | 201 | * <p> |
aoqi@0 | 202 | * This is possible only when an endpoint address has |
aoqi@0 | 203 | * the corresponding {@link URLStreamHandler}. |
aoqi@0 | 204 | * |
aoqi@0 | 205 | * @throws IOException |
aoqi@0 | 206 | * if {@link URL#openConnection()} reports an error. |
aoqi@0 | 207 | * @throws AssertionError |
aoqi@0 | 208 | * if this endpoint doesn't have an associated URL. |
aoqi@0 | 209 | * if the code is written correctly this shall never happen. |
aoqi@0 | 210 | */ |
aoqi@0 | 211 | public URLConnection openConnection() throws IOException { |
aoqi@0 | 212 | if (url == null) { |
aoqi@0 | 213 | throw new WebServiceException("URI="+uri+" doesn't have the corresponding URL"); |
aoqi@0 | 214 | } |
aoqi@0 | 215 | if(proxy!=null && !dontUseProxyMethod) { |
aoqi@0 | 216 | try { |
aoqi@0 | 217 | return url.openConnection(proxy); |
aoqi@0 | 218 | } catch(UnsupportedOperationException e) { |
aoqi@0 | 219 | // Some OSGi and app server environments donot |
aoqi@0 | 220 | // override URLStreamHandler.openConnection(URL, Proxy) as it |
aoqi@0 | 221 | // is introduced in Java SE 5 API. Fallback to the other method. |
aoqi@0 | 222 | dontUseProxyMethod = true; |
aoqi@0 | 223 | } |
aoqi@0 | 224 | } |
aoqi@0 | 225 | return url.openConnection(); |
aoqi@0 | 226 | } |
aoqi@0 | 227 | |
aoqi@0 | 228 | @Override |
aoqi@0 | 229 | public String toString() { |
aoqi@0 | 230 | return stringForm; |
aoqi@0 | 231 | } |
aoqi@0 | 232 | } |