private void refresh() throws Exception { lock.lock(); try { FileTime lastModifiedTime = Files.getLastModifiedTime(file); String content = Paths2.readText(file, StandardCharsets.UTF_8); if (lastModifiedTime.equals(lastModifiedHolder.get()) && content.equals(contentHolder.get())) { return; } T previous = delegateHolder.getAndSet(null); if (previous != null) { releaser.release(previous); } delegateHolder.set(producer.produce(file, content)); this.lastModifiedHolder.set(lastModifiedTime); this.contentHolder.set(content); } finally { lock.unlock(); } }
public void include(Path path) { assertAtTopLevelOfRatpackDsl("include"); Exceptions.uncheck(() -> { Path resolvedPath = path; if (thisScript != null && !path.isAbsolute()) { resolvedPath = thisScript.resolveSibling(path); } String script = Paths2.readText(resolvedPath, StandardCharsets.UTF_8); RatpackDslClosures closures = new RatpackDslScriptCapture(false, new String[]{}, RatpackDslBacking::new).apply(resolvedPath, script); this.setServerConfig(closures.getServerConfig()); this.setBindings(closures.getBindings()); this.setHandlers(closures.getHandlers()); }); }
/** * Creates a registry building function from a Groovy script. * <p> * This method returns a function that can be used with {@link RatpackServerSpec#registry(Function)} when bootstrapping. * <p> * The script should call the {@link Script#ratpack(Closure)} method, and <b>only</b> invoke the {@link Ratpack#bindings(Closure)} method. * * @param compileStatic whether to statically compile the script * @param scriptPath the path to the script * @param args args to make available to the script via the {@code args} variable * @return a registry definition function * @since 1.1 */ public static Function<Registry, Registry> bindingsWithArgs(boolean compileStatic, String scriptPath, String... args) { checkGroovy(); return r -> { Path scriptFile = r.get(FileSystemBinding.class).file(scriptPath); String script = Paths2.readText(scriptFile, StandardCharsets.UTF_8); Closure<?> bindingsClosure = new RatpackDslScriptCapture(compileStatic, args, BindingsOnly::new).andThen(RatpackDslClosures::getBindings).apply(scriptFile, script); return Guice.registry(bindingsSpec -> { bindingsSpec.bindInstance(new FileBackedReloadInformant(scriptFile)); ClosureUtil.configureDelegateFirst(bindingsSpec, bindingsClosure); }).apply(r); }; } }
private static void doApp(RatpackServerSpec definition, boolean compileStatic, Path baseDir, Path scriptFile, String... args) throws Exception { String script = Paths2.readText(scriptFile, StandardCharsets.UTF_8); RatpackDslClosures closures = new RatpackDslScriptCapture(compileStatic, args, RatpackDslBacking::new).apply(scriptFile, script); definition.serverConfig(ClosureUtil.configureDelegateFirstAndReturn(loadPropsIfPresent(ServerConfig.builder().baseDir(baseDir), baseDir), closures.getServerConfig())); definition.registry(r -> { return Guice.registry(bindingsSpec -> { bindingsSpec.bindInstance(new FileBackedReloadInformant(scriptFile)); ClosureUtil.configureDelegateFirst(bindingsSpec, closures.getBindings()); }).apply(r); }); definition.handler(r -> { return Groovy.chain(r, closures.getHandlers()); }); }