Liferay 启动过程分析3-处理启动事件(第二部分)

简介:

 这篇文章继续对Liferay启动过程的processStartupEvents()方法进行分析。

 

在配置完portalSecurityManagerStrategy之后,它就会开始配置模板引擎:

 

(5) 配置FreeMarker模板引擎:

 
 
  1. // FreeMarker 
  2.  
  3.     if (_log.isDebugEnabled()) { 
  4.         _log.debug("Initialize FreeMarker engine"); 
  5.     } 
  6.  
  7.     FreeMarkerEngineUtil.init(); 

它会去调用FreeMarkerEngineUtil的init()方法,进而调用FreeMarkerEngineImpl中的init()方法:

它先创建LiferayTemplateLoader载入器,然后把模板配置进去:

 
 
  1. LiferayTemplateLoader liferayTemplateLoader = 
  2.             new LiferayTemplateLoader(); 
  3.  
  4.         liferayTemplateLoader.setTemplateLoaders( 
  5.             PropsValues.FREEMARKER_ENGINE_TEMPLATE_LOADERS); 

被设置的FREEMARKER_ENGINE_TEMPLATE_LOADERS最终是一些载入器类,他们最终在portal.properties文件中定义,一共有3个模板载入器:

 
 
  1. ... 
  2.  # 
  3.     # Input a list of comma delimited class names that extend 
  4.     # com.liferay.portal.freemarker.FreeMarkerTemplateLoader. These classes will 
  5.     # run in sequence to allow you to find the applicable TemplateLoader 
  6.     # to load a FreeMarker template. 
  7.     # 
  8.     freemarker.engine.template.loaders=com.liferay.portal.freemarker.ServletTemplateLoader,com.liferay.portal.freemarker.JournalTemplateLoader,com.liferay.portal.freemarker.ThemeLoaderTemplateLoader 
  9. ... 

这些载入器自己会去相应的目录去找自己负责解析的模板文件:

比如ServletTemplateLoader类中

 
 
  1. url = portalServletContext.getResource( 
  2.                     "/html/themes/_unstyled/template/init_custom.ftl"); 

 

光有载入器还不行,必须还配置如何解析模板:

为此,它创建一个Configuration对象,然后填充一些属性,特别注意的是吧刚才的3个模板加载器设置到Configuration对象中.

 
 
  1. _configuration = new Configuration(); 
  2.  
  3.     _configuration.setDefaultEncoding(StringPool.UTF8); 
  4.     _configuration.setLocalizedLookup( 
  5.         PropsValues.FREEMARKER_ENGINE_LOCALIZED_LOOKUP); 
  6.     _configuration.setObjectWrapper(new LiferayObjectWrapper()); 
  7.     _configuration.setSetting( 
  8.         "auto_import", PropsValues.FREEMARKER_ENGINE_MACRO_LIBRARY); 
  9.     _configuration.setSetting( 
  10.         "cache_storage", PropsValues.FREEMARKER_ENGINE_CACHE_STORAGE); 
  11.     _configuration.setSetting( 
  12.         "template_exception_handler"
  13.         PropsValues.FREEMARKER_ENGINE_TEMPLATE_EXCEPTION_HANDLER); 
  14.     _configuration.setTemplateLoader(multiTemplateLoader); 
  15.     _configuration.setTemplateUpdateDelay( 
  16.         PropsValues.FREEMARKER_ENGINE_MODIFICATION_CHECK_INTERVAL); 

这些都可以在portal.properties文件中找到:

 
 
  1. ## 
  2. ## FreeMarker Engine 
  3. ## 
  4.  
  5.     freemarker.engine.cache.storage=com.liferay.portal.freemarker.LiferayCacheStorage 
  6.     freemarker.engine.localized.lookup=false 
  7.     freemarker.engine.modification.check.interval=60 
  8.  
  9.     # 
  10.     # Exception handler can have it's value set to the name of a class 
  11.     # implementing FreeMarker TemplateExceptionHandler or rethrow, debug, 
  12.     # debug_html, ignore
  13.     # 
  14.     freemarker.engine.template.exception.handler=rethrow 
  15.  
  16.     # 
  17.     # Input a list of comma delimited class names that extend 
  18.     # com.liferay.portal.freemarker.FreeMarkerTemplateLoader. These classes will 
  19.     # run in sequence to allow you to find the applicable TemplateLoader 
  20.     # to load a FreeMarker template. 
  21.     # 
  22.     freemarker.engine.template.loaders=com.liferay.portal.freemarker.ServletTemplateLoader,com.liferay.portal.freemarker.JournalTemplateLoader,com.liferay.portal.freemarker.ThemeLoaderTemplateLoader 
  23.  
  24.     # 
  25.     # Input a list of comma delimited macros that will be loaded. These files 
  26.     # must exist in the class path. 
  27.     # 
  28.     freemarker.engine.macro.library=FTL_liferay.ftl as liferay 

 

