1
19
20 package com.liferay.portlet.admin.action;
21
22 import com.liferay.portal.kernel.cache.CacheRegistry;
23 import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
24 import com.liferay.portal.kernel.log.Log;
25 import com.liferay.portal.kernel.log.LogFactoryUtil;
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.ParamUtil;
31 import com.liferay.portal.kernel.util.StringPool;
32 import com.liferay.portal.kernel.util.Time;
33 import com.liferay.portal.kernel.util.Validator;
34 import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
35 import com.liferay.portal.model.Portlet;
36 import com.liferay.portal.search.lucene.LuceneIndexer;
37 import com.liferay.portal.security.auth.PrincipalException;
38 import com.liferay.portal.security.permission.PermissionChecker;
39 import com.liferay.portal.service.PortletLocalServiceUtil;
40 import com.liferay.portal.struts.PortletAction;
41 import com.liferay.portal.theme.ThemeDisplay;
42 import com.liferay.portal.util.PortalInstances;
43 import com.liferay.portal.util.PrefsPropsUtil;
44 import com.liferay.portal.util.PropsKeys;
45 import com.liferay.portal.util.ShutdownUtil;
46 import com.liferay.portal.util.WebKeys;
47
48 import java.util.Enumeration;
49 import java.util.Map;
50
51 import javax.portlet.ActionRequest;
52 import javax.portlet.ActionResponse;
53 import javax.portlet.PortletConfig;
54 import javax.portlet.PortletPreferences;
55
56 import org.apache.log4j.Level;
57 import org.apache.log4j.Logger;
58 import org.apache.struts.action.ActionForm;
59 import org.apache.struts.action.ActionMapping;
60
61
67 public class EditServerAction extends PortletAction {
68
69 public void processAction(
70 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
71 ActionRequest actionRequest, ActionResponse actionResponse)
72 throws Exception {
73
74 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
75 WebKeys.THEME_DISPLAY);
76
77 PermissionChecker permissionChecker =
78 themeDisplay.getPermissionChecker();
79
80 if (!permissionChecker.isOmniadmin()) {
81 SessionErrors.add(
82 actionRequest, PrincipalException.class.getName());
83
84 setForward(actionRequest, "portlet.admin.error");
85
86 return;
87 }
88
89 PortletPreferences prefs = PrefsPropsUtil.getPreferences();
90
91 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
92
93 if (cmd.equals("addLogLevel")) {
94 addLogLevel(actionRequest);
95 }
96 else if (cmd.equals("cacheDb")) {
97 cacheDb();
98 }
99 else if (cmd.equals("cacheMulti")) {
100 cacheMulti();
101 }
102 else if (cmd.equals("cacheSingle")) {
103 cacheSingle();
104 }
105 else if (cmd.equals("gc")) {
106 gc();
107 }
108 else if (cmd.equals("reIndex")) {
109 reIndex(actionRequest);
110 }
111 else if (cmd.equals("shutdown")) {
112 shutdown(actionRequest);
113 }
114 else if (cmd.equals("threadDump")) {
115 threadDump();
116 }
117 else if (cmd.equals("updateLogLevels")) {
118 updateLogLevels(actionRequest);
119 }
120 else if (cmd.equals("updateOpenOffice")) {
121 updateOpenOffice(actionRequest, prefs);
122 }
123
124 sendRedirect(actionRequest, actionResponse);
125 }
126
127 protected void addLogLevel(ActionRequest actionRequest) throws Exception {
128 String loggerName = ParamUtil.getString(actionRequest, "loggerName");
129 String priority = ParamUtil.getString(actionRequest, "priority");
130
131 Logger logger = Logger.getLogger(loggerName);
132
133 logger.setLevel(Level.toLevel(priority));
134 }
135
136 protected void cacheDb() throws Exception {
137 CacheRegistry.clear();
138 }
139
140 protected void cacheMulti() throws Exception {
141 MultiVMPoolUtil.clear();
142 }
143
144 protected void cacheSingle() throws Exception {
145 WebCachePoolUtil.clear();
146 }
147
148 protected void gc() throws Exception {
149 Runtime.getRuntime().gc();
150 }
151
152 protected void reIndex(ActionRequest actionRequest) throws Exception {
153 String portletId = ParamUtil.getString(actionRequest, "portletId");
154
155 long[] companyIds = PortalInstances.getCompanyIds();
156
157 if (Validator.isNull(portletId)) {
158 for (long companyId : companyIds) {
159 try {
160 LuceneIndexer indexer = new LuceneIndexer(companyId);
161
162 indexer.reIndex();
163 }
164 catch (Exception e) {
165 _log.error(e, e);
166 }
167 }
168 }
169 else {
170 Portlet portlet = PortletLocalServiceUtil.getPortletById(
171 companyIds[0], portletId);
172
173 if (portlet == null) {
174 return;
175 }
176
177 Indexer indexer = portlet.getIndexerInstance();
178
179 if (indexer == null) {
180 return;
181 }
182
183 for (long companyId : companyIds) {
184 try {
185 SearchEngineUtil.deletePortletDocuments(
186 companyId, portletId);
187
188 indexer.reIndex(new String[] {String.valueOf(companyId)});
189 }
190 catch (Exception e) {
191 _log.error(e, e);
192 }
193 }
194 }
195 }
196
197 protected void shutdown(ActionRequest actionRequest) throws Exception {
198 long minutes =
199 ParamUtil.getInteger(actionRequest, "minutes") * Time.MINUTE;
200 String message = ParamUtil.getString(actionRequest, "message");
201
202 if (minutes <= 0) {
203 ShutdownUtil.cancel();
204 }
205 else {
206 ShutdownUtil.shutdown(minutes, message);
207 }
208 }
209
210 protected void threadDump() throws Exception {
211 String jvm =
212 System.getProperty("java.vm.name") + " " +
213 System.getProperty("java.vm.version");
214
215 StringBuilder sb = new StringBuilder(
216 "Full thread dump " + jvm + "\n\n");
217
218 Map<Thread, StackTraceElement[]> stackTraces =
219 Thread.getAllStackTraces();
220
221 for (Thread thread : stackTraces.keySet()) {
222 StackTraceElement[] elements = stackTraces.get(thread);
223
224 sb.append(StringPool.QUOTE);
225 sb.append(thread.getName());
226 sb.append(StringPool.QUOTE);
227
228 if (thread.getThreadGroup() != null) {
229 sb.append(StringPool.SPACE);
230 sb.append(StringPool.OPEN_PARENTHESIS);
231 sb.append(thread.getThreadGroup().getName());
232 sb.append(StringPool.CLOSE_PARENTHESIS);
233 }
234
235 sb.append(", priority=" + thread.getPriority());
236 sb.append(", id=" + thread.getId());
237 sb.append(", state=" + thread.getState());
238 sb.append("\n");
239
240 for (int i = 0; i < elements.length; i++) {
241 sb.append("\t" + elements[i] + "\n");
242 }
243
244 sb.append("\n");
245 }
246
247 if (_log.isInfoEnabled()) {
248 _log.info(sb.toString());
249 }
250 else {
251 _log.error(
252 "Thread dumps require the log level to be at least INFO for " +
253 getClass().getName());
254 }
255 }
256
257 protected void updateLogLevels(ActionRequest actionRequest)
258 throws Exception {
259
260 Enumeration<String> enu = actionRequest.getParameterNames();
261
262 while (enu.hasMoreElements()) {
263 String name = enu.nextElement();
264
265 if (name.startsWith("logLevel")) {
266 String loggerName = name.substring(8, name.length());
267
268 String priority = ParamUtil.getString(
269 actionRequest, name, Level.INFO.toString());
270
271 Logger logger = Logger.getLogger(loggerName);
272
273 logger.setLevel(Level.toLevel(priority));
274 }
275 }
276 }
277
278 protected void updateOpenOffice(
279 ActionRequest actionRequest, PortletPreferences prefs)
280 throws Exception {
281
282 boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
283 int port = ParamUtil.getInteger(actionRequest, "port");
284
285 prefs.setValue(
286 PropsKeys.OPENOFFICE_SERVER_ENABLED, String.valueOf(enabled));
287 prefs.setValue(PropsKeys.OPENOFFICE_SERVER_PORT, String.valueOf(port));
288
289 prefs.store();
290 }
291
292 private static Log _log = LogFactoryUtil.getLog(EditServerAction.class);
293
294 }