private void loadScripts(List<String> scripts, String resourcePrefix) { for (String script : scripts) { if (script.length() > 4) { log.debug(String.format("Loading template %s from %s", script, resourcePrefix)); String template = ResourceLoader.getResourceAsString(this, resourcePrefix + script); String templateName = script.substring(script.startsWith("/") ? 1 : 0, script.length() - 3); this.defineTemplate(templateName, template); } } }
public static String getResourceAsString(Object context, String path) { InputStream is = (context == null ? ResourceLoader.class : context.getClass()).getResourceAsStream(path); return getResourceFromInputStream(is); }
public static List<String> getScripts(Object context, String resourcePath) { if (resourcePath == null || resourcePath.equals("")) { return null; } URL resourceURL = (context == null ? ResourceLoader.class : context.getClass()).getResource(resourcePath); if (resourceURL == null) { return null; } List<String> retVal = new ArrayList<String>(); if (resourceURL.getProtocol().equals("jar")) { loadFromJar(resourcePath, resourceURL, retVal); } else { loadFromURL(resourceURL, retVal); } // return the whole list return retVal; }
private void loadStartupScript(Object context, String scriptPath) { // Find a script in a location, then run it // start with the resource "autoexec.xxx" if (scriptPath == null) { scriptPath = "/startup/"; } log.debug("Loading scripts in " + scriptPath); List<String> scripts = ResourceLoader.getScripts(context, scriptPath); if (scripts != null) { for (String scriptName : scripts) { log.debug("Loading script at " + scriptPath + scriptName); String scriptBody = ResourceLoader.getResourceAsString(context, scriptPath + scriptName); if (!scriptBody.isEmpty()) { try { RaptureScript script = new RaptureScript(); script.setName(scriptPath + scriptName); script.setScript(scriptBody); ReflexRaptureScript reflex = new ReflexRaptureScript(); reflex.runProgram(ContextFactory.getKernelUser(), null, script, new HashMap<String, Object>()); log.info("Script " + scriptPath + scriptName + " ran successfully"); } catch (RaptureException e) { log.error("Failed to run Script " + scriptPath + scriptName + ": " + e.getMessage()); } } } } log.debug("Finished running scripts in " + scriptPath); }
public ResourceBasedStringTemplateGroup(String name, String primary, String secondary, String tertiary) { super(name, AngleBracketTemplateLexer.class); log.info(String.format("Loading templates for %s from %s", name, primary)); List<String> scripts = ResourceLoader.getScripts(this, primary); log.debug(String.format("Template names are %s", scripts.toString())); loadScripts(scripts, primary); if (secondary != null) { List<String> fallbackScripts = ResourceLoader.getScripts(this, secondary); fallbackScripts.removeAll(scripts); scripts.addAll(fallbackScripts); log.debug(String.format("Secondary template names are %s", fallbackScripts.toString())); loadScripts(fallbackScripts, secondary); } if (tertiary != null) { List<String> fallbackScripts = ResourceLoader.getScripts(this, tertiary); fallbackScripts.removeAll(scripts); log.debug(String.format("Tertiary template names are %s", fallbackScripts.toString())); loadScripts(fallbackScripts, tertiary); } log.info(String.format("Loaded [%s] templates", this.getTemplateNames().size())); log.debug("Loaded templates: " + this.getTemplateNames().toString()); }
public static StringTemplateGroup getApiSingleFileTemplates(String language) throws FileNotFoundException { String name = "/template/" + language + ".stg"; String guts = ResourceLoader.getResourceAsString(new Object(), name); if (guts == null) { throw new FileNotFoundException("Template file not found: " + name); } StringReader reader = new StringReader(guts); return new StringTemplateGroup(reader); }
public static StringTemplateGroup getSdkSingleFileTemplates(String language) throws FileNotFoundException { String name = "/template/" + language + "Addon.stg"; String guts = ResourceLoader.getResourceAsString(new Object(), name); if (guts == null) { throw new FileNotFoundException("Template file not found: " + name); } StringReader reader = new StringReader(guts); return new StringTemplateGroup(reader); }
public static StringTemplateGroup getTemplatesFromFile(String fileName) throws FileNotFoundException { String guts = ResourceLoader.getResourceAsString(new Object(), "/" + fileName); if (guts == null) { throw new FileNotFoundException("Template file not found: " + fileName); } StringReader reader = new StringReader(guts); return new StringTemplateGroup(reader); }
@Override public void setConfig(Map<String, String> config) { boolean useType = "true".equals(config.get("typerow")); String filePath = config.get("filename"); Preconditions.checkArgument(filePath != null, "Mandatory argument filename missing"); String prefix = config.get("prefix"); if (prefix == null) prefix = ""; String content = ResourceLoader.getResourceAsString(null, filePath); CSVSeriesCallback callback = new CSVSeriesCallback(this, useType, prefix); try { CSVExtractor.getCSV(content, callback); } catch (RecognitionException e) { RaptureException raptException = RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "Error parsing csv config"); String message = RaptureExceptionFormatter.getExceptionMessage(raptException, e); log.error(String.format("Error parsing csv content %s: %s", content, message)); throw raptException; } live = true; } }