Search in sources :

Example 21 with ResourceNotFoundException

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;
}
Also used : ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template)

Example 22 with ResourceNotFoundException

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());
    }
}
Also used : CloudConductorException(de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException) StringWriter(java.io.StringWriter) ParseErrorException(org.apache.velocity.exception.ParseErrorException) TransformationErrorException(de.cinovo.cloudconductor.agent.exceptions.TransformationErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 23 with ResourceNotFoundException

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);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template) FileOutputStream(java.io.FileOutputStream) MojoLogChute(sh.tak.appbundler.logging.MojoLogChute) OutputStreamWriter(java.io.OutputStreamWriter) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Example 24 with ResourceNotFoundException

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;
}
Also used : NumberTool(org.apache.velocity.tools.generic.NumberTool) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) DecimalFormat(java.text.DecimalFormat) FileOutputStream(java.io.FileOutputStream) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) IOException(java.io.IOException) Properties(java.util.Properties) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template)

Example 25 with ResourceNotFoundException

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...");
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileWriter(java.io.FileWriter) VelocityException(org.apache.velocity.exception.VelocityException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) VelocityException(org.apache.velocity.exception.VelocityException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Aggregations

ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)31 ParseErrorException (org.apache.velocity.exception.ParseErrorException)21 VelocityContext (org.apache.velocity.VelocityContext)17 Template (org.apache.velocity.Template)16 IOException (java.io.IOException)15 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)14 StringWriter (java.io.StringWriter)11 File (java.io.File)7 VelocityEngine (org.apache.velocity.app.VelocityEngine)7 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 FileOutputStream (java.io.FileOutputStream)4 FileWriter (java.io.FileWriter)4 Writer (java.io.Writer)4 SystemException (com.github.bordertech.wcomponents.util.SystemException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 PrintWriter (java.io.PrintWriter)3 Context (org.apache.velocity.context.Context)3 VelocityException (org.apache.velocity.exception.VelocityException)3 WComponent (com.github.bordertech.wcomponents.WComponent)2 FileInputStream (java.io.FileInputStream)2