<commit_before>package lux.appserver;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;

public class AppServer {
    
    private static final String LUX_APPSERVER_PORT = "lux.appserver.port";
    private static final String LUX_SOLR_URL = "lux.solr.url";
    private static final String LUX_SOLR_HOST = "lux.solr.host";
    private static final String LUX_SOLR_PORT = "lux.solr.port";
    private static final String LUX_SOLR_PATH = "lux.solr.path";
    private static final String LUX_SOLR_WEBAPP_FOLDER = "lux.solr.webapp-folder";
    public static final String LUX_SOLR_FORWARD_PATH = "lux.solr.forward-path";

    private static final String LUX_RESOURCE_BASE = "lux.appserver.resourceBase";
    private static final String LUX_CONTEXT = "lux.appserver.context";
    
    public static final String DEFAULT = "default";
    public static final String LUX_APP_FORWARDER = "lux.app-forwarder";
    
    // defaults
    private int port = 8080;            // -p
    private String context = "/";       // -c
    private String resourceBase = ".";  // -r

    private String solrWebappFolder = "webapps/solr";
    private String solrForwardPath = "/solr";
    private String luxForwardPath = "/solr/lux";
    
    private ServletHolder appForwarderHolder;
    private ServletHolder resourcesHolder;
    private static WebAppContext luxWebapp;
    private Server server;
    
    public static void main (String ... argv) throws Exception {
        //System.setProperty("org.eclipse.jetty.LEVEL", "DEBUG");
        AppServer server = new AppServer(argv);
        server.start();
    }
    
    public AppServer (String ... argv) {
        
        AppForwarder xqueryForward = new AppForwarder();
        appForwarderHolder = new ServletHolder(xqueryForward);
        appForwarderHolder.setName(LUX_APP_FORWARDER);
        
        handleProperties();
        handleArguments(argv);
        
        AppHandler handler = new AppHandler();
        
        handler.addServlet(appForwarderHolder);
        
        DefaultServlet resources = new DefaultServlet();
        resourcesHolder = new ServletHolder(resources);
        resourcesHolder.setInitParameter("dirAllowed", "false");
        resourcesHolder.setName(DEFAULT);
        handler.addServlet(resourcesHolder);


        resourcesHolder.setInitParameter("resourceBase", resourceBase);
        appForwarderHolder.setInitParameter("resourceBase", resourceBase);
        
        if (appForwarderHolder.getInitParameter("solr-host") == null) {
            // embedded solr webapp
            luxWebapp = new WebAppContext(handler, solrWebappFolder, solrForwardPath);
            xqueryForward.setServletPath (solrForwardPath);
            xqueryForward.setForwardPath (luxForwardPath);
            xqueryForward.setSolrPort (port);
            handler.setSolrWebapp (luxWebapp);
        }

        server = new Server(port);

        ServletContextHandler root = new ServletContextHandler(server, context);
        root.setWelcomeFiles(new String[] { "index.xqy" });
        root.setServletHandler(handler);

    }
    
    // read properties from lux.properties in the current directory, if it exists
    private void handleProperties () {
        File f = new File ("lux.properties");
        if (!f.canRead()) {
            return;
        }
        Properties props = new Properties();
        try {
            props.load(new FileInputStream(f));
        } catch (FileNotFoundException e) {
           error (e.getMessage());
        } catch (IOException e) {
            error (e.getMessage());
        }<commit_msg>log message on startup<commit_after>package lux.appserver;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.LoggerFactory;

public class AppServer {
    
    private static final String LUX_APPSERVER_PORT = "lux.appserver.port";
    private static final String LUX_SOLR_URL = "lux.solr.url";
    private static final String LUX_SOLR_HOST = "lux.solr.host";
    private static final String LUX_SOLR_PORT = "lux.solr.port";
    private static final String LUX_SOLR_PATH = "lux.solr.path";
    private static final String LUX_SOLR_WEBAPP_FOLDER = "lux.solr.webapp-folder";
    public static final String LUX_SOLR_FORWARD_PATH = "lux.solr.forward-path";

    private static final String LUX_RESOURCE_BASE = "lux.appserver.resourceBase";
    private static final String LUX_CONTEXT = "lux.appserver.context";
    
    public static final String DEFAULT = "default";
    public static final String LUX_APP_FORWARDER = "lux.app-forwarder";
    
    // defaults
    private int port = 8080;            // -p
    private String context = "/";       // -c
    private String resourceBase = ".";  // -r

    private String solrWebappFolder = "webapps/solr";
    private String solrForwardPath = "/solr";
    private String luxForwardPath = "/solr/lux";
    
    private ServletHolder appForwarderHolder;
    private ServletHolder resourcesHolder;
    private static WebAppContext luxWebapp;
    private Server server;
    
    public static void main (String ... argv) throws Exception {
        //System.setProperty("org.eclipse.jetty.LEVEL", "DEBUG");
        AppServer server = new AppServer(argv);
        server.start();
    }
    
    public AppServer (String ... argv) {
        
        AppForwarder xqueryForward = new AppForwarder();
        appForwarderHolder = new ServletHolder(xqueryForward);
        appForwarderHolder.setName(LUX_APP_FORWARDER);
        
        handleProperties();
        handleArguments(argv);
        
        AppHandler handler = new AppHandler();
        
        handler.addServlet(appForwarderHolder);
        
        DefaultServlet resources = new DefaultServlet();
        resourcesHolder = new ServletHolder(resources);
        resourcesHolder.setInitParameter("dirAllowed", "false");
        resourcesHolder.setName(DEFAULT);
        handler.addServlet(resourcesHolder);


        resourcesHolder.setInitParameter("resourceBase", resourceBase);
        appForwarderHolder.setInitParameter("resourceBase", resourceBase);
        
        if (appForwarderHolder.getInitParameter("solr-host") == null) {
            // embedded solr webapp
            luxWebapp = new WebAppContext(handler, solrWebappFolder, solrForwardPath);
            xqueryForward.setServletPath (solrForwardPath);
            xqueryForward.setForwardPath (luxForwardPath);
            xqueryForward.setSolrPort (port);
            handler.setSolrWebapp (luxWebapp);
        }

        server = new Server(port);

        ServletContextHandler root = new ServletContextHandler(server, context);
        root.setWelcomeFiles(new String[] { "index.xqy" });
        root.setServletHandler(handler);

        LoggerFactory.getLogger(getClass()).info ("Lux AppServer listening on port {} resources at {}", port, resourceBase);
    }
    
    // read properties from lux.properties in the current directory, if it exists
    private void handleProperties () {
        File f = new File ("lux.properties");
        if (!f.canRead()) {
            return;
        }
        Properties props = new Properties();
        try {
            props.load(new FileInputStream(f));
        } catch (FileNotFoundException e) {
           error (e.getMessage());
        } catch (IOException e) {
            error (e.getMessage());
        }
{'fraction_non_alphanumeric': 0.09527326440177253, 'fraction_numerical': 0.0011078286558345643, 'mean_word_length': 2.9289168278529982, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2967964', 'n_tokens_mistral': 2272, 'n_tokens_neox': 2210, 'n_words': 521}