RemoteInvocationResult.java

/*
** Module   : RemoteInvocationResult.java
** Abstract : Container for the result of a remote server procedure call. 
**
** Copyright (c) 2013-2025, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 CA  20131220 Created initial version.
** 002 CA  20190628 Consume any thrown legacy 4GL exceptions.
** 003 CA  20220208 Added support for legacy OO errors thrown from a remote appserver call, via RETURN ERROR.
** 004 GBB 20250403 setError to accept isDisabledLegacyErrors instead of looking up in AppServerManager.
*/
/*
** 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 com.goldencode.p2j.oo.lang.*;
import com.goldencode.p2j.util.*;

/**
 * This class acts as a container for the various data which is returned by or updated during 
 * remote server calls.  This includes the function/procedure return value, updated 
 * OUTPUT/INPUT-OUTPUT parameters or the returned error.
 */
public class RemoteInvocationResult
{
   /** The updated parameters, after the remote call. */
   protected Object[] arguments = new Object[0];
   
   /** The function/procedure call result. */
   protected Object result;

   /** The resolved parameter modes for this call. */
   protected String modes;

   /** The error thrown during the remote call or connection request. */
   protected Throwable error;

   /**
    * Get the returned result, after a function or procedure invocation.
    * 
    * @return   The {@link #result} field.
    */
   public Object getResult()
   {
      return result;
   }

   /**
    * Set the result of this remote call.
    * 
    * @param    result
    *           The result of this remote invocation.
    */
   public void setResult(Object result)
   {
      if (result instanceof BaseDataType)
      {
         Class<?> cls = result.getClass();
         while (cls != null && cls.isAnonymousClass())
         {
            cls = cls.getSuperclass();
         }
         if (cls != null && cls != result.getClass())
         {
            try
            {
               BaseDataType bdt = (BaseDataType) cls.newInstance();
               bdt.assign(result);
               result = bdt;
            }
            catch (InstantiationException | IllegalAccessException e)
            {
               // TODO: what to do here?
            }
         }
      }
      this.result = result;
   }

   /**
    * Get any error thrown by the last remote request.
    * 
    * @return   The {@link #error} field.
    */
   public Throwable getError()
   {
      return this.error;
   }

   /**
    * Set the error thrown by the current remote request.
    * 
    * @param    error
    *           The caught error.
    */
   public void setError(Throwable error)
   {
      setError(error, AppServerManager.isDisabledLegacyErrors());
   }
   
   /**
    * Set the error thrown by the current remote request.
    *
    * @param    error
    *           The caught error.
    * @param    isDisabledLegacyErrors
    *           Flag to suppress legacy OO errors for the current connection.
    */
   public void setError(Throwable error, boolean isDisabledLegacyErrors)
   {
      this.error = error;
      
      if (error instanceof LegacyErrorException)
      {
         LegacyErrorException lex = (LegacyErrorException) error;
         // consume this reference
         lex.getError().setUnknown();
         
         // TODO: the condition bellow needs to be only for serializable errors.  at this time, as the
         // legacy OO serializable support is not implemented, we allow any errors, unless the legacy error
         // support is disabled (which is the case for LegacyJavaClient or appserver calls from outside the
         // FWD server).
         boolean send = ObjectOps.isSerializable(lex.getErrorRef()) || // TODO: replace this with &&
                        !isDisabledLegacyErrors;
         if (!send)
         {
            // log the error's messages, as we can't send it to the remote side.
            LegacyError errRef = lex.getErrorRef();
            int numMsgs = errRef.getNumMessages().intValue();
            for (int i = 0; i < numMsgs; i++)
            {
               String msg = errRef.getMessage(new integer(i + 1)).toStringMessage();
               ErrorManager.displayError(msg);
            }
            ErrorManager.displayError("Attempt to throw or return an error object to the client (14438)");
         }
         
         lex.clearError(!send);
      }
   }

   /**
    * Getter for the {@link #arguments} field.
    * 
    * @return   See above.
    */
   public Object[] getArguments()
   {
      return arguments;
   }

   /**
    * Setter for the {@link #arguments} field.
    * 
    * @param    arguments
    *           The updated arguments, to sent back to the caller.
    */
   public void setArguments(Object[] arguments)
   {
      this.arguments = arguments;
   }

   /**
    * Get the resolved parameter modes for this call.
    *  
    * @return   See above.
    */
   public String getModes()
   {
      return modes;
   }
   
   /**
    * Set the resolved parameter modes for this call.
    * 
    * @param    modes
    *           The resolved parameter modes.
    */
   public void setModes(String modes)
   {
      this.modes = modes;
   }
   
   /**
    * Check if the RETURN-VALUE will be set.
    * 
    * @return   By default, returns <code>false</code>.
    */
   public boolean setReturnValue()
   {
      return false;
   }
}