Package com.liferay.portlet.admin.action

Source Code of com.liferay.portlet.admin.action.ServerAction

/**
* Copyright (c) 2000-2005 Liferay, LLC. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.liferay.portlet.admin.action;

import java.util.Enumeration;

import com.dotcms.repackage.javax.portlet.ActionRequest;
import com.dotcms.repackage.javax.portlet.ActionResponse;
import com.dotcms.repackage.javax.portlet.PortletConfig;
import javax.servlet.jsp.PageContext;

import com.dotcms.repackage.org.apache.log4j.Level;
import com.dotcms.repackage.org.apache.log4j.Logger;
import com.dotcms.repackage.org.apache.struts.action.ActionForm;
import com.dotcms.repackage.org.apache.struts.action.ActionMapping;

import com.liferay.portal.auth.PrincipalException;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.util.Constants;
import com.liferay.portal.util.OmniadminUtil;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.ShutdownUtil;
import com.liferay.util.ParamUtil;
import com.liferay.util.Time;
import com.liferay.util.servlet.SessionErrors;

/**
* <a href="ServerAction.java.html"><b><i>View Source</i></b></a>
*
* @author  Brian Wing Shun Chan
* @version $Revision: 1.5 $
*
*/
public class ServerAction extends PortletAction {

  public void processAction(
      ActionMapping mapping, ActionForm form, PortletConfig config,
      ActionRequest req, ActionResponse res)
    throws Exception {

    if (!OmniadminUtil.isOmniadmin(PortalUtil.getUser(req).getUserId())) {
      SessionErrors.add(req, PrincipalException.class.getName());

      setForward(req, "portlet.admin.error");
    }
    else {
      String cmd = ParamUtil.getString(req, Constants.CMD);

      try {
        if (cmd.equals("gc")) {
          _gc();
        }
        else if (cmd.equals("shutdown")) {
          _shutdown(req);
        }
        else if (cmd.equals("log_levels")) {
          _updateLogLevels(req);
        }
      }
      catch (Exception e) {
        if (e != null &&
          e instanceof PrincipalException) {

          SessionErrors.add(req, e.getClass().getName());

          setForward(req, "portlet.admin.error");
        }
        else {
          req.setAttribute(PageContext.EXCEPTION, e);

          setForward(req, Constants.COMMON_ERROR);
        }
      }

      setForward(req, "portlet.admin.server");
    }
  }

  private void _gc() throws Exception {
    Runtime.getRuntime().gc();
  }

  private void _shutdown(ActionRequest req) throws Exception {
    long minutes = ParamUtil.getInteger(
      req, "shutdown_minutes") * Time.MINUTE;
    String message = ParamUtil.getString(req, "shutdown_message");

    if (minutes <= 0) {
      ShutdownUtil.cancel();
    }
    else {
      ShutdownUtil.shutdown(minutes, message);
    }
  }

  private void _updateLogLevels(ActionRequest req) throws Exception {
    Enumeration enu = req.getParameterNames();

    while (enu.hasMoreElements()) {
      String name = (String)enu.nextElement();

      if (name.startsWith("log_level_")) {
        String loggerName = name.substring(10, name.length());

        String priority = ParamUtil.getString(
          req, name, Level.INFO.toString());

        Logger logger = Logger.getLogger(loggerName);

        logger.setLevel(Level.toLevel(priority));
      }
    }
  }

}
TOP

Related Classes of com.liferay.portlet.admin.action.ServerAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.