LibraryDaemon.java

/*
** Module   : LibraryDaemon.java
** Abstract : remote access to native library management and function invocation
**
** Copyright (c) 2013-2024, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 GES 20140110 First version. The idea is to provide remote access from the server to the client-side 
**                  native library management and function invocation. For security purposes, this class is 
**                  intentionally package private.
** 002 CA  20220515 Allow library and memptr calls to be executed on server-side.
** 003 GBB 20240826 Instantiation of the class only from the package osresource.
*/
/*
** 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.osresource;

import com.goldencode.p2j.library.*;
import com.goldencode.p2j.net.*;

/**
 * Provides remote access to the client-side {@link LibraryManager} API.
 */
class LibraryDaemon
implements NativeAPICaller
{
   /** Token used to authenticate with the dispatcher when registering APIs. */
   private static Object modToken = null;
   
   /**
    * Create an instance and export its API to the network.
    *
    * @param    single
    *           <code>true</code> to startup within the server process which
    *           must bypass the shared infrastructure initialization. Use
    *           <code>false</code> for the normal client JVM startup.
    */
   LibraryDaemon(boolean single)
   {
      synchronized (LibraryDaemon.class)
      {
         modToken = RemoteObject.registerServer(NativeAPICaller.class, this, modToken, single);
      }
   }
      
   /**
    * Invoke the defined native API call, loading (and optionally unloading) the library as
    * needed.
    * <p>
    * All exceptions will be raised on the server-side (not here). Instead of throwing
    * an exception, this code will set an error code and return.
    * 
    * @param    libname
    *           The library name where this native procedure should be found.
    * @param    funcname
    *           The function name being called in the library (except where an ordinal is being
    *           used).
    * @param    ordinal
    *           The entry point's ordinal or -1 if the entry point should be found by name.
    * @param    persistent
    *           <code>true</code> to leave the library loaded when the native call is complete. 
    * @param    conv
    *           The calling convention to be used in this native call.
    * @param    signature
    *           Contains the return value and argument descriptors.  In the case of the arguments
    *           there will be 1 descriptor for each argument to the call (where the 0 index is
    *           the leftmost argument and the numargs - 1 index is the rightmost).  Each
    *           descriptor defines the type and will contain any input value in the case of an
    *           argument.  If the return value descriptor is <code>null</code>, the call should
    *           be treated as a void return.
    *
    * @return   The same signature instance will be updated with output (and/or return data)
    *           upon a successful call or with an error code on any failure.
    */
   public Signature invoke(String            libname,
                           String            funcname,
                           int               ordinal,
                           boolean           persistent,
                           CallingConvention conv,
                           Signature         signature)
   {
      return LibraryManager.invoke(libname, funcname, ordinal, persistent, conv, signature);
   }
   
   /**
    * Attempts to unload the library identified by the given name.
    *
    * @param    libname
    *           The name of the library to attempt to unload.
    */ 
   public void release(String libname)
   {
      LibraryManager.release(libname);
   }
}