# HighLevelObject.java

    /*
    ** Module   : HighLevelObject.java
    ** Abstract : base class for remote objects
    **
    ** Copyright (c) 2005-2023, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- --JPRM-- ----------------Description-----------------
    ** 001 SIY 20050201   @19495 Created an initial version with three helper
    **                           methods (getKey, transact and forward) which
    **                           don't need to be replaced.
    **                           Application may replace getTimeout() to
    **                           provide different or configurable timeout.
    ** 002 SIY 20050315   @20381 Fixed spelling and other minor cleanups.
    ** 003 SIY 20050329   @20544 Fixed comments.
    ** 004 SIY 20050505   @21110 Changed visibility of getTimeout() method.
    ** 005 NVS 20060704   @27789 getKey() now throws an exception instead of
    **                           silently returning a null RoutingKey.
    ** 006 GES 20070115   @31832 Cleanup of code/formatting. The interface is
    **                           now public since this class does nothing that
    **                           can't already be done via the public Queue
    **                           interface.  Matched a minor method name
    **                           change in Queue.  Moved the timeout to a
    **                           member rather than forcing subclasses to
    **                           override the method.
    ** 007 ECF 20071106   @35881 Replaced Queue with Session.
    ** 008 SIY 20090831   @43797 Added handling of null routing keys and check
    **                           for possible causes of exceptions when
    **                           obtaining routing keys.
    ** 009 HC  20230427          Added logging.
    ** 010 GBB 20230512          Logging methods replaced by CentralLogger/ConversionStatus.
    */
    /*
    ** 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.net;

    import com.goldencode.p2j.util.logging.*;

    import java.util.logging.*;

    /**
     * Base class for remote objects which provides some useful utility methods.
     */
    public class HighLevelObject
    {
       /** Logger */
       private static CentralLogger LOG = CentralLogger.get(HighLevelObject.class);

       /** Session which represents connection. */
       protected Session session;

       /** The timeout for synchronous calls. */
       private int timeout = 10000;

       /**
        * Constructs an instance using the given transport.
        *
        * @param    session
        *           Session with properly initialized transport.
        */
       public HighLevelObject(Session session)
       {
          this.session = session;
       }

       /**
        * Constructs an instance using the given transport and timeout.
        *
        * @param    session
        *           Session with properly initialized transport.
        * @param    timeout
        *           The transaction timeout in milliseconds.
        */
       public HighLevelObject(Session session, int timeout)
       {
          this.session = session;
          this.timeout = timeout;
       }

       /**
        * Get the transaction timeout in milliseconds.
        *
        * @return   The timeout in milliseconds.
        */
       public int getTimeout()
       {
          return timeout;
       }

       /**
        * Set the transaction timeout in milliseconds.
        *
        * @param    timeout
        *           The timeout in milliseconds.
        */
       public void setTimeout(int timeout)
       {
          this.timeout = timeout;
       }

       /**
        * Get the routing key instance that represents the given group and method
        * at the remote side of the session.
        *
        * @param    group
        *           Group name.
        * @param    method
        *           Method name.
        *
        * @return   The instance of the routing key which corresponds to given
        *           the group and method.
        *
        * @throws   Exception
        *           If any problem happens during processing of the request.
        */
       public RoutingKey getKey(String group, String method)
       throws Exception
       {
          try
          {
             Object[] parms = new Object[] {group, method};

             RoutingKey key = (RoutingKey) transact(new RoutingKey(), parms);

             if (key == null)
             {
                String err = "No export or no access to " + group + ":" + method;
                throw new RuntimeException(err);
             }

             return key;
          }
          catch (IllegalStateException ise)
          {
             LOG.log(Level.SEVERE, "Failed to obtain routing key", ise);
             return null;
          }
       }

       /**
        * Convenience wrapper for {@link Queue#forward}.
        *
        * @param    key
        *           An instance of RoutingKey which points to method to call at
        *           remote side.
        * @param    parms
        *           Method parameters.
        */
       public void forward(RoutingKey key, Object[] parms)
       {
          session.forward(new Message(key, parms));
       }

       /**
        * Convenience wrapper for {@link Queue#transact}.
        *
        * @param    key
        *           The routing key which represents the method to call at the
        *           remote side.
        * @param    parms
        *           Method parameters.
        *
        * @return   The transaction result.
        *
        * @throws   Exception
        *           If any problem occurs during {@link Queue#transact}.
        */
       public Object transact(RoutingKey key, Object[] parms)
       throws Exception
       {
          return session.transact(new Message(key, parms), timeout);
       }
    }