Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #12 from jenkinsci/jetty9.2
[JENKINS-23378] Servlet 3.1 support
  • Loading branch information
kohsuke committed Feb 26, 2016
2 parents 5681958 + ca60463 commit f8efd00
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 92 deletions.
12 changes: 9 additions & 3 deletions pom.xml
Expand Up @@ -47,6 +47,7 @@ THE SOFTWARE.

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty.version>9.2.15.v20160210</jetty.version>
</properties>

<licenses>
Expand Down Expand Up @@ -88,9 +89,14 @@ THE SOFTWARE.
</exclusions>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.26</version>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-security</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
62 changes: 33 additions & 29 deletions src/main/java/org/jvnet/hudson/test/HudsonTestCase.java
Expand Up @@ -127,6 +127,17 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.security.LoginService;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.security.Password;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebXmlConfiguration;
import org.jvnet.hudson.test.HudsonHomeLoader.CopyExisting;
import org.jvnet.hudson.test.recipes.Recipe;
import org.jvnet.hudson.test.recipes.Recipe.Runner;
Expand All @@ -140,14 +151,6 @@
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.mortbay.jetty.MimeTypes;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.security.HashUserRealm;
import org.mortbay.jetty.security.UserRealm;
import org.mortbay.jetty.webapp.Configuration;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.jetty.webapp.WebXmlConfiguration;
import org.mozilla.javascript.tools.debugger.Dim;
import org.mozilla.javascript.tools.shell.Global;
import org.springframework.dao.DataAccessException;
Expand Down Expand Up @@ -505,27 +508,32 @@ public void setPluginManager(PluginManager pluginManager) {
* that we need for testing.
*/
protected ServletContext createWebServer() throws Exception {
server = new Server();
server = new Server(new ThreadPoolImpl(new ThreadPoolExecutor(10, 10, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(),new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("Jetty Thread Pool");
return t;
}
})));

explodedWarDir = WarExploder.getExplodedDir();
WebAppContext context = new WebAppContext(explodedWarDir.getPath(), contextPath);
context.setResourceBase(explodedWarDir.getPath());
context.setClassLoader(getClass().getClassLoader());
context.setConfigurations(new Configuration[]{new WebXmlConfiguration(), new NoListenerConfiguration()});
context.setConfigurations(new Configuration[]{new WebXmlConfiguration()});
context.addBean(new NoListenerConfiguration(context));
server.setHandler(context);
context.setMimeTypes(MIME_TYPES);
context.getSecurityHandler().setLoginService(configureUserRealm());

SocketConnector connector = new SocketConnector();
connector.setHeaderBufferSize(12*1024); // use a bigger buffer as Stapler traces can get pretty large on deeply nested URL
ServerConnector connector = new ServerConnector(server);

HttpConfiguration config = connector.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration();
// use a bigger buffer as Stapler traces can get pretty large on deeply nested URL
config.setRequestHeaderSize(12 * 1024);
connector.setHost("localhost");

server.setThreadPool(new ThreadPoolImpl(new ThreadPoolExecutor(10, 10, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(),new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("Jetty Thread Pool");
return t;
}
})));
server.addConnector(connector);
server.addUserRealm(configureUserRealm());
server.start();

