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