aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.transport.http.server; aoqi@0: aoqi@0: import com.sun.net.httpserver.HttpContext; aoqi@0: import com.sun.xml.internal.ws.transport.http.HttpAdapter; aoqi@0: import com.sun.xml.internal.ws.transport.http.HttpAdapterList; aoqi@0: import com.sun.xml.internal.ws.server.ServerRtException; aoqi@0: import com.sun.xml.internal.ws.resources.ServerMessages; aoqi@0: aoqi@0: import javax.xml.ws.EndpointReference; aoqi@0: import java.util.concurrent.Executor; aoqi@0: import java.net.MalformedURLException; aoqi@0: import java.net.URL; aoqi@0: aoqi@0: import org.w3c.dom.Element; aoqi@0: aoqi@0: /** aoqi@0: * Hides {@link HttpContext} so that {@link EndpointImpl} aoqi@0: * may load even without {@link HttpContext}. aoqi@0: * aoqi@0: * TODO: But what's the point? If Light-weight HTTP server isn't present, aoqi@0: * all the publish operations will fail way. Why is it better to defer aoqi@0: * the failure, as opposed to cause the failure as earyl as possible? -KK aoqi@0: * aoqi@0: * @author Jitendra Kotamraju aoqi@0: */ aoqi@0: public final class HttpEndpoint extends com.sun.xml.internal.ws.api.server.HttpEndpoint { aoqi@0: private String address; aoqi@0: private HttpContext httpContext; aoqi@0: private final HttpAdapter adapter; aoqi@0: private final Executor executor; aoqi@0: aoqi@0: public HttpEndpoint(Executor executor, HttpAdapter adapter) { aoqi@0: this.executor = executor; aoqi@0: this.adapter = adapter; aoqi@0: } aoqi@0: aoqi@0: public void publish(String address) { aoqi@0: this.address = address; aoqi@0: httpContext = ServerMgr.getInstance().createContext(address); aoqi@0: publish(httpContext); aoqi@0: } aoqi@0: aoqi@0: public void publish(Object serverContext) { aoqi@0: if (serverContext instanceof javax.xml.ws.spi.http.HttpContext) { aoqi@0: setHandler((javax.xml.ws.spi.http.HttpContext)serverContext); aoqi@0: return; aoqi@0: } aoqi@0: if (serverContext instanceof HttpContext) { aoqi@0: this.httpContext = (HttpContext)serverContext; aoqi@0: setHandler(httpContext); aoqi@0: return; aoqi@0: } aoqi@0: throw new ServerRtException(ServerMessages.NOT_KNOW_HTTP_CONTEXT_TYPE( aoqi@0: serverContext.getClass(), HttpContext.class, aoqi@0: javax.xml.ws.spi.http.HttpContext.class)); aoqi@0: } aoqi@0: aoqi@0: HttpAdapterList getAdapterOwner() { aoqi@0: return adapter.owner; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * This can be called only after publish aoqi@0: * @return address of the Endpoint aoqi@0: */ aoqi@0: private String getEPRAddress() { aoqi@0: if (address == null) aoqi@0: return httpContext.getServer().getAddress().toString(); aoqi@0: try { aoqi@0: URL u = new URL(address); aoqi@0: if (u.getPort() == 0) { aoqi@0: return new URL(u.getProtocol(),u.getHost(), aoqi@0: httpContext.getServer().getAddress().getPort(),u.getFile()).toString(); aoqi@0: } aoqi@0: } catch (MalformedURLException murl) {} aoqi@0: return address; aoqi@0: } aoqi@0: aoqi@0: public void stop() { aoqi@0: if (httpContext != null) { aoqi@0: if (address == null) { aoqi@0: // Application created its own HttpContext aoqi@0: // httpContext.setHandler(null); aoqi@0: httpContext.getServer().removeContext(httpContext); aoqi@0: } else { aoqi@0: // Remove HttpContext created by JAXWS runtime aoqi@0: ServerMgr.getInstance().removeContext(httpContext); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Invoke WebService Life cycle method aoqi@0: adapter.getEndpoint().dispose(); aoqi@0: } aoqi@0: aoqi@0: private void setHandler(HttpContext context) { aoqi@0: context.setHandler(new WSHttpHandler(adapter, executor)); aoqi@0: } aoqi@0: aoqi@0: private void setHandler(javax.xml.ws.spi.http.HttpContext context) { aoqi@0: context.setHandler(new PortableHttpHandler(adapter, executor)); aoqi@0: } aoqi@0: aoqi@0: public T getEndpointReference(Class clazz, Element...referenceParameters) { aoqi@0: String eprAddress = getEPRAddress(); aoqi@0: return clazz.cast(adapter.getEndpoint().getEndpointReference(clazz, eprAddress,eprAddress+"?wsdl", referenceParameters)); aoqi@0: } aoqi@0: aoqi@0: }