天天看點

Liferay 啟動過程分析3-處理啟動事件(第二部分)

 這篇文章繼續對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); 

        "cache_storage", PropsValues.FREEMARKER_ENGINE_CACHE_STORAGE); 

        "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 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(); 

            _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)); 

            "string." + _RESOURCE_LOADER + ".class", 

            StringResourceLoader.class.getName()); 

            "string." + _RESOURCE_LOADER + ".repository.class", 

            StringResourceRepositoryImpl.class.getName()); 

            "servlet." + _RESOURCE_LOADER + ".cache", 

            "servlet." + _RESOURCE_LOADER + ".class", 

            LiferayResourceLoader.class.getName()); 

            org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CLASS, 

            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER)); 

            org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CACHE_CLASS, 

            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE)); 

            org.apache.velocity.app.VelocityEngine.VM_LIBRARY, 

            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_VELOCIMACRO_LIBRARY)); 

            org.apache.velocity.app.VelocityEngine.VM_LIBRARY_AUTORELOAD, 

                !PropsValues.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED)); 

            org.apache.velocity.app.VelocityEngine. 

                VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL, 

            org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS, 

            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER)); 

            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 

    # 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 

    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(); 

        _standardToolsContext, null); 

本文轉自 charles_wang888 51CTO部落格,原文連結:http://blog.51cto.com/supercharles888/905813,如需轉載請自行聯系原作者