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