LegacyObject.java

/*
** Module   : LegacyObject.java
** Abstract : Helper to transport serialized object instances over network.
**
** Copyright (c) 2022-2024, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 CA  20220602 First version.
**     SVL 20230113 Improved performance by replacing some "for-each" loops with indexed "for" loops.
** 002 GBB 20230512 Logging methods replaced by CentralLogger/ConversionStatus.
** 003 CA  20230915 Refactored to allow BDT array serialization and also allow non-BDT fields to be serialized
**                  (only from internal FWD legacy classes).
** 004 CA  20230929 Javadoc fixes.
** 005 AP  20240802 Improved support for TableWrapper objects being sent as parameters to remote procedures
**                  inside the same JVM.
** 006 DDF 20240211 Added an additional parameter for avoiding execution of the constructor method during
**                  the object initialization.
** 007 CA  20240911 Fixed deserialization of dyanmic extent fields.  HANDLE fields are not serialized. 
*/

/*
** 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;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.logging.*;

import com.goldencode.p2j.oo.lang.*;
import com.goldencode.p2j.persist.*;
import com.goldencode.p2j.util.logging.*;

/**
 * Provides a transport mechanism of {@link object} instances over the network, for i.e. appserver calls.
 */
public class LegacyObject
implements Externalizable
{
   /** Logger */
   private static final CentralLogger LOG = CentralLogger.get(LegacyObject.class);
   
   /** The legacy type being transported. */
   private String legacyClass;
   
   /** Flag indicating if this is an unknown instance. */
   private boolean unknown;

   /** The converted type of the object being transported. */
   private Class<? extends _BaseObject_> type;
   
   /** Serialization of each instance field. */
   private LinkedHashMap<Field, Object> serialized = new LinkedHashMap<>();

   /** Flag indicating whether the appserver, which sent the object, is running in the same
    *  JVM as the requester.
    */
   private boolean local = false;
   
   /**
    * Default c'tor, used for deserialization.
    */
   public LegacyObject()
   {
   }

   /**
    * Calls {@link #serialize} in order to prepare the object to be transported over network.
    *
    * @param    o
    *           The instance.
    * @param    local
    *           Flag indicating if the object is being transported in the same JVM.
    */
   public LegacyObject(object<? extends _BaseObject_> o, boolean local)
   {
      this.local = local;
      serialize(o);
   }

   /**
    * Calls {@link #serialize} in order to prepare the object to be transported over network.
    * 
    * @param    o
    *           The instance.
    */
   public LegacyObject(object<? extends _BaseObject_> o)
   {
      serialize(o);
   }
   
   /**
    * Write the state of this instance to the specified stream.
    * 
    * @param    out
    *           The stream where the state of this instance will be sent.
    */
   @Override
   public void writeExternal(ObjectOutput out) 
   throws IOException
   {
      out.writeUTF(legacyClass);
      out.writeBoolean(unknown);
      
      if (unknown)
      {
         return;
      }
      
      out.writeInt(serialized.size());
      for (Map.Entry<Field, Object> entry : serialized.entrySet())
      {
         Field f = entry.getKey();
         Object ser = entry.getValue();
         
         out.writeUTF(f.getDeclaringClass().getName());
         out.writeUTF(f.getName());
         out.writeObject(ser);
      }
   }

   /**
    * Initialize this instance by reading its state from the specified stream.
    * 
    * @param    in
    *           The stream from where the state of this instance will be read.
    */
   @Override
   public void readExternal(ObjectInput in) 
   throws IOException, 
          ClassNotFoundException
   {
      this.legacyClass = in.readUTF();
      this.unknown = in.readBoolean();
      this.type = ObjectOps.resolveClass(legacyClass);
      
      if (unknown)
      {
         return;
      }
      
      serialized.clear();
      int numFields = in.readInt();
      for (int i = 0; i < numFields; i++)
      {
         String fclass = in.readUTF();
         String fname = in.readUTF();
         Object val = in.readObject();
         try
         {
            Field field = Class.forName(fclass).getDeclaredField(fname);
            serialized.put(field, val);
         }
         catch (NoSuchFieldException | 
                SecurityException    | 
                ClassNotFoundException e)
         {
            LOG.severe("", e);
         }
      }
   }

   /**
    * Get the object's {@link #type}.
    * 
    * @return   See above.
    */
   public Class<? extends _BaseObject_> getType()
   {
      return type;
   }

   public void assign(LegacyObject ref)
   {
      this.legacyClass = ref.legacyClass;
      this.type = ref.type;
      this.unknown = ref.unknown;
      this.serialized.clear();
      this.serialized.putAll(ref.serialized);
   }

   /**
    * Create a new instance and set its state from the {@link #serialized} information.
    * 
    * @return   See above.
    */
   public _BaseObject_ restore()
   {
      return restore(false);
   }

   /**
    * Create a new instance and set its state from the {@link #serialized} information.
    *
    * @param    local
    *           Flag indicating if the object is being transported in the same JVM.
    * 
    * @return   See above.
    */
   _BaseObject_ restore(boolean local)
   {
      if (unknown)
      {
         return null;
      }
      
      if (local)
      {
         // a quirk for in-JVM appserver calls: emulate network transport, otherwise TableWrapper can't be 
         // deserialized
         try
         {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(baos);
            this.writeExternal(out);
            out.close();
   
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream in = new ObjectInputStream(bais);
            this.readExternal(in);
            in.close();
         } 
         catch (IOException | 
                ClassNotFoundException e)
         {
            LOG.severe("", e);
         }
      }
      
      object<?> ref = ObjectOps.newInstance(type, true);
      ObjectOps.pendingAssign(ref.ref);
      
      if (!ref._isValid())
      {
         LOG.log(Level.SEVERE, "An instance of type " + type + " could not be created.");
         return null;
      }
      
      for (Map.Entry<Field, Object> entry : serialized.entrySet())
      {
         Field f = entry.getKey();
         Object ser = entry.getValue();
         
         try
         {
            restoreField(f, ref.ref, ser);
         } 
         catch (IllegalArgumentException | 
                IllegalAccessException e)
         {
            LOG.severe("", e);
         }
      }
      
      return ref.ref;
   }

   /**
    * Serialize the given {@link object} reference, and save it, to be transported over network.
    *
    * @param o
    */
   private void serialize(object<? extends _BaseObject_> o)
   {
      unknown = o.ref == null;
      type = (unknown ? o.type() : o.ref.getClass());
      legacyClass = type == null ? "Progress.Lang.Object" : ObjectOps.getLegacyName(type);

      save(o.ref);
   }

   /**
    * Serialize the given reference.
    * 
    * @param    ref
    *           The reference to serialize.
    */
   private void save(_BaseObject_ ref)
   {
      if (unknown)
      {
         return;
      }
      
      // TODO: check type to have LegacySerializable annotation
      
      ArrayList<Field> fields = collectInstanceFields(ref);
      
      for (int i = 0; i < fields.size(); i++)
      {
         Field f = fields.get(i);
         try
         {
            Object ser = serializeField(ref, f);
            serialized.put(f, ser);
         }
         catch (IllegalArgumentException | 
                IllegalAccessException e)
         {
            LOG.severe("", e);
         }
      }
   }
   
   /**
    * Restore the given field from its serialized state.
    * 
    * @param    f
    *           The field to restore.
    * @param    ref
    *           The reference where the field needs to be set.
    * @param    ser
    *           The field's new value, which will either be assigned or set directly.
    */
   private void restoreField(Field f, _BaseObject_ ref, Object ser)
   throws IllegalArgumentException, 
          IllegalAccessException
   {
      Class<?> ftype = f.getType();
      f.setAccessible(true);
      Object dest = f.get(ref);
      
      if (ser instanceof LegacyArray)
      {
         LegacyArray arr = (LegacyArray) ser;
         int size = Array.getLength(dest);
         if (size == 0)
         {
            size = Array.getLength(arr.val);
            if (size == 0)
            {
               // nothing else to do, just an empty array
               return;
            }
            
            dest = ArrayAssigner.resizeField((BaseDataType[]) dest, size, ref);
            f.set(ref, dest);
         }
         Class<?> elType = ftype.getComponentType();
         for (int i = 0; i < size; i++)
         {
            Externalizable elSer = (Externalizable) Array.get(arr.val, i);
            Object destEl = Array.get(dest, i);
            if (!deserializeObject(elType, destEl, elSer))
            {
               // if this is not a FWD type, then just set the element.
               Array.set(dest, i, elSer);
            }
         }
      }
      else
      {
         if (!deserializeObject(ftype, dest, ser))
         {
            f.set(ref, ser);
         }
      }
   }

   /**
    * Restore the state of a field using the received (serialized) value.
    * 
    * @param    ftype
    *           The field's type.
    * @param    dest
    *           The field's current value.
    * @param    ser
    *           The field's value to be assigned.
    *           
    * @return   <code>true</code> if the field was updated.
    */
   private boolean deserializeObject(Class<?> ftype, Object dest, Object ser)
   {
      if (object.class.isAssignableFrom(ftype))
      {
         deserializeObject(dest, (LegacyObject) ser);
         return true;
      }
      
      if (memptr.class.isAssignableFrom(ftype))
      {
         deserializeMemptr(dest, (MemoryBuffer) ser);
         return true;
      }
      
      if (DataSet.class.isAssignableFrom(ftype))
      {
         deserializeDataSet(dest, (longchar) ser);
         return true;
      }
      
      if (Buffer.class.isAssignableFrom(ftype))
      {
         deserializeTable(dest, (TableWrapper) ser);
         return true;
      }
      
      if (BaseDataType.class.isAssignableFrom(ftype))
      {
         deserialize(dest, (BaseDataType) ser);
         return true;
      }
      
      return false;
   }
   
   /**
    * Restore the given field from its serialized state.
    * 
    * @param    dest
    *           The reference where the field needs to be set.
    * @param    ser
    *           The serialized state.
    */
   private void deserialize(Object dest, BaseDataType ser)
   {
      ((BaseDataType) dest).assign(ser);
   }

   /**
    * Restore the given table field from its serialized state.
    * 
    * @param    dest
    *           The reference where the field needs to be set.
    * @param    ser
    *           The serialized state.
    */
   private void deserializeTable(Object dest, TableWrapper ser)
   {
      Buffer buffer = (Buffer) dest;
      Temporary dmo = TemporaryBuffer.getDefaultBuffer(buffer.tableHandle());
      TemporaryBuffer.insertAllRows(ser, dmo, false, TemporaryBuffer.CopyTableMode.INPUT_PARAM_MODE, -1);
   }

   /**
    * Restore the given {@link DataSet} field from its serialized state.
    * 
    * @param    dest
    *           The reference where the field needs to be set.
    * @param    ser
    *           The serialized state.
    */
   private void deserializeDataSet(Object dest, longchar ser)
   {
      DataSet ds = (DataSet) dest;
      ds.readXml(new SourceData("longchar", ser), new character("empty"), new character(), new logical());
   }

   /**
    * Restore the given {@link memptr} field from its serialized state.
    * 
    * @param    dest
    *           The reference where the field needs to be set.
    * @param    ser
    *           The serialized state.
    */
   private void deserializeMemptr(Object dest, MemoryBuffer ser)
   {
      memptr m = (memptr) dest;
      m.assign(ser.getValue());
   }

   /**
    * Restore the given {@link object} field from its serialized state.
    * 
    * @param    dest
    *           The reference where the field needs to be set.
    * @param    ser
    *           The serialized state.
    */
   private void deserializeObject(Object dest, LegacyObject ser)
   {
      object o = (object) dest;
      o.assign(ser.restore());
   }

   /**
    * Get the field's serialized state.
    * 
    * @param    ref
    *           The reference from where the field is read.
    * @param    f
    *           The field to read.
    *           
    * @return   See above.
    */
   private Object serializeField(_BaseObject_ ref, Field f) 
   throws IllegalArgumentException, 
          IllegalAccessException
   {
      Class<?> ftype = f.getType();
      f.setAccessible(true);
      Object val = f.get(ref);
      if (ftype.isArray())
      {
         return new LegacyArray(val);
      }
      
      return serializeField(ftype, val, local);
   }

   /**
    * Get the field's value using a serializable instance, assuming that
    * the sent object is not being transported in the same JVM.
    *
    * @param    ftype
    *           The field's type.
    * @param    val
    *           The field's value.
    *
    * @return   The serializable instance of this value or the actual value.
    */
   private static Object serializeField(Class<?> ftype, Object val)
   {
     return serializeField(ftype, val, false);
   }

   /**
    * Get the field's value using a serializable instance.
    * 
    * @param    ftype
    *           The field's type.
    * @param    val
    *           The field's value.
    * @param    local
    *           Flag indicating if the object is being transported in the same JVM.
    *
    * @return   The serializable instance of this value or the actual value.
    */
   private static Object serializeField(Class<?> ftype, Object val, boolean local)
   {
      if (object.class.isAssignableFrom(ftype))
      {
         return serializeObject((object) val);
      }
      else if (memptr.class.isAssignableFrom(ftype))
      {
         return serializeMemptr((memptr) val);
      }
      else if (DataSet.class.isAssignableFrom(ftype))
      {
         return serializeDataSet((DataSet) val);
      }
      else if (Buffer.class.isAssignableFrom(ftype))
      {
         return serializeTable((Buffer) val, local);
      }
      else if (BaseDataType.class.isAssignableFrom(ftype))
      {
         return serialize((BaseDataType) val);
      }
      else if (val == null || val instanceof Serializable)
      {
         return val;
      }
      else
      {
         // this will abend when the data is transferred via serialization,  so lets log the stack trace here,
         // but do not completely abend
         Class<?> type = val.getClass();
         LOG.log(Level.SEVERE, "Value " + type + " is not serializable.", new NotSerializableException());
         ErrorManager.recordOrThrowError(17315, 
                                        "Object of class type `" + type + 
                                        "` is not allowed to be marshaled remotely", 
                                        false);
         return null;
      }
   }
   
   /**
    * Get the field's serialized state.
    * 
    * @param    ref
    *           The field's value.
    *           
    * @return   See above.
    */
   private static  Externalizable serialize(BaseDataType ref)
   {
      return ref;
   }

   /**
    * Get the field's serialized state.
    * 
    * @param    buffer
    *           The field's value.
    *           
    * @return   See above.
    */
   private static  Externalizable serializeTable(Buffer buffer, boolean local)
   {
      Temporary dmo = TemporaryBuffer.getDefaultBuffer(buffer.tableHandle());
      TempTableResultSet resultSet = new TempTableResultSet(dmo, true, true, false, true);
      TableWrapper tw = new TableWrapper(resultSet);

      if(local)
      {
         tw = preProcessTable(tw);
      }

      return tw;
   }

   /**
    * This method emulates as if {@link TableWrapper} was sent over the network.
    *
    * @param tableWrapper
    *        The {@link TableWrapper} object which has to be serialized.
    */
   private static TableWrapper preProcessTable(TableWrapper tableWrapper)
   {
      try
      {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ObjectOutputStream out = new ObjectOutputStream(baos);
         tableWrapper.writeExternal(out);
         out.close();
         
         tableWrapper = new TableWrapper();
         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
         ObjectInputStream in = new ObjectInputStream(bais);
         tableWrapper.readExternal(in);
         in.close();

         return tableWrapper;
      }
      catch (Throwable t)
      {
         throw new RuntimeException(t);
      }
   }

   /**
    * Get the field's serialized state.
    * 
    * @param    dataSet
    *           The field's value.
    *           
    * @return   See above.
    */
   private static Externalizable serializeDataSet(DataSet dataSet)
   {
      longchar lc = new longchar("");
      dataSet.writeXml(new TargetData(TargetData.TYPE_LONGCHAR, lc), new logical(true));
      return lc;
   }

   /**
    * Get the field's serialized state.
    * 
    * @param    ref
    *           The field's value.
    *           
    * @return   See above.
    */
   private static Externalizable serializeMemptr(memptr ref)
   {
      return new MemoryBuffer(ref.getByteArray());
   }

   /**
    * Get the field's serialized state.
    * 
    * @param    ref
    *           The field's value.
    *           
    * @return   See above.
    */
   private static Externalizable serializeObject(object ref)
   {
      // TODO: circular references....
      
      return new LegacyObject(ref);
   }

   /**
    * Collect all instance fields defined in the reference's type.
    * 
    * @param    ref
    *           The reference.
    *           
    * @return   See above.
    */
   private ArrayList<Field> collectInstanceFields(_BaseObject_ ref)
   {
      // TODO: cache this.
      
      ArrayList<Field> res = new ArrayList<>();
      
      Class<? extends _BaseObject_> parent = type;
      while (parent != BaseObject.class)
      {
         // internal FWD types allow presence of non-BDT fields - these will be serialized directly
         boolean internalType = parent.isAnnotationPresent(LegacyResource.class);
         
         Field[] fields = parent.getDeclaredFields();
         for (Field f : fields)
         {
            int mods = f.getModifiers();
            if (Modifier.isStatic(mods))
            {
               continue;
            }
            
            // TODO: omit NOT-SERIALIZABLE fields
            
            Class<?> ftype = f.getType();
            
            LegacySignature ls = f.getAnnotation(LegacySignature.class);
            // check to be a dataset or temp-table
            if (ls == null    &&
                !internalType &&
                !(ftype == DataSet.class || TempTableBuffer.class.isAssignableFrom(ftype)))
            {
               LOG.log(Level.WARNING, 
                       "Omitting field " + f.toString() + " when serializing type " +  ref.getClass());
               continue;
            }
            
            if (ftype == handle.class || (ftype.isArray() && ftype.getComponentType() == handle.class))
            {
               LOG.log(Level.FINE, "HANDLE fields can not be serialized: " + f.toString());
               continue;
            }
            
            try
            {
               f.setAccessible(true);
               Object val = f.get(ref);
               
               if (val instanceof Buffer)
               {
                  // TODO: check to be the default buffer for an actual temp-table and not an explicit buffer
               }
               
               res.add(f);
            }
            catch (IllegalArgumentException | 
                   IllegalAccessException e)
            {
               LOG.severe("", e);
            }
         }
         
         parent = (Class<? extends _BaseObject_>) parent.getSuperclass();
      }
      
      return res;
   }
   
   /**
    * Serializable storage of a BDT array associated with a field.
    */
   public static class LegacyArray
   implements Externalizable
   {
      /** The field's value. */
      private Object val;

      /**
       * Default constructor, for deserialization purposes.
       */
      public LegacyArray()
      {
      }
      
      /**
       * Create a new instance to serialize the given array.
       * 
       * @param   val
       *          The array to serialize.
       */
      public LegacyArray(Object val)
      {
         this.val = val;
      }
      
      /**
       * Serialize the {@link #val value}.  Relies on {@link LegacyObject#serializeField} to get the 
       * serialized representation of the array elements.
       */
      @Override
      public void writeExternal(ObjectOutput out)
      throws IOException
      {
         Class<?> type = val == null ? Object.class : val.getClass().getComponentType();
         int size = val == null ? 0 : Array.getLength(val);
         out.writeInt(size);
         for (int i = 0; i < size; i++)
         {
            Object valEl = Array.get(val, i);
            Object el = serializeField(type, valEl);
            out.writeObject(el);
         }
      }
      
      /**
       * Deserialize the {@link #val value}.
       */
      @Override
      public void readExternal(ObjectInput in) 
      throws IOException, 
             ClassNotFoundException
      {
         int size = in.readInt();
         val = Array.newInstance(Externalizable.class, size);
         for (int i = 0; i < size; i++)
         {
            Externalizable extEl = (Externalizable) in.readObject();
            Array.set(val,  i, extEl);
         }
      }
   }
}