DirectoryDiff.java

/*
** Module   : DirectoryDiff.java
** Abstract : compare and report on two directory trees
**
** Copyright (c) 2006-2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- --JPRM-- ----------------Description-----------------
** 001 NVS 20060808   @28502 Created initial version
** 002 NVS 20060808   @28518 Added new feature: comparison of a branch vs
**                           the whole directory. A branch can be commonly
**                           named in both directories or even have two
**                           different names.
** 003 GES 20061215   @31703 Changes in bootstrap config interface.
** 004 GES 20101117          Switched to new static schema loader that reads
**                           from well-known resources rather than a manually
**                           defined configuration file.
** 005 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.directory;

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

/**
 * This class compares two directories and reports the differences.
 * <p>
 * To be able to compare directories, this class needs access to any directory
 * schema extension files and the directories to compare. The extensions (if
 * they exist) are loaded from application-specific JAR files in the classpath.
 * <p>
 * The directories to compare are specified with the command line and can be
 * anywhere.
 * <p>
 * The class starts with the root node and compares every node in both 
 * directories. Nodes are treated as different, if they have different object
 * class or different number or order of attribute values.
 * <p>
 * Every found difference is reported to the standard output. The first
 * directory in the command line is marked as L (meaning Left) and the second
 * one is marked as R (meaning Right).
 * <p>
 * This is the first cut of this utility class. It can be enhanced in multiple
 * directions:
 * <ul>
 *   <li>the output can be made clearer; a good example to follow is the
 *       GCD COMPDIRS utility;
 *   <li>functionality can be added to:
 *     <ul>
 *       <li>limit the comparison to only the specified branch of tree;
 *       <li>exclude some branches from comparison;
 *       <li>exclude some attributes from comparison;
 *       <li>compare only listed attributes;
 *       <li>produce a diff tree as a file;
 *       <li>synchronize trees selectively etc.
 *     </ul>
 * </ul>
 * 
 * @author  NVS
 */
public class DirectoryDiff
{
   /** Logger */
   private static final CentralLogger LOG = CentralLogger.get(DirectoryDiff.class);
   
   /** backend for the first file */
   private Remapper r1 = null;
   
   /** backend for the first file */
   private Remapper r2 = null;
   
   /** starting left directory path */
   private String pathL = "";
   
   /** starting right directory path */
   private String pathR = "";
   
   /**
    * Construct an instance of DirectoryDiff for the given command line
    * parameters.
    * 
    * @param   args
    *          Command line parameters. Expected are two file names for the
    *          first (left) and the second (right) directories.
    * @throws  ConfigurationException
    *          In case of configuration error.
    */
   public DirectoryDiff(String[] args)
   throws ConfigurationException
   {
      if (args.length < 2 || args.length > 4)
      {
         say("Usage: java DirectoryDiff left-dir.xml right-dir.xml " + 
             "[ path | left-path [ right-path] ]");

         throw new ConfigurationException("");
      }
      
      if (args.length > 2)
      {
         pathL = args[2];
         pathR = args[2];
      }

      if (args.length == 4)
         pathR = args[3];

      // Empty BootstrapConfigs
      BootstrapConfig bc1 = new BootstrapConfig(null, null, null, null);
      BootstrapConfig bc2 = new BootstrapConfig(null, null, null, null);
      
      // directory package configuration
      bc1.setServer(true);
      bc1.setConfigItem("directory", "backend", "type",     "xml");
      bc1.setConfigItem("directory", "xml",     "filename", args[0]);

      bc2.setServer(true);
      bc2.setConfigItem("directory", "backend", "type",     "xml");
      bc2.setConfigItem("directory", "xml",     "filename", args[1]);
       
      // initialize directory schema
      SchemaLoad.initSchema();

      // instantiating directory tree backends
      r1 = DirectoryService.genRemapper(bc1);
      r2 = DirectoryService.genRemapper(bc2);
   }

   /**
    * Shortcut for the System.out.prinln()
    * 
    * @param   msg
    *          Message to print to standard output.
    */
   static void say(String msg)
   {
      System.out.println(msg);
   }

   /**
    * Main application worker routine.
    * 
    */
   private void run()
   {
      boolean res = true;
      
      // binding to the directories
      do
      {
         res = r1.bind();
         
         if (!res)
         {
            say("Binding to left directory failed");
            break;
         }
         
         res = r2.bind();

         if (!res)
         {
            say("Binding to right directory failed");
            break;
         }
      }
      while (false);
      
      if (!res)
         return;
      
      // do comparison
      boolean rc = diffNodes(pathL, pathR);
      
      // unbinding from the directories
      res = r1.unbind();
      
      if (!res)
         say("Unbinding from tree 1 failed");
      
      res = r2.unbind();

      if (!res)
         say("Unbinding from tree 2 failed");
      
      if (rc)
         say("Directories are identical");
   }

