com.sun.grizzly.http.servlet.deployer.ServletWarDeployer Maven / Gradle / Ivy
The newest version!
/*
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, 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/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*
*/
package com.sun.grizzly.http.servlet.deployer;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.Servlet;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import com.sun.grizzly.arp.AsyncHandler;
import com.sun.grizzly.arp.DefaultAsyncHandler;
import com.sun.grizzly.comet.CometAsyncFilter;
import com.sun.grizzly.http.SelectorThread;
import com.sun.grizzly.http.servlet.ServletAdapter;
import com.sun.grizzly.http.servlet.xsd.FilterType;
import com.sun.grizzly.http.servlet.xsd.ListenerType;
import com.sun.grizzly.http.servlet.xsd.ParamValueType;
import com.sun.grizzly.http.servlet.xsd.ServletMappingType;
import com.sun.grizzly.http.servlet.xsd.ServletType;
import com.sun.grizzly.http.servlet.xsd.UrlPatternType;
import com.sun.grizzly.http.servlet.xsd.WebAppType;
import com.sun.grizzly.standalone.servlet.ServletLauncher;
import com.sun.grizzly.tcp.Adapter;
import com.sun.grizzly.util.ClassLoaderUtil;
/**
* Basic startup class used when Grizzly standalone is used
* The web schemas from Glassfish are used to parse web.xml.
* (glassfish/appserv-commons/schemas)
*
* Based on the example from JF-Arcard : Hudson-on-grizzly
*
* example : java com.sun.grizzly.servlet.deployer.ServletWarDeployer -p 8080 -a hudson.war -c /hudson
*
* https://grizzly.dev.java.net/issues/show_bug.cgi?id=363
* @author Sebastien Dionne
*/
public class ServletWarDeployer extends ServletLauncher {
public static final String DEFAULT_CONTEXT = "/";
private String context = null;
/**
*
* @return the contextPath
*/
public String getContext() {
return context;
}
/**
* Set the contextPath for the war.
* @param context context path
*/
public void setContext(String context) {
if (!context.startsWith("/")) {
this.context = "/" + context;
} else {
this.context = context;
}
}
public static void main(String args[]) throws Exception {
ServletWarDeployer sl = new ServletWarDeployer();
sl.start(args);
}
@Override
public Adapter configureAdapter(SelectorThread st) {
ServletAdapter adapter = new ServletAdapter();
String warPath = SelectorThread.getWebAppRootPath();
return initServletAdapter(adapter, warPath);
}
/**
* Init the ServletAdapter from the web.xml if found. If web.xml is not found,
* the ServletAdapter will be initiated manually.
*
* @param adapter ServletAdapter
* @param warPath the war path
* @return ServletAdapter
*/
private ServletAdapter initServletAdapter(ServletAdapter adapter, String warPath) {
// check if we can populate the adapter from the web.xml
File webxmlFile = new File(warPath + File.separator + "WEB-INF" + File.separator + "web.xml");
if (webxmlFile.exists()) {
try {
populate(adapter, warPath, webxmlFile);
} catch (Exception e) {
e.printStackTrace();
}
} else {
populateManually(adapter, warPath);
}
return adapter;
}
/**
* Initialization from warPath/WEB-INF/web.xml .
*
*
* @param adapter ServletAdapter
* @param warPath warpath
* @param webxmlFile File on web.xml
* @throws Exception
*/
private void populate(ServletAdapter adapter, String warPath, File webxmlFile) throws Exception {
adapter.setRootFolder(warPath);
adapter.setHandleStaticResources(true);
// the package name was generated by the ant task.
JAXBContext jc = JAXBContext.newInstance("com.sun.grizzly.http.servlet.xsd");
// create an Unmarshaller
Unmarshaller u = jc.createUnmarshaller();
JAXBElement root = (JAXBElement) u.unmarshal(new FileInputStream(webxmlFile));
WebAppType webApp = (WebAppType) root.getValue();
List © 2015 - 2025 Weber Informatics LLC | Privacy Policy