Permalink
Browse files

servers - introduced new restx-server-jetty7 for servlet 2.5 compatib…

…le embedded jetty server
  • Loading branch information...
fcamblor committed Aug 23, 2017
1 parent 9a93edf commit 934f931e0fa4e675686cce16b961c76f893839a9
View
@@ -233,6 +233,11 @@
<artifactId>restx-specs-tests-java8</artifactId>
<version>${restx.version}</version>
</dependency>
<dependency>
<groupId>io.restx</groupId>
<artifactId>restx-server-jetty7</artifactId>
<version>${restx.version}</version>
</dependency>
<dependency>
<groupId>io.restx</groupId>
<artifactId>restx-server-jetty8</artifactId>
@@ -581,6 +586,7 @@
<module>restx-jongo</module>
<module>restx-jongo-specs-tests</module>
<module>restx-servlet</module>
<module>restx-server-jetty7</module>
<module>restx-server-jetty8</module>
<module>restx-server-tomcat</module>
<module>restx-server-simple</module>
@@ -0,0 +1,23 @@
{
"parent": "io.restx:restx-parent:${restx.version}",
"module": "io.restx:restx-server-jetty7:${restx.version}",
"properties": {
"@files": ["../restx.build.properties.json"]
},
"dependencies": {
"compile": [
"io.restx:restx-factory:${restx.version}",
"io.restx:restx-core:${restx.version}",
"javax.servlet:servlet-api:2.5",
"io.restx:restx-servlet:${restx.version}",
"org.eclipse.jetty:jetty-server:${jetty7.version}",
"org.eclipse.jetty:jetty-servlet:${jetty7.version}",
"org.eclipse.jetty:jetty-security:${jetty7.version}",
"org.eclipse.jetty:jetty-util:${jetty7.version}",
"org.eclipse.jetty:jetty-webapp:${jetty7.version}",
"org.eclipse.jetty:jetty-servlets:${jetty7.version}"
]
}
}
@@ -0,0 +1,28 @@
<ivy-module version="2.0" xmlns:ea="http://www.easyant.org">
<info organisation="io.restx" module="restx-server-jetty7" revision="0.35" status="integration">
<ea:build organisation="org.apache.easyant.buildtypes" module="build-std-java" revision="0.9"
compile.java.source.version="1.7"
compile.java.target.version="1.7"
/>
</info>
<configurations>
<conf name="default"/>
<conf name="runtime"/>
<conf name="test"/>
</configurations>
<publications>
<artifact type="jar"/>
</publications>
<dependencies>
<dependency org="io.restx" name="restx-factory" rev="latest.integration" conf="default" />
<dependency org="io.restx" name="restx-core" rev="latest.integration" conf="default" />
<dependency org="javax.servlet" name="servlet-api" rev="2.5" conf="default" />
<dependency org="io.restx" name="restx-servlet" rev="latest.integration" conf="default" />
<dependency org="org.eclipse.jetty" name="jetty-server" rev="7.6.21.v20160908" conf="default" />
<dependency org="org.eclipse.jetty" name="jetty-servlet" rev="7.6.21.v20160908" conf="default" />
<dependency org="org.eclipse.jetty" name="jetty-security" rev="7.6.21.v20160908" conf="default" />
<dependency org="org.eclipse.jetty" name="jetty-util" rev="7.6.21.v20160908" conf="default" />
<dependency org="org.eclipse.jetty" name="jetty-webapp" rev="7.6.21.v20160908" conf="default" />
<dependency org="org.eclipse.jetty" name="jetty-servlets" rev="7.6.21.v20160908" conf="default" />
</dependencies>
</ivy-module>
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.restx</groupId>
<artifactId>restx-parent</artifactId>
<version>0.35-SNAPSHOT</version>
</parent>
<artifactId>restx-server-jetty7</artifactId>
<name>restx-server-jetty7</name>
<properties>
<jetty7.version>7.6.21.v20160908</jetty7.version>
<servlet-api.version>2.5</servlet-api.version>
</properties>
<dependencies>
<dependency>
<groupId>io.restx</groupId>
<artifactId>restx-factory</artifactId>
</dependency>
<dependency>
<groupId>io.restx</groupId>
<artifactId>restx-core</artifactId>
</dependency>
<dependency>
<groupId>io.restx</groupId>
<artifactId>restx-servlet</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- Jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty7.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty7.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-security</artifactId>
<version>${jetty7.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>${jetty7.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty7.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>${jetty7.version}</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,35 @@
package restx.server;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import restx.factory.Module;
import restx.factory.Provides;
import javax.inject.Named;
/**
* @author fcamblor
*/
@Module(priority = 1000)
public class JettyServerModule {
@Provides
@Named("restx.server.jetty.webxml.default.location")
public String restxServerJettyWebXmlDefaultLocation(@Named("restx.server.jetty.appbase.default.location") String appBase) {
return appBase+"/WEB-INF/web.xml";
}
@Provides
@Named("restx.server.jetty.appbase.default.location")
public String restxServerJettyAppBaseDefaultLocation() {
return "src/main/webapp";
}
@Provides
@Named("restx.server.jetty")
public WebServerSupplier jettyWebServerSupplier(
@Named("restx.server.jetty.appbase.default.location") String appBase,
@Named("restx.server.jetty.webxml.default.location") String webxml){
return JettyWebServer.jettyWebServerSupplier(webxml, appBase);
}
}
@@ -0,0 +1,204 @@
package restx.server;
import com.google.common.base.Strings;
import org.eclipse.jetty.security.DefaultIdentityService;
import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import restx.common.Version;
import java.util.concurrent.atomic.AtomicLong;
import static com.google.common.base.Preconditions.checkNotNull;
import static restx.common.MoreFiles.checkFileExists;
import static restx.common.MoreIO.checkCanOpenSocket;
public class JettyWebServer implements WebServer {
private static final AtomicLong SERVER_ID = new AtomicLong();
private static final Logger logger = LoggerFactory.getLogger(JettyWebServer.class);
private Server server;
private int port;
private String bindInterface;
private String appBase;
private String webInfLocation;
private String serverId;
public JettyWebServer(String appBase, int aPort) {
this(null, appBase, aPort, null);
}
public JettyWebServer(String webInfLocation, String appBase, int port, String bindInterface) {
checkFileExists(checkNotNull(appBase));
if (webInfLocation != null) {
checkFileExists(webInfLocation);
}
this.port = port;
this.bindInterface = bindInterface;
this.appBase = appBase;
this.webInfLocation = webInfLocation;
this.serverId = "Jetty#" + SERVER_ID.incrementAndGet();
}
/**
* Sets the serverId used by this server.
*
* Must not be called when server is started.
*
* The serverId is used to uniquely identify the main Factory used by REST main router in this server.
* It allows to access the Factory with Factory.getInstance(serverId).
*
* @param serverId the server id to set. Must be unique in the JVM.
*
* @return current server
*/
public synchronized JettyWebServer setServerId(final String serverId) {
if (isStarted()) {
throw new IllegalStateException("can't set server id when server is started");
}
this.serverId = serverId;
return this;
}
@Override
public String getServerId() {
return serverId;
}
@Override
public int getPort() {
return port;
}
@Override
public String baseUrl() {
return WebServers.baseUri("127.0.0.1", port);
}
@Override
public String getServerType() {
return "Jetty " + Version.getVersion("org.eclipse.jetty", "jetty-server") + ", embedded";
}
@Override
public synchronized void start() throws Exception {
checkCanOpenSocket(port);
server = new Server();
WebServers.register(this);
server.setThreadPool(createThreadPool());
server.addConnector(createConnector());
server.setHandler(createHandlers(createContext()));
server.setStopAtShutdown(true);
server.start();
}
@Override
public void startAndAwait() throws Exception {
start();
await();
}
@Override
public void await() throws InterruptedException {
server.join();
}
@Override
public synchronized void stop() throws Exception {
server.stop();
server = null;
WebServers.unregister(serverId);
}
@Override
public synchronized boolean isStarted() {
return server != null;
}
protected ThreadPool createThreadPool() {
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setMinThreads(1);
threadPool.setMaxThreads(Math.max(10, Runtime.getRuntime().availableProcessors()));
return threadPool;
}
protected SelectChannelConnector createConnector() {
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(port);
connector.setHost(bindInterface);
return connector;
}
protected HandlerCollection createHandlers(WebAppContext webAppContext) {
HandlerList contexts = new HandlerList();
contexts.setHandlers(new Handler[]{webAppContext});
HandlerCollection result = new HandlerCollection();
result.setHandlers(new Handler[]{contexts});
return result;
}
protected WebAppContext createContext() {
final WebAppContext ctx = new WebAppContext();
ctx.setContextPath("/");
ctx.setWar(appBase);
if(!Strings.isNullOrEmpty(webInfLocation)) {
ctx.setDescriptor(webInfLocation);
}
// configure security to avoid err println "Null identity service, trying login service:"
// but I've found no way to get rid of LoginService=xxx log on system err :(
HashLoginService loginService = new HashLoginService();
loginService.setIdentityService(new DefaultIdentityService());
ctx.getSecurityHandler().setLoginService(loginService);
ctx.getSecurityHandler().setIdentityService(loginService.getIdentityService());
ctx.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() {
@Override
public void lifeCycleStarting(LifeCycle event) {
ctx.getServletContext().setInitParameter("restx.baseServerUri", baseUrl());
ctx.getServletContext().setInitParameter("restx.serverId", getServerId());
}
});
return ctx;
}
public static WebServerSupplier jettyWebServerSupplier(final String webInfLocation, final String appBase) {
return new WebServerSupplier() {
@Override
public WebServer newWebServer(int port) {
return new JettyWebServer(webInfLocation, appBase, port, "0.0.0.0");
}
};
}
public static void main(String[] args) throws Exception {
if (args.length == 0) {
System.err.println("usage: jetty-run <appbase> [<port>]");
System.exit(1);
}
String appBase = args[0];
int port = args.length > 1 ? Integer.parseInt(args[1]) : 8086;
new JettyWebServer(appBase + "WEB-INF/web.xml", appBase, port, "0.0.0.0").startAndAwait();
}
}
Oops, something went wrong.

0 comments on commit 934f931

Please sign in to comment.