1   /**
2    * Copyright (c) 2000-2009 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.mail.service.MailServiceUtil;
26  import com.liferay.portal.convert.ConvertProcess;
27  import com.liferay.portal.kernel.cache.CacheRegistry;
28  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.mail.Account;
32  import com.liferay.portal.kernel.messaging.DestinationNames;
33  import com.liferay.portal.kernel.messaging.MessageBusUtil;
34  import com.liferay.portal.kernel.search.Indexer;
35  import com.liferay.portal.kernel.search.SearchEngineUtil;
36  import com.liferay.portal.kernel.servlet.SessionErrors;
37  import com.liferay.portal.kernel.util.Constants;
38  import com.liferay.portal.kernel.util.InstancePool;
39  import com.liferay.portal.kernel.util.ParamUtil;
40  import com.liferay.portal.kernel.util.StringPool;
41  import com.liferay.portal.kernel.util.StringUtil;
42  import com.liferay.portal.kernel.util.Time;
43  import com.liferay.portal.kernel.util.Validator;
44  import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
45  import com.liferay.portal.model.Portlet;
46  import com.liferay.portal.search.lucene.LuceneIndexer;
47  import com.liferay.portal.security.auth.PrincipalException;
48  import com.liferay.portal.security.permission.PermissionChecker;
49  import com.liferay.portal.service.PortletLocalServiceUtil;
50  import com.liferay.portal.struts.PortletAction;
51  import com.liferay.portal.theme.ThemeDisplay;
52  import com.liferay.portal.util.MaintenanceUtil;
53  import com.liferay.portal.util.PortalInstances;
54  import com.liferay.portal.util.PrefsPropsUtil;
55  import com.liferay.portal.util.PropsKeys;
56  import com.liferay.portal.util.ShutdownUtil;
57  import com.liferay.portal.util.WebKeys;
58  import com.liferay.portlet.ActionResponseImpl;
59  import com.liferay.util.log4j.Log4JUtil;
60  
61  import java.util.Enumeration;
62  import java.util.Map;
63  
64  import javax.portlet.ActionRequest;
65  import javax.portlet.ActionResponse;
66  import javax.portlet.PortletConfig;
67  import javax.portlet.PortletPreferences;
68  import javax.portlet.PortletSession;
69  import javax.portlet.PortletURL;
70  import javax.portlet.WindowState;
71  
72  import org.apache.log4j.Level;
73  import org.apache.log4j.Logger;
74  import org.apache.struts.action.ActionForm;
75  import org.apache.struts.action.ActionMapping;
76  
77  /**
78   * <a href="EditServerAction.java.html"><b><i>View Source</i></b></a>
79   *
80   * @author Brian Wing Shun Chan
81   *
82   */
83  public class EditServerAction extends PortletAction {
84  
85      public void processAction(
86              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
87              ActionRequest actionRequest, ActionResponse actionResponse)
88          throws Exception {
89  
90          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
91              WebKeys.THEME_DISPLAY);
92  
93          PermissionChecker permissionChecker =
94              themeDisplay.getPermissionChecker();
95  
96          if (!permissionChecker.isOmniadmin()) {
97              SessionErrors.add(
98                  actionRequest, PrincipalException.class.getName());
99  
100             setForward(actionRequest, "portlet.admin.error");
101 
102             return;
103         }
104 
105         PortletPreferences preferences = PrefsPropsUtil.getPreferences();
106 
107         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
108 
109         String redirect = null;
110 
111         if (cmd.equals("addLogLevel")) {
112             addLogLevel(actionRequest);
113         }
114         else if (cmd.equals("cacheDb")) {
115             cacheDb();
116         }
117         else if (cmd.equals("cacheMulti")) {
118             cacheMulti();
119         }
120         else if (cmd.equals("cacheSingle")) {
121             cacheSingle();
122         }
123         else if (cmd.startsWith("convertProcess.")) {
124             redirect = convertProcess(actionRequest, actionResponse, cmd);
125         }
126         else if (cmd.equals("gc")) {
127             gc();
128         }
129         else if (cmd.equals("reIndex")) {
130             reIndex(actionRequest);
131         }
132         else if (cmd.equals("shutdown")) {
133             shutdown(actionRequest);
134         }
135         else if (cmd.equals("threadDump")) {
136             threadDump();
137         }
138         else if (cmd.equals("updateFileUploads")) {
139             updateFileUploads(actionRequest, preferences);
140         }
141         else if (cmd.equals("updateLogLevels")) {
142             updateLogLevels(actionRequest);
143         }
144         else if (cmd.equals("updateMail")) {
145             updateMail(actionRequest, preferences);
146         }
147         else if (cmd.equals("updateOpenOffice")) {
148             updateOpenOffice(actionRequest, preferences);
149         }
150 
151         sendRedirect(actionRequest, actionResponse, redirect);
152     }
153 
154     protected void addLogLevel(ActionRequest actionRequest) throws Exception {
155         String loggerName = ParamUtil.getString(actionRequest, "loggerName");
156         String priority = ParamUtil.getString(actionRequest, "priority");
157 
158         Logger logger = Logger.getLogger(loggerName);
159 
160         logger.setLevel(Level.toLevel(priority));
161     }
162 
163     protected void cacheDb() throws Exception {
164         CacheRegistry.clear();
165     }
166 
167     protected void cacheMulti() throws Exception {
168         MultiVMPoolUtil.clear();
169     }
170 
171     protected void cacheSingle() throws Exception {
172         WebCachePoolUtil.clear();
173     }
174 
175     protected String convertProcess(
176             ActionRequest actionRequest, ActionResponse actionResponse,
177             String cmd)
178         throws Exception {
179 
180         ActionResponseImpl actionResponseImpl =
181             (ActionResponseImpl)actionResponse;
182 
183         PortletSession portletSession = actionRequest.getPortletSession();
184 
185         String className = StringUtil.replaceFirst(
186             cmd, "convertProcess.", StringPool.BLANK);
187 
188         ConvertProcess convertProcess = (ConvertProcess)InstancePool.get(
189             className);
190 
191         String path = convertProcess.getPath();
192 
193         if (path != null) {
194             PortletURL portletURL = actionResponseImpl.createRenderURL();
195 
196             portletURL.setWindowState(WindowState.MAXIMIZED);
197 
198             portletURL.setParameter("struts_action", path);
199 
200             return portletURL.toString();
201         }
202         else {
203             MaintenanceUtil.maintain(portletSession.getId(), className);
204 
205             MessageBusUtil.sendMessage(
206                 DestinationNames.CONVERT_PROCESS, className);
207 
208             return null;
209         }
210     }
211 
212     protected void gc() throws Exception {
213         Runtime.getRuntime().gc();
214     }
215 
216     protected String getFileExtensions(
217         ActionRequest actionRequest, String name) {
218 
219         String value = ParamUtil.getString(actionRequest, name);
220 
221         return value.replace(", .", ",.");
222     }
223 
224     protected void reIndex(ActionRequest actionRequest) throws Exception {
225         String portletId = ParamUtil.getString(actionRequest, "portletId");
226 
227         long[] companyIds = PortalInstances.getCompanyIds();
228 
229         if (Validator.isNull(portletId)) {
230             for (long companyId : companyIds) {
231                 try {
232                     LuceneIndexer indexer = new LuceneIndexer(companyId);
233 
234                     indexer.reIndex();
235                 }
236                 catch (Exception e) {
237                     _log.error(e, e);
238                 }
239             }
240         }
241         else {
242             Portlet portlet = PortletLocalServiceUtil.getPortletById(
243                 companyIds[0], portletId);
244 
245             if (portlet == null) {
246                 return;
247             }
248 
249             Indexer indexer = portlet.getIndexerInstance();
250 
251             if (indexer == null) {
252                 return;
253             }
254 
255             for (long companyId : companyIds) {
256                 try {
257                     SearchEngineUtil.deletePortletDocuments(
258                         companyId, portletId);
259 
260                     indexer.reIndex(new String[] {String.valueOf(companyId)});
261                 }
262                 catch (Exception e) {
263                     _log.error(e, e);
264                 }
265             }
266         }
267     }
268 
269     protected void shutdown(ActionRequest actionRequest) throws Exception {
270         long minutes =
271             ParamUtil.getInteger(actionRequest, "minutes") * Time.MINUTE;
272         String message = ParamUtil.getString(actionRequest, "message");
273 
274         if (minutes <= 0) {
275             ShutdownUtil.cancel();
276         }
277         else {
278             ShutdownUtil.shutdown(minutes, message);
279         }
280     }
281 
282     protected void threadDump() throws Exception {
283         String jvm =
284             System.getProperty("java.vm.name") + " " +
285                 System.getProperty("java.vm.version");
286 
287         StringBuilder sb = new StringBuilder(
288             "Full thread dump " + jvm + "\n\n");
289 
290         Map<Thread, StackTraceElement[]> stackTraces =
291             Thread.getAllStackTraces();
292 
293         for (Thread thread : stackTraces.keySet()) {
294             StackTraceElement[] elements = stackTraces.get(thread);
295 
296             sb.append(StringPool.QUOTE);
297             sb.append(thread.getName());
298             sb.append(StringPool.QUOTE);
299 
300             if (thread.getThreadGroup() != null) {
301                 sb.append(StringPool.SPACE);
302                 sb.append(StringPool.OPEN_PARENTHESIS);
303                 sb.append(thread.getThreadGroup().getName());
304                 sb.append(StringPool.CLOSE_PARENTHESIS);
305             }
306 
307             sb.append(", priority=" + thread.getPriority());
308             sb.append(", id=" + thread.getId());
309             sb.append(", state=" + thread.getState());
310             sb.append("\n");
311 
312             for (int i = 0; i < elements.length; i++) {
313                 sb.append("\t" + elements[i] + "\n");
314             }
315 
316             sb.append("\n");
317         }
318 
319         if (_log.isInfoEnabled()) {
320             _log.info(sb.toString());
321         }
322         else {
323             _log.error(
324                 "Thread dumps require the log level to be at least INFO for " +
325                     getClass().getName());
326         }
327     }
328 
329     protected void updateFileUploads(
330             ActionRequest actionRequest, PortletPreferences preferences)
331         throws Exception {
332 
333         String dlFileExtensions = getFileExtensions(
334             actionRequest, "dlFileExtensions");
335         long dlFileMaxSize = ParamUtil.getLong(actionRequest, "dlFileMaxSize");
336         String igImageExtensions = getFileExtensions(
337             actionRequest, "igImageExtensions");
338         long igImageMaxSize = ParamUtil.getLong(
339             actionRequest, "igImageMaxSize");
340         long igThumbnailMaxDimension = ParamUtil.getLong(
341             actionRequest, "igImageThumbnailMaxDimensions");
342         String journalImageExtensions = getFileExtensions(
343             actionRequest, "journalImageExtensions");
344         long journalImageSmallMaxSize = ParamUtil.getLong(
345             actionRequest, "journalImageSmallMaxSize");
346         String shoppingImageExtensions = getFileExtensions(
347             actionRequest, "shoppingImageExtensions");
348         long scImageMaxSize = ParamUtil.getLong(
349             actionRequest, "scImageMaxSize");
350         long scImageThumbnailMaxHeight = ParamUtil.getLong(
351             actionRequest, "scImageThumbnailMaxHeight");
352         long scImageThumbnailMaxWidth = ParamUtil.getLong(
353             actionRequest, "scImageThumbnailMaxWidth");
354         long shoppingImageLargeMaxSize = ParamUtil.getLong(
355             actionRequest, "shoppingImageLargeMaxSize");
356         long shoppingImageMediumMaxSize = ParamUtil.getLong(
357             actionRequest, "shoppingImageMediumMaxSize");
358         long shoppingImageSmallMaxSize = ParamUtil.getLong(
359             actionRequest, "shoppingImageSmallMaxSize");
360         long uploadServletRequestImplMaxSize = ParamUtil.getLong(
361             actionRequest, "uploadServletRequestImplMaxSize");
362         String uploadServletRequestImplTempDir = ParamUtil.getString(
363             actionRequest, "uploadServletRequestImplTempDir");
364         long usersImageMaxSize = ParamUtil.getLong(
365             actionRequest, "usersImageMaxSize");
366 
367         preferences.setValue(
368             PropsKeys.DL_FILE_EXTENSIONS, dlFileExtensions);
369         preferences.setValue(
370             PropsKeys.DL_FILE_MAX_SIZE, String.valueOf(dlFileMaxSize));
371         preferences.setValue(
372             PropsKeys.IG_IMAGE_EXTENSIONS, igImageExtensions);
373         preferences.setValue(
374             PropsKeys.IG_IMAGE_MAX_SIZE, String.valueOf(igImageMaxSize));
375         preferences.setValue(
376             PropsKeys.IG_IMAGE_THUMBNAIL_MAX_DIMENSION,
377             String.valueOf(igThumbnailMaxDimension));
378         preferences.setValue(
379             PropsKeys.JOURNAL_IMAGE_EXTENSIONS, journalImageExtensions);
380         preferences.setValue(
381             PropsKeys.JOURNAL_IMAGE_SMALL_MAX_SIZE,
382             String.valueOf(journalImageSmallMaxSize));
383         preferences.setValue(
384             PropsKeys.SHOPPING_IMAGE_EXTENSIONS, shoppingImageExtensions);
385         preferences.setValue(
386             PropsKeys.SHOPPING_IMAGE_LARGE_MAX_SIZE,
387             String.valueOf(shoppingImageLargeMaxSize));
388         preferences.setValue(
389             PropsKeys.SHOPPING_IMAGE_MEDIUM_MAX_SIZE,
390             String.valueOf(shoppingImageMediumMaxSize));
391         preferences.setValue(
392             PropsKeys.SHOPPING_IMAGE_SMALL_MAX_SIZE,
393             String.valueOf(shoppingImageSmallMaxSize));
394         preferences.setValue(
395             PropsKeys.SC_IMAGE_MAX_SIZE, String.valueOf(scImageMaxSize));
396         preferences.setValue(
397             PropsKeys.SC_IMAGE_THUMBNAIL_MAX_HEIGHT,
398             String.valueOf(scImageThumbnailMaxHeight));
399         preferences.setValue(
400             PropsKeys.SC_IMAGE_THUMBNAIL_MAX_WIDTH,
401             String.valueOf(scImageThumbnailMaxWidth));
402         preferences.setValue(
403             PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_MAX_SIZE,
404             String.valueOf(uploadServletRequestImplMaxSize));
405 
406         if (Validator.isNotNull(uploadServletRequestImplTempDir)) {
407             preferences.setValue(
408                 PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_TEMP_DIR,
409                 uploadServletRequestImplTempDir);
410         }
411 
412         preferences.setValue(
413             PropsKeys.USERS_IMAGE_MAX_SIZE, String.valueOf(usersImageMaxSize));
414 
415         preferences.store();
416     }
417 
418     protected void updateLogLevels(ActionRequest actionRequest)
419         throws Exception {
420 
421         Enumeration<String> enu = actionRequest.getParameterNames();
422 
423         while (enu.hasMoreElements()) {
424             String name = enu.nextElement();
425 
426             if (name.startsWith("logLevel")) {
427                 String loggerName = name.substring(8, name.length());
428 
429                 String priority = ParamUtil.getString(
430                     actionRequest, name, Level.INFO.toString());
431 
432                 Log4JUtil.setLevel(loggerName, priority);
433             }
434         }
435     }
436 
437     protected void updateMail(
438             ActionRequest actionRequest, PortletPreferences preferences)
439         throws Exception {
440 
441         String advancedProperties = ParamUtil.getString(
442             actionRequest, "advancedProperties");
443         String pop3Host = ParamUtil.getString(actionRequest, "pop3Host");
444         String pop3Password = ParamUtil.getString(
445             actionRequest, "pop3Password");
446         int pop3Port = ParamUtil.getInteger(actionRequest, "pop3Port");
447         boolean pop3Secure = ParamUtil.getBoolean(actionRequest, "pop3Secure");
448         String pop3User = ParamUtil.getString(actionRequest, "pop3User");
449         String smtpHost = ParamUtil.getString(actionRequest, "smtpHost");
450         String smtpPassword = ParamUtil.getString(
451             actionRequest, "smtpPassword");
452         int smtpPort = ParamUtil.getInteger(actionRequest, "smtpPort");
453         boolean smtpSecure = ParamUtil.getBoolean(actionRequest, "smtpSecure");
454         String smtpUser = ParamUtil.getString(actionRequest, "smtpUser");
455 
456         String storeProtocol = Account.PROTOCOL_POP;
457 
458         if (pop3Secure) {
459             storeProtocol = Account.PROTOCOL_POPS;
460         }
461 
462         String transportProtocol = Account.PROTOCOL_SMTP;
463 
464         if (smtpSecure) {
465             transportProtocol = Account.PROTOCOL_SMTPS;
466         }
467 
468         preferences.setValue(PropsKeys.MAIL_SESSION_MAIL, "true");
469         preferences.setValue(
470             PropsKeys.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES,
471             advancedProperties);
472         preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_POP3_HOST, pop3Host);
473         preferences.setValue(
474             PropsKeys.MAIL_SESSION_MAIL_POP3_PASSWORD, pop3Password);
475         preferences.setValue(
476             PropsKeys.MAIL_SESSION_MAIL_POP3_PORT, String.valueOf(pop3Port));
477         preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_POP3_USER, pop3User);
478         preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_SMTP_HOST, smtpHost);
479         preferences.setValue(
480             PropsKeys.MAIL_SESSION_MAIL_SMTP_PASSWORD, smtpPassword);
481         preferences.setValue(
482             PropsKeys.MAIL_SESSION_MAIL_SMTP_PORT, String.valueOf(smtpPort));
483         preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_SMTP_USER, smtpUser);
484         preferences.setValue(
485             PropsKeys.MAIL_SESSION_MAIL_STORE_PROTOCOL, storeProtocol);
486         preferences.setValue(
487             PropsKeys.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL, transportProtocol);
488 
489         preferences.store();
490 
491         MailServiceUtil.clearSession();
492     }
493 
494     protected void updateOpenOffice(
495             ActionRequest actionRequest, PortletPreferences preferences)
496         throws Exception {
497 
498         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
499         int port = ParamUtil.getInteger(actionRequest, "port");
500 
501         preferences.setValue(
502             PropsKeys.OPENOFFICE_SERVER_ENABLED, String.valueOf(enabled));
503         preferences.setValue(
504             PropsKeys.OPENOFFICE_SERVER_PORT, String.valueOf(port));
505 
506         preferences.store();
507     }
508 
509     private static Log _log = LogFactoryUtil.getLog(EditServerAction.class);
510 
511 }