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.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  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 path = convertProcess.getPath();
191 
192         if (path != null) {
193             PortletURL portletURL = actionResponseImpl.createRenderURL();
194 
195             portletURL.setWindowState(WindowState.MAXIMIZED);
196 
197             portletURL.setParameter("struts_action", path);
198 
199             return portletURL.toString();
200         }
201         else {
202             MaintenanceUtil.maintain(portletSession.getId(), className);
203 
204             MessageBusUtil.sendMessage(
205                 DestinationNames.CONVERT_PROCESS, className);
206 
207             return null;
208         }
209     }
210 
211     protected void gc() throws Exception {
212         Runtime.getRuntime().gc();
213     }
214 
215     protected String getFileExtensions(
216         ActionRequest actionRequest, String name) {
217 
218         String value = ParamUtil.getString(actionRequest, name);
219 
220         return value.replace(", .", ",.");
221     }
222 
223     protected void reIndex(ActionRequest actionRequest) throws Exception {
224         String portletId = ParamUtil.getString(actionRequest, "portletId");
225 
226         long[] companyIds = PortalInstances.getCompanyIds();
227 
228         if (Validator.isNull(portletId)) {
229             for (long companyId : companyIds) {
230                 try {
231                     LuceneIndexer indexer = new LuceneIndexer(companyId);
232 
233                     indexer.reIndex();
234                 }
235                 catch (Exception e) {
236                     _log.error(e, e);
237                 }
238             }
239         }
240         else {
241             Portlet portlet = PortletLocalServiceUtil.getPortletById(
242                 companyIds[0], portletId);
243 
244             if (portlet == null) {
245                 return;
246             }
247 
248             Indexer indexer = portlet.getIndexerInstance();
249 
250             if (indexer == null) {
251                 return;
252             }
253 
254             for (long companyId : companyIds) {
255                 try {
256                     SearchEngineUtil.deletePortletDocuments(
257                         companyId, portletId);
258 
259                     indexer.reIndex(new String[] {String.valueOf(companyId)});
260                 }
261                 catch (Exception e) {
262                     _log.error(e, e);
263                 }
264             }
265         }
266     }
267 
268     protected void shutdown(ActionRequest actionRequest) throws Exception {
269         long minutes =
270             ParamUtil.getInteger(actionRequest, "minutes") * Time.MINUTE;
271         String message = ParamUtil.getString(actionRequest, "message");
272 
273         if (minutes <= 0) {
274             ShutdownUtil.cancel();
275         }
276         else {
277             ShutdownUtil.shutdown(minutes, message);
278         }
279     }
280 
281     protected void threadDump() throws Exception {
282         String jvm =
283             System.getProperty("java.vm.name") + " " +
284                 System.getProperty("java.vm.version");
285 
286         StringBuilder sb = new StringBuilder(
287             "Full thread dump " + jvm + "\n\n");
288 
289         Map<Thread, StackTraceElement[]> stackTraces =
290             Thread.getAllStackTraces();
291 
292         for (Thread thread : stackTraces.keySet()) {
293             StackTraceElement[] elements = stackTraces.get(thread);
294 
295             sb.append(StringPool.QUOTE);
296             sb.append(thread.getName());
297             sb.append(StringPool.QUOTE);
298 
299             if (thread.getThreadGroup() != null) {
300                 sb.append(StringPool.SPACE);
301                 sb.append(StringPool.OPEN_PARENTHESIS);
302                 sb.append(thread.getThreadGroup().getName());
303                 sb.append(StringPool.CLOSE_PARENTHESIS);
304             }
305 
306             sb.append(", priority=" + thread.getPriority());
307             sb.append(", id=" + thread.getId());
308             sb.append(", state=" + thread.getState());
309             sb.append("\n");
310 
311             for (int i = 0; i < elements.length; i++) {
312                 sb.append("\t" + elements[i] + "\n");
313             }
314 
315             sb.append("\n");
316         }
317 
318         if (_log.isInfoEnabled()) {
319             _log.info(sb.toString());
320         }
321         else {
322             _log.error(
323                 "Thread dumps require the log level to be at least INFO for " +
324                     getClass().getName());
325         }
326     }
327 
328     protected void updateFileUploads(
329             ActionRequest actionRequest, PortletPreferences preferences)
330         throws Exception {
331 
332         String dlFileExtensions = getFileExtensions(
333             actionRequest, "dlFileExtensions");
334         long dlFileMaxSize = ParamUtil.getLong(actionRequest, "dlFileMaxSize");
335         String igImageExtensions = getFileExtensions(
336             actionRequest, "igImageExtensions");
337         long igImageMaxSize = ParamUtil.getLong(
338             actionRequest, "igImageMaxSize");
339         long igThumbnailMaxDimension = ParamUtil.getLong(
340             actionRequest, "igImageThumbnailMaxDimensions");
341         String journalImageExtensions = getFileExtensions(
342             actionRequest, "journalImageExtensions");
343         long journalImageSmallMaxSize = ParamUtil.getLong(
344             actionRequest, "journalImageSmallMaxSize");
345         String shoppingImageExtensions = getFileExtensions(
346             actionRequest, "shoppingImageExtensions");
347         long scImageMaxSize = ParamUtil.getLong(
348             actionRequest, "scImageMaxSize");
349         long scImageThumbnailMaxHeight = ParamUtil.getLong(
350             actionRequest, "scImageThumbnailMaxHeight");
351         long scImageThumbnailMaxWidth = ParamUtil.getLong(
352             actionRequest, "scImageThumbnailMaxWidth");
353         long shoppingImageLargeMaxSize = ParamUtil.getLong(
354             actionRequest, "shoppingImageLargeMaxSize");
355         long shoppingImageMediumMaxSize = ParamUtil.getLong(
356             actionRequest, "shoppingImageMediumMaxSize");
357         long shoppingImageSmallMaxSize = ParamUtil.getLong(
358             actionRequest, "shoppingImageSmallMaxSize");
359         long uploadServletRequestImplMaxSize = ParamUtil.getLong(
360             actionRequest, "uploadServletRequestImplMaxSize");
361         String uploadServletRequestImplTempDir = ParamUtil.getString(
362             actionRequest, "uploadServletRequestImplTempDir");
363         long usersImageMaxSize = ParamUtil.getLong(
364             actionRequest, "usersImageMaxSize");
365 
366         preferences.setValue(
367             PropsKeys.DL_FILE_EXTENSIONS, dlFileExtensions);
368         preferences.setValue(
369             PropsKeys.DL_FILE_MAX_SIZE, String.valueOf(dlFileMaxSize));
370         preferences.setValue(
371             PropsKeys.IG_IMAGE_EXTENSIONS, igImageExtensions);
372         preferences.setValue(
373             PropsKeys.IG_IMAGE_MAX_SIZE, String.valueOf(igImageMaxSize));
374         preferences.setValue(
375             PropsKeys.IG_IMAGE_THUMBNAIL_MAX_DIMENSION,
376             String.valueOf(igThumbnailMaxDimension));
377         preferences.setValue(
378             PropsKeys.JOURNAL_IMAGE_EXTENSIONS, journalImageExtensions);
379         preferences.setValue(
380             PropsKeys.JOURNAL_IMAGE_SMALL_MAX_SIZE,
381             String.valueOf(journalImageSmallMaxSize));
382         preferences.setValue(
383             PropsKeys.SHOPPING_IMAGE_EXTENSIONS, shoppingImageExtensions);
384         preferences.setValue(
385             PropsKeys.SHOPPING_IMAGE_LARGE_MAX_SIZE,
386             String.valueOf(shoppingImageLargeMaxSize));
387         preferences.setValue(
388             PropsKeys.SHOPPING_IMAGE_MEDIUM_MAX_SIZE,
389             String.valueOf(shoppingImageMediumMaxSize));
390         preferences.setValue(
391             PropsKeys.SHOPPING_IMAGE_SMALL_MAX_SIZE,
392             String.valueOf(shoppingImageSmallMaxSize));
393         preferences.setValue(
394             PropsKeys.SC_IMAGE_MAX_SIZE, String.valueOf(scImageMaxSize));
395         preferences.setValue(
396             PropsKeys.SC_IMAGE_THUMBNAIL_MAX_HEIGHT,
397             String.valueOf(scImageThumbnailMaxHeight));
398         preferences.setValue(
399             PropsKeys.SC_IMAGE_THUMBNAIL_MAX_WIDTH,
400             String.valueOf(scImageThumbnailMaxWidth));
401         preferences.setValue(
402             PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_MAX_SIZE,
403             String.valueOf(uploadServletRequestImplMaxSize));
404 
405         if (Validator.isNotNull(uploadServletRequestImplTempDir)) {
406             preferences.setValue(
407                 PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_TEMP_DIR,
408                 uploadServletRequestImplTempDir);
409         }
410 
411         preferences.setValue(
412             PropsKeys.USERS_IMAGE_MAX_SIZE, String.valueOf(usersImageMaxSize));
413 
414         preferences.store();
415     }
416 
417     protected void updateLogLevels(ActionRequest actionRequest)
418         throws Exception {
419 
420         Enumeration<String> enu = actionRequest.getParameterNames();
421 
422         while (enu.hasMoreElements()) {
423             String name = enu.nextElement();
424 
425             if (name.startsWith("logLevel")) {
426                 String loggerName = name.substring(8, name.length());
427 
428                 String priority = ParamUtil.getString(
429                     actionRequest, name, Level.INFO.toString());
430 
431                 Log4JUtil.setLevel(loggerName, priority);
432             }
433         }
434     }
435 
436     protected void updateMail(
437             ActionRequest actionRequest, PortletPreferences preferences)
438         throws Exception {
439 
440         String advancedProperties = ParamUtil.getString(
441             actionRequest, "advancedProperties");
442         String pop3Host = ParamUtil.getString(actionRequest, "pop3Host");
443         String pop3Password = ParamUtil.getString(
444             actionRequest, "pop3Password");
445         int pop3Port = ParamUtil.getInteger(actionRequest, "pop3Port");
446         boolean pop3Secure = ParamUtil.getBoolean(actionRequest, "pop3Secure");
447         String pop3User = ParamUtil.getString(actionRequest, "pop3User");
448         String smtpHost = ParamUtil.getString(actionRequest, "smtpHost");
449         String smtpPassword = ParamUtil.getString(
450             actionRequest, "smtpPassword");
451         int smtpPort = ParamUtil.getInteger(actionRequest, "smtpPort");
452         boolean smtpSecure = ParamUtil.getBoolean(actionRequest, "smtpSecure");
453         String smtpUser = ParamUtil.getString(actionRequest, "smtpUser");
454 
455         String storeProtocol = Account.PROTOCOL_POP;
456 
457         if (pop3Secure) {
458             storeProtocol = Account.PROTOCOL_POPS;
459         }
460 
461         String transportProtocol = Account.PROTOCOL_SMTP;
462 
463         if (smtpSecure) {
464             transportProtocol = Account.PROTOCOL_SMTPS;
465         }
466 
467         preferences.setValue(PropsKeys.MAIL_SESSION_MAIL, "true");
468         preferences.setValue(
469             PropsKeys.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES,
470             advancedProperties);
471         preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_POP3_HOST, pop3Host);
472         preferences.setValue(
473             PropsKeys.MAIL_SESSION_MAIL_POP3_PASSWORD, pop3Password);
474         preferences.setValue(
475             PropsKeys.MAIL_SESSION_MAIL_POP3_PORT, String.valueOf(pop3Port));
476         preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_POP3_USER, pop3User);
477         preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_SMTP_HOST, smtpHost);
478         preferences.setValue(
479             PropsKeys.MAIL_SESSION_MAIL_SMTP_PASSWORD, smtpPassword);
480         preferences.setValue(
481             PropsKeys.MAIL_SESSION_MAIL_SMTP_PORT, String.valueOf(smtpPort));
482         preferences.setValue(PropsKeys.MAIL_SESSION_MAIL_SMTP_USER, smtpUser);
483         preferences.setValue(
484             PropsKeys.MAIL_SESSION_MAIL_STORE_PROTOCOL, storeProtocol);
485         preferences.setValue(
486             PropsKeys.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL, transportProtocol);
487 
488         preferences.store();
489 
490         MailServiceUtil.clearSession();
491     }
492 
493     protected void updateOpenOffice(
494             ActionRequest actionRequest, PortletPreferences preferences)
495         throws Exception {
496 
497         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
498         int port = ParamUtil.getInteger(actionRequest, "port");
499 
500         preferences.setValue(
501             PropsKeys.OPENOFFICE_SERVER_ENABLED, String.valueOf(enabled));
502         preferences.setValue(
503             PropsKeys.OPENOFFICE_SERVER_PORT, String.valueOf(port));
504 
505         preferences.store();
506     }
507 
508     private static Log _log = LogFactoryUtil.getLog(EditServerAction.class);
509 
510 }