<commit_before>/*
 *  *
 * ProActive Parallel Suite(TM): The Java(TM) library for
 *    Parallel, Distributed, Multi-Core Computing for
 *    Enterprise Grids & Clouds
 *
 * Copyright (C) 1997-2014 INRIA/University of
 *                 Nice-Sophia Antipolis/ActiveEon
 * Contact: <EMAIL> or <EMAIL>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation; version 3 of
 * the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://proactive.inria.fr/team_members.htm
 *  Contributor(s):
 *
 *  * $$PROACTIVE_INITIAL_DEV$$
 */
package org.ow2.proactive.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.objectweb.proactive.core.config.CentralPAPropertyRepository;
import org.ow2.proactive.scheduler.core.properties.PASchedulerProperties;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.HandlerCollection;
import org.mortbay.jetty.webapp.WebAppContext;


public class JettyStarter {

    private static final String FOLDER_TO_DEPLOY = "/dist/war/";
    private static final String REST_CONFIG_PATH = "/config/rest/settings.ini";

    private static Logger logger = Logger.getLogger(JettyStarter.class);

    /**
     * To run Jetty in standalone mode
     */
    public static void main(String[] args) {
        BasicConfigurator.configure();
        CentralPAPropertyRepository.PA_CLASSLOADING_USEHTTP.setValue(false);
        PASchedulerProperties.SCHEDULER_HOME.updateProperty(".");
        JettyStarter.runWars("", "");
    }

    public static void runWars(String rmUrl, String schedulerUrl) {
        Properties properties = readRestProperties();
        if ("true".equals(properties.getProperty("rest.deploy"))) {
            int restPort = Integer.parseInt(properties.getProperty("rest.port", "8080"));

            // for REST API
            setSystemPropertyIfNotDefined("rm.url", rmUrl);
            setSystemPropertyIfNotDefined("scheduler.url", schedulerUrl);

            // for web portals
            setSystemPropertyIfNotDefined("sched.rest.url", "http://localhost:" + restPort + "/rest/rest");
            setSystemPropertyIfNotDefined("rm.rest.url", "http://localhost:" + restPort + "/rest/rest");

            Server server = new Server(restPort);
            server.setStopAtShutdown(true);
            server.setHandler(new HandlerCollection());

            deployWars(server);

            startServer(server, restPort);
        }
    }

    private static void startServer(Server server, int restPort) {
        try {
            if (server.getHandler() == null) {
                logger.info("SCHEDULER_HOME/dist/war folder is empty, nothing is deployed");
            } else {
                server.start();
                if (server.isStarted()) {
                    for (Handler handler : server.getHandlers()) {
                        WebAppContext webAppContext = (WebAppContext) handler;

                        Throwable startException = webAppContext.getUnavailableException();
                        if (startException == null) {
                            logger.info("The web application " + webAppContext.getContextPath()
                              + " created on http://localhost:" + restPort + webAppContext.getContextPath());
                        } else {
                            logger.warn("Failed to start context " + webAppContext.getContextPath(),
                              startException);
                        }
                    }
                } else {
                    logger.warn("Failed to start web modules (REST API, portals)");
                }
            }
        } catch (Exception e) {
            logger.warn("Failed to start web modules (REST API, portals)", e);
        }
    }

    private static void deployWars(Server server) {
        File warFolder = new File(getSchedulerHome() + FOLDER_TO_DEPLOY);
        File[] warFolderContent = warFolder.listFiles();
        if (warFolderContent != null) {
            for (File fileToDeploy : warFolderContent) {
                if (isExplodedWebApp(fileToDeploy)) {
                    deployExplodedWebApp(server, fileToDeploy);
                } else if (isWarFile(fileToDeploy)) {
                    deployWarFile(server, fileToDeploy);
                } else if(isStaticFolder(fileToDeploy)){
                    deployStaticFolder(server, fileToDeploy);
                }
            }
        }
    }

    private static void deployWarFile(Server server, File file) {
        String contextPath = "/" + FilenameUtils.getBaseName(file.getName());
        WebAppContext webApp = createWebAppContext(contextPath);
        webApp.setWar(file.getAbsolutePath());
        server.addHandler(webApp);
        logger.debug("Deploying " + contextPath + " using war file " + file);
    }

    private static void deployExplodedWebApp(Server server, File file) {
        String contextPath = "/" + file.getName();
        WebAppContext webApp = createWebAppContext(contextPath);
        webApp.setDescriptor(new File(file, "/WEB-INF/web.xml").getAbsolutePath());
        webApp.setResourceBase(file.getAbsolutePath());
        server.addHandler(webApp);
        logger.debug("Deploying " + contextPath + " using exploded war " + file);

    }

    private static void deployStaticFolder(Server server, File file) {
        String contextPath = file.getName();
        WebAppContext webApp = createWebAppContext(contextPath);
        webApp.setWar(file.getAbsolutePath());
        server.addHandler(webApp);
        logger.debug("Deploying " + contextPath + " using folder " + file);
    }

