/* * * Copyright (c) 1999 - 2011 my-Channels Ltd * Copyright (c) 2012 - 2017 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors. * * Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG. * */ namespace com.pcbsys.nirvana.nAdminAPI { using System; using System.Collections.Generic; using System.Linq; using System.Text; using com.pcbsys.nirvana.client; public class nDiff { String Rname1 = null; String Rname2 = null; /** * constructor */ public nDiff() { } /** * Establishes a connection to a realm and returns the realm node * @param rname The RNAME for the realm to connect to * @return The nRealmNode for the realm specified by rname */ public nRealmNode getRealm(String rname) { try { Console.WriteLine("Establishing realm node..."); nSessionAttributes nsa = new nSessionAttributes(rname); nRealmNode rNode = new nRealmNode(nsa); if (!rNode.isAuthorised()) { Console.WriteLine("User not authorised on this node " + nsa); Environment.Exit(1); } Console.WriteLine("done\n"); return rNode; } catch (Exception ) { //TODO Environment.Exit(1); } return null; } /** * Loops through each config group and calls processGroup to compare each item in the config groups * @param realm1 nRealmNode of the first realm to compare * @param realm2 nRealmNode of the second realm to compare */ public void configDifferences(nRealmNode realm1, nRealmNode realm2) { System.Collections.IEnumerator cGroups1 = realm1.getConfigGroups(); System.Collections.IEnumerator cGroups2 = realm2.getConfigGroups(); while (cGroups1.MoveNext()) { cGroups2.MoveNext(); nConfigGroup group1 = (nConfigGroup)cGroups1.Current; nConfigGroup group2 = (nConfigGroup)cGroups2.Current; if (!(group1.Name.Equals(group2.Name))) { Console.WriteLine("group1 = " + group1.Name + " group2 = " + group2.Name); Environment.Exit(1); } processGroup(group1, group2); } } /** * Loops through each item in the first realms config group and looks that item up in the second configs group * If the item is not found then the printMissingEntry method is called. If the item is found but the values are * different then the printEntryDiffs method is called. If both values match then nothing happens. After iterating * over the first realms group, the method then iterates over the second realms group, this time only checking to * see if any items are in the second group but not the first. * * @param group1 The config group for the first realm * @param group2 The config groups for the second realm */ public void processGroup(nConfigGroup group1, nConfigGroup group2) { bool foundDiff = false; System.Collections.IEnumerator items1 = group1.getItems(); while (items1.MoveNext()) { nConfigEntry entry1 = (nConfigEntry)items1.Current; try { nConfigEntry entry2 = group2.find(entry1.Name); if (entry2 != null) { if (!(entry2.Value.Equals(entry1.Value))) { if (!foundDiff) { foundDiff = true; printGroupHeader(group1.Name); } printEntryDiffs(entry1, entry2); } } else { if (!foundDiff) { foundDiff = true; printGroupHeader(group1.Name); } printMissingEntry(entry1, 1); } } catch (Exception ) { //TODO Environment.Exit(1); } } System.Collections.IEnumerator items2 = group2.getItems(); while (items2.MoveNext()) { nConfigEntry entry2 = (nConfigEntry)items2.Current; try { nConfigEntry entry1 = group1.find(entry2.Name); if (entry1 == null) { if (!foundDiff) { foundDiff = true; printGroupHeader(group1.Name); } printMissingEntry(entry2, 2); } } catch (Exception ) { //TODO Environment.Exit(1); } } if (foundDiff) { Console.WriteLine("~~~~~~~~~~~~~~~~~~~~\n"); } } /** * This method is called when an entry is not found in one of the realms config groups. * @param entry The entry which was found in one group but not the other * @param realmNum The number of the realm that HAS the entry. */ public void printMissingEntry(nConfigEntry entry, int realmNum) { Console.WriteLine("\t-" + entry.Name); if (realmNum == 1) { Console.WriteLine("\t\t" + Rname1 + "\t=\t" + entry.Value); Console.WriteLine("\t\t" + Rname2 + "\t=\tNo entry found"); } else { Console.WriteLine("\t\t" + Rname1 + "\t=\tNo entry found"); Console.WriteLine("\t\t" + Rname2 + "\t=\t" + entry.Value); } Console.WriteLine(); } /** * prints the group name * @param gName name of the group to print out */ public void printGroupHeader(String gName) { Console.WriteLine("~~~" + gName + "~~~\n"); } /** * This method is called when an entry is present in both groups but the values do not match. * @param entry1 nConfigEntry from the first realm * @param entry2 nConfigEntry from the second realm */ public void printEntryDiffs(nConfigEntry entry1, nConfigEntry entry2) { Console.WriteLine("\t-" + entry1.Name); Console.WriteLine("\t\t" + Rname1 + "\t=\t" + entry1.Value); Console.WriteLine("\t\t" + Rname2 + "\t=\t" + entry2.Value); Console.WriteLine(); } /** * Check the arguments and initialises the global values * @param args program arguments */ public void processArgs(String[] args) { switch (args.Length) { case 0: { usage(); break; } case 1: { usage(); break; } default: { Rname1 = args[0]; Rname2 = args[1]; break; } } } public static void Main(String[] args) { nDiff myself = new nDiff(); myself.processArgs(args); nRealmNode r1 = myself.getRealm(myself.Rname1); nRealmNode r2 = myself.getRealm(myself.Rname2); Console.WriteLine("Realm1 = " + myself.Rname1); Console.WriteLine("Realm2 = " + myself.Rname2 + "\n\n"); myself.configDifferences(r1, r2); try { r1.close(); r2.close(); } catch (Exception ) { } //Close any other sessions within this JVM so that we can exit nSessionFactory.shutdown(); } /** * This is called when the program arguments are invalid. I prints out usage information to help the user * to input the correct arguments. */ public static void usage() { Console.WriteLine("Usage ...\n"); Console.WriteLine("nDiff \n"); Console.WriteLine( "\n[Required Arguments] \n"); Console.WriteLine( " - the RNAME of a the first realm to compare \n"); Console.WriteLine( " - the RNAME of a the second realm to compare \n"); } } }