001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.transport.tcp.server.glassfish;
038:
039: import com.sun.xml.ws.api.server.Container;
040: import com.sun.xml.ws.transport.http.DeploymentDescriptorParser;
041: import com.sun.xml.ws.transport.tcp.resources.MessagesMessages;
042: import com.sun.xml.ws.transport.tcp.server.TCPAdapter;
043: import com.sun.xml.ws.transport.tcp.server.TCPContext;
044: import com.sun.xml.ws.transport.tcp.server.TCPResourceLoader;
045: import com.sun.xml.ws.transport.tcp.server.TCPServletContext;
046: import java.io.IOException;
047: import java.net.URL;
048: import java.util.List;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051: import javax.servlet.ServletContext;
052: import javax.servlet.ServletContextAttributeEvent;
053: import javax.servlet.ServletContextAttributeListener;
054: import javax.servlet.ServletContextEvent;
055: import javax.servlet.ServletContextListener;
056: import javax.servlet.ServletException;
057: import javax.servlet.http.HttpServlet;
058: import javax.servlet.http.HttpServletRequest;
059: import javax.servlet.http.HttpServletResponse;
060: import javax.xml.ws.WebServiceException;
061:
062: /**
063: * @author JAX-WS team
064: */
065: @SuppressWarnings({"unchecked"})
066: public final class WSStartupServlet extends HttpServlet implements
067: ServletContextAttributeListener, ServletContextListener {
068:
069: private static final Logger logger = Logger
070: .getLogger(com.sun.xml.ws.transport.tcp.util.TCPConstants.LoggingDomain
071: + ".server");
072:
073: private static final String JAXWS_RI_RUNTIME = "/WEB-INF/sun-jaxws.xml";
074:
075: private WSTCPLifeCycleModule transportModule;
076:
077: private List<TCPAdapter> adapters;
078:
079: protected void doPost(final HttpServletRequest req,
080: final HttpServletResponse resp) throws ServletException,
081: IOException {
082: }
083:
084: protected void doGet(final HttpServletRequest req,
085: final HttpServletResponse resp) throws ServletException,
086: IOException {
087: }
088:
089: public void contextInitialized(
090: final ServletContextEvent contextEvent) {
091: logger.log(Level.FINE, "WSStartupServlet.contextInitialized");
092: final ServletContext servletContext = contextEvent
093: .getServletContext();
094: final TCPContext context = new TCPServletContext(servletContext);
095: ClassLoader classLoader = Thread.currentThread()
096: .getContextClassLoader();
097: if (classLoader == null) {
098: classLoader = getClass().getClassLoader();
099: }
100: final ServletContainer container = new ServletContainer(
101: servletContext);
102:
103: try {
104: transportModule = WSTCPLifeCycleModule.getInstance();
105: final DeploymentDescriptorParser<TCPAdapter> parser = new DeploymentDescriptorParser<TCPAdapter>(
106: classLoader, new TCPResourceLoader(context),
107: container, TCPAdapter.FACTORY);
108: final URL sunJaxWsXml = context
109: .getResource(JAXWS_RI_RUNTIME);
110: if (sunJaxWsXml == null)
111: throw new WebServiceException(MessagesMessages
112: .WSTCP_0014_NO_JAXWS_DESCRIPTOR());
113: adapters = parser.parse(sunJaxWsXml.toExternalForm(),
114: sunJaxWsXml.openStream());
115:
116: transportModule.register(servletContext.getContextPath(),
117: adapters);
118: } catch (Exception e) {
119: logger.log(Level.SEVERE, e.getMessage(), e);
120: throw new IllegalStateException("listener.parsingFailed", e);
121: }
122: }
123:
124: public void contextDestroyed(final ServletContextEvent contextEvent) {
125: logger.log(Level.FINE, "WSStartupServlet.contextDestroyed");
126: if (transportModule != null && adapters != null) {
127: transportModule.free(contextEvent.getServletContext()
128: .getContextPath(), adapters);
129: }
130: }
131:
132: public void attributeAdded(final ServletContextAttributeEvent scab) {
133: }
134:
135: public void attributeRemoved(final ServletContextAttributeEvent scab) {
136: }
137:
138: public void attributeReplaced(
139: final ServletContextAttributeEvent scab) {
140: }
141:
142: /**
143: * Provides access to {@link ServletContext} via {@link Container}. Pipes
144: * can get ServletContext from Container and use it to load some resources.
145: */
146: private static final class ServletContainer extends Container {
147: private final ServletContext servletContext;
148:
149: ServletContainer(final ServletContext servletContext) {
150: this .servletContext = servletContext;
151: }
152:
153: public <T> T getSPI(final Class<T> spiType) {
154: if (spiType == ServletContext.class) {
155: return (T) servletContext;
156: }
157: return null;
158: }
159: }
160: }
|