   /**
    * Compares two nodes.
    * 
    * @param   nid1
    *          node ID for the first tree node
    * @param   nid2
    *          node ID for the second tree node
    */
   private boolean diffNodes(String nid1, String nid2)
   {
      boolean rc = true;
      
      // compare the directory node class
      String oc1 = r1.getNodeClassName(nid1);
      String oc2 = r2.getNodeClassName(nid2);
      String pid1 = new String("L '" + nid1 + "' (" + oc1 + ") ");
      String pid2 = new String("R '" + nid2 + "' (" + oc2 + ") ");
      if (!oc1.equals(oc2))
      {
         say("Object classes mismatch; attributes comparison skipped");
         say(pid1);
         say(pid2);
         rc = false;
      }
      
      // compare attributes if of the same class
      Set sa1 = new HashSet();
      Set sa2 = new HashSet();
      Set su1 = new HashSet();
      Set su2 = new HashSet();
      Set sc  = new HashSet();

      if (rc)
      {
         // enumerate attributes
         AttributeDefinition[] a1 = r1.enumerateNodeAttributes(nid1);
         int la1 = a1 == null ? 0 : a1.length;
         for (int i = 0; i < la1; i ++)
            sa1.add(a1[i].getName());
   
         AttributeDefinition[] a2 = r2.enumerateNodeAttributes(nid2);
         int la2 = a2 == null ? 0 : a2.length;
         for (int i = 0; i < la2; i ++)
            sa2.add(a2[i].getName());
         
         // separate attributes into 3 groups: 1st only, common, 2nd only
         su1.addAll(sa1);
         su1.removeAll(sa2);
         su2.addAll(sa2);
         su2.removeAll(sa1);
         sc.addAll(sa1);
         sc.retainAll(sa2);
         
         // report unique attributes from the 1st node
         if (su1.size() != 0)
         {
            say(pid1 + "has unique attributes:");
            
            Iterator s = su1.iterator();
            while (s.hasNext())
            {
               String name = (String)s.next();
               say("   '" + name + "'");
            }
            
            rc = false;
         }
         
         // report unique attributes from the 2nd node
         if (su2.size() != 0)
         {
            say(pid2 + "has unique attributes:");
            
            Iterator s = su2.iterator();
            while (s.hasNext())
            {
               String name = (String)s.next();
               say("   '" + name + "'");
            }
   
            rc = false;
         }
   
         // compare attribute values from the common set
         if (sc.size() != 0)
         {
            Iterator s = sc.iterator();
            while (s.hasNext())
            {
               String name = (String)s.next();
               Attribute al1 = DirectoryService.getNodeAttribute(r1, nid1, 
                                                                 name);
               Attribute al2 = DirectoryService.getNodeAttribute(r2, nid2, 
                                                                 name);
               String[] v1 = al1 == null ? null : al1.getValues();
               String[] v2 = al2 == null ? null : al2.getValues();
               int l1 = v1 == null ? 0 : v1.length;
               int l2 = v2 == null ? 0 : v2.length;
   
               boolean rcc = l1 == l2;
               if (rcc && l1 > 0)
               {
                  for (int i = 0; i < l1; i ++)
                  {
                     if (!v1[i].equals(v2[i]))
                     {
                        rcc = false;
                        break;
                     }
                  }
               }
               
               // report discrepancies
               if (!rcc)
               {
                  say(pid1 + "attribute '" + name + 
                      "' has different value(s):");
                  for (int i = 0; i < l1; i ++)
                     say("   " + v1[i]);
                  
                  say(pid2 + "attribute '" + name + 
                      "' has different value(s):");
                  for (int i = 0; i < l2; i ++)
                     say("   " + v2[i]);
               }
   
               if (rc && !rcc)
                  rc = false;
            }
         }
      }
      
      // enumerate child nodes
      String[] n1 = r1.enumerateNodes(nid1);
      int la1 = n1 == null ? 0 : n1.length;
      sa1.clear();
      for (int i = 0; i < la1; i ++)
         sa1.add(n1[i]);

      String[] n2 = r2.enumerateNodes(nid2);
      int la2 = n2 == null ? 0 : n2.length;
      sa2.clear();
      for (int i = 0; i < la2; i ++)
         sa2.add(n2[i]);

      // separate children into 3 groups: 1st only, common, 2nd only
      su1.clear();
      su2.clear();
      sc.clear();
      su1.addAll(sa1);
      su1.removeAll(sa2);
      su2.addAll(sa2);
      su2.removeAll(sa1);
      sc.addAll(sa1);
      sc.retainAll(sa2);
      
      // report unique children from the 1st node
      if (su1.size() != 0)
      {
         say(pid1 + "has unique children:");
         
         Iterator s = su1.iterator();
         while (s.hasNext())
         {
            String name = (String)s.next();
            say("   '" + name + "' (" + 
                r1.getNodeClassName(new String(nid1 + "/" + name)) + ")");
         }
         
         rc = false;
      }
      
      // report unique attributes from the 2nd node
      if (su2.size() != 0)
      {
         say(pid2 + "has unique children:");
         
         Iterator s = su2.iterator();
         while (s.hasNext())
         {
            String name = (String)s.next();
            say("   '" + name + "' (" + 
                r2.getNodeClassName(new String(nid2 + "/" + name)) + ")");
         }

         rc = false;
      }

      // compare child nodes from the common set
      if (sc.size() != 0)
      {
         Iterator s = sc.iterator();
         while (s.hasNext())
         {
            String name = (String)s.next();
            String nc1 = new String(nid1 + "/" + name);
            String nc2 = new String(nid2 + "/" + name);
            boolean rcc = diffNodes(nc1, nc2);
            if (rc && !rcc)
               rc = false;
         }
      }

      // return
      return rc;
   }

   /**
    * Application entry point.
    * 
    * @param   args
    *          Command line parameters.
    */
   public static void main(String[] args)
   {
      DirectoryDiff dir = null;
      
      try
      {
         dir = new DirectoryDiff(args);
         dir.run();
      }
      catch (ConfigurationException e)
      {
         LOG.severe("", e);
      }
   }
}