之后,它设置一些解析Freemarker模板的工具类:

 
 
  1. _restrictedToolsContext = new FreeMarkerContextImpl(); 
  2.  
  3.         FreeMarkerVariablesUtil.insertHelperUtilities( 
  4.             _restrictedToolsContext, 
  5.             PropsValues.JOURNAL_TEMPLATE_FREEMARKER_RESTRICTED_VARIABLES); 
  6.  
  7.         _standardToolsContext = new FreeMarkerContextImpl(); 
  8.  
  9.         FreeMarkerVariablesUtil.insertHelperUtilities( 
  10.             _standardToolsContext, null); 

 

(6) 配置Velocity模板引擎:

 
 
  1. // Velocity 
  2.  
  3.         if (_log.isDebugEnabled()) { 
  4.             _log.debug("Initialize Velocity engine"); 
  5.         } 
  6.  
  7.         VelocityEngineUtil.init(); 

它会去调用VelocityEngineUtil类的init()方法,进而调用VelocityEngineImpl类的init()方法:

它首先配置一组VelocityResourceListener:

 
 
  1. _velocityEngine = new org.apache.velocity.app.VelocityEngine(); 
  2.  
  3.     LiferayResourceLoader.setVelocityResourceListeners( 
  4.         PropsValues.VELOCITY_ENGINE_RESOURCE_LISTENERS); 

这些Listener最终在portal.properties中定义:

 
 
  1.    # Input a list of comma delimited class names that extend 
  2.    # com.liferay.util.velocity.VelocityResourceListener. These classes will 
  3.    # run in sequence to allow you to find the applicable ResourceLoader 
  4.    # to load a Velocity template. 
  5.    # 
  6.    velocity.engine.resource.listeners=com.liferay.portal.velocity.ServletVelocityResourceListener,com.liferay.portal.velocity.JournalTemplateVelocityResourceListener,com.liferay.portal.velocity.ThemeLoaderVelocityResourceListener,com.liferay.portal.velocity.ClassLoaderVelocityResourceListener 

 

然后它会为解析过程额外配置一些属性:

 
 
  1. ExtendedProperties extendedProperties = new FastExtendedProperties(); 
  2.  
  3.         extendedProperties.setProperty(_RESOURCE_LOADER, "string,servlet"); 
  4.  
  5.         extendedProperties.setProperty( 
  6.             "string." + _RESOURCE_LOADER + ".cache"
  7.             String.valueOf( 
  8.                 PropsValues.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED)); 
  9.  
  10.         extendedProperties.setProperty( 
  11.             "string." + _RESOURCE_LOADER + ".class"
  12.             StringResourceLoader.class.getName()); 
  13.  
  14.         extendedProperties.setProperty( 
  15.             "string." + _RESOURCE_LOADER + ".repository.class"
  16.             StringResourceRepositoryImpl.class.getName()); 
  17.  
  18.         extendedProperties.setProperty( 
  19.             "servlet." + _RESOURCE_LOADER + ".cache"
  20.             String.valueOf( 
  21.                 PropsValues.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED)); 
  22.  
  23.         extendedProperties.setProperty( 
  24.             "servlet." + _RESOURCE_LOADER + ".class"
  25.             LiferayResourceLoader.class.getName()); 
  26.  
  27.         extendedProperties.setProperty( 
  28.             org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CLASS, 
  29.             PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER)); 
  30.  
  31.         extendedProperties.setProperty( 
  32.             org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CACHE_CLASS, 
  33.             PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE)); 
  34.  
  35.         extendedProperties.setProperty( 
  36.             org.apache.velocity.app.VelocityEngine.VM_LIBRARY, 
  37.             PropsUtil.get(PropsKeys.VELOCITY_ENGINE_VELOCIMACRO_LIBRARY)); 
  38.  
  39.         extendedProperties.setProperty( 
  40.             org.apache.velocity.app.VelocityEngine.VM_LIBRARY_AUTORELOAD, 
  41.             String.valueOf( 
  42.                 !PropsValues.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED)); 
  43.  
  44.         extendedProperties.setProperty( 
  45.             org.apache.velocity.app.VelocityEngine. 
  46.                 VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL, 
  47.             String.valueOf( 
  48.                 !PropsValues.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED)); 
  49.  
  50.         extendedProperties.setProperty( 
  51.             org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS, 
  52.             PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER)); 
  53.  
  54.         extendedProperties.setProperty( 
  55.             org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM + 
  56.                 ".log4j.category"
  57.             PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER_CATEGORY)); 
  58.  
  59.         _velocityEngine.setExtendedProperties(extendedProperties); 

