All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cj.qunit.mojo.http.WebServerUtils Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package com.cj.qunit.mojo.http;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.log.Logger;
import org.httpobjects.HttpObject;
import org.httpobjects.jetty.HttpObjectsJettyHandler;
import org.httpobjects.util.ClasspathResourcesObject;
import org.httpobjects.util.FilesystemResourcesObject;

import com.cj.qunit.mojo.QunitMavenRunner;
import com.cj.qunit.mojo.Scanner;

public class WebServerUtils {
    
    public static void main(String[] args) {
        launchHttpServer("", Collections.emptyList(), Collections.emptyList(), "", null, false);
    }
    
    public static class JettyPlusPortPlusScanner {
        public final int port;
        public final Server server;
        public final Scanner scanner;
        
		public JettyPlusPortPlusScanner(int port, Server server, Scanner scanner) {
			super();
			this.port = port;
			this.server = server;
			this.scanner = scanner;
		}
        
    }

    public static JettyPlusPortPlusScanner launchHttpServer(final String webRoot, final List codePaths, final List extraPathsToServe, final String webPathToRequireDotJsConfig, final Logger log, final boolean cacheTests) {
        return launchHttpServer(webRoot, codePaths, extraPathsToServe, webPathToRequireDotJsConfig, log, cacheTests, null);
    }

    public static JettyPlusPortPlusScanner launchHttpServer(final String webRoot, final List codePaths, final List extraPathsToServe, final String webPathToRequireDotJsConfig, final Logger log, final boolean cacheTests, final String filterPattern) {

        if(log!=null){
            org.eclipse.jetty.util.log.Log.setLog(log);
        }
        
        List pathsToServe = new ArrayList(codePaths);
        pathsToServe.addAll(extraPathsToServe);
        

        
        for(int port: new Integer[]{8098, 8198, 8298, 8398, 8498, 8598, 8695, 8796}){
            try{

                final Scanner scanner = new Scanner(port, webPathToRequireDotJsConfig, pathsToServe, webRoot, cacheTests, filterPattern);
                List resources = new ArrayList(Arrays.asList(new TestListingResource("/", webRoot, webPathToRequireDotJsConfig, pathsToServe, scanner)));
                
                for(File projectDirectory: pathsToServe){
                    resources.add(new FilesystemResourcesObject(webRoot + "/{resource*}", projectDirectory));
                }
                resources.add(new ClasspathResourcesObject("/qunit-mojo/{resource*}", WebServerUtils.class, "/qunit-mojo"));
                resources.add(new AutoGeneratedQunitHtmlWrappers(webRoot, webPathToRequireDotJsConfig, pathsToServe, scanner));
            	
                Server jetty = HttpObjectsJettyHandler.launchServer(port, resources.toArray(new HttpObject[]{}));
                return new JettyPlusPortPlusScanner(port, jetty, scanner);
            }catch(Exception e){
                System.out.println("Looks like port " + port + " didn't work for me.  I'm assuming it is in use?");
            }
        }

        throw new RuntimeException("Could not find a port to which to bind :(");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy