Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
ohair@286 | 1 | /* |
ohair@286 | 2 | * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.ws.api; |
ohair@286 | 27 | |
ohair@286 | 28 | |
ohair@286 | 29 | import com.sun.istack.internal.Nullable; |
ohair@286 | 30 | |
ohair@286 | 31 | import javax.xml.ws.WebServiceException; |
ohair@286 | 32 | import java.io.IOException; |
ohair@286 | 33 | import java.net.MalformedURLException; |
ohair@286 | 34 | import java.net.Proxy; |
ohair@286 | 35 | import java.net.ProxySelector; |
ohair@286 | 36 | import java.net.URI; |
ohair@286 | 37 | import java.net.URISyntaxException; |
ohair@286 | 38 | import java.net.URL; |
ohair@286 | 39 | import java.net.URLConnection; |
ohair@286 | 40 | import java.net.URLStreamHandler; |
ohair@286 | 41 | import java.util.Iterator; |
ohair@286 | 42 | |
ohair@286 | 43 | /** |
ohair@286 | 44 | * Represents the endpoint address URI. |
ohair@286 | 45 | * |
ohair@286 | 46 | * <p> |
ohair@286 | 47 | * Conceptually this can be really thought of as an {@link URI}, |
ohair@286 | 48 | * but it hides some of the details that improve the performance. |
ohair@286 | 49 | * |
ohair@286 | 50 | * <p> |
ohair@286 | 51 | * Being an {@link URI} allows this class to represent custom made-up URIs |
ohair@286 | 52 | * (like "jms" for example.) Whenever possible, this object |
ohair@286 | 53 | * also creates an {@link URL} (this is only possible when the address |
ohair@286 | 54 | * has a registered {@link URLStreamHandler}), so that if the clients |
ohair@286 | 55 | * of this code wants to use it, it can do so. |
ohair@286 | 56 | * |
ohair@286 | 57 | * |
ohair@286 | 58 | * <h3>How it improves the performance</h3> |
ohair@286 | 59 | * <ol> |
ohair@286 | 60 | * <li> |
ohair@286 | 61 | * Endpoint address is often eventually turned into an {@link URLConnection}, |
ohair@286 | 62 | * and given that generally this value is read more often than being set, |
ohair@286 | 63 | * it makes sense to eagerly turn it into an {@link URL}, |
ohair@286 | 64 | * thereby avoiding a repeated conversion. |
ohair@286 | 65 | * |
ohair@286 | 66 | * <li> |
ohair@286 | 67 | * JDK spends a lot of time choosing a list of {@link Proxy} |
ohair@286 | 68 | * to connect to an {@link URL}. Since the default proxy selector |
ohair@286 | 69 | * implementation always return the same proxy for the same URL, |
ohair@286 | 70 | * we can determine the proxy by ourselves to let JDK skip its |
ohair@286 | 71 | * proxy-discovery step. |
ohair@286 | 72 | * |
ohair@286 | 73 | * (That said, user-defined proxy selector can do a lot of interesting things |
ohair@286 | 74 | * --- like doing a round-robin, or pick one from a proxy farm randomly, |
ohair@286 | 75 | * and so it's dangerous to stick to one proxy. For this case, |
ohair@286 | 76 | * we still let JDK decide the proxy. This shouldn't be that much of an |
ohair@286 | 77 | * disappointment, since most people only mess with system properties, |
ohair@286 | 78 | * and never with {@link ProxySelector}. Also, avoiding optimization |
ohair@286 | 79 | * with non-standard proxy selector allows people to effectively disable |
ohair@286 | 80 | * this optimization, which may come in handy for a trouble-shooting.) |
ohair@286 | 81 | * </ol> |
ohair@286 | 82 | * |
ohair@286 | 83 | * @author Kohsuke Kawaguchi |
ohair@286 | 84 | */ |
ohair@286 | 85 | public final class EndpointAddress { |
ohair@286 | 86 | @Nullable |
ohair@286 | 87 | private URL url; |
ohair@286 | 88 | private final URI uri; |
ohair@286 | 89 | private final String stringForm; |
ohair@286 | 90 | private volatile boolean dontUseProxyMethod; |
ohair@286 | 91 | /** |
ohair@286 | 92 | * Pre-selected proxy. |
ohair@286 | 93 | * |
ohair@286 | 94 | * If {@link #url} is null, this field is null. |
ohair@286 | 95 | * Otherwise, this field could still be null if the proxy couldn't be chosen |
ohair@286 | 96 | * upfront. |
ohair@286 | 97 | */ |
ohair@286 | 98 | private Proxy proxy; |
ohair@286 | 99 | |
ohair@286 | 100 | public EndpointAddress(URI uri) { |
ohair@286 | 101 | this.uri = uri; |
ohair@286 | 102 | this.stringForm = uri.toString(); |
ohair@286 | 103 | try { |
ohair@286 | 104 | initURL(); |
ohair@286 | 105 | proxy = chooseProxy(); |
ohair@286 | 106 | } catch (MalformedURLException e) { |
ohair@286 | 107 | // ignore |
ohair@286 | 108 | } |
ohair@286 | 109 | } |
ohair@286 | 110 | |
ohair@286 | 111 | /** |
ohair@286 | 112 | * |
ohair@286 | 113 | * @see #create(String) |
ohair@286 | 114 | */ |
ohair@286 | 115 | public EndpointAddress(String url) throws URISyntaxException { |
ohair@286 | 116 | this.uri = new URI(url); |
ohair@286 | 117 | this.stringForm = url; |
ohair@286 | 118 | try { |
ohair@286 | 119 | initURL(); |
ohair@286 | 120 | proxy = chooseProxy(); |
ohair@286 | 121 | } catch (MalformedURLException e) { |
ohair@286 | 122 | // ignore |
ohair@286 | 123 | } |
ohair@286 | 124 | } |
ohair@286 | 125 | |
ohair@286 | 126 | |
ohair@286 | 127 | private void initURL() throws MalformedURLException { |
ohair@286 | 128 | String scheme = uri.getScheme(); |
ohair@286 | 129 | //URI.toURL() only works when scheme is not null. |
ohair@286 | 130 | if (scheme == null) { |
ohair@286 | 131 | this.url = new URL(uri.toString()); |
ohair@286 | 132 | return; |
ohair@286 | 133 | } |
ohair@286 | 134 | scheme =scheme.toLowerCase(); |
ohair@286 | 135 | if ("http".equals(scheme) || "https".equals(scheme)) { |
ohair@286 | 136 | url = new URL(uri.toASCIIString()); |
ohair@286 | 137 | } else { |
ohair@286 | 138 | this.url = uri.toURL(); |
ohair@286 | 139 | } |
ohair@286 | 140 | } |
ohair@286 | 141 | |
ohair@286 | 142 | /** |
ohair@286 | 143 | * Creates a new {@link EndpointAddress} with a reasonably |
ohair@286 | 144 | * generic error handling. |
ohair@286 | 145 | */ |
ohair@286 | 146 | public static EndpointAddress create(String url) { |
ohair@286 | 147 | try { |
ohair@286 | 148 | return new EndpointAddress(url); |
ohair@286 | 149 | } catch(URISyntaxException e) { |
ohair@286 | 150 | throw new WebServiceException("Illegal endpoint address: "+url,e); |
ohair@286 | 151 | } |
ohair@286 | 152 | } |
ohair@286 | 153 | |
ohair@286 | 154 | private Proxy chooseProxy() { |
ohair@286 | 155 | ProxySelector sel = |
ohair@286 | 156 | java.security.AccessController.doPrivileged( |
ohair@286 | 157 | new java.security.PrivilegedAction<ProxySelector>() { |
ohair@286 | 158 | public ProxySelector run() { |
ohair@286 | 159 | return ProxySelector.getDefault(); |
ohair@286 | 160 | } |
ohair@286 | 161 | }); |
ohair@286 | 162 | |
ohair@286 | 163 | if(sel==null) |
ohair@286 | 164 | return Proxy.NO_PROXY; |
ohair@286 | 165 | |
ohair@286 | 166 | |
ohair@286 | 167 | if(!sel.getClass().getName().equals("sun.net.spi.DefaultProxySelector")) |
ohair@286 | 168 | // user-defined proxy. may return a different proxy for each invocation |
ohair@286 | 169 | return null; |
ohair@286 | 170 | |
ohair@286 | 171 | Iterator<Proxy> it = sel.select(uri).iterator(); |
ohair@286 | 172 | if(it.hasNext()) |
ohair@286 | 173 | return it.next(); |
ohair@286 | 174 | |
ohair@286 | 175 | return Proxy.NO_PROXY; |
ohair@286 | 176 | } |
ohair@286 | 177 | |
ohair@286 | 178 | /** |
ohair@286 | 179 | * Returns an URL of this endpoint adress. |
ohair@286 | 180 | * |
ohair@286 | 181 | * @return |
ohair@286 | 182 | * null if this endpoint address doesn't have a registered {@link URLStreamHandler}. |
ohair@286 | 183 | */ |
ohair@286 | 184 | public URL getURL() { |
ohair@286 | 185 | return url; |
ohair@286 | 186 | } |
ohair@286 | 187 | |
ohair@286 | 188 | /** |
ohair@286 | 189 | * Returns an URI of the endpoint address. |
ohair@286 | 190 | * |
ohair@286 | 191 | * @return |
ohair@286 | 192 | * always non-null. |
ohair@286 | 193 | */ |
ohair@286 | 194 | public URI getURI() { |
ohair@286 | 195 | return uri; |
ohair@286 | 196 | } |
ohair@286 | 197 | |
ohair@286 | 198 | /** |
ohair@286 | 199 | * Tries to open {@link URLConnection} for this endpoint. |
ohair@286 | 200 | * |
ohair@286 | 201 | * <p> |
ohair@286 | 202 | * This is possible only when an endpoint address has |
ohair@286 | 203 | * the corresponding {@link URLStreamHandler}. |
ohair@286 | 204 | * |
ohair@286 | 205 | * @throws IOException |
ohair@286 | 206 | * if {@link URL#openConnection()} reports an error. |
ohair@286 | 207 | * @throws AssertionError |
ohair@286 | 208 | * if this endpoint doesn't have an associated URL. |
ohair@286 | 209 | * if the code is written correctly this shall never happen. |
ohair@286 | 210 | */ |
ohair@286 | 211 | public URLConnection openConnection() throws IOException { |
ohair@286 | 212 | assert url!=null : uri+" doesn't have the corresponding URL"; |
ohair@286 | 213 | if (url == null) { |
ohair@286 | 214 | throw new WebServiceException("URI="+uri+" doesn't have the corresponding URL"); |
ohair@286 | 215 | } |
ohair@286 | 216 | if(proxy!=null && !dontUseProxyMethod) { |
ohair@286 | 217 | try { |
ohair@286 | 218 | return url.openConnection(proxy); |
ohair@286 | 219 | } catch(UnsupportedOperationException e) { |
ohair@286 | 220 | // Some OSGi and app server environments donot |
ohair@286 | 221 | // override URLStreamHandler.openConnection(URL, Proxy) as it |
ohair@286 | 222 | // is introduced in Java SE 5 API. Fallback to the other method. |
ohair@286 | 223 | dontUseProxyMethod = true; |
ohair@286 | 224 | } |
ohair@286 | 225 | } |
ohair@286 | 226 | return url.openConnection(); |
ohair@286 | 227 | } |
ohair@286 | 228 | |
ohair@286 | 229 | public String toString() { |
ohair@286 | 230 | return stringForm; |
ohair@286 | 231 | } |
ohair@286 | 232 | } |