/*
* Copyright 2011- Per Wendel
*
* 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 spark.webserver;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Spark server implementation
*
* @author Per Wendel
*/
public class SparkServer {
private static final int SPARK_DEFAULT_PORT = 4567;
private static final String NAME = "Spark";
private Handler handler;
private Server server;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public SparkServer(Handler handler) {
this.handler = handler;
System.setProperty("org.mortbay.log.class", "spark.JettyLogger");
}
/**
* Ignites the spark server, listening on the specified port, running SSL secured with the specified keystore
* and truststore. If truststore is null, keystore is reused.
*
* @param host The address to listen on
* @param port - the port
* @param keystoreFile - The keystore file location as string
* @param keystorePassword - the password for the keystore
* @param truststoreFile - the truststore file location as string, leave null to reuse keystore
* @param truststorePassword - the trust store password
* @param staticFilesFolder - the route to static files in classPath
* @param externalFilesFolder - the route to static files external to classPath.
* @param maxThreads - max nbr of threads.
* @param minThreads - min nbr of threads.
* @param threadIdleTimeoutMillis - idle timeout (ms).
*/
public void ignite(String host,
int port,
String keystoreFile,
String keystorePassword,
String truststoreFile,
String truststorePassword,
String staticFilesFolder,
String externalFilesFolder,
CountDownLatch latch,
int maxThreads,
int minThreads,
int threadIdleTimeoutMillis) {
if (port == 0) {
try (ServerSocket s = new ServerSocket(0)) { |