Last active
July 22, 2019 00:05
-
-
Save scaryghost/77372c919ec43e01258632545835261c to your computer and use it in GitHub Desktop.
Sample code with groovy class loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This Java source file was generated by the Gradle 'init' task. | |
*/ | |
import groovy.lang.GroovyClassLoader; | |
import groovy.cli.commons.CliBuilder; | |
import groovy.cli.Option; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.*; | |
import java.util.function.IntSupplier; | |
import static java.nio.file.StandardWatchEventKinds.*; | |
public class App { | |
interface OptionInterface { | |
@Option(longName="root", description="Path to the root directory to load plugins") String root(); | |
@Option(longName="plugin", description="Name of the plugin to load") String plugin(); | |
} | |
public static void main(String[] args) throws IOException { | |
CliBuilder builder = new CliBuilder(); | |
builder.setUsage("groovy-plugin --root [path]"); | |
OptionInterface options = builder.parseFromSpec(OptionInterface.class, args); | |
Path root = Paths.get(options.root()); | |
GroovyClassLoader gcl= new GroovyClassLoader(); | |
gcl.addClasspath(options.root()); | |
WatchService watcher = FileSystems.getDefault().newWatchService(); | |
Files.walk(root) | |
.filter(p -> p.toFile().isDirectory()) | |
.forEach(p -> { | |
try { | |
System.out.println("Registering '" + p.toAbsolutePath().toString() + "'"); | |
p.register(watcher, ENTRY_CREATE, ENTRY_MODIFY); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
}); | |
while(true) { | |
try { | |
WatchKey result = watcher.take(); | |
for(WatchEvent<?> e: result.pollEvents()) { | |
Path resultRoot = (Path) result.watchable(); | |
Path relative = Paths.get(options.root()).relativize(resultRoot); | |
String filename = relative.resolve(((WatchEvent<Path>) e).context()).toString(); | |
if (filename.endsWith(".groovy")) { | |
String classname = filename.substring(0, filename.lastIndexOf('.')) | |
.replace(File.separator, "."); | |
if (classname.contains(options.plugin())) { | |
Class<?> clazz = gcl.loadClass(classname, true, false); | |
Object newObj = clazz.newInstance(); | |
System.out.printf("%s()=%d%n", | |
filename, | |
((IntSupplier) newObj).getAsInt() | |
); | |
} | |
} | |
} | |
result.reset(); | |
} catch (InterruptedException | InstantiationException | IllegalAccessException | ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.cli.Option; | |
import groovy.cli.commons.CliBuilder; | |
import groovy.lang.Binding; | |
import groovy.util.GroovyScriptEngine; | |
import groovy.util.ResourceException; | |
import groovy.util.ScriptException; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.nio.file.*; | |
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE; | |
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; | |
public class AppScript { | |
interface OptionInterface { | |
@Option(longName="root", description="Path to the root directory to load plugins") String root(); | |
@Option(longName="script", description="Groovy script to execute") String script(); | |
} | |
public static void main(String[] args) throws IOException { | |
CliBuilder builder = new CliBuilder(); | |
builder.setUsage("groovy-plugin --root [path] --script [name]"); | |
OptionInterface options = builder.parseFromSpec(OptionInterface.class, args); | |
Path root = Paths.get(options.root()); | |
WatchService watcher = FileSystems.getDefault().newWatchService(); | |
Files.walk(root) | |
.filter(p -> p.toFile().isDirectory()) | |
.forEach(p -> { | |
try { | |
System.out.println("Registering '" + p.toAbsolutePath().toString() + "'"); | |
p.register(watcher, ENTRY_CREATE, ENTRY_MODIFY); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
}); | |
Binding binding = new Binding(); | |
binding.setVariable("n", 10); | |
GroovyScriptEngine engine = new GroovyScriptEngine(new URL[] { | |
root.toUri().toURL() | |
}); | |
while(true) { | |
try { | |
WatchKey result = watcher.take(); | |
for(WatchEvent<?> e: result.pollEvents()) { | |
Path resultRoot = (Path) result.watchable(); | |
Path relative = Paths.get(options.root()).relativize(resultRoot); | |
String filename = relative.resolve(((WatchEvent<Path>) e).context()).toString(); | |
if (filename.equals(options.script())) { | |
engine.run(filename, binding); | |
} | |
} | |
result.reset(); | |
} catch (InterruptedException | ResourceException | ScriptException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment