com.googlecode.mycontainer.web.jetty.JettyServerDeployer Maven / Gradle / Ivy
/*
* Copyright 2008 Whohoo Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
* law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package com.googlecode.mycontainer.web.jetty;
import java.util.Collection;
import java.util.EventListener;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.Filter;
import javax.servlet.http.HttpServlet;
import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.RequestLogHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.FilterMapping;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.security.Password;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
import com.googlecode.mycontainer.kernel.KernelRuntimeException;
import com.googlecode.mycontainer.kernel.reflect.ReflectUtil;
import com.googlecode.mycontainer.web.ContextWebServer;
import com.googlecode.mycontainer.web.FilterDesc;
import com.googlecode.mycontainer.web.Realm;
import com.googlecode.mycontainer.web.Realm.UserRole;
import com.googlecode.mycontainer.web.ServletDesc;
import com.googlecode.mycontainer.web.WebServerDeployer;
public class JettyServerDeployer extends WebServerDeployer {
private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(JettyServerDeployer.class);
private static final long serialVersionUID = 3380253274628567229L;
public static final String JETTY_USE_FILE_MAPPTED_BUFFER = "org.eclipse.jetty.servlet.Default.useFileMappedBuffer";
private Server server;
public JettyServerDeployer() {
server = new Server();
System.setProperty("com.sun.faces.InjectionProvider", "com.googlecode.mycontainer.jsfprovider.MyContainerInjectionProvider");
RequestLogHandler requestLogHandler = new RequestLogHandler();
requestLogHandler.setRequestLog(new JettyRequestLogImpl());
requestLogHandler.setServer(server);
// server.addBean(requestLogHandler);
}
public Server getServer() {
return server;
}
@Override
protected Object getResource() {
return server;
}
@Override
public void shutdown() {
LOG.info("Shutting down Jetty web server...");
try {
server.stop();
server.join();
} catch (Exception e) {
throw new KernelRuntimeException(e);
}
super.shutdown();
}
@Override
public void bindPort(int port) {
Connector connector = createConnector(port);
server.addConnector(connector);
}
private SelectChannelConnector createConnector(int port) {
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(port);
connector.setMaxIdleTime(30000);
return connector;
}
@Override
public void bindPort(int port, int confidentialPort) {
SelectChannelConnector connector = createConnector(port);
connector.setConfidentialPort(confidentialPort);
server.addConnector(connector);
}
@Deprecated
@Override
public void bindSSLPort(int port, String keystore, String password) {
SslConnectorInfo info = new SslConnectorInfo(port, keystore);
info.setKeyManagerPassword(password);
info.setKeyStorePassword(password);
bindSSLPort(info);
}
@Override
public void bindSSLPort(SslConnectorInfo info) {
Connector connector = info.createConnector();
server.addConnector(connector);
}
@Override
public void deploy() {
try {
deployWebContexts();
server.start();
} catch (Exception e) {
throw new KernelRuntimeException(e);
}
super.deploy();
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private void deployWebContexts() {
List webContexts = getWebContexts();
Handler[] handlers = new Handler[webContexts.size()];
int i = 0;
for (ContextWebServer contextWebServer : webContexts) {
ServletContextHandler handler;
if (contextWebServer.getResources() != null) {
handler = new WebAppContext(contextWebServer.getResources(), contextWebServer.getContext());
} else {
handler = new ServletContextHandler(null, contextWebServer.getContext(), ServletContextHandler.SESSIONS | ServletContextHandler.SECURITY);
handler.addServlet(DefaultServlet.class, "/*");
}
handler.setAttribute(
WebInfConfiguration.CONTAINER_JAR_PATTERN,
".*/.*jsp-api-[^/]*\\.jar$|.*/.*jsp-[^/]*\\.jar$|.*/.*taglibs[^/]*\\.jar$|.*/.*jstl[^/]*\\.jar$|.*/.*jsf-impl-[^/]*\\.jar$|.*/.*javax.faces-[^/]*\\.jar$|.*/.*myfaces-impl-[^/]*\\.jar$");
// performRemoveTagLibConfiguration((WebAppContext) handler);
Set> attrs = contextWebServer.getAttributes().entrySet();
for (Entry entry : attrs) {
handler.setAttribute(entry.getKey(), entry.getValue());
}
Set> params = contextWebServer.getInitParameters().entrySet();
for (Entry entry : params) {
handler.setInitParameter(entry.getKey(), entry.getValue());
}
configWindows(handler);
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy