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