Java tutorial
/* * Copyrights 2002-2013 Webb Fontaine * Developer: Sargis Harutyunyan * Date: 11 avr. 2013 * This software is the proprietary information of Webb Fontaine. * Its use is subject to License terms. */ package com.webbfontaine.valuewebb.irms.core; import com.google.common.base.Throwables; import com.webbfontaine.twm.urmcore.server.script.AbstractScriptSourceGenerator; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.RandomStringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.stringtemplate.v4.ST; import java.io.IOException; import java.io.InputStream; public abstract class DefaultRuleScriptSourceGenerator extends AbstractScriptSourceGenerator<String> { private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRuleScriptSourceGenerator.class); @Override public String generateScriptName(String baseName, String scriptText) { return String.format("%s%s%d", baseName, RandomStringUtils.randomAlphabetic(8), System.nanoTime()); } @Override public String generateScriptSource(String scriptName, String script) { ST stringTemplate = getScriptStringTemplate(); setAttributes(stringTemplate); stringTemplate.add("name", scriptName); stringTemplate.add("script", script); String generatedScript = stringTemplate.render(); LOGGER.debug("Generated script: {}", generatedScript); return generatedScript; } @SuppressWarnings("IOResourceOpenedButNotSafelyClosed") @Override protected String getTemplateDefinition() { InputStream resourceAsStream = null; try { resourceAsStream = Thread.currentThread().getContextClassLoader() .getResourceAsStream(getTemplateResourcePath()); return IOUtils.toString(resourceAsStream); } catch (IOException e) { throw Throwables.propagate(e); } finally { IOUtils.closeQuietly(resourceAsStream); } } protected void setAttributes(ST stringTemplate) { stringTemplate.add("imports", getScriptImports()); stringTemplate.add("base", getBaseBeanClassName()); } protected abstract String getTemplateResourcePath(); protected abstract String getBaseBeanClassName(); protected abstract String getScriptImports(); }