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 =
197                     className + StringPool.PERIOD + parameters[i];
198 
199                 if (parameters[i].contains(StringPool.EQUAL) &&
200                     parameters[i].contains(StringPool.SEMICOLON)) {
201 
202                     String[] parameterPair = StringUtil.split(
203                         parameters[i], StringPool.EQUAL);
204 
205                     parameter =
206                         className + StringPool.PERIOD + parameterPair[0];
207                 }
208 
209                 values[i] = ParamUtil.getString(actionRequest, parameter);
210             }
211 
212             convertProcess.setParameterValues(values);
213         }
214 
215         String path = convertProcess.getPath();
216 
217         if (path != null) {
218             PortletURL portletURL = actionResponseImpl.createRenderURL();
219 
220             portletURL.setWindowState(WindowState.MAXIMIZED);
221 
222             portletURL.setParameter("struts_action", path);
223 
224             return portletURL.toString();
225         }
226         else {
227             MaintenanceUtil.maintain(portletSession.getId(), className);
228 
229             MessageBusUtil.sendMessage(
230                 DestinationNames.CONVERT_PROCESS, className);
231 
232             return null;
233         }
234     }
235 
236     protected void gc() throws Exception {
237         Runtime.getRuntime().gc();
238     }
239 
240     protected String getFileExtensions(
241         ActionRequest actionRequest, String name) {
242 
243         String value = ParamUtil.getString(actionRequest, name);
244 
245         return value.replace(", .", ",.");
246     }
247 
248     protected void reIndex(ActionRequest actionRequest) throws Exception {
249         String portletId = ParamUtil.getString(actionRequest, "portletId");
250 
251         long[] companyIds = PortalInstances.getCompanyIds();
252 
253         if (Validator.isNull(portletId)) {
254             for (long companyId : companyIds) {
255                 try {
256                     LuceneIndexer indexer = new LuceneIndexer(companyId);
257 
258                     indexer.reIndex();
259                 }
260                 catch (Exception e) {
261                     _log.error(e, e);
262                 }
263             }
264         }
265         else {
266             Portlet portlet = PortletLocalServiceUtil.getPortletById(
267                 companyIds[0], portletId);
268 
269             if (portlet == null) {
270                 return;
271             }
272 
273             Indexer indexer = portlet.getIndexerInstance();
274 
275             if (indexer == null) {
276                 return;
277             }
278 
279             for (long companyId : companyIds) {
280                 try {
281                     SearchEngineUtil.deletePortletDocuments(
282                         companyId, portletId);
283 
284                     indexer.reIndex(new String[] {String.valueOf(companyId)});
285                 }
286                 catch (Exception e) {
287                     _log.error(e, e);
288                 }
289             }
290         }
291     }
292 
293     protected void shutdown(ActionRequest actionRequest) throws Exception {
294         long minutes =
295             ParamUtil.getInteger(actionRequest, "minutes") * Time.MINUTE;
296         String message = ParamUtil.getString(actionRequest, "message");
297 
298         if (minutes <= 0) {
299             ShutdownUtil.cancel();
300         }
301         else {
302             ShutdownUtil.shutdown(minutes, message);
303         }
304     }
305 
306     protected void threadDump() throws Exception {
307         String jvm =
308             System.getProperty("java.vm.name") + " " +
309                 System.getProperty("java.vm.version");
310 
311         StringBuilder sb = new StringBuilder(
312             "Full thread dump " + jvm + "\n\n");
313 
314         Map<Thread, StackTraceElement[]> stackTraces =
315             Thread.getAllStackTraces();
316 
317         for (Thread thread : stackTraces.keySet()) {
318             StackTraceElement[] elements = stackTraces.get(thread);
319 
320             sb.append(StringPool.QUOTE);
321             sb.append(thread.getName());
322             sb.append(StringPool.QUOTE);
323 
324             if (thread.getThreadGroup() != null) {
325                 sb.append(StringPool.SPACE);
326                 sb.append(StringPool.OPEN_PARENTHESIS);
327                 sb.append(thread.getThreadGroup().getName());
328                 sb.append(StringPool.CLOSE_PARENTHESIS);
329             }
330 
331             sb.append(", priority=" + thread.getPriority());
332             sb.append(", id=" + thread.getId());
333             sb.append(", state=" + thread.getState());
334             sb.append("\n");
335 
336             for (int i = 0; i < elements.length; i++) {
337                 sb.append("\t" + elements[i] + "\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     private static Log _log = LogFactoryUtil.getLog(EditServerAction.class);
534 
535 }