WebAllocatedResources.java

/*
** Module   : WebAllocatedResources.java
** Abstract : WebAllocatedResources represents the web client resources.
**
** Copyright (c) 2017-2024, Golden Code Development Corporation.
**
** -#- -I- --Date-- ----------------Description----------------------
** 001 SBI 20170628 Created initial version.
** 002 IAS 20200914 Re-work (de)serialization
** 003 IAS 20200922 Get rid of possible NPE on serialization.
** 004 SBI 20210926 Added javaDebuggerPort and jmxAgentPort.
** 005 SBI 20221121 Added language.
** 006 SBI 20230505 Changed to be immutable object with final field values except these fields
**                  uuid, user, webRoot, forwardedHost and forwardedProto that are set for each
**                  new client, reusing the preallocated object. 
** 007 GBB 20240709 WebClientConfig.java renamed to WebAllocatedResources.java.
*/
/*
** 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.main;

import java.util.concurrent.*;
import java.util.concurrent.locks.*;

/**
 * Represents the web client resources.
 */
public class WebAllocatedResources
{
   /**
    * Read and write lock object
    */
   private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
   
   /**
    * The direct host assigned to launch the spawned web client
    */
   private final String host;
   
   /**
    * The web  client port
    */
   private final int port;
   
   /**
    * The web  client java debugger port for development/testing use case
    */
   private final int debugAgentPort;
   
   /**
    * The web  client jmx agent port for development/testing use case
    */
   private final int jmxAgentPort;

   /**
    * The associated web resource queue
    */
   private final BlockingQueue<WebAllocatedResources> hostResources;

   /**
    * The web root prefix for all resources of the web client
    */
   private String webRoot;

   /**
    * The forwarded host
    */
   private String forwardedHost;

   /**
    * The http/https protocol used by the forwarded host
    */
   private String forwardedProto;

   /**
    * The user running this web client
    */
   private String osUser;

   /** The web client uuid */
   private String uuid;

   /**
    * Creates a WebClientConfig object.
    * 
    * @param    host
    *           The web client host name or ip address
    * @param    port
    *           The web client port
    * @param    debugAgentPort
    *           The web client java debugger port
    * @param    jmxAgentPort
    *           The web client jmx agent port
    * @param    webRoot
    *           The web root
    * @param    forwardedHost
    *           The forwarded host
    * @param    forwardedProto
    *           The forwarded protocol
    * @param    osUser
    *           The OS user name/id
    * @param    uuid
    *           The web client uuid
    * @param    hostResources
    *           The host resources queue
    */
   public WebAllocatedResources(String host,
                                int port,
                                int debugAgentPort,
                                int jmxAgentPort,
                                String webRoot,
                                String forwardedHost,
                                String forwardedProto,
                                String osUser,
                                String uuid,
                                BlockingQueue<WebAllocatedResources> hostResources)
   {
      this.host = host;
      this.port = port;
      this.debugAgentPort = debugAgentPort;
      this.jmxAgentPort = jmxAgentPort;
      this.webRoot = webRoot;
      this.forwardedHost = forwardedHost;
      this.forwardedProto = forwardedProto;
      this.osUser = osUser;
      this.uuid = uuid;
      this.hostResources = hostResources;
   }

   /**
    * The host on which this web client is running
    * 
    * @return   The web client host
    */
   public String getHost()
   {
      return host;
   }

   /**
    * Gets the web client port number.
    * 
    * @return   The web client port number.
    */
   public int getPort()
   {
      return port;
   }

   /**
    * Gets the java debugger port.
    * 
    * @return   The java debugger port
    */
   public int getDebugAgentPort()
   {
      return debugAgentPort;
   }

   /**
    * Gets the jmx agent port.
    * 
    * @return   The jmx agent port
    */
   public int getJmxAgentPort()
   {
      return jmxAgentPort;
   }

   /**
    * Gets a reference to its resource queue.
    * 
    * @return   The host resources
    */
   public BlockingQueue<WebAllocatedResources> getHostResources()
   {
      return hostResources;
   }


   /**
    * Returns a web client unique identifier.
    * 
    * @return   The web client id
    */
   public String getUuid()
   {
      Lock rl = readWriteLock.readLock();
      rl.lock();
      try
      {
         return uuid;
      }
      finally
      {
         rl.unlock();
      }
   }

   /**
    * Gets the web client root.
    * 
    * @return   The web client root
    */
   public String getWebRoot()
   {
      Lock rl = readWriteLock.readLock();
      rl.lock();
      try
      {
         return webRoot;
      }
      finally
      {
         rl.unlock();
      }
   }

   /**
    * Gets the forwarded host.
    * 
    * @return   The forwarded host
    */
   public String getForwardedHost()
   {
      Lock rl = readWriteLock.readLock();
      rl.lock();
      try
      {
         return forwardedHost;
      }
      finally
      {
         rl.unlock();
      }
   }

   /**
    * Gets the forwarded host protocol.
    * 
    * @return   The forwarded host protocol
    */
   public String getForwardedProto()
   {
      Lock rl = readWriteLock.readLock();
      rl.lock();
      try
      {
         return forwardedProto;
      }
      finally
      {
         rl.unlock();
      }
   }

   /**
    * Get the user running this web client.
    * 
    * @return   The user
    */
   public String getOsUser()
   {
      Lock rl = readWriteLock.readLock();
      rl.lock();
      try
      {
         return osUser;
      }
      finally
      {
         rl.unlock();
      }
   }

   /**
    * Added allocated resources back to its queue.
    * 
    * @throws   InterruptedException
    *           If interrupted while waiting
    */
   public void release()
   throws InterruptedException
   {
      if (hostResources != null)
      {
         hostResources.put(this);
      }
   }

   /**
    * Get a string presentation of this object
    * 
    * @return   The object string presentation
    */
   @Override
   public String toString()
   {
      Lock rl = readWriteLock.readLock();
      rl.lock();
      try
      {
         return "WebClientConfig "
                  + "["
                  + "uuid=" + uuid + ", "
                  + "host=" + host + ", "
                  + "port=" + port + ", "
                  + "webRoot=" + String.valueOf(webRoot) + ", "
                  + "forwardedHost=" + forwardedHost + ", "
                  + "forwardedProto=" + forwardedProto + ", "
                  + "osUser=" + osUser + ", "
                  + "debugAgentPort=" + debugAgentPort + ", "
                  + "jmxAgentPort=" + jmxAgentPort
                  + "]";
      }
      finally
      {
         rl.unlock();
      }
   }

   /**
    * Set new client configuration for this preallocated ports configuration.
    * 
    * @param    uuid
    *           The uuid to set
    * @param    webRoot
    *           The webRoot to set
    * @param    osUser
    *           The OS user to set
    * @param    forwardedProto
    *           The forwardedProto to set
    * @param    forwardedHost
    *           The forwardedHost to set
    */
   public void setNewClient(String uuid,
                            String webRoot,
                            String osUser,
                            String forwardedProto,
                            String forwardedHost)
   {
      Lock wl = readWriteLock.writeLock();
      wl.lock();
      try
      {
         this.uuid = uuid;
         this.webRoot = webRoot;
         this.osUser = osUser;
         this.forwardedProto = forwardedProto;
         this.forwardedHost = forwardedHost;
      }
      finally
      {
         wl.unlock();
      }
   }
}