1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.theme;
24  
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.kernel.xml.Document;
29  import com.liferay.portal.kernel.xml.Element;
30  import com.liferay.portal.kernel.xml.SAXReaderUtil;
31  import com.liferay.portal.service.ThemeLocalServiceUtil;
32  import com.liferay.portal.util.PropsValues;
33  
34  import java.io.File;
35  
36  import java.util.HashMap;
37  import java.util.Map;
38  
39  import javax.servlet.ServletContext;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  /**
45   * <a href="ThemeLoader.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class ThemeLoader {
51  
52      public String getServletContextName() {
53          return _servletContextName;
54      }
55  
56      public String getThemesPath() {
57          return _themesPath;
58      }
59  
60      public File getFileStorage() {
61          return _fileStorage;
62      }
63  
64      public synchronized void loadThemes() {
65          if (_log.isInfoEnabled()) {
66              _log.info("Loading themes in " + _fileStorage);
67          }
68  
69          File[] files = _fileStorage.listFiles();
70  
71          if (files == null) {
72              if (_log.isWarnEnabled()) {
73                  _log.warn(
74                      "There are no directories to process for  " + _fileStorage);
75              }
76  
77              return;
78          }
79  
80          for (int i = 0; i < files.length; i++) {
81              if (_log.isDebugEnabled()) {
82                  _log.debug("Process directory " + files[i]);
83              }
84  
85              File liferayLookAndFeelXML = new File(
86                  files[i] + "/liferay-look-and-feel.xml");
87  
88              if (liferayLookAndFeelXML.exists()) {
89                  String lastModifiedKey = liferayLookAndFeelXML.toString();
90  
91                  Long prevLastModified = _lastModifiedMap.get(lastModifiedKey);
92  
93                  long lastModified = liferayLookAndFeelXML.lastModified();
94  
95                  if ((prevLastModified == null) ||
96                      (prevLastModified.longValue() < lastModified)) {
97  
98                      registerTheme(liferayLookAndFeelXML);
99  
100                     _lastModifiedMap.put(lastModifiedKey, lastModified);
101                 }
102                 else {
103                     if (_log.isDebugEnabled()) {
104                         _log.debug(
105                             "Do not refresh " + liferayLookAndFeelXML +
106                                 " because it is has not been modified");
107                     }
108                 }
109             }
110             else {
111                 if (_log.isWarnEnabled()) {
112                     _log.warn(liferayLookAndFeelXML + " does not exist");
113                 }
114             }
115         }
116     }
117 
118     protected ThemeLoader(
119         String servletContextName, ServletContext servletContext,
120         String[] xmls) {
121 
122         _servletContextName = servletContextName;
123         _servletContext = servletContext;
124 
125         try {
126             Document doc = SAXReaderUtil.read(xmls[0], true);
127 
128             Element root = doc.getRootElement();
129 
130             _themesPath = GetterUtil.getString(
131                 root.elementText("themes-path"), "/themes");
132 
133             String fileStorageValue = PropsValues.THEME_LOADER_STORAGE_PATH;
134 
135             fileStorageValue = GetterUtil.getString(
136                 root.elementText("file-storage"), fileStorageValue);
137 
138             if (Validator.isNotNull(fileStorageValue)) {
139                 _fileStorage = new File(fileStorageValue);
140                 _loadFromServletContext = false;
141             }
142             else {
143                 _fileStorage = new File(
144                     servletContext.getRealPath(_themesPath));
145                 _loadFromServletContext = true;
146             }
147 
148             if (!_fileStorage.exists()) {
149                 if (_log.isWarnEnabled()) {
150                     _log.warn(
151                         "File storage " + _fileStorage + " does not exist");
152                 }
153 
154                 if (!_fileStorage.mkdirs()) {
155                     _log.error(
156                         "Unable to create theme loader file storage at " +
157                             _fileStorage);
158                 }
159             }
160         }
161         catch (Exception e) {
162             _log.error(e, e);
163         }
164 
165         loadThemes();
166     }
167 
168     protected void destroy() {
169     }
170 
171     protected void registerTheme(File liferayLookAndFeelXML) {
172         if (_log.isDebugEnabled()) {
173             _log.debug("Registering " + liferayLookAndFeelXML);
174         }
175 
176         try {
177             String content = FileUtil.read(liferayLookAndFeelXML);
178 
179             ThemeLocalServiceUtil.init(
180                 _servletContextName, _servletContext, _themesPath,
181                 _loadFromServletContext, new String[] {content}, null);
182         }
183         catch (Exception e) {
184             _log.error(
185                 "Error registering theme " + liferayLookAndFeelXML.toString(),
186                 e);
187         }
188     }
189 
190     private static Log _log = LogFactory.getLog(ThemeLoader.class);
191 
192     private String _servletContextName;
193     private ServletContext _servletContext;
194     private String _themesPath;
195     private File _fileStorage;
196     private boolean _loadFromServletContext = true;
197     private Map<String, Long> _lastModifiedMap = new HashMap<String, Long>();
198 
199 }