use of org.apache.velocity.exception.MethodInvocationException in project cachecloud by sohutv.
the class VelocityUtils method createText.
/**
* 邮件模板
*
* @param appDesc 应用信息
* @param appAudit 处理信息
* @param templatePath 模板路径
* @param customCharset 编码
*/
public static synchronized String createText(VelocityEngine engine, AppDesc appDesc, AppAudit appAudit, AppDailyData appDailyData, List<InstanceAlertValueResult> instanceAlertValueResultList, String templatePath, String customCharset) {
if (!StringUtils.isEmpty(customCharset)) {
charset = customCharset;
}
Properties p = new Properties();
p.setProperty("file.resource.loader.path", Thread.currentThread().getContextClassLoader().getResource("").getPath());
p.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
Velocity.init(p);
logger.info("velocity: init done.");
VelocityContext context = new VelocityContext();
context.put("appDesc", appDesc);
context.put("appAudit", appAudit);
context.put("appDailyData", appDailyData);
context.put("instanceAlertValueResultList", instanceAlertValueResultList);
context.put("numberTool", new NumberTool());
context.put("ccDomain", ConstUtils.CC_DOMAIN);
context.put("decimalFormat", new DecimalFormat("###,###"));
context.put("StringUtils", StringUtils.class);
FileOutputStream fos = null;
StringWriter writer = null;
try {
Template template = engine.getTemplate(templatePath);
writer = new StringWriter();
template.merge(context, writer);
} catch (ResourceNotFoundException ex) {
logger.error("error: velocity vm resource not found.", ex);
} catch (ParseErrorException ex) {
logger.error("error: velocity parse vm file error.", ex);
} catch (MethodInvocationException ex) {
logger.error("error: velocity template merge.", ex);
} catch (Exception ex) {
logger.error("error", ex);
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
logger.error("error: close writer", e);
}
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
logger.error("error: close output stream.", e);
}
}
logger.info("velocity: create text done.");
if (writer != null) {
return writer.toString();
}
return null;
}
use of org.apache.velocity.exception.MethodInvocationException in project cloudconductor-agent-redhat by cinovo.
the class ServerCom method getFileData.
/**
* @param cf the file
* @return the data
* @throws CloudConductorException thrown if communication with cloudconductor failed
* @throws TransformationErrorException error on generating the localized config file
*/
public static String getFileData(ConfigFile cf) throws CloudConductorException, TransformationErrorException {
try {
String content = ServerCom.agent.getConfigFileData(cf.getName());
content = content.replaceAll("\\r\\n", "\n");
content = content.replaceAll("\\r", "\n");
if (!cf.isTemplate()) {
return content;
}
StringWriter w = new StringWriter();
try {
Velocity.evaluate(AgentState.vContext(), w, "configfileGen", content);
} catch (ParseErrorException | MethodInvocationException | ResourceNotFoundException | IOException e) {
throw new TransformationErrorException("Failed to generate template", e);
}
return w.toString();
} catch (RuntimeException e) {
throw new CloudConductorException(e.getMessage());
}
}
use of org.apache.velocity.exception.MethodInvocationException in project BIMserver by opensourceBIM.
the class TemplateEngine method process.
public String process(Map<String, Object> context, TemplateIdentifier templateIdentifier) {
StringWriter stringWriter = new StringWriter();
VelocityContext velocityContext = new VelocityContext(context);
try {
velocityEngine.mergeTemplate(templateIdentifier.getFileName(), "UTF-8", velocityContext, stringWriter);
return stringWriter.toString();
} catch (ResourceNotFoundException e) {
LOGGER.error("", e);
} catch (ParseErrorException e) {
LOGGER.error("", e);
} catch (MethodInvocationException e) {
LOGGER.error("", e);
} catch (Exception e) {
LOGGER.error("", e);
}
return "";
}
Aggregations