AppserverPresenter.java

/*
** Module   : AppserverPresenter.java
** Abstract : AppserverPresenter is a controller that implements business methods to manage appservers.
**
** Copyright (c) 2024-2025, Golden Code Development Corporation.
**
** -#- -I- --Date-- ----------------Description----------------------
** 001 GBB 20240930 Created initial version.
** 002 GBB 20250403 Adding terminateSessions.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
** 
** Additional terms under GNU Affero GPL version 3 section 7:
** 
**   Under Section 7 of the GNU Affero GPL version 3, the following additional
**   terms apply to the works covered under the License.  These additional terms
**   are non-permissive additional terms allowed under Section 7 of the GNU
**   Affero GPL version 3 and may not be removed by you.
** 
**   0. Attribution Requirement.
** 
**     You must preserve all legal notices or author attributions in the covered
**     work or Appropriate Legal Notices displayed by works containing the covered
**     work.  You may not remove from the covered work any author or developer
**     credit already included within the covered work.
** 
**   1. No License To Use Trademarks.
** 
**     This license does not grant any license or rights to use the trademarks
**     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
**     of Golden Code Development Corporation. You are not authorized to use the
**     name Golden Code, FWD, or the names of any author or contributor, for
**     publicity purposes without written authorization.
** 
**   2. No Misrepresentation of Affiliation.
** 
**     You may not represent yourself as Golden Code Development Corporation or FWD.
** 
**     You may not represent yourself for publicity purposes as associated with
**     Golden Code Development Corporation, FWD, or any author or contributor to
**     the covered work, without written authorization.
** 
**   3. No Misrepresentation of Source or Origin.
** 
**     You may not represent the covered work as solely your work.  All modified
**     versions of the covered work must be marked in a reasonable way to make it
**     clear that the modified work is not originating from Golden Code Development
**     Corporation or FWD.  All modified versions must contain the notices of
**     attribution required in this license.
*/

package com.goldencode.p2j.admin.client.application.home.console.appservers;

import com.goldencode.p2j.admin.*;
import com.goldencode.p2j.admin.client.*;
import com.goldencode.p2j.admin.client.application.*;
import com.goldencode.p2j.admin.client.application.home.*;
import com.goldencode.p2j.admin.shared.*;
import com.google.gwt.regexp.shared.*;
import com.google.gwt.user.client.rpc.*;
import com.google.inject.*;
import com.google.web.bindery.event.shared.*;
import com.gwtplatform.mvp.client.*;
import com.gwtplatform.mvp.client.annotations.*;
import com.gwtplatform.mvp.client.presenter.slots.*;
import com.gwtplatform.mvp.client.proxy.*;
import com.gwtplatform.mvp.shared.proxy.*;

import java.util.logging.*;

/**
 * AppserverPresenter is a controller that implements business methods to manage appservers.
 */
