GenerateLegacyProxyOpenClient.java

/*
** Module   : GenerateLegacyProxyOpenClient.java
** Abstract : Generate the Java proxy programs for the .xpxg configured programs, associated with legacy 
**            OpenClient proxies.
**
** Copyright (c) 2022-2025, Golden Code Development Corporation.
**
** -#- -I- --Date-- --------------------------------------Description---------------------------------------
** 001 CA  20220516 Created initial version.
**     CA  20220517 Generate proxy OpenClient programs only for programs part of the initial conversion list. 
** 002 GBB 20230512 Logging methods replaced by CentralLogger/ConversionStatus.
** 003 CA  20250515 Now parsing pulls all .cls dependencies into conversion - after that, only the proxy 
**                  programs are needed, so filter the ASTs to remove anything else added by parser.
*/

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

import java.io.*;
import java.io.IOException;
import java.util.*;
import java.util.logging.*;

import com.goldencode.ast.*;
import com.goldencode.p2j.cfg.*;
import com.goldencode.p2j.pattern.*;
import com.goldencode.p2j.schema.SchemaException;
import com.goldencode.p2j.util.*;

/**
 * Given the list of .xpxg files configured in {@code p2j.cfg.xml} under the {@code proxy-programs-cfg} 
 * parameter, it will parse, annotate and generate the Java proxy programs for all program files configured.
 * <p>
 * Although the middle front and middle parts are being executed for this sub-set of programs, no other Java
 * code is generated, either for the schema or for the 4GL programs configured as 'proxy programs'.
 * <p>
 * The {@link #back()} processing will generate only the Java code for the proxy programs.
 */
public class GenerateLegacyProxyOpenClient
extends ConversionDriver
{
   /** Logger. */
   private static final ConversionStatus LOG = ConversionStatus.get(GenerateLegacyProxyOpenClient.class);
   
   /** Job title. */
   private static final String TITLE = "FWD Proxy Program Generator";
   
   /** The set of programs for which OpenClient proxies need to be generated. */
   private static Set<File> proxyFiles = null;
   
   /**
    * Create a new job.
    * 
    * @param    job
    *           The job configuration.
    */
   private GenerateLegacyProxyOpenClient(JobDefinition job)
   {
      super(job);
   }

   /**
    * Using the source name list as input, this method creates duplicate lists with the proper 
    * <code>.ast</code> or <code>.jast</code> file suffixes on each entry. The results are saved into the 
    * Progress AST name list.
    * <p>
    * This also ensures that only the {@link #proxyFiles} ASTs remain to be processed by next phases.
    */
   @Override
   protected void convertSourceNamesToAstNames()
   {
      super.convertSourceNamesToAstNames();
      
      // leave only the proxy ASTs
      asts = getProxyAsts();
   }
   
   /**
    * Drive the back end of the conversion process to generate the Java code for the proxy programs. 
    * This uses the file list of Progress AST files configured in the .xpxg files.
    * 
    * <p>
    * This runs the following:
    * <ul>
    *    <li> Proxy Client Programs Annotations, in {@code annotations/legacy_services}.  This is an extract
    *    from annotations_prep and annotation phases, to annotate the ASTs with Java names.
    *    <li> {@link #generateProxyPrograms};
    * </ul>
    *
    * @throws   ConfigurationException   If there is problem reading/parsing the configuration.
    * @throws   AstException             If there is an problem processing ASTs.
    * @throws   SchemaException          If there is a problem with the schema.
    * @throws   IOException              If there is a problem with I/O.
    */
   @Override
   protected void back() 
   throws AstException, 
          ConfigurationException
   {
      processTrees("Proxy Client Programs Annotations",
                   "annotations/legacy_services",
                   Configuration.getParameter("codenames"),
                   false,
                   asts,
                   debug);
      
      generateProxyPrograms();
   }
   
   /**
    * Get the list of {@link TransformDriver#asts all ASTs} which are associated with the {@link #proxyFiles},
    * removing anything else.
    * 
    * @return   See above.
    */
   private FileList getProxyAsts()
   {
      // keep only the files which were initially in conversion (without the auto-added .cls dependencies)
      File[] allAsts = asts.list();
      ExplicitFileList proxies = new ExplicitFileList(Configuration.isCaseSensitive());
      
      for (int i = 0; i < allAsts.length; i++)
      {
         boolean ok = false;
         File ast = allAsts[i];
         String astname = ast.getPath();
         
         for (File f : proxyFiles)
         {
            String fname = f.getPath();
            if (astname.equals(fname + ".ast"))
            {
               ok = true;
               break;
            }
         }
         
         if (ok)
         {
            proxies.add(astname);
         }
      }
      
      return proxies;
   }
   
   /**
    * Main method.  No arguments are required.  The configuration is read from the {@code "proxy-programs-cfg"}
    * parameter of {@code p2j.cfg.xml}.
    * 
    * @param    args
    *           None.
    */
   public static void main(String[] args) 
   {
      ServiceSupport.initializeProxyConfigurations();
      
      // get all the files configured in the proxy .xpxg files
      List<LegacyProxyConfig> cfgs = ServiceSupport.getProxyConfigs();
      if (cfgs == null)
      {
         LOG.log(Level.SEVERE, "No proxy .xpxg file is configured in p2j.cfg.xml, 'proxy-programs-cfg'");
         System.exit(-1);
      }
      
      ExplicitFileList files = new ExplicitFileList(Configuration.isCaseSensitive());
      for (LegacyProxyConfig cfg : cfgs)
      {
         List<String> programFiles = cfg.getProxyProgramFiles();
         for (String program : programFiles)
         {
            try
            {
               String pfile = FileOperationsWorker.findFile(program, null);
               
               if (pfile == null)
               {
                  System.out.println("WARNING: Could not find program '" + program + "' for proxy config: " + 
                                     cfg.getAppObject() + " in package " + cfg.getPkg());
                  continue;
               }

               System.out.println("Adding program '" + pfile + "' for proxy config: " + cfg.getAppObject() + 
                                  " in package " + cfg.getPkg());
               files.add(pfile);
            }
            catch (ConfigurationException e)
            {
               LOG.log(Level.SEVERE, "", e);
               System.exit(-2);
            }
         }
      }
      proxyFiles = new LinkedHashSet<>(Arrays.asList(files.list()));
      
      int debug = MSG_STATUS;
      RunMode run = new CvtRunMode(true, true, true, false, true, false, false, true, true);
      JobDefinition job = new JobDefinition(run, null, files, null, 1, true, false, debug);
      GenerateLegacyProxyOpenClient gen = new GenerateLegacyProxyOpenClient(job);
      gen.executeJob(TITLE);
   }
}