这篇文章继续对Liferay启动过程的processStartupEvents()方法进行分析。
在配置完portalSecurityManagerStrategy之后,它就会开始配置模板引擎:
(5) 配置FreeMarker模板引擎:
- // FreeMarker
- if (_log.isDebugEnabled()) {
- _log.debug("Initialize FreeMarker engine");
- }
- FreeMarkerEngineUtil.init();
它会去调用FreeMarkerEngineUtil的init()方法,进而调用FreeMarkerEngineImpl中的init()方法:
它先创建LiferayTemplateLoader载入器,然后把模板配置进去:
- LiferayTemplateLoader liferayTemplateLoader =
- new LiferayTemplateLoader();
- liferayTemplateLoader.setTemplateLoaders(
- PropsValues.FREEMARKER_ENGINE_TEMPLATE_LOADERS);
被设置的FREEMARKER_ENGINE_TEMPLATE_LOADERS最终是一些载入器类,他们最终在portal.properties文件中定义,一共有3个模板载入器:
- ...
- #
- # Input a list of comma delimited class names that extend
- # com.liferay.portal.freemarker.FreeMarkerTemplateLoader. These classes will
- # run in sequence to allow you to find the applicable TemplateLoader
- # to load a FreeMarker template.
- #
- freemarker.engine.template.loaders=com.liferay.portal.freemarker.ServletTemplateLoader,com.liferay.portal.freemarker.JournalTemplateLoader,com.liferay.portal.freemarker.ThemeLoaderTemplateLoader
- ...
这些载入器自己会去相应的目录去找自己负责解析的模板文件:
比如ServletTemplateLoader类中
- url = portalServletContext.getResource(
- "/html/themes/_unstyled/template/init_custom.ftl");
光有载入器还不行,必须还配置如何解析模板:
为此,它创建一个Configuration对象,然后填充一些属性,特别注意的是吧刚才的3个模板加载器设置到Configuration对象中.
- _configuration = new Configuration();
- _configuration.setDefaultEncoding(StringPool.UTF8);
- _configuration.setLocalizedLookup(
- PropsValues.FREEMARKER_ENGINE_LOCALIZED_LOOKUP);
- _configuration.setObjectWrapper(new LiferayObjectWrapper());
- _configuration.setSetting(
- "auto_import", PropsValues.FREEMARKER_ENGINE_MACRO_LIBRARY);
- _configuration.setSetting(
- "cache_storage", PropsValues.FREEMARKER_ENGINE_CACHE_STORAGE);
- _configuration.setSetting(
- "template_exception_handler",
- PropsValues.FREEMARKER_ENGINE_TEMPLATE_EXCEPTION_HANDLER);
- _configuration.setTemplateLoader(multiTemplateLoader);
- _configuration.setTemplateUpdateDelay(
- PropsValues.FREEMARKER_ENGINE_MODIFICATION_CHECK_INTERVAL);
这些都可以在portal.properties文件中找到:
- ##
- ## FreeMarker Engine
- ##
- freemarker.engine.cache.storage=com.liferay.portal.freemarker.LiferayCacheStorage
- freemarker.engine.localized.lookup=false
- freemarker.engine.modification.check.interval=60
- #
- # Exception handler can have it's value set to the name of a class
- # implementing FreeMarker TemplateExceptionHandler or rethrow, debug,
- # debug_html, ignore.
- #
- freemarker.engine.template.exception.handler=rethrow
- #
- # Input a list of comma delimited class names that extend
- # com.liferay.portal.freemarker.FreeMarkerTemplateLoader. These classes will
- # run in sequence to allow you to find the applicable TemplateLoader
- # to load a FreeMarker template.
- #
- freemarker.engine.template.loaders=com.liferay.portal.freemarker.ServletTemplateLoader,com.liferay.portal.freemarker.JournalTemplateLoader,com.liferay.portal.freemarker.ThemeLoaderTemplateLoader
- #
- # Input a list of comma delimited macros that will be loaded. These files
- # must exist in the class path.
- #
- freemarker.engine.macro.library=FTL_liferay.ftl as liferay
之后,它设置一些解析Freemarker模板的工具类:
- _restrictedToolsContext = new FreeMarkerContextImpl();
- FreeMarkerVariablesUtil.insertHelperUtilities(
- _restrictedToolsContext,
- PropsValues.JOURNAL_TEMPLATE_FREEMARKER_RESTRICTED_VARIABLES);
- _standardToolsContext = new FreeMarkerContextImpl();
- FreeMarkerVariablesUtil.insertHelperUtilities(
- _standardToolsContext, null);
(6) 配置Velocity模板引擎:
- // Velocity
- if (_log.isDebugEnabled()) {
- _log.debug("Initialize Velocity engine");
- }
- VelocityEngineUtil.init();
它会去调用VelocityEngineUtil类的init()方法,进而调用VelocityEngineImpl类的init()方法:
它首先配置一组VelocityResourceListener:
- _velocityEngine = new org.apache.velocity.app.VelocityEngine();
- LiferayResourceLoader.setVelocityResourceListeners(
- PropsValues.VELOCITY_ENGINE_RESOURCE_LISTENERS);
这些Listener最终在portal.properties中定义:
- #
- # Input a list of comma delimited class names that extend
- # com.liferay.util.velocity.VelocityResourceListener. These classes will
- # run in sequence to allow you to find the applicable ResourceLoader
- # to load a Velocity template.
- #
- velocity.engine.resource.listeners=com.liferay.portal.velocity.ServletVelocityResourceListener,com.liferay.portal.velocity.JournalTemplateVelocityResourceListener,com.liferay.portal.velocity.ThemeLoaderVelocityResourceListener,com.liferay.portal.velocity.ClassLoaderVelocityResourceListener
然后它会为解析过程额外配置一些属性:
- ExtendedProperties extendedProperties = new FastExtendedProperties();
- extendedProperties.setProperty(_RESOURCE_LOADER, "string,servlet");
- extendedProperties.setProperty(
- "string." + _RESOURCE_LOADER + ".cache",
- String.valueOf(
- PropsValues.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
- extendedProperties.setProperty(
- "string." + _RESOURCE_LOADER + ".class",
- StringResourceLoader.class.getName());
- extendedProperties.setProperty(
- "string." + _RESOURCE_LOADER + ".repository.class",
- StringResourceRepositoryImpl.class.getName());
- extendedProperties.setProperty(
- "servlet." + _RESOURCE_LOADER + ".cache",
- String.valueOf(
- PropsValues.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
- extendedProperties.setProperty(
- "servlet." + _RESOURCE_LOADER + ".class",
- LiferayResourceLoader.class.getName());
- extendedProperties.setProperty(
- org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CLASS,
- PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER));
- extendedProperties.setProperty(
- org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CACHE_CLASS,
- PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE));
- extendedProperties.setProperty(
- org.apache.velocity.app.VelocityEngine.VM_LIBRARY,
- PropsUtil.get(PropsKeys.VELOCITY_ENGINE_VELOCIMACRO_LIBRARY));
- extendedProperties.setProperty(
- org.apache.velocity.app.VelocityEngine.VM_LIBRARY_AUTORELOAD,
- String.valueOf(
- !PropsValues.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
- extendedProperties.setProperty(
- org.apache.velocity.app.VelocityEngine.
- VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL,
- String.valueOf(
- !PropsValues.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
- extendedProperties.setProperty(
- org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS,
- PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER));
- extendedProperties.setProperty(
- org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM +
- ".log4j.category",
- PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER_CATEGORY));
- _velocityEngine.setExtendedProperties(extendedProperties);
这些属性都可以在portal.properties中找到value值;
- ##
- ## Velocity Engine
- ##
- #
- # Input a list of comma delimited class names that extend
- # com.liferay.util.velocity.VelocityResourceListener. These classes will
- # run in sequence to allow you to find the applicable ResourceLoader
- # to load a Velocity template.
- #
- velocity.engine.resource.listeners=com.liferay.portal.velocity.ServletVelocityResourceListener,com.liferay.portal.velocity.JournalTemplateVelocityResourceListener,com.liferay.portal.velocity.ThemeLoaderVelocityResourceListener,com.liferay.portal.velocity.ClassLoaderVelocityResourceListener
- #
- # Set the Velocity resource managers. We extend the Velocity's default
- # resource managers for better scalability.
- #
- # Note that the modification check interval is not respected because the
- # resource loader implementation does not know the last modified date of a
- # resource. This means you will need to turn off caching if you want to be
- # able to modify VM templates in themes and see the changes right away.
- #
- velocity.engine.resource.manager=com.liferay.portal.velocity.LiferayResourceManager
- velocity.engine.resource.manager.cache=com.liferay.portal.velocity.LiferayResourceCache
- velocity.engine.resource.manager.cache.enabled=true
- #velocity.engine.resource.manager.modification.check.interval=0
- #
- # Input a list of comma delimited macros that will be loaded. These files
- # must exist in the class path.
- #
- velocity.engine.velocimacro.library=VM_global_library.vm,VM_liferay.vm
- #
- # Set the Velocity logging configuration.
- #
- velocity.engine.logger=org.apache.velocity.runtime.log.SimpleLog4JLogSystem
- velocity.engine.logger.category=org.apache.velocity
最后配置一些工具类,不展开了
- _velocityEngine.init();
- _restrictedToolsContext = new VelocityContextImpl();
- VelocityVariablesUtil.insertHelperUtilities(
- _restrictedToolsContext,
- PropsValues.JOURNAL_TEMPLATE_VELOCITY_RESTRICTED_VARIABLES);
- _standardToolsContext = new VelocityContextImpl();
- VelocityVariablesUtil.insertHelperUtilities(
- _standardToolsContext, null);
- }