MainServlet中,当初始化社交结束之后,就开始初始化主题了,对应代码是:
- if (_log.isDebugEnabled()) {
- _log.debug("Initialize themes");
- }
- try {
- initThemes(pluginPackage, portlets);
- }
- ..
它对应的代码在initThemes方法中:
- protected void initThemes(
- PluginPackage pluginPackage, List<Portlet> portlets)
- throws Exception {
- ServletContext servletContext = getServletContext();
- String[] xmls = new String[] {
- HttpUtil.URLtoString(
- servletContext.getResource(
- "/WEB-INF/liferay-look-and-feel.xml")),
- HttpUtil.URLtoString(
- servletContext.getResource(
- "/WEB-INF/liferay-look-and-feel-ext.xml"))
- };
- ThemeLocalServiceUtil.init(
- servletContext, null, true, xmls, pluginPackage);
- }
这里可以看出,它仍然是先获取servletContext,然后获得theme有关的配置文件,然后从第16行开始来解析这些配置文件:
解析是调用ThemLocalServiceUtil的init方法:
- public static java.util.List<java.lang.String> init(
- javax.servlet.ServletContext servletContext,
- java.lang.String themesPath, boolean loadFromServletContext,
- java.lang.String[] xmls,
- com.liferay.portal.kernel.plugin.PluginPackage pluginPackage) {
- return getService()
- .init(servletContext, themesPath, loadFromServletContext,
- xmls, pluginPackage);
- }
作为封装,实际是调用的ThemeLocalServiceImpl的5参数的init方法,而它又调用重载的6参数的init方法:
- public List<String> init(
- String servletContextName, ServletContext servletContext,
- String themesPath, boolean loadFromServletContext, String[] xmls,
- PluginPackage pluginPackage) {
- List<String> themeIdsList = new ArrayList<String>();
- try {
- for (String xml : xmls) {
- Set<String> themeIds = _readThemes(
- servletContextName, servletContext, themesPath,
- loadFromServletContext, xml, pluginPackage);
- for (String themeId : themeIds) {
- if (!themeIdsList.contains(themeId)) {
- themeIdsList.add(themeId);
- }
- }
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- _themesPool.clear();
- return themeIdsList;
- }
它会遍历xml配置文件(1个或2个),然后对于每个xml文件调用_readThemes方法,然后把合法theme的id添加到列表中并且返回。
所以我们现在来研究_readthemes方法:
首先,它会进行兼容性判断,它先获取Liferay服务器的version,然后读取配置文件中的<compatibility>元素,然后获取<compatibility>元素的所有<version>元素,然后比较Liferay服务器的version是否被包含在兼容列表中:
- Version portalVersion = _getVersion(ReleaseInfo.getVersion());
- boolean compatible = false;
- Element compatibilityElement = rootElement.element("compatibility");
- if (compatibilityElement != null) {
- List<Element> versionElements = compatibilityElement.elements(
- "version");
- for (Element versionElement : versionElements) {
- Version version = _getVersion(versionElement.getTextTrim());
- if (version.includes(portalVersion)) {
- compatible = true;
- break;
- }
- }
- }
然后,它分析<company-limits>字段,并且吧其中的includes,excludes信息都填充到ThemeCompanyLimit对象中:
- ThemeCompanyLimit companyLimit = null;
- Element companyLimitElement = rootElement.element("company-limit");
- if (companyLimitElement != null) {
- companyLimit = new ThemeCompanyLimit();
- Element companyIncludesElement = companyLimitElement.element(
- "company-includes");
- if (companyIncludesElement != null) {
- companyLimit.setIncludes(
- _getCompanyLimitIncludes(companyIncludesElement));
- }
- Element companyExcludesElement = companyLimitElement.element(
- "company-excludes");
- if (companyExcludesElement != null) {
- companyLimit.setExcludes(
- _getCompanyLimitExcludes(companyExcludesElement));
- }
- }
然后它分析<group-limits>字段,并且吧其中的includes,excludes信息都填充到ThemeGroupLimit对象中:
- ThemeGroupLimit groupLimit = null;
- Element groupLimitElement = rootElement.element("group-limit");
- if (groupLimitElement != null) {
- groupLimit = new ThemeGroupLimit();
- Element groupIncludesElement = groupLimitElement.element(
- "group-includes");
- if (groupIncludesElement != null) {
- groupLimit.setIncludes(
- _getGroupLimitIncludes(groupIncludesElement));
- }
- Element groupExcludesElement = groupLimitElement.element(
- "group-excludes");
- if (groupExcludesElement != null) {
- groupLimit.setExcludes(
- _getGroupLimitExcludes(groupExcludesElement));
- }
- }
其实,以上3部分就是用于标识theme的使用范围,下面开始正式解析<theme>元素,因为一个xml配置文件中会有多个<theme>元素,因此它会进行遍历,并且吧其中的各个信息都去填充Theme对象:
- ContextReplace themeContextReplace = new ContextReplace();
- themeContextReplace.addValue("themes-path", themesPath);
- String themeId = themeElement.attributeValue("id");
- ...
- if (!theme.isWapTheme()) {
- _setSpriteImages(servletContext, theme, p_w_picpathsPath);
- }
- if (!_themes.containsKey(themeId)) {
- _themes.put(themeId, theme);
- }
- }
- return themeIds;
- }
最终,这些Theme就都是可用状态了。
转载于:https://blog.51cto.com/supercharles888/907014