这些属性都可以在portal.properties中找到value值;

 
 
  1. ## 
  2. ## Velocity Engine 
  3. ## 
  4.  
  5.     # 
  6.     # Input a list of comma delimited class names that extend 
  7.     # com.liferay.util.velocity.VelocityResourceListener. These classes will 
  8.     # run in sequence to allow you to find the applicable ResourceLoader 
  9.     # to load a Velocity template. 
  10.     # 
  11.     velocity.engine.resource.listeners=com.liferay.portal.velocity.ServletVelocityResourceListener,com.liferay.portal.velocity.JournalTemplateVelocityResourceListener,com.liferay.portal.velocity.ThemeLoaderVelocityResourceListener,com.liferay.portal.velocity.ClassLoaderVelocityResourceListener 
  12.  
  13.     # 
  14.     # Set the Velocity resource managers. We extend the Velocity's default 
  15.     # resource managers for better scalability. 
  16.     # 
  17.     # Note that the modification check interval is not respected because the 
  18.     # resource loader implementation does not know the last modified date of a 
  19.     # resource. This means you will need to turn off caching if you want to be 
  20.     # able to modify VM templates in themes and see the changes right away. 
  21.     # 
  22.     velocity.engine.resource.manager=com.liferay.portal.velocity.LiferayResourceManager 
  23.     velocity.engine.resource.manager.cache=com.liferay.portal.velocity.LiferayResourceCache 
  24.     velocity.engine.resource.manager.cache.enabled=true 
  25.     #velocity.engine.resource.manager.modification.check.interval=0 
  26.  
  27.     # 
  28.     # Input a list of comma delimited macros that will be loaded. These files 
  29.     # must exist in the class path. 
  30.     # 
  31.     velocity.engine.velocimacro.library=VM_global_library.vm,VM_liferay.vm 
  32.  
  33.     # 
  34.     # Set the Velocity logging configuration. 
  35.     # 
  36.     velocity.engine.logger=org.apache.velocity.runtime.log.SimpleLog4JLogSystem 
  37.     velocity.engine.logger.category=org.apache.velocity 

 

最后配置一些工具类,不展开了

 

 
 
  1. _velocityEngine.init(); 
  2.  
  3.     _restrictedToolsContext = new VelocityContextImpl(); 
  4.  
  5.     VelocityVariablesUtil.insertHelperUtilities( 
  6.         _restrictedToolsContext, 
  7.         PropsValues.JOURNAL_TEMPLATE_VELOCITY_RESTRICTED_VARIABLES); 
  8.  
  9.     _standardToolsContext = new VelocityContextImpl(); 
  10.  
  11.     VelocityVariablesUtil.insertHelperUtilities( 
  12.         _standardToolsContext, null); 




本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/905813,如需转载请自行联系原作者

目录
相关文章
|
应用服务中间件 编译器 数据库
【bug:eclispe中启动Tomcat警告】无法启动组件、子容器启动失败
【bug:eclispe中启动Tomcat警告】无法启动组件、子容器启动失败
1013 0
【bug:eclispe中启动Tomcat警告】无法启动组件、子容器启动失败
|
Java Android开发 消息中间件
Android系统默认Home应用程序(Launcher)的启动过程源代码分析
转自 :http://blog.csdn.net/luoshengyang/article/details/6767736    在前面一篇文章中,我们分析了Android系统在启动时安装应用程序的过程,这些应用程序安装好之后,还需要有一个Home应用程序来负责把它们在桌面上展示出来,在Android系统中,这个默认的Home应用程序就是Launcher了,本文将详细分析Laun
2136 0

相关实验场景

更多