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