localPort = connector.getLocalPort();
Expand All @@ -536,16 +544,12 @@ public Thread newThread(Runnable r) {
/**
* Configures a security realm for a test.
*/
protected UserRealm configureUserRealm() {
HashUserRealm realm = new HashUserRealm();
protected LoginService configureUserRealm() {
HashLoginService realm = new HashLoginService();
realm.setName("default"); // this is the magic realm name to make it effective on everywhere
realm.put("alice","alice");
realm.put("bob","bob");
realm.put("charlie","charlie");

realm.addUserToRole("alice","female");
realm.addUserToRole("bob","male");
realm.addUserToRole("charlie","male");
realm.update("alice", new Password("alice"), new String[]{"female"});
realm.update("bob", new Password("bob"), new String[]{"male"});
realm.update("charlie", new Password("charlie"), new String[]{"male"});

return realm;
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/jvnet/hudson/test/JavaNetReverseProxy.java
Expand Up @@ -3,11 +3,11 @@
import hudson.Util;
import hudson.util.IOUtils;
import org.apache.commons.io.FileUtils;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.handler.ContextHandlerCollection;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
Expand Down Expand Up @@ -40,10 +40,10 @@ public JavaNetReverseProxy(File cacheFolder) throws Exception {
ContextHandlerCollection contexts = new ContextHandlerCollection();
server.setHandler(contexts);

Context root = new Context(contexts, "/", Context.SESSIONS);
ServletContextHandler root = new ServletContextHandler(contexts, "/", ServletContextHandler.SESSIONS);
root.addServlet(new ServletHolder(this), "/");

SocketConnector connector = new SocketConnector();
ServerConnector connector = new ServerConnector(server);
server.addConnector(connector);
server.start();

Expand Down
60 changes: 31 additions & 29 deletions src/main/java/org/jvnet/hudson/test/JenkinsRule.java
Expand Up @@ -175,6 +175,17 @@
import org.apache.commons.io.IOUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.security.LoginService;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.security.Password;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebXmlConfiguration;
import org.hamcrest.Matchers;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
Expand Down Expand Up @@ -203,14 +214,6 @@
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.mortbay.jetty.MimeTypes;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.security.HashUserRealm;
import org.mortbay.jetty.security.UserRealm;
import org.mortbay.jetty.webapp.Configuration;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.jetty.webapp.WebXmlConfiguration;
import org.mozilla.javascript.tools.debugger.Dim;
import org.mozilla.javascript.tools.shell.Global;
import org.springframework.dao.DataAccessException;
Expand Down Expand Up @@ -606,29 +609,32 @@ public File getWebAppRoot() throws Exception {
* that we need for testing.
*/
protected ServletContext createWebServer() throws Exception {
server = new Server();
server = new Server(new ThreadPoolImpl(new ThreadPoolExecutor(10, 10, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(),new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("Jetty Thread Pool");
return t;
}
})));

WebAppContext context = new WebAppContext(WarExploder.getExplodedDir().getPath(), contextPath);
context.setClassLoader(getClass().getClassLoader());
context.setConfigurations(new Configuration[]{new WebXmlConfiguration(), new NoListenerConfiguration()});
context.setConfigurations(new Configuration[]{new WebXmlConfiguration()});
context.addBean(new NoListenerConfiguration(context));
server.setHandler(context);
context.setMimeTypes(MIME_TYPES);
context.getSecurityHandler().setLoginService(configureUserRealm());
context.setResourceBase(WarExploder.getExplodedDir().getPath());

SocketConnector connector = new SocketConnector();
connector.setHeaderBufferSize(12*1024); // use a bigger buffer as Stapler traces can get pretty large on deeply nested URL
ServerConnector connector = new ServerConnector(server);
HttpConfiguration config = connector.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration();
// use a bigger buffer as Stapler traces can get pretty large on deeply nested URL
config.setRequestHeaderSize(12 * 1024);
connector.setHost("localhost");
if (System.getProperty("port")!=null)
connector.setPort(Integer.parseInt(System.getProperty("port")));

server.setThreadPool(new ThreadPoolImpl(new ThreadPoolExecutor(10, 10, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(),new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("Jetty Thread Pool");
return t;
}
})));
server.addConnector(connector);
server.addUserRealm(configureUserRealm());
server.start();

localPort = connector.getLocalPort();
Expand All @@ -640,16 +646,12 @@ public Thread newThread(Runnable r) {
/**
* Configures a security realm for a test.
*/
public UserRealm configureUserRealm() {
HashUserRealm realm = new HashUserRealm();
protected LoginService configureUserRealm() {
HashLoginService realm = new HashLoginService();
realm.setName("default"); // this is the magic realm name to make it effective on everywhere
realm.put("alice","alice");
realm.put("bob","bob");
realm.put("charlie","charlie");

realm.addUserToRole("alice","female");
realm.addUserToRole("bob","male");
realm.addUserToRole("charlie","male");
realm.update("alice", new Password("alice"), new String[]{"female"});
realm.update("bob", new Password("bob"), new String[]{"male"});
realm.update("charlie", new Password("charlie"), new String[]{"male"});

return realm;
}
Expand Down
27 changes: 8 additions & 19 deletions src/main/java/org/jvnet/hudson/test/NoListenerConfiguration.java
Expand Up @@ -23,8 +23,9 @@
*/
package org.jvnet.hudson.test;

import org.mortbay.jetty.webapp.Configuration;
import org.mortbay.jetty.webapp.WebAppContext;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.webapp.AbstractConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;

import javax.servlet.ServletContextListener;

Expand All @@ -37,27 +38,15 @@
*
* @author Kohsuke Kawaguchi
*/
final class NoListenerConfiguration implements Configuration {
private WebAppContext context;
final class NoListenerConfiguration extends AbstractLifeCycle {
private final WebAppContext context;

public void setWebAppContext(WebAppContext context) {
NoListenerConfiguration(WebAppContext context) {
this.context = context;
}

public WebAppContext getWebAppContext() {
return context;
}

public void configureClassLoader() throws Exception {
}

public void configureDefaults() throws Exception {
}

public void configureWebApp() throws Exception {
@Override
protected void doStart() throws Exception {
context.setEventListeners(null);
}

public void deconfigureWebApp() throws Exception {
}
}
11 changes: 6 additions & 5 deletions src/main/java/org/jvnet/hudson/test/ThreadPoolImpl.java
@@ -1,9 +1,10 @@
package org.jvnet.hudson.test;

import org.mortbay.component.AbstractLifeCycle;
import org.mortbay.thread.ThreadPool;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.thread.ThreadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -16,12 +17,12 @@ public ThreadPoolImpl(ExecutorService es) {
this.es = es;
}

public boolean dispatch(Runnable job) {
@Override
public void execute(Runnable job) {
if (!isRunning() || job==null)
return false;
throw new RejectedExecutionException();

es.submit(job);
return true;
}

public void join() throws InterruptedException {
Expand Down

0 comments on commit f8efd00

Please sign in to comment.