Search in sources :

Example 16 with MethodInvocationException

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;
}
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 17 with MethodInvocationException

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());
    }
}
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 18 with MethodInvocationException

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 "";
}
Also used : StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Aggregations

MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)18 ParseErrorException (org.apache.velocity.exception.ParseErrorException)13 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)13 IOException (java.io.IOException)9 Template (org.apache.velocity.Template)8 VelocityContext (org.apache.velocity.VelocityContext)8 StringWriter (java.io.StringWriter)7 FileOutputStream (java.io.FileOutputStream)3 SystemException (com.github.bordertech.wcomponents.util.SystemException)2 File (java.io.File)2 FileWriter (java.io.FileWriter)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 Writer (java.io.Writer)2 DecimalFormat (java.text.DecimalFormat)2 Properties (java.util.Properties)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 NumberTool (org.apache.velocity.tools.generic.NumberTool)2 AbstractWComponent (com.github.bordertech.wcomponents.AbstractWComponent)1 WComponent (com.github.bordertech.wcomponents.WComponent)1