1   /**
2    * Copyright (c) 2000-2007 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.portlet.admin.action;
24  
25  import com.liferay.portal.kernel.cache.CacheRegistry;
26  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
27  import com.liferay.portal.kernel.security.permission.PermissionChecker;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.lastmodified.LastModifiedCSS;
32  import com.liferay.portal.lastmodified.LastModifiedJavaScript;
33  import com.liferay.portal.lucene.LuceneIndexer;
34  import com.liferay.portal.security.auth.PrincipalException;
35  import com.liferay.portal.struts.PortletAction;
36  import com.liferay.portal.struts.StrutsUtil;
37  import com.liferay.portal.theme.ThemeDisplay;
38  import com.liferay.portal.util.PortalInstances;
39  import com.liferay.portal.util.PortalUtil;
40  import com.liferay.portal.util.ShutdownUtil;
41  import com.liferay.portal.util.WebCachePool;
42  import com.liferay.portal.util.WebKeys;
43  import com.liferay.util.Time;
44  import com.liferay.util.servlet.NullServletResponse;
45  import com.liferay.util.servlet.SessionErrors;
46  
47  import java.util.Enumeration;
48  import java.util.Iterator;
49  import java.util.Set;
50  import java.util.TreeSet;
51  
52  import javax.portlet.ActionRequest;
53  import javax.portlet.ActionResponse;
54  import javax.portlet.PortletConfig;
55  
56  import javax.servlet.RequestDispatcher;
57  import javax.servlet.ServletContext;
58  import javax.servlet.http.HttpServletRequest;
59  import javax.servlet.http.HttpServletResponse;
60  
61  import org.apache.commons.logging.Log;
62  import org.apache.commons.logging.LogFactory;
63  import org.apache.log4j.Level;
64  import org.apache.log4j.Logger;
65  import org.apache.struts.action.ActionForm;
66  import org.apache.struts.action.ActionMapping;
67  
68  import org.dom4j.Document;
69  import org.dom4j.Element;
70  
71  /**
72   * <a href="EditServerAction.java.html"><b><i>View Source</i></b></a>
73   *
74   * @author Brian Wing Shun Chan
75   *
76   */
77  public class EditServerAction extends PortletAction {
78  
79      public void processAction(
80              ActionMapping mapping, ActionForm form, PortletConfig config,
81              ActionRequest req, ActionResponse res)
82          throws Exception {
83  
84          ThemeDisplay themeDisplay =
85              (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
86  
87          PermissionChecker permissionChecker =
88              themeDisplay.getPermissionChecker();
89  
90          if (!permissionChecker.isOmniadmin()) {
91              SessionErrors.add(req, PrincipalException.class.getName());
92  
93              setForward(req, "portlet.admin.error");
94  
95              return;
96          }
97  
98          String cmd = ParamUtil.getString(req, Constants.CMD);
99  
100         if (cmd.equals("addLogLevel")) {
101             addLogLevel(req);
102         }
103         else if (cmd.equals("cacheDb")) {
104             cacheDb();
105         }
106         else if (cmd.equals("cacheMulti")) {
107             cacheMulti();
108         }
109         else if (cmd.equals("cacheSingle")) {
110             cacheSingle();
111         }
112         else if (cmd.equals("gc")) {
113             gc();
114         }
115         else if (cmd.equals("precompile")) {
116             precompile(req, res);
117         }
118         else if (cmd.equals("reIndex")) {
119             reIndex();
120         }
121         else if (cmd.equals("shutdown")) {
122             shutdown(req);
123         }
124         else if (cmd.equals("updateLogLevels")) {
125             updateLogLevels(req);
126         }
127 
128         sendRedirect(req, res);
129     }
130 
131     protected void addLogLevel(ActionRequest req) throws Exception {
132         String loggerName = ParamUtil.getString(req, "loggerName");
133         String priority = ParamUtil.getString(req, "priority");
134 
135         Logger logger = Logger.getLogger(loggerName);
136 
137         logger.setLevel(Level.toLevel(priority));
138     }
139 
140     protected void cacheDb() throws Exception {
141         CacheRegistry.clear();
142     }
143 
144     protected void cacheMulti() throws Exception {
145         MultiVMPoolUtil.clear();
146     }
147 
148     protected void cacheSingle() throws Exception {
149         LastModifiedCSS.clear();
150         LastModifiedJavaScript.clear();
151         WebCachePool.clear();
152     }
153 
154     protected void gc() throws Exception {
155         Runtime.getRuntime().gc();
156     }
157 
158     protected void precompile(ActionRequest req, ActionResponse res)
159         throws Exception {
160 
161         Set jsps = new TreeSet();
162 
163         ServletContext ctx = (ServletContext)req.getAttribute(WebKeys.CTX);
164 
165         // Struts
166 
167         Document doc = PortalUtil.readDocumentFromStream(
168             ctx.getResourceAsStream("/WEB-INF/struts-config.xml"));
169 
170         Element root = doc.getRootElement();
171 
172         Iterator itr1 = root.element("global-forwards").elements(
173             "forward").iterator();
174 
175         while (itr1.hasNext()) {
176             Element action = (Element)itr1.next();
177 
178             String fileName = action.attributeValue("path");
179 
180             if ((Validator.isNotNull(fileName)) &&
181                 (fileName.endsWith(".jsp"))) {
182 
183                 jsps.add(fileName);
184             }
185         }
186 
187         itr1 = root.element("action-mappings").elements("action").iterator();
188 
189         while (itr1.hasNext()) {
190             Element action = (Element)itr1.next();
191 
192             String fileName = action.attributeValue("forward");
193 
194             if ((Validator.isNotNull(fileName)) &&
195                 (fileName.endsWith(".jsp"))) {
196 
197                 jsps.add(fileName);
198             }
199             else {
200                 Iterator itr2 = action.elements("forward").iterator();
201 
202                 while (itr2.hasNext()) {
203                     Element forward = (Element)itr2.next();
204 
205                     fileName = forward.attributeValue("path");
206 
207                     if ((Validator.isNotNull(fileName)) &&
208                         (fileName.endsWith(".jsp"))) {
209 
210                         jsps.add(fileName);
211                     }
212                 }
213             }
214         }
215 
216         // Tiles
217 
218         doc = PortalUtil.readDocumentFromStream(
219             ctx.getResourceAsStream("/WEB-INF/tiles-defs.xml"));
220 
221         root = doc.getRootElement();
222 
223         itr1 = root.elements("definition").iterator();
224 
225         while (itr1.hasNext()) {
226             Element definition = (Element)itr1.next();
227 
228             String fileName = definition.attributeValue("path");
229 
230             if ((Validator.isNotNull(fileName)) &&
231                 (fileName.endsWith(".jsp"))) {
232 
233                 jsps.add(fileName);
234             }
235             else {
236                 Iterator itr2 = definition.elements("put").iterator();
237 
238                 while (itr2.hasNext()) {
239                     Element put = (Element)itr2.next();
240 
241                     fileName = put.attributeValue("value");
242 
243                     if ((Validator.isNotNull(fileName)) &&
244                         (fileName.endsWith(".jsp"))) {
245 
246                         jsps.add(fileName);
247                     }
248                 }
249             }
250         }
251 
252         // Precompile JSPs
253 
254         HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
255         HttpServletResponse httpRes = new NullServletResponse(
256             PortalUtil.getHttpServletResponse(res));
257 
258         itr1 = jsps.iterator();
259 
260         while (itr1.hasNext()) {
261             try {
262                 String jsp = StrutsUtil.TEXT_HTML_DIR + itr1.next();
263 
264                 RequestDispatcher rd = ctx.getRequestDispatcher(jsp);
265 
266                 if (rd != null) {
267                     if (_log.isInfoEnabled()) {
268                         _log.info("Precompiling " + jsp);
269                     }
270 
271                     rd.include(httpReq, httpRes);
272                 }
273             }
274             catch (Exception e) {
275                 _log.debug(e, e);
276             }
277         }
278     }
279 
280     protected void reIndex() throws Exception {
281         long[] companyIds = PortalInstances.getCompanyIds();
282 
283         for (int i = 0; i < companyIds.length; i++) {
284             long companyId = companyIds[i];
285 
286             try {
287                 LuceneIndexer indexer = new LuceneIndexer(companyId);
288 
289                 indexer.reIndex();
290             }
291             catch (Exception e) {
292                 _log.error(e, e);
293             }
294         }
295     }
296 
297     protected void shutdown(ActionRequest req) throws Exception {
298         long minutes = ParamUtil.getInteger(req, "minutes") * Time.MINUTE;
299         String message = ParamUtil.getString(req, "message");
300 
301         if (minutes <= 0) {
302             ShutdownUtil.cancel();
303         }
304         else {
305             ShutdownUtil.shutdown(minutes, message);
306         }
307     }
308 
309     protected void updateLogLevels(ActionRequest req) throws Exception {
310         Enumeration enu = req.getParameterNames();
311 
312         while (enu.hasMoreElements()) {
313             String name = (String)enu.nextElement();
314 
315             if (name.startsWith("logLevel")) {
316                 String loggerName = name.substring(8, name.length());
317 
318                 String priority = ParamUtil.getString(
319                     req, name, Level.INFO.toString());
320 
321                 Logger logger = Logger.getLogger(loggerName);
322 
323                 logger.setLevel(Level.toLevel(priority));
324             }
325         }
326     }
327 
328     private static Log _log = LogFactory.getLog(EditServerAction.class);
329 
330 }