1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.theme;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.FileUtil;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.kernel.xml.Document;
23  import com.liferay.portal.kernel.xml.Element;
24  import com.liferay.portal.kernel.xml.SAXReaderUtil;
25  import com.liferay.portal.service.ThemeLocalServiceUtil;
26  import com.liferay.portal.util.PropsValues;
27  
28  import java.io.File;
29  
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  import javax.servlet.ServletContext;
34  
35  /**
36   * <a href="ThemeLoader.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class ThemeLoader {
41  
42      public String getServletContextName() {
43          return _servletContextName;
44      }
45  
46      public String getThemesPath() {
47          return _themesPath;
48      }
49  
50      public File getFileStorage() {
51          return _fileStorage;
52      }
53  
54      public synchronized void loadThemes() {
55          if (_log.isInfoEnabled()) {
56              _log.info("Loading themes in " + _fileStorage);
57          }
58  
59          File[] files = _fileStorage.listFiles();
60  
61          if (files == null) {
62              if (_log.isWarnEnabled()) {
63                  _log.warn(
64                      "There are no directories to process for  " + _fileStorage);
65              }
66  
67              return;
68          }
69  
70          for (int i = 0; i < files.length; i++) {
71              if (_log.isDebugEnabled()) {
72                  _log.debug("Process directory " + files[i]);
73              }
74  
75              File liferayLookAndFeelXML = new File(
76                  files[i] + "/liferay-look-and-feel.xml");
77  
78              if (liferayLookAndFeelXML.exists()) {
79                  String lastModifiedKey = liferayLookAndFeelXML.toString();
80  
81                  Long prevLastModified = _lastModifiedMap.get(lastModifiedKey);
82  
83                  long lastModified = liferayLookAndFeelXML.lastModified();
84  
85                  if ((prevLastModified == null) ||
86                      (prevLastModified.longValue() < lastModified)) {
87  
88                      registerTheme(liferayLookAndFeelXML);
89  
90                      _lastModifiedMap.put(lastModifiedKey, lastModified);
91                  }
92                  else {
93                      if (_log.isDebugEnabled()) {
94                          _log.debug(
95                              "Do not refresh " + liferayLookAndFeelXML +
96                                  " because it is has not been modified");
97                      }
98                  }
99              }
100             else {
101                 if (_log.isWarnEnabled()) {
102                     _log.warn(liferayLookAndFeelXML + " does not exist");
103                 }
104             }
105         }
106     }
107 
108     protected ThemeLoader(
109         String servletContextName, ServletContext servletContext,
110         String[] xmls) {
111 
112         _servletContextName = servletContextName;
113         _servletContext = servletContext;
114 
115         try {
116             Document doc = SAXReaderUtil.read(xmls[0], true);
117 
118             Element root = doc.getRootElement();
119 
120             _themesPath = GetterUtil.getString(
121                 root.elementText("themes-path"), "/themes");
122 
123             String fileStorageValue = PropsValues.THEME_LOADER_STORAGE_PATH;
124 
125             fileStorageValue = GetterUtil.getString(
126                 root.elementText("file-storage"), fileStorageValue);
127 
128             if (Validator.isNotNull(fileStorageValue)) {
129                 _fileStorage = new File(fileStorageValue);
130                 _loadFromServletContext = false;
131             }
132             else {
133                 _fileStorage = new File(
134                     servletContext.getRealPath(_themesPath));
135                 _loadFromServletContext = true;
136             }
137 
138             if (!_fileStorage.exists()) {
139                 if (_log.isWarnEnabled()) {
140                     _log.warn(
141                         "File storage " + _fileStorage + " does not exist");
142                 }
143 
144                 if (!_fileStorage.mkdirs()) {
145                     _log.error(
146                         "Unable to create theme loader file storage at " +
147                             _fileStorage);
148                 }
149             }
150         }
151         catch (Exception e) {
152             _log.error(e, e);
153         }
154 
155         loadThemes();
156     }
157 
158     protected void destroy() {
159     }
160 
161     protected void registerTheme(File liferayLookAndFeelXML) {
162         if (_log.isDebugEnabled()) {
163             _log.debug("Registering " + liferayLookAndFeelXML);
164         }
165 
166         try {
167             String content = FileUtil.read(liferayLookAndFeelXML);
168 
169             ThemeLocalServiceUtil.init(
170                 _servletContextName, _servletContext, _themesPath,
171                 _loadFromServletContext, new String[] {content}, null);
172         }
173         catch (Exception e) {
174             _log.error(
175                 "Error registering theme " + liferayLookAndFeelXML.toString(),
176                 e);
177         }
178     }
179 
180     private static Log _log = LogFactoryUtil.getLog(ThemeLoader.class);
181 
182     private String _servletContextName;
183     private ServletContext _servletContext;
184     private String _themesPath;
185     private File _fileStorage;
186     private boolean _loadFromServletContext = true;
187     private Map<String, Long> _lastModifiedMap = new HashMap<String, Long>();
188 
189 }