    private static WebAppContext createWebAppContext(String contextPath) {
        WebAppContext webApp = new WebAppContext();
        webApp.setParentLoaderPriority(true);
        webApp.setContextPath(contextPath);
        return webApp;
    }
<commit_msg>SCHEDULING-2010 JettyStarter.deployStaticFolder does not prepend / to context path<commit_after>/*
 *  *
 * ProActive Parallel Suite(TM): The Java(TM) library for
 *    Parallel, Distributed, Multi-Core Computing for
 *    Enterprise Grids & Clouds
 *
 * Copyright (C) 1997-2014 INRIA/University of
 *                 Nice-Sophia Antipolis/ActiveEon
 * Contact: <EMAIL> or <EMAIL>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation; version 3 of
 * the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://proactive.inria.fr/team_members.htm
 *  Contributor(s):
 *
 *  * $$PROACTIVE_INITIAL_DEV$$
 */
package org.ow2.proactive.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.objectweb.proactive.core.config.CentralPAPropertyRepository;
import org.ow2.proactive.scheduler.core.properties.PASchedulerProperties;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.HandlerCollection;
import org.mortbay.jetty.webapp.WebAppContext;


public class JettyStarter {

    private static final String FOLDER_TO_DEPLOY = "/dist/war/";
    private static final String REST_CONFIG_PATH = "/config/rest/settings.ini";

    private static Logger logger = Logger.getLogger(JettyStarter.class);

    /**
     * To run Jetty in standalone mode
     */
    public static void main(String[] args) {
        BasicConfigurator.configure();
        CentralPAPropertyRepository.PA_CLASSLOADING_USEHTTP.setValue(false);
        PASchedulerProperties.SCHEDULER_HOME.updateProperty(".");
        JettyStarter.runWars("", "");
    }

    public static void runWars(String rmUrl, String schedulerUrl) {
        Properties properties = readRestProperties();
        if ("true".equals(properties.getProperty("rest.deploy"))) {
            int restPort = Integer.parseInt(properties.getProperty("rest.port", "8080"));

            // for REST API
            setSystemPropertyIfNotDefined("rm.url", rmUrl);
            setSystemPropertyIfNotDefined("scheduler.url", schedulerUrl);

            // for web portals
            setSystemPropertyIfNotDefined("sched.rest.url", "http://localhost:" + restPort + "/rest/rest");
            setSystemPropertyIfNotDefined("rm.rest.url", "http://localhost:" + restPort + "/rest/rest");

            Server server = new Server(restPort);
            server.setStopAtShutdown(true);
            server.setHandler(new HandlerCollection());

            deployWars(server);

            startServer(server, restPort);
        }
    }

    private static void startServer(Server server, int restPort) {
        try {
            if (server.getHandler() == null) {
                logger.info("SCHEDULER_HOME/dist/war folder is empty, nothing is deployed");
            } else {
                server.start();
                if (server.isStarted()) {
                    for (Handler handler : server.getHandlers()) {
                        WebAppContext webAppContext = (WebAppContext) handler;

                        Throwable startException = webAppContext.getUnavailableException();
                        if (startException == null) {
                            logger.info("The web application " + webAppContext.getContextPath()
                              + " created on http://localhost:" + restPort + webAppContext.getContextPath());
                        } else {
                            logger.warn("Failed to start context " + webAppContext.getContextPath(),
                              startException);
                        }
                    }
                } else {
                    logger.warn("Failed to start web modules (REST API, portals)");
                }
            }
        } catch (Exception e) {
            logger.warn("Failed to start web modules (REST API, portals)", e);
        }
    }

    private static void deployWars(Server server) {
        File warFolder = new File(getSchedulerHome() + FOLDER_TO_DEPLOY);
        File[] warFolderContent = warFolder.listFiles();
        if (warFolderContent != null) {
            for (File fileToDeploy : warFolderContent) {
                if (isExplodedWebApp(fileToDeploy)) {
                    deployExplodedWebApp(server, fileToDeploy);
                } else if (isWarFile(fileToDeploy)) {
                    deployWarFile(server, fileToDeploy);
                } else if(isStaticFolder(fileToDeploy)){
                    deployStaticFolder(server, fileToDeploy);
                }
            }
        }
    }

    private static void deployWarFile(Server server, File file) {
        String contextPath = "/" + FilenameUtils.getBaseName(file.getName());
        WebAppContext webApp = createWebAppContext(contextPath);
        webApp.setWar(file.getAbsolutePath());
        server.addHandler(webApp);
        logger.debug("Deploying " + contextPath + " using war file " + file);
    }

    private static void deployExplodedWebApp(Server server, File file) {
        String contextPath = "/" + file.getName();
        WebAppContext webApp = createWebAppContext(contextPath);
        webApp.setDescriptor(new File(file, "/WEB-INF/web.xml").getAbsolutePath());
        webApp.setResourceBase(file.getAbsolutePath());
        server.addHandler(webApp);
        logger.debug("Deploying " + contextPath + " using exploded war " + file);

    }

    private static void deployStaticFolder(Server server, File file) {
        String contextPath = "/" + file.getName();
        WebAppContext webApp = createWebAppContext(contextPath);
        webApp.setWar(file.getAbsolutePath());
        server.addHandler(webApp);
        logger.debug("Deploying " + contextPath + " using folder " + file);
    }

    private static WebAppContext createWebAppContext(String contextPath) {
        WebAppContext webApp = new WebAppContext();
        webApp.setParentLoaderPriority(true);
        webApp.setContextPath(contextPath);
        return webApp;
    }

{'fraction_non_alphanumeric': 0.09780147542180995, 'fraction_numerical': 0.0051128478562559344, 'mean_word_length': 2.4955322951238195, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1092866', 'n_tokens_mistral': 3739, 'n_tokens_neox': 3582, 'n_words': 917}