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