io.proscript.jlight.Runtime.java Source code

Java tutorial

Introduction

Here is the source code for io.proscript.jlight.Runtime.java

Source

/*
 * Copyright 2015 Matiss Treinis <mrtreinis@gmail.com>
 *
 * This file is part of JLight project.
 * Project home: http://proscript.io/jlight/
 *
 * 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 io.proscript.jlight;

import org.apache.commons.cli.CommandLine;

import javax.tools.Diagnostic;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class Runtime {
    private static final Logger log = Logger.getLogger("jlight");

    public static void run(CommandLine line) throws IOException {

        Environment environment = new Environment();

        if (1 == line.getArgList().size()) {
            File cwdCandidate = new File(line.getArgList().get(0));
            if (cwdCandidate.isDirectory()) {
                environment.setCwd(cwdCandidate);
            } else {
                throw new IOException("Last argument must be valid source root directory");
            }
        } else {
            environment.setCwd(new File(System.getProperty("user.dir")));
        }

        if (line.hasOption("port")) {
            environment.setPort(Integer.valueOf(line.getOptionValue("port")));
        }

        if (line.hasOption("host")) {
            environment.setHost(line.getOptionValue("host"));
        }

        environment.setCacheDirectory(new File(environment.getCwd().toString() + "/.jlcache"));
        environment.setMainClassName(line.getOptionValue("class"));

        CacheManager cacheManager = new CacheManager(environment);

        try {
            cacheManager.initialize();
        } catch (IOException e) {
            log.log(Level.SEVERE, "Failed to initialize class file cache: %s", e.getMessage());
            return;
        }

        Collection<File> javaFiles = SourcesLocator.locateRecursive(environment.getCwd());

        ApplicationCompiler compiler = new ApplicationCompiler(environment.getCacheDirectory());

        boolean result = false;

        try {
            result = compiler.compile(javaFiles);
        } catch (IOException e) {
            log.log(Level.SEVERE, "Compilation failed: %s", e.getMessage());
            return;
        }

        if (!result) {
            for (Diagnostic diagnostic : compiler.getLastDiagnosticCollector().getDiagnostics()) {
                System.out.println(diagnostic.getMessage(Locale.getDefault()));
                LogRecord record = new LogRecord(Level.SEVERE, diagnostic.getMessage(Locale.getDefault()));
                log.log(record);
            }
        } else {

            Application application = buildApplication(environment);
            ThreadedHTTPServer server = new ThreadedHTTPServer(environment);
            server.run(application);
        }

        try {
            cacheManager.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static Application buildApplication(Environment environment) {
        ClassLoader cl = null;
        try {
            cl = ClassLoaderBuilder.build(environment.getCacheDirectory());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            return new Application(cl, environment.getMainClassName(), "main");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

}