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.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.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
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.security.permission.PermissionChecker;
36  import com.liferay.portal.struts.PortletAction;
37  import com.liferay.portal.theme.ThemeDisplay;
38  import com.liferay.portal.util.PortalInstances;
39  import com.liferay.portal.util.PrefsPropsUtil;
40  import com.liferay.portal.util.PropsUtil;
41  import com.liferay.portal.util.ShutdownUtil;
42  import com.liferay.portal.util.WebKeys;
43  import com.liferay.util.Time;
44  import com.liferay.util.servlet.SessionErrors;
45  
46  import java.util.Enumeration;
47  import java.util.Map;
48  
49  import javax.portlet.ActionRequest;
50  import javax.portlet.ActionResponse;
51  import javax.portlet.PortletConfig;
52  import javax.portlet.PortletPreferences;
53  
54  import org.apache.commons.logging.Log;
55  import org.apache.commons.logging.LogFactory;
56  import org.apache.log4j.Level;
57  import org.apache.log4j.Logger;
58  import org.apache.struts.action.ActionForm;
59  import org.apache.struts.action.ActionMapping;
60  
61  /**
62   * <a href="EditServerAction.java.html"><b><i>View Source</i></b></a>
63   *
64   * @author Brian Wing Shun Chan
65   *
66   */
67  public class EditServerAction extends PortletAction {
68  
69      public void processAction(
70              ActionMapping mapping, ActionForm form, PortletConfig config,
71              ActionRequest req, ActionResponse res)
72          throws Exception {
73  
74          ThemeDisplay themeDisplay =
75              (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
76  
77          PermissionChecker permissionChecker =
78              themeDisplay.getPermissionChecker();
79  
80          if (!permissionChecker.isOmniadmin()) {
81              SessionErrors.add(req, PrincipalException.class.getName());
82  
83              setForward(req, "portlet.admin.error");
84  
85              return;
86          }
87  
88          PortletPreferences prefs = PrefsPropsUtil.getPreferences();
89  
90          String cmd = ParamUtil.getString(req, Constants.CMD);
91  
92          if (cmd.equals("addLogLevel")) {
93              addLogLevel(req);
94          }
95          else if (cmd.equals("cacheDb")) {
96              cacheDb();
97          }
98          else if (cmd.equals("cacheMulti")) {
99              cacheMulti();
100         }
101         else if (cmd.equals("cacheSingle")) {
102             cacheSingle();
103         }
104         else if (cmd.equals("gc")) {
105             gc();
106         }
107         else if (cmd.equals("reIndex")) {
108             reIndex();
109         }
110         else if (cmd.equals("shutdown")) {
111             shutdown(req);
112         }
113         else if (cmd.equals("threadDump")) {
114             threadDump();
115         }
116         else if (cmd.equals("updateLogLevels")) {
117             updateLogLevels(req);
118         }
119         else if (cmd.equals("updateOpenOffice")) {
120             updateOpenOffice(req, prefs);
121         }
122 
123         sendRedirect(req, res);
124     }
125 
126     protected void addLogLevel(ActionRequest req) throws Exception {
127         String loggerName = ParamUtil.getString(req, "loggerName");
128         String priority = ParamUtil.getString(req, "priority");
129 
130         Logger logger = Logger.getLogger(loggerName);
131 
132         logger.setLevel(Level.toLevel(priority));
133     }
134 
135     protected void cacheDb() throws Exception {
136         CacheRegistry.clear();
137     }
138 
139     protected void cacheMulti() throws Exception {
140         MultiVMPoolUtil.clear();
141     }
142 
143     protected void cacheSingle() throws Exception {
144         LastModifiedCSS.clear();
145         LastModifiedJavaScript.clear();
146         WebCachePoolUtil.clear();
147     }
148 
149     protected void gc() throws Exception {
150         Runtime.getRuntime().gc();
151     }
152 
153     protected void reIndex() throws Exception {
154         long[] companyIds = PortalInstances.getCompanyIds();
155 
156         for (int i = 0; i < companyIds.length; i++) {
157             long companyId = companyIds[i];
158 
159             try {
160                 LuceneIndexer indexer = new LuceneIndexer(companyId);
161 
162                 indexer.reIndex();
163             }
164             catch (Exception e) {
165                 _log.error(e, e);
166             }
167         }
168     }
169 
170     protected void shutdown(ActionRequest req) throws Exception {
171         long minutes = ParamUtil.getInteger(req, "minutes") * Time.MINUTE;
172         String message = ParamUtil.getString(req, "message");
173 
174         if (minutes <= 0) {
175             ShutdownUtil.cancel();
176         }
177         else {
178             ShutdownUtil.shutdown(minutes, message);
179         }
180     }
181 
182     protected void threadDump() throws Exception {
183         String jvm =
184             System.getProperty("java.vm.name") + " " +
185                 System.getProperty("java.vm.version");
186 
187         StringBuffer sb = new StringBuffer("Full thread dump " + jvm + "\n\n");
188 
189         Map<Thread, StackTraceElement[]> stackTraces =
190             Thread.getAllStackTraces();
191 
192         for (Thread thread : stackTraces.keySet()) {
193             StackTraceElement[] elements = stackTraces.get(thread);
194 
195             sb.append(StringPool.QUOTE);
196             sb.append(thread.getName());
197             sb.append(StringPool.QUOTE);
198 
199             if (thread.getThreadGroup() != null) {
200                 sb.append(StringPool.SPACE);
201                 sb.append(StringPool.OPEN_PARENTHESIS);
202                 sb.append(thread.getThreadGroup().getName());
203                 sb.append(StringPool.CLOSE_PARENTHESIS);
204             }
205 
206             sb.append(", priority=" + thread.getPriority());
207             sb.append(", id=" + thread.getId());
208             sb.append(", state=" + thread.getState());
209             sb.append("\n");
210 
211             for (int i = 0; i < elements.length; i++) {
212                 sb.append("\t" + elements[i] + "\n");
213             }
214 
215             sb.append("\n");
216         }
217 
218         if (_log.isInfoEnabled()) {
219             _log.info(sb.toString());
220         }
221         else {
222             _log.error(
223                 "Thread dumps require the log level to be at least INFO for " +
224                     getClass().getName());
225         }
226     }
227 
228     protected void updateLogLevels(ActionRequest req) throws Exception {
229         Enumeration<String> enu = req.getParameterNames();
230 
231         while (enu.hasMoreElements()) {
232             String name = enu.nextElement();
233 
234             if (name.startsWith("logLevel")) {
235                 String loggerName = name.substring(8, name.length());
236 
237                 String priority = ParamUtil.getString(
238                     req, name, Level.INFO.toString());
239 
240                 Logger logger = Logger.getLogger(loggerName);
241 
242                 logger.setLevel(Level.toLevel(priority));
243             }
244         }
245     }
246 
247     protected void updateOpenOffice(ActionRequest req, PortletPreferences prefs)
248         throws Exception {
249 
250         boolean enabled = ParamUtil.getBoolean(req, "enabled");
251         String host = ParamUtil.getString(req, "host");
252         int port = ParamUtil.getInteger(req, "port");
253 
254         prefs.setValue(
255             PropsUtil.OPENOFFICE_SERVER_ENABLED, String.valueOf(enabled));
256         prefs.setValue(PropsUtil.OPENOFFICE_SERVER_HOST, host);
257         prefs.setValue(PropsUtil.OPENOFFICE_SERVER_PORT, String.valueOf(port));
258 
259         prefs.store();
260     }
261 
262     private static Log _log = LogFactory.getLog(EditServerAction.class);
263 
264 }