# ClassicAppserverDefinition.java

    /*
    ** Module   : ClassicAppserverDefinition.java
    ** Abstract : The settings of classic appserver.
    **
    ** Copyright (c) 2013-2025, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- ---------------------------------Description----------------------------------
    ** 001 CA  20130704 Created initial version.
    ** 002 CA  20140206 Removed settings which need to be at the associated P2J process, as the
    **                  appserver will start using the scheduler and spawner infrastructure.
    **                  Moved logging support to use the logging provided by LogHelper.
    ** 003 OM  20150108 Progress properties read from directory are can be obtained with getProperties
    **                  method to be passed to spawned client process environment.
    ** 004 OM  20150127 Fixed potential NPE when /properties collection is missing from appserver node.
    ** 005 CA  20150909 Fixed a race condition on DirectoryService usage - as this may be done in a
    **                  separate thread using the server's context, all threads need to sync on this
    **                  object, as the DirectoryService is not thread-safe.
    ** 006 OM  20160425 Project awareness (support for multiple project running in same JVM.
    ** 007 CA  20180509 Removed synchronized (it isn't needed, if we will add configuration via the
    **                  FWD Admin Console, we will see then).
    ** 008 CA  20191211 Allow appservers to be marked as multi-session agent appserver.
    ** 009 CA  20211214 For State-free appservers, the agents can be bound to remote persistent procedures; this
    **                  binding must be made using the agent's ID, as a client can invoke multiple remote
    **                  persistent procedures, and using the procedure's ID for this binding can lead to
    **                  collisions, as the pair (connection ID, procedure ID) is not guaranteed to be unique for
    **                  a connection.
    ** 010 GBB 20230512 Logging methods replaced by CentralLogger/ConversionStatus.
    ** 011 GBB 20241010 Adding new fields, getters and value resolving for processName, systemUser,
    **                  schedulerMode, enableSsl, logFilename, loggingLevel, logFileThresholdSize,
    **                  logFilesMaxNumber, alias.
    ** 012 GBB 20250122 AppServerDefinition renamed to ClassicAppserverDefinition.
    */
    /*
    ** 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.util.appserver;

    import com.goldencode.p2j.directory.*;
    import com.goldencode.p2j.security.SecurityManager;
    import com.goldencode.p2j.util.*;
    import com.goldencode.p2j.util.logging.*;

    import java.util.*;
    import java.util.concurrent.*;
    import java.util.logging.*;

    /**
     * A container for all the settings of classic appservers.
     */
    public class ClassicAppserverDefinition
    extends AppserverDefinition
    {
       /** Logger. */
       private static final CentralLogger LOG = CentralLogger.get(ClassicAppserverDefinition.class);

       /** A map of all initialized appserver definitions. */
       private static final Map<String, ClassicAppserverDefinition> definitions = new ConcurrentHashMap<>();

       /** The appserver's operating mode. */
       private final AppServerOperatingMode operatingMode;

       /** The OS user. */
       private String systemUser;

       /** Flag to indicate if it's a multi-session agent appserver definition. */
       private final boolean isMultiSession;

       /**
        * The amount of time (in seconds) to wait for an agent to be available, before attempting
        * to auto-start a new one.
        */
       private final Integer requestTimeout;

       /**
        * The number of seconds waiting before auto-trimming the agents, if they exceed the
        * minimum capacity.
        */
       private final Integer autoTrimTimeout;

       /** The number of agents to start initially. */
       private final Integer initialAgents;

       /** The minimum number of agents required for this appserver. */
       private final Integer minAgents;

       /** The maximum number of agents which can be alive. */
       private final Integer maxAgents;

       /** The activate procedure; may be null/empty. */
       private final String activate;

       /** The deactivate procedure; may be null/empty. */
       private final String deactivate;

       /** The connect procedure; may be null/empty. */
       private final String connect;

       /** The disconnect procedure; may be null/empty. */
       private final String disconnect;

       /** The startup procedure; may be null/empty. */
       private final String startup;

       /** The parameter for the startup procedure; may be null/empty. */
       private final String startupParameter;

       /** The shutdown procedure; may be null/empty. */
       private final String shutdown;

       /**
        * Read the definition for the given appserver, and save it in this instance.
        *
        * @param    appserverName
        *           The application's server name.
        * @param    ds
        *           The directory service.
        * @param    appSrvPath
        *           The path to the appserver node in the directory.
        *
        * @throws   IllegalArgumentException
        *           If no appserver (with the given name) is configured in the directory.
        */
       ClassicAppserverDefinition(String appserverName, DirectoryService ds, String appSrvPath)
       {
          super(appserverName, ds, appSrvPath);

          String mode = ds.getNodeString(appSrvPath, "operating_mode");
          this.operatingMode = AppServerOperatingMode.fromString(mode);

          this.systemUser = SecurityManager.getInstance().getSystemUser(getProcessName());
          Boolean multiSession = ds.getNodeBoolean(appSrvPath, "multisession");
          this.isMultiSession = multiSession == null ? false : multiSession;
          this.requestTimeout = ds.getNodeInteger(appSrvPath, "request_timeout");
          this.autoTrimTimeout = ds.getNodeInteger(appSrvPath, "auto_trim_timeout");
          this.initialAgents = ds.getNodeInteger(appSrvPath, "initial_agents");
          this.minAgents = ds.getNodeInteger(appSrvPath, "min_agents");
          this.maxAgents = ds.getNodeInteger(appSrvPath, "max_agents");

          this.activate = ds.getNodeString(appSrvPath, "activate");
          this.deactivate = ds.getNodeString(appSrvPath, "deactivate");
          this.connect = ds.getNodeString(appSrvPath, "connect");
          this.disconnect = ds.getNodeString(appSrvPath, "disconnect");

          if (operatingMode.isSessionFree() && LOG.isLoggable(Level.WARNING))
          {
             if (connect != null && connect.length() > 0)
             {
                LOG.log(Level.WARNING,
                        "Connect procedure specified for appserver %s in Session-free operating mode",
                        appserverName);
             }
             if (disconnect != null && disconnect.length() > 0)
             {
                LOG.log(Level.WARNING,
                        "Disconnect procedure specified for appserver %s in Session-free operating mode",
                        appserverName);
             }
          }

          this.startup = ds.getNodeString(appSrvPath, "startup");
          this.startupParameter = ds.getNodeString(appSrvPath, "startup_parameter");
          this.shutdown = ds.getNodeString(appSrvPath, "shutdown");

          definitions.put(appserverName, this);
       }

       /**
        * Get the settings for the specified appserver.
        *
        * @param    appServer
        *           The name of the appserver.
        *
        * @return   The instance containing the settings for the specified appserver.
        */
       public static ClassicAppserverDefinition get(String appServer)
       {
          return definitions.get(appServer);
       }

       /**
        * Returns an array with all instantiated appserver process definitions.
        *
        * @return   An array with all instantiated appserver process definitions.
        */
       public static ClassicAppserverDefinition[] getAll()
       {
          return definitions.values().toArray(new ClassicAppserverDefinition[0]);
       }

       /**
        * Returns <code>true</code> if this definition is for a multi-session agent appserver.
        *
        * @return    See above.
        */
       @Override
       public boolean isMultiSession()
       {
          return isMultiSession;
       }

       /**
        * Getter for {@link #systemUser}.
        *
        * @return  See above.
        */
       public String getSystemUser()
       {
          return systemUser;
       }

       /**
        * Get the Session-free state of this appserver {@link #operatingMode}.
        *
        * @return   <code>true</code> only if the {@link #operatingMode} is
        *           {@link AppServerOperatingMode#STATE_FREE}.
        */
       public boolean isSessionFree()
       {
          return operatingMode != null && operatingMode.isSessionFree();
       }

       /**
        * Getter for the {@link #operatingMode} field.
        *
        * @return   See above.
        */
       public AppServerOperatingMode getOperatingMode()
       {
          return operatingMode;
       }

       /**
        * Getter for the {@link #requestTimeout} field.
        *
        * @return   See above.
        */
       public Integer getRequestTimeout()
       {
          return requestTimeout;
       }

       /**
        * Getter for the {@link #autoTrimTimeout} field.
        *
        * @return   See above.
        */
       public Integer getAutoTrimTimeout()
       {
          return autoTrimTimeout;
       }

       /**
        * Getter for the {@link #initialAgents} field.
        *
        * @return   See above.
        */
       public Integer getInitialAgents()
       {
          return initialAgents;
       }

       /**
        * Getter for the {@link #minAgents} field.
        *
        * @return   See above.
        */
       public Integer getMinAgents()
       {
          return minAgents;
       }

       /**
        * Getter for the {@link #maxAgents} field.
        *
        * @return   See above.
        */
       public Integer getMaxAgents()
       {
          return maxAgents;
       }

       /**
        * Getter for the {@link #activate} field.
        *
        * @return   See above.
        */
       public String getActivate()
       {
          return activate;
       }

       /**
        * Getter for the {@link #deactivate} field.
        *
        * @return   See above.
        */
       public String getDeactivate()
       {
          return deactivate;
       }

       /**
        * Getter for the {@link #connect} field.
        *
        * @return   See above.
        */
       public String getConnect()
       {
          return connect;
       }

       /**
        * Getter for the {@link #disconnect} field.
        *
        * @return   See above.
        */
       public String getDisconnect()
       {
          return disconnect;
       }

       /**
        * Getter for the {@link #startup} field.
        *
        * @return   See above.
        */
       public String getStartup()
       {
          return startup;
       }

       /**
        * Getter for the {@link #startupParameter} field.
        *
        * @return   See above.
        */
       public String getStartupParameter()
       {
          return startupParameter;
       }

       /**
        * Getter for the {@link #shutdown} field.
        *
        * @return   See above.
        */
       public String getShutdown()
       {
          return shutdown;
       }
    }