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