use of org.apache.velocity.app.VelocityEngine in project carbon-apimgt by wso2.
the class APIThrottlePolicyTemplateBuilder method getThrottlePolicyTemplateForPipelines.
/**
* Generate policy for api level throttling
*
* @return throttle policies for api level
* @throws APITemplateException throws if generation failure occur
*/
public Map<String, String> getThrottlePolicyTemplateForPipelines() throws APITemplateException {
if (log.isDebugEnabled()) {
log.debug("Generating Siddhi App for apiLevel :" + apiPolicy.toString());
}
// get velocity template for API policy pipeline and generate the template
Map<String, String> policyArray = new HashMap<String, String>();
StringWriter writer;
VelocityContext context;
VelocityEngine velocityengine = initVelocityEngine();
Template template = velocityengine.getTemplate(getTemplatePathForAPI());
// Generate template for pipeline conditions if pipelines not null
if (apiPolicy.getPipelines() != null) {
for (Pipeline pipeline : apiPolicy.getPipelines()) {
// set values for velocity context
context = new VelocityContext();
setConstantContext(context);
context.put(PIPELINE_ITEM, pipeline);
context.put(POLICY, apiPolicy);
context.put(QUOTA_POLICY, pipeline.getQuotaPolicy());
context.put(PIPELINE, CONDITION + UNDERSCORE + pipeline.getId());
String conditionString = getPolicyCondition(pipeline.getConditions());
context.put(CONDITION, AND + conditionString);
writer = new StringWriter();
template.merge(context, writer);
if (log.isDebugEnabled()) {
log.debug("Generated Siddhi App : " + writer.toString());
}
String policyName = PolicyConstants.POLICY_LEVEL_RESOURCE + UNDERSCORE + apiPolicy.getPolicyName() + UNDERSCORE + CONDITION + UNDERSCORE + pipeline.getId();
policyArray.put(policyName, writer.toString());
}
}
return policyArray;
}
use of org.apache.velocity.app.VelocityEngine in project carbon-apimgt by wso2.
the class GatewaySourceGeneratorImpl method getConfigStringFromTemplate.
@Override
public String getConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources) throws APITemplateException {
StringWriter writer = new StringWriter();
String templatePath = "resources" + File.separator + "template" + File.separator + "template.xml";
try {
// build the context for template and apply the necessary decorators
apiConfigContext.validate();
ConfigContext configContext = new ResourceConfigContext(apiConfigContext, apiResources);
VelocityContext context = configContext.getContext();
VelocityEngine velocityengine = new VelocityEngine();
velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
velocityengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
velocityengine.init();
Template template = velocityengine.getTemplate(templatePath);
template.merge(context, writer);
} catch (ResourceNotFoundException e) {
log.error("Template " + templatePath + " not Found", e);
throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
} catch (ParseErrorException e) {
log.error("Syntax error in " + templatePath, e);
throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
}
return writer.toString();
}
use of org.apache.velocity.app.VelocityEngine in project carbon-apimgt by wso2.
the class GatewaySourceGeneratorImpl method getCompositeAPIConfigStringFromTemplate.
@Override
public String getCompositeAPIConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources, List<CompositeAPIEndpointDTO> compositeApiEndpoints) throws APITemplateException {
StringWriter writer = new StringWriter();
String templatePath = "resources" + File.separator + "template" + File.separator + "composite_template.xml";
try {
// build the context for template and apply the necessary decorators
apiConfigContext.validate();
CompositeAPIConfigContext configContext = new CompositeAPIConfigContext(apiConfigContext, apiResources, compositeApiEndpoints);
VelocityContext context = configContext.getContext();
VelocityEngine velocityengine = new VelocityEngine();
velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
velocityengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
velocityengine.init();
Template template = velocityengine.getTemplate(templatePath);
template.merge(context, writer);
} catch (ResourceNotFoundException e) {
log.error("Template " + templatePath + " not Found", e);
throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
} catch (ParseErrorException e) {
log.error("Syntax error in " + templatePath, e);
throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
}
return writer.toString();
}
use of org.apache.velocity.app.VelocityEngine in project tomee by apache.
the class Container method copyTemplateTo.
private void copyTemplateTo(final File targetDir, final String filename) throws Exception {
final File file = new File(targetDir, filename);
if (file.exists()) {
return;
}
// don't break apps using Velocity facade
final VelocityEngine engine = new VelocityEngine();
engine.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new NullLogChute());
engine.setProperty(Velocity.RESOURCE_LOADER, "class");
engine.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader");
engine.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName());
engine.init();
final Template template = engine.getTemplate("/org/apache/tomee/configs/" + filename);
final VelocityContext context = new VelocityContext();
context.put("tomcatHttpPort", Integer.toString(configuration.getHttpPort()));
context.put("tomcatShutdownPort", Integer.toString(configuration.getStopPort()));
final Writer writer = new FileWriter(file);
template.merge(context, writer);
writer.flush();
writer.close();
}
use of org.apache.velocity.app.VelocityEngine in project gocd by gocd.
the class VelocityTemplateEngineFactory method afterPropertiesSet.
@Override
public void afterPropertiesSet() throws Exception {
Properties properties = new Properties();
// this is setup so that file takes preference over classpath, so that we can reload stuff in dev environment
// and fallback to spring classloader in prod environment
properties.setProperty(RESOURCE_LOADER, "file," + SpringResourceLoader.NAME);
properties.setProperty(FILE_RESOURCE_LOADER_CACHE, "true");
properties.setProperty(EVENTHANDLER_REFERENCEINSERTION, org.apache.velocity.app.event.implement.EscapeHtmlReference.class.getName());
properties.setProperty(FILE_RESOURCE_LOADER_PATH, resourceBasePaths);
properties.setProperty("file.resource.loader.modificationCheckInterval", String.valueOf(modificationCheckInterval));
properties.setProperty(SpringResourceLoader.SPRING_RESOURCE_LOADER_CLASS, SpringResourceLoader.class.getName());
properties.setProperty(SpringResourceLoader.SPRING_RESOURCE_LOADER_CACHE, "true");
if (modificationCheckInterval != 0) {
properties.setProperty(VM_LIBRARY_AUTORELOAD, "true");
}
properties.setProperty(VM_LIBRARY, "macros/urls.vm");
this.velocityEngine = new VelocityEngine(properties);
this.velocityEngine.setApplicationAttribute(SpringResourceLoader.SPRING_RESOURCE_LOADER, resourceLoader);
this.velocityEngine.setApplicationAttribute(SpringResourceLoader.SPRING_RESOURCE_LOADER_PATH, resourceBasePaths);
}
Aggregations