VelocityEngine engine = new VelocityEngine(); Properties props = new Properties(); props.put("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem"); props.put("runtime.log.logsystem.log4j.category", "velocity"); props.put("runtime.log.logsystem.log4j.logger", "velocity"); engine.init(props);
private static String applyTemplate(String templateName, VelocityContext context) { VelocityEngine velocity = new VelocityEngine(); Properties properties = new Properties(); properties.setProperty("resource.loader", "class"); properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); velocity.init(properties); Writer writer = new StringWriter(); velocity.mergeTemplate(templateName, "UTF-8", context, writer); try { writer.flush(); } catch (IOException e) { throw new IllegalStateException(e); } return writer.toString(); }
/** * 新建Velocity模板引擎 * * @param templateDir 模板所在目录,绝对路径 * @param charset 编码 * @return VelocityEngine */ public static VelocityEngine newEngine(String templateDir, String charset) { final VelocityEngine ve = new VelocityEngine(); ve.setProperty(Velocity.FILE_RESOURCE_LOADER_CACHE, true); // 使用缓存 ve.init(_newInitedProp(templateDir, charset)); return ve; }
/** * 新建Velocity模板引擎 * * @param templateDir 模板所在目录,绝对路径 * @param charset 编码 * @return VelocityEngine */ public static VelocityEngine newEngine(String templateDir, String charset) { final VelocityEngine ve = new VelocityEngine(); ve.setProperty(Velocity.FILE_RESOURCE_LOADER_CACHE, true); // 使用缓存 ve.init(_newInitedProp(templateDir, charset)); return ve; }
public static VelocityEngine getEngine() { try { VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.setProperty(RuntimeConstants.ENCODING_DEFAULT, "UTF-8"); //velocityEngine.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8"); velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); velocityEngine.setProperty("classpath.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); //velocityEngine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new SLF4JLogChute()); velocityEngine.init(); return velocityEngine; } catch (Exception e) { throw new RuntimeException("Error configuring velocity", e); } }
/** * Constructs a new ReportGenerator. * * @param applicationName the application name being analyzed * @param dependencies the list of dependencies * @param analyzers the list of analyzers used * @param properties the database properties (containing timestamps of the * NVD CVE data) * @param settings a reference to the database settings */ public ReportGenerator(String applicationName, List<Dependency> dependencies, List<Analyzer> analyzers, DatabaseProperties properties, Settings settings) { this.settings = settings; velocityEngine = createVelocityEngine(); velocityEngine.init(); context = createContext(applicationName, dependencies, analyzers, properties); }
public EmbulkNew(final String categoryWithLanguage, final String nameGiven, final String embulkVersion) throws IOException { this.basePath = Paths.get(".").toAbsolutePath(); final LanguageAndCategory languageAndCategory = LanguageAndCategory.of(categoryWithLanguage); this.language = languageAndCategory.getLanguage(); this.category = languageAndCategory.getCategory(); this.nameGiven = nameGiven; this.embulkVersion = embulkVersion; if (category.equals("file_input")) { this.embulkCategory = "input"; } else if (category.equals("file_output")) { this.embulkCategory = "output"; } else { this.embulkCategory = category; } this.name = nameGiven.replaceAll("[^a-zA-Z0-9_]+", "_"); this.fullProjectName = "embulk-" + embulkCategory + "-" + name; this.pluginDirectory = "lib/embulk"; this.pluginPath = pluginDirectory + "/" + embulkCategory + "/" + name + ".rb"; this.pluginBasePath = this.basePath.resolve(fullProjectName); this.velocityEngine = new VelocityEngine(); this.velocityEngine.init(); this.velocityEngine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogSystem"); }
/** * Render a template and return the result * * @param templateName the template name only without the .vm extension * @param context the context * @return string */ public static String renderTemplate(final String templateName, final VelocityContext context) { final StringWriter stringWriter = new StringWriter(); final VelocityEngine engine = new VelocityEngine(); engine.init("src/test/resources/velocity.properties"); engine.mergeTemplate(TEMPLATE_BASE_DIR + templateName + ".vm", "UTF-8", context, stringWriter); return stringWriter.getBuffer().toString(); }
@Test public void testUploadButtonIsVisibleToAllWhenGlobalPropertyIsNotSet() throws NoSuchFieldException, IllegalAccessException { final HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); final HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); final User user = TestUtils.getTestUser(); final Session session = new Session("fake-session-id", user, "127.0.0.1"); final VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.init("src/test/resources/velocity.properties"); final Page page = new Page(httpServletRequest, httpServletResponse, velocityEngine, "azkaban/webapp/servlet/velocity/permissionspage.vm"); final UserManager userManager = TestUtils.createTestXmlUserManager(); PageUtils.hideUploadButtonWhenNeeded(page, session, userManager, false); final Field velocityContextField = Page.class.getDeclaredField("context"); velocityContextField.setAccessible(true); final VelocityContext velocityContext = (VelocityContext) velocityContextField.get(page); assertThat(velocityContext.containsKey("hideUploadProject")).isFalse(); }
@Test public void testUploadButtonisHiddenWhenGlobalPropertyIsSet() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException, ClassNotFoundException, InstantiationException, IOException { final HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); final HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); final User user = TestUtils.getTestUser(); final Session session = new Session("fake-session-id", user, "127.0.0.1"); final VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.init("src/test/resources/velocity.properties"); final Page page = new Page(httpServletRequest, httpServletResponse, velocityEngine, "azkaban/webapp/servlet/velocity/permissionspage.vm"); final UserManager userManager = TestUtils.createTestXmlUserManager(); PageUtils.hideUploadButtonWhenNeeded(page, session, userManager, true); final Field velocityContextField = Page.class.getDeclaredField("context"); velocityContextField.setAccessible(true); final VelocityContext velocityContext = (VelocityContext) velocityContextField.get(page); assertThat((boolean) velocityContext.get("hideUploadProject")).isTrue(); }
@Test public void testUploadButtonIsEnabledForAdminUser() throws NoSuchFieldException, IllegalAccessException, UserManagerException { final HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); final HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); final UserManager userManager = TestUtils.createTestXmlUserManager(); final User testAdmin = userManager.getUser("testAdmin", "testAdmin"); final Session session = new Session("fake-session-id", testAdmin, "127.0.0.1"); final VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.init("src/test/resources/velocity.properties"); final Page page = new Page(httpServletRequest, httpServletResponse, velocityEngine, "azkaban/webapp/servlet/velocity/permissionspage.vm"); PageUtils.hideUploadButtonWhenNeeded(page, session, userManager, false); final Field velocityContextField = Page.class.getDeclaredField("context"); velocityContextField.setAccessible(true); final VelocityContext velocityContext = (VelocityContext) velocityContextField.get(page); assertThat(velocityContext.get("hideUploadProject")).isNull(); } }
private static void initEventsModule() { geofenceManager = new GeofenceManager(dataManager); calendarManager = new CalendarManager(dataManager); maintenancesManager = new MaintenancesManager(dataManager); notificationManager = new NotificationManager(dataManager); notificatorManager = new NotificatorManager(); Properties velocityProperties = new Properties(); velocityProperties.setProperty("file.resource.loader.path", Context.getConfig().getString("templates.rootPath", "templates") + "/"); velocityProperties.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogChute"); String address; try { address = config.getString("web.address", InetAddress.getLocalHost().getHostAddress()); } catch (UnknownHostException e) { address = "localhost"; } String webUrl = URIUtil.newURI("http", address, config.getInteger("web.port", 8082), "", ""); webUrl = Context.getConfig().getString("web.url", webUrl); velocityProperties.setProperty("web.url", webUrl); velocityEngine = new VelocityEngine(); velocityEngine.init(velocityProperties); motionEventHandler = new MotionEventHandler(tripsConfig); overspeedEventHandler = new OverspeedEventHandler( Context.getConfig().getLong("event.overspeed.minimalDuration") * 1000, Context.getConfig().getBoolean("event.overspeed.notRepeat"), Context.getConfig().getBoolean("event.overspeed.preferLowest")); }
ve.init(); return ve;
ve.init(); return ve;
VelocityEngine ve = new VelocityEngine(); ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, "path/to/templates"); ve.init(); Template t = ve.getTemplate("foo.vm");
/** * Public constructor TODO: needs better error handling * * @throws MessageException */ public VelocityMessagePreparator() throws MessageException { try { engine = new VelocityEngine(); engine.init(); } catch (Exception e) { log.error("Failed to create velocity engine " + e.getMessage(), e); throw new MessageException(e); } }
public String convert(MacroObject format){ try { String defaultVm = "macros.vm"; // 进行初始化操作 ve.init(); // 加载模板,设定模板编码 Template template = ve.getTemplate(vmPath + defaultVm, encode); // 设置初始化数据 VelocityContext context = new VelocityContext(); context.put("msg", format); // 输出 StringWriter writer = new StringWriter(); template.merge(context, writer); return writer.toString(); } catch (Exception e) { e.printStackTrace(); return null; } } }
public String convert(MessageObject format){ try { // 进行初始化操作 ve.init(); // 加载模板,设定模板编码 Template template = ve.getTemplate(vmPath + format.getVmFileName(), encode); // 设置初始化数据 VelocityContext context = new VelocityContext(); context.put("msg", format); // 输出 StringWriter writer = new StringWriter(); template.merge(context, writer); return writer.toString(); } catch (Exception e) { e.printStackTrace(); return null; } }
public void init(URL resource) { LOGGER.info("Using \"" + StringUtils.getPrettyFileUrl(resource) + "\" as template dir"); velocityEngine = new VelocityEngine(); velocityEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "url"); velocityEngine.setProperty("url.resource.loader.class", URLResourceLoader.class.getName()); velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute"); velocityEngine.setProperty("runtime.log.logsystem.log4j.logger", "velocity"); try { velocityEngine.setProperty("url.resource.loader.root", URLDecoder.decode(resource.toString(), "UTF-8")); } catch (UnsupportedEncodingException e) { LOGGER.error("", e); } try { velocityEngine.init(); } catch (Exception e) { LOGGER.error("", e); } } }
protected VelocityEngine initVelocity() { VelocityEngine ve = new VelocityEngine(); ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); ve.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem"); ve.setProperty("runtime.log.logsystem.log4j.category", "velocity"); ve.setProperty("runtime.log.logsystem.log4j.logger", "velocity"); ve.init(); return ve; }