|
| 1 | +package restx.server; |
| 2 | + |
| 3 | +import com.google.common.base.Strings; |
| 4 | +import org.eclipse.jetty.security.DefaultIdentityService; |
| 5 | +import org.eclipse.jetty.security.HashLoginService; |
| 6 | +import org.eclipse.jetty.server.Handler; |
| 7 | +import org.eclipse.jetty.server.Server; |
| 8 | +import org.eclipse.jetty.server.handler.HandlerCollection; |
| 9 | +import org.eclipse.jetty.server.handler.HandlerList; |
| 10 | +import org.eclipse.jetty.server.nio.SelectChannelConnector; |
| 11 | +import org.eclipse.jetty.util.component.AbstractLifeCycle; |
| 12 | +import org.eclipse.jetty.util.component.LifeCycle; |
| 13 | +import org.eclipse.jetty.util.thread.QueuedThreadPool; |
| 14 | +import org.eclipse.jetty.util.thread.ThreadPool; |
| 15 | +import org.eclipse.jetty.webapp.WebAppContext; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | +import restx.common.Version; |
| 19 | + |
| 20 | +import java.util.concurrent.atomic.AtomicLong; |
| 21 | + |
| 22 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | +import static restx.common.MoreFiles.checkFileExists; |
| 24 | +import static restx.common.MoreIO.checkCanOpenSocket; |
| 25 | + |
| 26 | +public class JettyWebServer implements WebServer { |
| 27 | + private static final AtomicLong SERVER_ID = new AtomicLong(); |
| 28 | + |
| 29 | + private static final Logger logger = LoggerFactory.getLogger(JettyWebServer.class); |
| 30 | + |
| 31 | + private Server server; |
| 32 | + private int port; |
| 33 | + private String bindInterface; |
| 34 | + private String appBase; |
| 35 | + private String webInfLocation; |
| 36 | + private String serverId; |
| 37 | + |
| 38 | + |
| 39 | + public JettyWebServer(String appBase, int aPort) { |
| 40 | + this(null, appBase, aPort, null); |
| 41 | + } |
| 42 | + |
| 43 | + public JettyWebServer(String webInfLocation, String appBase, int port, String bindInterface) { |
| 44 | + checkFileExists(checkNotNull(appBase)); |
| 45 | + |
| 46 | + if (webInfLocation != null) { |
| 47 | + checkFileExists(webInfLocation); |
| 48 | + } |
| 49 | + |
| 50 | + this.port = port; |
| 51 | + this.bindInterface = bindInterface; |
| 52 | + this.appBase = appBase; |
| 53 | + this.webInfLocation = webInfLocation; |
| 54 | + this.serverId = "Jetty#" + SERVER_ID.incrementAndGet(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Sets the serverId used by this server. |
| 59 | + * |
| 60 | + * Must not be called when server is started. |
| 61 | + * |
| 62 | + * The serverId is used to uniquely identify the main Factory used by REST main router in this server. |
| 63 | + * It allows to access the Factory with Factory.getInstance(serverId). |
| 64 | + * |
| 65 | + * @param serverId the server id to set. Must be unique in the JVM. |
| 66 | + * |
| 67 | + * @return current server |
| 68 | + */ |
| 69 | + public synchronized JettyWebServer setServerId(final String serverId) { |
| 70 | + if (isStarted()) { |
| 71 | + throw new IllegalStateException("can't set server id when server is started"); |
| 72 | + } |
| 73 | + this.serverId = serverId; |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public String getServerId() { |
| 79 | + return serverId; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public int getPort() { |
| 84 | + return port; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String baseUrl() { |
| 89 | + return WebServers.baseUri("127.0.0.1", port); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String getServerType() { |
| 94 | + return "Jetty " + Version.getVersion("org.eclipse.jetty", "jetty-server") + ", embedded"; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public synchronized void start() throws Exception { |
| 99 | + checkCanOpenSocket(port); |
| 100 | + |
| 101 | + server = new Server(); |
| 102 | + WebServers.register(this); |
| 103 | + |
| 104 | + server.setThreadPool(createThreadPool()); |
| 105 | + server.addConnector(createConnector()); |
| 106 | + server.setHandler(createHandlers(createContext())); |
| 107 | + server.setStopAtShutdown(true); |
| 108 | + |
| 109 | + server.start(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void startAndAwait() throws Exception { |
| 114 | + start(); |
| 115 | + await(); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void await() throws InterruptedException { |
| 120 | + server.join(); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public synchronized void stop() throws Exception { |
| 125 | + server.stop(); |
| 126 | + server = null; |
| 127 | + WebServers.unregister(serverId); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public synchronized boolean isStarted() { |
| 132 | + return server != null; |
| 133 | + } |
| 134 | + |
| 135 | + protected ThreadPool createThreadPool() { |
| 136 | + QueuedThreadPool threadPool = new QueuedThreadPool(); |
| 137 | + threadPool.setMinThreads(1); |
| 138 | + threadPool.setMaxThreads(Math.max(10, Runtime.getRuntime().availableProcessors())); |
| 139 | + return threadPool; |
| 140 | + } |
| 141 | + |
| 142 | + protected SelectChannelConnector createConnector() { |
| 143 | + SelectChannelConnector connector = new SelectChannelConnector(); |
| 144 | + connector.setPort(port); |
| 145 | + connector.setHost(bindInterface); |
| 146 | + return connector; |
| 147 | + } |
| 148 | + |
| 149 | + protected HandlerCollection createHandlers(WebAppContext webAppContext) { |
| 150 | + |
| 151 | + HandlerList contexts = new HandlerList(); |
| 152 | + contexts.setHandlers(new Handler[]{webAppContext}); |
| 153 | + |
| 154 | + HandlerCollection result = new HandlerCollection(); |
| 155 | + result.setHandlers(new Handler[]{contexts}); |
| 156 | + |
| 157 | + return result; |
| 158 | + } |
| 159 | + |
| 160 | + protected WebAppContext createContext() { |
| 161 | + final WebAppContext ctx = new WebAppContext(); |
| 162 | + ctx.setContextPath("/"); |
| 163 | + ctx.setWar(appBase); |
| 164 | + if(!Strings.isNullOrEmpty(webInfLocation)) { |
| 165 | + ctx.setDescriptor(webInfLocation); |
| 166 | + } |
| 167 | + // configure security to avoid err println "Null identity service, trying login service:" |
| 168 | + // but I've found no way to get rid of LoginService=xxx log on system err :( |
| 169 | + HashLoginService loginService = new HashLoginService(); |
| 170 | + loginService.setIdentityService(new DefaultIdentityService()); |
| 171 | + ctx.getSecurityHandler().setLoginService(loginService); |
| 172 | + ctx.getSecurityHandler().setIdentityService(loginService.getIdentityService()); |
| 173 | + |
| 174 | + ctx.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() { |
| 175 | + @Override |
| 176 | + public void lifeCycleStarting(LifeCycle event) { |
| 177 | + ctx.getServletContext().setInitParameter("restx.baseServerUri", baseUrl()); |
| 178 | + ctx.getServletContext().setInitParameter("restx.serverId", getServerId()); |
| 179 | + } |
| 180 | + }); |
| 181 | + |
| 182 | + return ctx; |
| 183 | + } |
| 184 | + |
| 185 | + public static WebServerSupplier jettyWebServerSupplier(final String webInfLocation, final String appBase) { |
| 186 | + return new WebServerSupplier() { |
| 187 | + @Override |
| 188 | + public WebServer newWebServer(int port) { |
| 189 | + return new JettyWebServer(webInfLocation, appBase, port, "0.0.0.0"); |
| 190 | + } |
| 191 | + }; |
| 192 | + } |
| 193 | + |
| 194 | + public static void main(String[] args) throws Exception { |
| 195 | + if (args.length == 0) { |
| 196 | + System.err.println("usage: jetty-run <appbase> [<port>]"); |
| 197 | + System.exit(1); |
| 198 | + } |
| 199 | + |
| 200 | + String appBase = args[0]; |
| 201 | + int port = args.length > 1 ? Integer.parseInt(args[1]) : 8086; |
| 202 | + new JettyWebServer(appBase + "WEB-INF/web.xml", appBase, port, "0.0.0.0").startAndAwait(); |
| 203 | + } |
| 204 | +} |
0 commit comments