1
22
23 package com.liferay.portal.theme;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.Validator;
27 import com.liferay.portal.service.impl.ThemeLocalUtil;
28 import com.liferay.portal.util.PortalUtil;
29 import com.liferay.portal.util.PropsUtil;
30 import com.liferay.util.CollectionFactory;
31 import com.liferay.util.FileUtil;
32
33 import java.io.File;
34
35 import java.util.Map;
36
37 import javax.servlet.ServletContext;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41
42 import org.dom4j.Document;
43 import org.dom4j.Element;
44
45
51 public class ThemeLoader {
52
53 public String getServletContextName() {
54 return _servletContextName;
55 }
56
57 public String getThemesPath() {
58 return _themesPath;
59 }
60
61 public File getFileStorage() {
62 return _fileStorage;
63 }
64
65 public synchronized void loadThemes() {
66 if (_log.isInfoEnabled()) {
67 _log.info("Loading themes in " + _fileStorage);
68 }
69
70 File[] files = _fileStorage.listFiles();
71
72 if (files == null) {
73 if (_log.isWarnEnabled()) {
74 _log.warn(
75 "There are no directories to process for " + _fileStorage);
76 }
77
78 return;
79 }
80
81 for (int i = 0; i < files.length; i++) {
82 if (_log.isDebugEnabled()) {
83 _log.debug("Process directory " + files[i]);
84 }
85
86 File liferayLookAndFeelXML = new File(
87 files[i] + "/liferay-look-and-feel.xml");
88
89 if (liferayLookAndFeelXML.exists()) {
90 String lastModifiedKey = liferayLookAndFeelXML.toString();
91
92 Long prevLastModified =
93 (Long)_lastModifiedMap.get(lastModifiedKey);
94
95 long lastModified = liferayLookAndFeelXML.lastModified();
96
97 if ((prevLastModified == null) ||
98 (prevLastModified.longValue() < lastModified)) {
99
100 registerTheme(liferayLookAndFeelXML);
101
102 _lastModifiedMap.put(
103 lastModifiedKey, new Long(lastModified));
104 }
105 else {
106 if (_log.isDebugEnabled()) {
107 _log.debug(
108 "Do not refresh " + liferayLookAndFeelXML +
109 " because it is has not been modified");
110 }
111 }
112 }
113 else {
114 if (_log.isWarnEnabled()) {
115 _log.warn(liferayLookAndFeelXML + " does not exist");
116 }
117 }
118 }
119 }
120
121 protected ThemeLoader(
122 String servletContextName, ServletContext ctx, String[] xmls) {
123
124 _servletContextName = servletContextName;
125 _ctx = ctx;
126
127 try {
128 Document doc = PortalUtil.readDocumentFromXML(xmls[0], true);
129
130 Element root = doc.getRootElement();
131
132 _themesPath = GetterUtil.getString(
133 root.elementText("themes-path"), "/themes");
134
135 String fileStorageValue = GetterUtil.getString(PropsUtil.get(
136 PropsUtil.THEME_LOADER_STORAGE_PATH));
137
138 fileStorageValue = GetterUtil.getString(
139 root.elementText("file-storage"), fileStorageValue);
140
141 if (Validator.isNotNull(fileStorageValue)) {
142 _fileStorage = new File(fileStorageValue);
143 _loadFromServletContext = false;
144 }
145 else {
146 _fileStorage = new File(ctx.getRealPath(_themesPath));
147 _loadFromServletContext = true;
148 }
149
150 if (!_fileStorage.exists()) {
151 if (_log.isWarnEnabled()) {
152 _log.warn(
153 "File storage " + _fileStorage + " does not exist");
154 }
155
156 if (!_fileStorage.mkdirs()) {
157 _log.error(
158 "Unable to create theme loader file storage at " +
159 _fileStorage);
160 }
161 }
162 }
163 catch (Exception e) {
164 _log.error(e, e);
165 }
166
167 loadThemes();
168 }
169
170 protected void destroy() {
171 }
172
173 protected void registerTheme(File liferayLookAndFeelXML) {
174 if (_log.isDebugEnabled()) {
175 _log.debug("Registering " + liferayLookAndFeelXML);
176 }
177
178 try {
179 String content = FileUtil.read(liferayLookAndFeelXML);
180
181 ThemeLocalUtil.init(
182 _servletContextName, _ctx, _themesPath, _loadFromServletContext,
183 new String[] {content}, null);
184 }
185 catch (Exception e) {
186 _log.error(
187 "Error registering theme " + liferayLookAndFeelXML.toString(),
188 e);
189 }
190 }
191
192 private static Log _log = LogFactory.getLog(ThemeLoader.class);
193
194 private String _servletContextName;
195 private ServletContext _ctx;
196 private String _themesPath;
197 private File _fileStorage;
198 private boolean _loadFromServletContext = true;
199 private Map _lastModifiedMap = CollectionFactory.getHashMap();
200
201 }