All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.xml.ws.api.EndpointAddress Maven / Gradle / Ivy

There is a newer version: 4.0.2
Show newest version
/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License).  You may not use this file except in
 * compliance with the License.
 * 
 * You can obtain a copy of the license at
 * https://glassfish.dev.java.net/public/CDDLv1.0.html.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * you own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * 
 * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
 */

package com.sun.xml.ws.api;


import com.sun.istack.Nullable;

import javax.xml.ws.WebServiceException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.Iterator;

/**
 * Represents the endpoint address URI.
 *
 * 

* Conceptually this can be really thought of as an {@link URI}, * but it hides some of the details that improve the performance. * *

* Being an {@link URI} allows this class to represent custom made-up URIs * (like "jms" for example.) Whenever possible, this object * also creates an {@link URL} (this is only possible when the address * has a registered {@link URLStreamHandler}), so that if the clients * of this code wants to use it, it can do so. * * *

How it improves the performance

*
    *
  1. * Endpoint address is often eventually turned into an {@link URLConnection}, * and given that generally this value is read more often than being set, * it makes sense to eagerly turn it into an {@link URL}, * thereby avoiding a repeated conversion. * *
  2. * JDK spends a lot of time choosing a list of {@link Proxy} * to connect to an {@link URL}. Since the default proxy selector * implementation always return the same proxy for the same URL, * we can determine the proxy by ourselves to let JDK skip its * proxy-discovery step. * * (That said, user-defined proxy selector can do a lot of interesting things * --- like doing a round-robin, or pick one from a proxy farm randomly, * and so it's dangerous to stick to one proxy. For this case, * we still let JDK decide the proxy. This shouldn't be that much of an * disappointment, since most people only mess with system properties, * and never with {@link ProxySelector}. Also, avoiding optimization * with non-standard proxy selector allows people to effectively disable * this optimization, which may come in handy for a trouble-shooting.) *
* * @author Kohsuke Kawaguchi */ public final class EndpointAddress { @Nullable private URL url; private final URI uri; private final String stringForm; /** * Pre-selected proxy. * * If {@link #url} is null, this field is null. * Otherwise, this field could still be null if the proxy couldn't be chosen * upfront. */ private Proxy proxy; public EndpointAddress(URI uri) { this.uri = uri; this.stringForm = uri.toString(); try { this.url = uri.toURL(); proxy = chooseProxy(); } catch (MalformedURLException e) { // ignore } } /** * * @see #create(String) */ public EndpointAddress(String url) throws URISyntaxException { this.uri = new URI(url); this.stringForm = url; try { this.url = new URL(url); proxy = chooseProxy(); } catch (MalformedURLException e) { // ignore } } /** * Creates a new {@link EndpointAddress} with a reasonably * generic error handling. */ public static EndpointAddress create(String url) { try { return new EndpointAddress(url); } catch(URISyntaxException e) { throw new WebServiceException("Illegal endpoint address: "+url,e); } } private Proxy chooseProxy() { ProxySelector sel = java.security.AccessController.doPrivileged( new java.security.PrivilegedAction() { public ProxySelector run() { return ProxySelector.getDefault(); } }); if(sel==null) return Proxy.NO_PROXY; if(!sel.getClass().getName().equals("sun.net.spi.DefaultProxySelector")) // user-defined proxy. may return a different proxy for each invocation return null; Iterator it = sel.select(uri).iterator(); if(it.hasNext()) return it.next(); return Proxy.NO_PROXY; } /** * Returns an URL of this endpoint adress. * * @return * null if this endpoint address doesn't have a registered {@link URLStreamHandler}. */ public URL getURL() { return url; } /** * Returns an URI of the endpoint address. * * @return * always non-null. */ public URI getURI() { return uri; } /** * Tries to open {@link URLConnection} for this endpoint. * *

* This is possible only when an endpoint address has * the corresponding {@link URLStreamHandler}. * * @throws IOException * if {@link URL#openConnection()} reports an error. * @throws AssertionError * if this endpoint doesn't have an associated URL. * if the code is written correctly this shall never happen. */ public URLConnection openConnection() throws IOException { assert url!=null : uri+" doesn't have the corresponding URL"; if(proxy!=null) return url.openConnection(proxy); else return url.openConnection(); } public String toString() { return stringForm; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy