use of org.apache.velocity.exception.ResourceNotFoundException in project traccar by traccar.
the class NotificationFormatter method getTemplate.
public static Template getTemplate(Event event, String path) {
String templateFilePath;
Template template;
try {
templateFilePath = Paths.get(path, event.getType() + ".vm").toString();
template = Context.getVelocityEngine().getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
} catch (ResourceNotFoundException error) {
Log.warning(error);
templateFilePath = Paths.get(path, "unknown.vm").toString();
template = Context.getVelocityEngine().getTemplate(templateFilePath, StandardCharsets.UTF_8.name());
}
return template;
}
use of org.apache.velocity.exception.ResourceNotFoundException 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.ResourceNotFoundException in project appbundle-maven-plugin by federkasten.
the class CreateApplicationBundleMojo method writeInfoPlist.
/**
* Writes an Info.plist file describing this bundle.
*
* @param infoPlist The file to write Info.plist contents to
* @param files A list of file names of the jar files to add in $JAVAROOT
* @throws MojoExecutionException
*/
private void writeInfoPlist(File infoPlist, List<String> files) throws MojoExecutionException {
Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new MojoLogChute(this));
Velocity.setProperty("file.resource.loader.path", TARGET_CLASS_ROOT);
try {
Velocity.init();
} catch (Exception ex) {
throw new MojoExecutionException("Exception occured in initializing velocity", ex);
}
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("mainClass", mainClass);
velocityContext.put("cfBundleExecutable", javaLauncherName);
velocityContext.put("bundleName", cleanBundleName(bundleName));
velocityContext.put("workingDirectory", workingDirectory);
if (embeddJre && jrePath != null) {
velocityContext.put("jrePath", "JRE");
velocityContext.put("jreFullPath", "");
} else if (embeddJre && jreFullPath != null) {
velocityContext.put("jrePath", "");
velocityContext.put("jreFullPath", jreFullPath);
} else {
velocityContext.put("jrePath", "");
velocityContext.put("jreFullPath", "");
}
if (iconFile == null) {
velocityContext.put("iconFile", "GenericJavaApp.icns");
} else {
File f = searchFile(iconFile, project.getBasedir());
velocityContext.put("iconFile", (f != null && f.exists() && f.isFile()) ? f.getName() : "GenericJavaApp.icns");
}
velocityContext.put("version", version);
velocityContext.put("jvmVersion", jvmVersion);
StringBuilder options = new StringBuilder();
options.append("<array>").append("\n ");
for (String jvmOption : defaultJvmOptions) {
options.append(" ").append("<string>").append(jvmOption).append("</string>").append("\n");
}
options.append(" ").append("<string>").append("-Xdock:name=" + bundleName).append("</string>").append("\n");
if (jvmOptions != null) {
for (String jvmOption : jvmOptions) {
options.append(" ").append("<string>").append(jvmOption).append("</string>").append("\n");
}
}
options.append(" ").append("</array>");
velocityContext.put("jvmOptions", options);
StringBuilder jarFiles = new StringBuilder();
jarFiles.append("<array>").append("\n");
for (String file : files) {
jarFiles.append(" ").append("<string>").append(file).append("</string>").append("\n");
}
if (additionalClasspath != null) {
for (String pathElement : additionalClasspath) {
jarFiles.append(" ").append("<string>").append(pathElement).append("</string>");
}
}
jarFiles.append(" ").append("</array>");
velocityContext.put("classpath", jarFiles.toString());
try {
File sourceInfoPlist = new File(TARGET_CLASS_ROOT, dictionaryFile);
if (sourceInfoPlist.exists() && sourceInfoPlist.isFile()) {
String encoding = detectEncoding(sourceInfoPlist);
getLog().debug("Detected encoding " + encoding + " for dictionary file " + dictionaryFile);
Writer writer = new OutputStreamWriter(new FileOutputStream(infoPlist), encoding);
Template template = Velocity.getTemplate(dictionaryFile, encoding);
template.merge(velocityContext, writer);
writer.close();
} else {
Writer writer = new OutputStreamWriter(new FileOutputStream(infoPlist), "UTF-8");
velocity.getEngine().mergeTemplate(dictionaryFile, "UTF-8", velocityContext, writer);
writer.close();
}
} catch (IOException ex) {
throw new MojoExecutionException("Could not write Info.plist to file " + infoPlist, ex);
} catch (ParseErrorException ex) {
throw new MojoExecutionException("Error parsing " + dictionaryFile, ex);
} catch (ResourceNotFoundException ex) {
throw new MojoExecutionException("Could not find resource for template " + dictionaryFile, ex);
} catch (MethodInvocationException ex) {
throw new MojoExecutionException("MethodInvocationException occured merging Info.plist template " + dictionaryFile, ex);
} catch (Exception ex) {
throw new MojoExecutionException("Exception occured merging Info.plist template " + dictionaryFile, ex);
}
}
use of org.apache.velocity.exception.ResourceNotFoundException in project new-cloud by xie-summer.
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.ResourceNotFoundException in project maven-plugins by apache.
the class VelocityTemplate method generate.
/**
* Using a specified Velocity Template and provided context, create the outputFilename.
*
* @param outputFilename the file to be generated.
* @param template the velocity template to use.
* @param context the velocity context map.
* @throws VelocityException if the template was not found or any other Velocity exception.
* @throws MojoExecutionException if merging the velocity template failed.
* @throws IOException if there was an error when writing to the output file.
*/
public void generate(String outputFilename, String template, Context context) throws VelocityException, MojoExecutionException, IOException {
Writer writer = null;
try {
File f = new File(outputFilename);
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
writer = new FileWriter(f);
getVelocity().getEngine().mergeTemplate(templateDirectory + "/" + template, context, writer);
} catch (ResourceNotFoundException e) {
throw new ResourceNotFoundException("Template not found: " + templateDirectory + "/" + template, e);
} catch (VelocityException | IOException e) {
// to get past generic catch for Exception.
throw e;
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
} finally {
if (writer != null) {
writer.flush();
writer.close();
getLog().debug("File " + outputFilename + " created...");
}
}
}
Aggregations