Search in sources :

Example 6 with GroovitySource

use of com.disney.groovity.source.GroovitySource in project groovity by disney.

the class Groovity method compile.

/**
 * Compile a named set of source paths; compilation also handles static init and destroy lifecycle
 * management and jar file creation
 *
 * @param force if true, recompile all sources, if false recompile only changed sources
 * @param sourcePaths a list of source paths to compile
 * @throws Exception
 */
public void compile(boolean force, boolean init, List<String> sourcePaths) throws Exception {
    if (inCompile.compareAndSet(false, true)) {
        try {
            compileEvents.clear();
            List<GroovitySource> sources = new ArrayList<GroovitySource>();
            for (int i = 0; i < sourcePaths.size(); i++) {
                String path = sourcePaths.get(i);
                try {
                    GroovitySource source = null;
                    Exception error = null;
                    for (GroovitySourceLocator sourceLocator : sourceLocators) {
                        try {
                            source = sourceLocator.getGroovityScriptSource(path);
                            if (source != null && source.exists()) {
                                break;
                            }
                        } catch (Exception e) {
                            error = e;
                        }
                    }
                    if (source != null) {
                        sources.add(source);
                    } else if (error != null) {
                        throw error;
                    } else {
                        throw new FileNotFoundException(path);
                    }
                } catch (Exception e) {
                    log.log(Level.SEVERE, "Unable to load source " + path, e);
                }
            }
            compile(force, init, sources.toArray(new GroovitySource[0]));
        } finally {
            inCompile.set(false);
        }
    } else {
        throw new RuntimeException("Groovy compiler is already busy, please retry later");
    }
}
Also used : GroovitySourceLocator(com.disney.groovity.source.GroovitySourceLocator) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) GroovitySource(com.disney.groovity.source.GroovitySource) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 7 with GroovitySource

use of com.disney.groovity.source.GroovitySource in project groovity by disney.

the class GroovityServletContainer method enterConsole.

public void enterConsole() {
    groovity.getArgsLookup().chainLast(new ArgsLookup.ConsoleArgsLookup());
    try {
        Console console = System.console();
        if (console == null) {
            log.warning("Unable to retrieve System.console(), GroovityServletContainer will not be interactive");
            try {
                Thread.currentThread().join();
            } catch (InterruptedException e) {
            }
            return;
        }
        String cmdFmt = "(l)ist, (a)ll, (q)uit, or script path [%1$s] : ";
        String scriptPath = "";
        String command = console.readLine(cmdFmt, scriptPath);
        while (!"q".equals(command)) {
            if (!command.trim().isEmpty()) {
                scriptPath = command.trim();
            }
            if (scriptPath.isEmpty()) {
                console.printf("Error: enter a script path or q to quit\n");
            } else {
                // pick up any code changes
                groovity.compileAll(false, true);
                final AtomicBoolean error = new AtomicBoolean(false);
                groovity.getCompilerEvents().forEach((k, v) -> {
                    if (v.getError() != null) {
                        error.set(true);
                    }
                });
                if (!error.get()) {
                    String[] pathParts;
                    if ("a".equals(scriptPath) || "l".equals(scriptPath)) {
                        ArrayList<String> paths = new ArrayList<>();
                        for (GroovitySourceLocator locator : groovity.getSourceLocators()) {
                            if (locator instanceof FileGroovitySourceLocator) {
                                String directory = ((FileGroovitySourceLocator) locator).getDirectory().toURI().toString();
                                if (consoleSourceLocations.contains(directory)) {
                                    for (GroovitySource source : locator) {
                                        paths.add(source.getPath().substring(0, source.getPath().lastIndexOf(".")));
                                    }
                                }
                            }
                        }
                        if (paths.isEmpty()) {
                            paths.addAll(groovity.getGroovityScriptNames());
                        }
                        pathParts = paths.toArray(new String[0]);
                    } else {
                        pathParts = scriptPath.split("\\s*,\\s*");
                    }
                    if ("l".equals(scriptPath)) {
                        console.printf("All available paths:\n");
                        for (String pathPart : pathParts) {
                            console.printf("\t%1$s\n", pathPart);
                        }
                    } else {
                        for (String pathPart : pathParts) {
                            try {
                                run(pathPart);
                            } catch (Throwable e) {
                                console.printf("%1$s running script %2$s :\n", e.getClass().getName(), pathPart);
                                e.printStackTrace(console.writer());
                            }
                        }
                    }
                }
            }
            command = console.readLine(cmdFmt, scriptPath);
        }
    } finally {
        groovity.getArgsLookup().removeLast();
    }
}
Also used : ArrayList(java.util.ArrayList) FileGroovitySourceLocator(com.disney.groovity.source.FileGroovitySourceLocator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArgsLookup(com.disney.groovity.ArgsLookup) GroovitySourceLocator(com.disney.groovity.source.GroovitySourceLocator) FileGroovitySourceLocator(com.disney.groovity.source.FileGroovitySourceLocator) Console(java.io.Console) GroovitySource(com.disney.groovity.source.GroovitySource)

Aggregations

GroovitySource (com.disney.groovity.source.GroovitySource)7 ArrayList (java.util.ArrayList)7 GroovitySourceLocator (com.disney.groovity.source.GroovitySourceLocator)5 Script (groovy.lang.Script)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 IOException (java.io.IOException)4 GroovityClassLoader (com.disney.groovity.compile.GroovityClassLoader)3 FileNotFoundException (java.io.FileNotFoundException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 GroovyClass (org.codehaus.groovy.tools.GroovyClass)3 GroovityCompilerEvent (com.disney.groovity.compile.GroovityCompilerEvent)2 FileGroovitySourceLocator (com.disney.groovity.source.FileGroovitySourceLocator)2 File (java.io.File)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ArgsLookup (com.disney.groovity.ArgsLookup)1