protected void execute(GroovyCodeSource s) { try { createShell().evaluate(s); } catch (RuntimeException x) { LOGGER.log(WARNING, "Failed to run script " + s.getName(), x); } }
private void updateLocalDependencyCache(GroovyCodeSource codeSource, LocalData localData) { // we put the old dependencies into local cache so createCompilationUnit // can pick it up. We put that entry under the name "." ScriptCacheEntry origEntry = scriptCache.get(codeSource.getName()); Set<String> origDep = null; if (origEntry != null) origDep = origEntry.dependencies; if (origDep != null) { Set<String> newDep = new HashSet<String>(origDep.size()); for (String depName : origDep) { ScriptCacheEntry dep = scriptCache.get(depName); try { if (origEntry == dep || GroovyScriptEngine.this.isSourceNewer(dep)) { newDep.add(depName); } } catch (ResourceException re) { } } StringSetMap cache = localData.dependencyCache; cache.put(".", newDep); } }
private String genSourceCacheKey(GroovyCodeSource codeSource) { StringBuilder strToDigest; String scriptText = codeSource.getScriptText(); if (null != scriptText) { strToDigest = new StringBuilder((int) (scriptText.length() * 1.2)); strToDigest.append("scriptText:").append(scriptText); CodeSource cs = codeSource.getCodeSource(); if (null != cs) { strToDigest.append("/codeSource:").append(cs); } } else { strToDigest = new StringBuilder(32); // if the script text is null, i.e. the script content is invalid // use the name as cache key for the time being to trigger the validation by `groovy.lang.GroovyClassLoader.validate` // note: the script will not be cached due to the invalid script content, // so it does not matter even if cache key is not the md5 value of script content strToDigest.append("name:").append(codeSource.getName()); } try { return EncodingGroovyMethods.md5(strToDigest); } catch (NoSuchAlgorithmException e) { throw new GroovyRuntimeException(e); // should never reach here! } }
su = unit.addSource(url); } else { su = unit.addSource(codeSource.getName(), codeSource.getScriptText());
/** * Load a LensKit configuration script. This method is for internal use that needs to override how configuration * scripts are loaded. * * @param source The source * @param base The base URI for this configuration * @return the configuration script. * @throws RecommenderConfigurationException */ public LenskitConfigScript loadScript(GroovyCodeSource source, URI base) throws RecommenderConfigurationException { logger.debug("loading script from {}", source.getName()); LenskitConfigScript script; try { script = (LenskitConfigScript) shell.parse(source); } catch (GroovyRuntimeException e) { throw new RecommenderConfigurationException("Error loading Groovy script", e); } script.setDelegate(new LenskitConfigDSL(this, base)); return script; }
/** * Parses the given code source into a Java class. If there is a class file * for the given code source, then no parsing is done, instead the cached class is returned. * * @param shouldCacheSource if true then the generated class will be stored in the source cache * @return the main class defined in the given script */ public Class parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource) throws CompilationFailedException { synchronized (sourceCache) { Class answer = sourceCache.get(codeSource.getName()); if (answer != null) return answer; answer = doParseClass(codeSource); if (shouldCacheSource) sourceCache.put(codeSource.getName(), answer); return answer; } }
protected void execute(GroovyCodeSource s) { try { createShell().evaluate(s); } catch (RuntimeException x) { LOGGER.log(WARNING, "Failed to run script " + s.getName(), x); } }
/** * Load a LensKit configuration script. This method is for internal use that needs to override how configuration * scripts are loaded. * * @param source The source * @param base The base URI for this configuration * @return the configuration script. * @throws RecommenderConfigurationException */ public LenskitConfigScript loadScript(GroovyCodeSource source, URI base) throws RecommenderConfigurationException { logger.info("loading script from {}", source.getName()); LenskitConfigScript script; try { script = (LenskitConfigScript) shell.parse(source); } catch (GroovyRuntimeException e) { throw new RecommenderConfigurationException("Error loading Groovy script", e); } script.setDelegate(new LenskitConfigDSL(this, base)); return script; }
ScriptCacheEntry origEntry = scriptCache.get(codeSource.getName()); Set<String> origDep = null; if (origEntry != null) origDep = origEntry.dependencies;
Class answer = (Class) sourceCache.get(codeSource.getName()); if (answer != null) return answer; SourceUnit su = null; if (codeSource.getFile() == null) { su = unit.addSource(codeSource.getName(), codeSource.getInputStream()); } else { su = unit.addSource(codeSource.getFile()); setClassCacheEntry(clazz); if (shouldCacheSource) sourceCache.put(codeSource.getName(), answer); return answer;
Class answer = (Class) sourceCache.get(codeSource.getName()); if (answer != null) return answer; SourceUnit su = null; if (codeSource.getFile() == null) { su = unit.addSource(codeSource.getName(), codeSource.getInputStream()); } else { su = unit.addSource(codeSource.getFile()); setClassCacheEntry(clazz); if (shouldCacheSource) sourceCache.put(codeSource.getName(), answer); return answer;
Class answer = (Class) sourceCache.get(codeSource.getName()); if (answer != null) return answer; SourceUnit su = null; if (codeSource.getFile() == null) { su = unit.addSource(codeSource.getName(), codeSource.getInputStream()); } else { su = unit.addSource(codeSource.getFile()); setClassCacheEntry(clazz); if (shouldCacheSource) sourceCache.put(codeSource.getName(), answer); return answer;
private Class doParseClass(GroovyCodeSource codeSource) { validate(codeSource); Class answer; // Was neither already loaded nor compiling, so compile and add to cache. CompilationUnit unit = createCompilationUnit(config, codeSource.getCodeSource()); SourceUnit su = null; if (codeSource.getFile() == null) { su = unit.addSource(codeSource.getName(), codeSource.getScriptText()); } else { su = unit.addSource(codeSource.getFile()); } ClassCollector collector = createCollector(unit, su); unit.setClassgenCallback(collector); int goalPhase = Phases.CLASS_GENERATION; if (config != null && config.getTargetDirectory() != null) goalPhase = Phases.OUTPUT; unit.compile(goalPhase); answer = collector.generatedClass; String mainClass = su.getAST().getMainClassName(); for (Object o : collector.getLoadedClasses()) { Class clazz = (Class) o; String clazzName = clazz.getName(); definePackage(clazzName); setClassCacheEntry(clazz); if (clazzName.equals(mainClass)) answer = clazz; } return answer; }