public class AppserverPresenter
extends BasePresenter<AppserverPresenter.MyView, AppserverPresenter.MyProxy>
implements AppserverUIHandler
{
   /** The attachment point for modal dialogs on this view*/
   public static final NestedSlot MODAL_CONTENT = new NestedSlot();
   
   /** Administration server interface */
   private final AdminServiceAsync adminService;
   
   /** The logger */
   private final Logger logger = Logger.getLogger(getClass().getSimpleName());
   
   /**
    * This view interface that must be implemented by the concrete view according to GWTP
    * framework. Defines the exposed business methods.
    */
   public interface MyView
   extends View,
           HasUiHandlers<AppserverUIHandler>,
           PrintingContext
   {
      /**
       * Sets the appserver model.
       * 
       * @param    appservers
       *           The array of appserver descriptors
       * @param    filter
       *           The filter criteria
       */
      void setAppservers(AppserverInfo[] appservers, RegExp filter);

      /**
       * Sets the agents table data.
       *
       * @param    appserver
       *           The appserver name
       * @param    agentInfos
       *           The array of agent descriptors
       * @param    filterExp
       *           The filter criteria
       */
      void setAgents(String appserver, AppserverInfo.AgentInfo[] agentInfos, RegExp filterExp);

      /**
       * Shows a warning message.
       *
       * @param    msg
       *           The warning message
       */
      void showWarningMsg(String msg);
   }

   /**
    * Defines the proxy place for GWTP framework.
    */
   @ProxyStandard
   @NameToken(NameTokens.APPSERVERS)
   public interface MyProxy
   extends ProxyPlace<AppserverPresenter>
   {}

   /**
    * Defines Session presenter.
    * 
    * @param    eventBus
    *           The event bus
    * @param    view
    *           The associated view
    * @param    proxy
    *           The proxy place
    * @param    placeManager
    *           The place manager
    * @param    adminService
    *           The administration service
    */
   @Inject
   AppserverPresenter(EventBus eventBus,
                      MyView view,
                      MyProxy proxy,
                      PlaceManager placeManager,
                      AdminServiceAsync adminService)
   {
      super(eventBus, view, proxy, HomePresenter.SLOT_CONTENT, placeManager);
      this.adminService = adminService;
      getView().setUiHandlers(this);
   }

   /**
    * Executes an asynchronous action to retrieve the current appserver list from the server and
    * to update the current sessions table view.
    */
   @Override
   public void updateAppserverList()
   {
      this.adminService.getAppserverList(new AsyncCallback<AppserverInfo[]>()
      {
         @Override
         public void onSuccess(AppserverInfo[] result)
         {
            getView().setAppservers(result, filterExp);
         }

         @Override
         public void onFailure(Throwable caught)
         {
            String msg = "Can't retrieve the list of appservers.";
            getView().showWarningMsg(msg);
            logger.log(Level.WARNING, msg, caught);
         }
      });
   }

   /**
    * Executes an asynchronous action to retrieve the current agents list from the server and
    * to update the current agents table view.
    * 
    * @param    appserver
    *           The appserver name
    */
   @Override
   public void refreshAgents(String appserver)
   {
      this.adminService.getAppserverAgents(appserver, new AsyncCallback<AppserverInfo.AgentInfo[]>()
      {
         @Override
         public void onSuccess(AppserverInfo.AgentInfo[] result)
         {
            getView().setAgents(appserver, result, filterExp);
         }

         @Override
         public void onFailure(Throwable caught)
         {
            String msg = "Can't retrieve agents info for appserver " + appserver;
            getView().showWarningMsg(msg);
            logger.log(Level.WARNING, msg, caught);
         }
      });
   }

   /**
    * Executes an asynchronous action to restart all agents and on its completion
    * to update the current agents table view.
    *
    * @param    appserver
    *           The appserver name
    */
   @Override
   public void restartAgents(String appserver)
   {
      this.adminService.restartAgents(appserver, new AsyncCallback<AppserverInfo.AgentInfo[]>()
      {
         @Override
         public void onSuccess(AppserverInfo.AgentInfo[] result)
         {
            getView().setAgents(appserver, result, filterExp);
         }

         @Override
         public void onFailure(Throwable caught)
         {
            String msg = "Can't restart agents for appserver " + appserver;
            getView().showWarningMsg(msg);
            logger.log(Level.WARNING, msg, caught);
         }
      });
   }

   /**
    * Executes an asynchronous action to trim the unused agents and on its completion
    * to update the current agents table view.
    *
    * @param    appserver
    *           The appserver name
    */
   @Override
   public void trimAgents(String appserver)
   {
      this.adminService.trimAgents(appserver, new AsyncCallback<AppserverInfo.AgentInfo[]>()
      {
         @Override
         public void onSuccess(AppserverInfo.AgentInfo[] result)
         {
            getView().setAgents(appserver, result, filterExp);
         }

         @Override
         public void onFailure(Throwable caught)
         {
            String msg = "Can't trim agents for appserver " + appserver;
            getView().showWarningMsg(msg);
            logger.log(Level.WARNING, msg, caught);
         }
      });
   }

   /**
    * Executes an asynchronous action to start a new agent and on its completion
    * to update the current agents table view.
    *
    * @param    appserver
    *           The appserver name
    */
   @Override
   public void startAppserver(String appserver)
   {
      this.adminService.startAppserver(appserver, new AsyncCallback<AppserverInfo.AgentInfo[]>()
      {
         @Override
         public void onSuccess(AppserverInfo.AgentInfo[] result)
         {
            getView().setAgents(appserver, result, filterExp);
         }

         @Override
         public void onFailure(Throwable caught)
         {
            String msg = "Can't start a new agent for appserver " + appserver;
            getView().showWarningMsg(msg);
            logger.log(Level.WARNING, msg, caught);
         }
      });
   }

   /**
    * Executes an asynchronous action to terminate the given agents and on its completion
    * to update the current agents table view.
    *
    * @param    appserver
    *           The appserver name
    * @param    agentIds
    *           The ids of the agents to be stopped
    */
   @Override
   public void terminateAgents(String appserver, int[] agentIds)
   {
      this.adminService.terminateAgents(appserver, agentIds, new AsyncCallback<AppserverInfo.AgentInfo[]>()
      {
         @Override
         public void onSuccess(AppserverInfo.AgentInfo[] result)
         {
            getView().setAgents(appserver, result, filterExp);
         }

         @Override
         public void onFailure(Throwable caught)
         {
            String msg = "Can't terminate selected agents for appserver " + appserver;
            getView().showWarningMsg(msg);
            logger.log(Level.WARNING, msg, caught);
         }
      });
   }
   
   /**
    * Calls the admin service to terminate the selected sessions.
    *
    * @param    appserver
    *           The appserver name
    * @param    sessions
    *           The sessions to be terminated.
    */
   @Override
   public void terminateSessions(String appserver, MultiSessionAgentSessionDef[] sessions)
   {
      this.adminService.terminateSessions(appserver,
                                          sessions,
                                          new AsyncCallback<AppserverInfo.AgentInfo[]>()
      {
         @Override
         public void onSuccess(AppserverInfo.AgentInfo[] result)
         {
            getView().setAgents(appserver, result, filterExp);
         }

         @Override
         public void onFailure(Throwable caught)
         {
            String msg = "Can't terminate selected sessions for appserver " + appserver;
            getView().showWarningMsg(msg);
            logger.log(Level.WARNING, msg, caught);
         }
      });
   }

   /**
    * Loads the data before the associated view will be displayed.
    * 
    * @param    request
    *           The current request
    */
   @Override
   public void prepareFromRequest(PlaceRequest request)
   {
      super.prepareFromRequest(request);
      setFilter(request.getParameter(FILTER, ""));
      updateAppserverList();
   }
   
   /**
    * Sets the current search string used as a filter to display the target results.
    * 
    * @param    value
    *           The new search string used as a filter.
    */
   @Override
   public void setDataSelectionFilter(String value)
   {
      setFilter(value);
   }
}