src/share/jaxws_classes/com/sun/xml/internal/ws/transport/http/server/HttpEndpoint.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/transport/http/server/HttpEndpoint.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,135 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, 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.transport.http.server;
    1.30 +
    1.31 +import com.sun.net.httpserver.HttpContext;
    1.32 +import com.sun.xml.internal.ws.transport.http.HttpAdapter;
    1.33 +import com.sun.xml.internal.ws.transport.http.HttpAdapterList;
    1.34 +import com.sun.xml.internal.ws.server.ServerRtException;
    1.35 +import com.sun.xml.internal.ws.server.WSEndpointImpl;
    1.36 +import com.sun.xml.internal.ws.resources.ServerMessages;
    1.37 +
    1.38 +import javax.xml.ws.EndpointReference;
    1.39 +import java.util.concurrent.Executor;
    1.40 +import java.net.MalformedURLException;
    1.41 +import java.net.URL;
    1.42 +
    1.43 +import org.w3c.dom.Element;
    1.44 +
    1.45 +/**
    1.46 + * Hides {@link HttpContext} so that {@link EndpointImpl}
    1.47 + * may load even without {@link HttpContext}.
    1.48 + *
    1.49 + * TODO: But what's the point? If Light-weight HTTP server isn't present,
    1.50 + * all the publish operations will fail way. Why is it better to defer
    1.51 + * the failure, as opposed to cause the failure as earyl as possible? -KK
    1.52 + *
    1.53 + * @author Jitendra Kotamraju
    1.54 + */
    1.55 +public final class HttpEndpoint extends com.sun.xml.internal.ws.api.server.HttpEndpoint {
    1.56 +    private String address;
    1.57 +    private HttpContext httpContext;
    1.58 +    private final HttpAdapter adapter;
    1.59 +    private final Executor executor;
    1.60 +
    1.61 +    public HttpEndpoint(Executor executor, HttpAdapter adapter) {
    1.62 +        this.executor = executor;
    1.63 +        this.adapter = adapter;
    1.64 +    }
    1.65 +
    1.66 +    public void publish(String address) {
    1.67 +        this.address = address;
    1.68 +        httpContext = ServerMgr.getInstance().createContext(address);
    1.69 +        publish(httpContext);
    1.70 +    }
    1.71 +
    1.72 +    public void publish(Object serverContext) {
    1.73 +        if (serverContext instanceof javax.xml.ws.spi.http.HttpContext) {
    1.74 +            setHandler((javax.xml.ws.spi.http.HttpContext)serverContext);
    1.75 +            return;
    1.76 +        }
    1.77 +        if (serverContext instanceof HttpContext) {
    1.78 +            this.httpContext = (HttpContext)serverContext;
    1.79 +            setHandler(httpContext);
    1.80 +            return;
    1.81 +        }
    1.82 +        throw new ServerRtException(ServerMessages.NOT_KNOW_HTTP_CONTEXT_TYPE(
    1.83 +                serverContext.getClass(), HttpContext.class,
    1.84 +                javax.xml.ws.spi.http.HttpContext.class));
    1.85 +    }
    1.86 +
    1.87 +    HttpAdapterList getAdapterOwner() {
    1.88 +        return adapter.owner;
    1.89 +    }
    1.90 +
    1.91 +    /**
    1.92 +     * This can be called only after publish
    1.93 +     * @return address of the Endpoint
    1.94 +     */
    1.95 +    private String getEPRAddress() {
    1.96 +        if (address == null)
    1.97 +                return httpContext.getServer().getAddress().toString();
    1.98 +        try {
    1.99 +                URL u = new URL(address);
   1.100 +                if (u.getPort() == 0) {
   1.101 +                        return new URL(u.getProtocol(),u.getHost(),
   1.102 +                                        httpContext.getServer().getAddress().getPort(),u.getFile()).toString();
   1.103 +                }
   1.104 +        } catch (MalformedURLException murl) {}
   1.105 +        return address;
   1.106 +    }
   1.107 +
   1.108 +    public void stop() {
   1.109 +        if (httpContext != null) {
   1.110 +            if (address == null) {
   1.111 +                // Application created its own HttpContext
   1.112 +                // httpContext.setHandler(null);
   1.113 +                httpContext.getServer().removeContext(httpContext);
   1.114 +            } else {
   1.115 +                // Remove HttpContext created by JAXWS runtime
   1.116 +                ServerMgr.getInstance().removeContext(httpContext);
   1.117 +            }
   1.118 +        }
   1.119 +
   1.120 +        // Invoke WebService Life cycle method
   1.121 +        adapter.getEndpoint().dispose();
   1.122 +    }
   1.123 +
   1.124 +    private void setHandler(HttpContext context) {
   1.125 +        context.setHandler(new WSHttpHandler(adapter, executor));
   1.126 +    }
   1.127 +
   1.128 +    private void setHandler(javax.xml.ws.spi.http.HttpContext context) {
   1.129 +        context.setHandler(new PortableHttpHandler(adapter, executor));
   1.130 +    }
   1.131 +
   1.132 +    public <T extends EndpointReference> T getEndpointReference(Class<T> clazz, Element...referenceParameters) {
   1.133 +        WSEndpointImpl endpointImpl = (WSEndpointImpl) adapter.getEndpoint();
   1.134 +        String eprAddress = getEPRAddress();
   1.135 +        return clazz.cast(endpointImpl.getEndpointReference(clazz, eprAddress,eprAddress+"?wsdl", referenceParameters));
   1.136 +    }
   1.137 +
   1.138 +}

mercurial