/* * * 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. * */ using System; using com.pcbsys.nirvana.client; using System.Collections.Generic; using System.IO; using System.Threading; namespace com.pcbsys.nirvana.apps { class dataGroupsManager : nSampleApp, nDataGroupListener { private static dataGroupsManager mySelf = null; private Dictionary propertiesDataGroups; private Dictionary propertiesDataStreams; private Dictionary DataGroupsManager = new Dictionary(); private void doit(String[] realmDetails, String propertiesFileGroup, String propertiesFileStreams, bool recreateGroups) { mySelf.constructSession(realmDetails); //Manages all data groups in a realm try { //Load the propertiesFile propertiesDataGroups = readPropertiesFile(propertiesFileGroup); propertiesDataStreams = readPropertiesFile(propertiesFileStreams); //Create all the data groups if requested if (recreateGroups) { Dictionary.KeyCollection propertyNames = propertiesDataGroups.Keys; List arr = new List(); foreach (String dg in propertyNames) { if (!dg.Equals("*")) arr.Add(dg); } String[] arrS = new String[arr.Count]; arrS = arr.ToArray(); try { mySession.deleteDataGroup(arrS); Thread.Sleep(5000); mySession.createDataGroups(arrS); } catch (Exception) { } } //Load up our data groups IEnumerable allDataGroup = mySession.getDataGroups(this); foreach (nDataGroup grp in allDataGroup) { DataGroupsManager.Add(grp.Name, grp); } //Setup all groups as per the propertiesDataGroups file foreach (nDataGroup grp in allDataGroup) { configureGroup(grp); } //Setup all data streams as per the propertiesDataStream file List streams = (List)mySession.getDefaultDataGroup().getStreams(); foreach (nDataStream stream in streams) { configureStream(stream); } // Manage groups and streams until the user presses a key Console.WriteLine("Press any key to quit !"); Console.ReadLine(); } catch (nSecurityException e) { Console.WriteLine("Insufficient permissions for the requested operation."); Console.WriteLine("Please check the ACL settings on the server."); Console.WriteLine(e.StackTrace); Environment.Exit(1); } catch (nSessionNotConnectedException snce) { Console.WriteLine("The session object used is not physically connected to the Nirvana realm."); Console.WriteLine("Please ensure the realm is up and check your RNAME value."); Console.WriteLine(snce.StackTrace); Environment.Exit(1); } catch (nUnexpectedResponseException ure) { Console.WriteLine("The Nirvana REALM has returned an unexpected response."); Console.WriteLine("Please ensure the Nirvana REALM and client API used are compatible."); Console.WriteLine(ure.StackTrace); Environment.Exit(1); } catch (nRequestTimedOutException rtoe) { Console.WriteLine("The requested operation has timed out waiting for a response from the REALM."); Console.WriteLine("If this is a very busy REALM ask your administrator to increase the client timeout values."); Console.WriteLine(rtoe.StackTrace); Environment.Exit(1); } catch (nSessionPausedException e) { Console.WriteLine("Session has been paused, please resume the session"); Console.WriteLine(e.StackTrace); Environment.Exit(1); } catch (ThreadInterruptedException e) { Console.WriteLine(e.StackTrace); } catch (nDataGroupDeletedException e) { Console.WriteLine(e.StackTrace); } finally { //Close the session we opened try { nSessionFactory.close(mySession); } catch (Exception) { } //Close any other sessions within this JVM so that we can exit nSessionFactory.shutdown(); } } /* * Configures the given group based on the props file */ private void configureGroup(nDataGroup grp) { //Check for wildcard adds addGroupToGroup("*", grp); //Check for specific group adds addGroupToGroup(grp.Name, grp); } private void addGroupToGroup(String subject, nDataGroup group) { //Check if there is any properties for this group String groupProperties = ""; propertiesDataGroups.TryGetValue(subject, out groupProperties); if (groupProperties != null && groupProperties.Length > 0) { String[] groupPropertiesArray = groupProperties.Split(new Char[] { ',' }); foreach (String groupProperty in groupPropertiesArray) { //Check this is not a wild card of wildcards as that would have undefined and unwanted behaviour //If its just a wildcard add (group will have all groups under it) then continue if (groupProperty.Equals("*") && !subject.Equals("*")) { foreach (nDataGroup addGroup in DataGroupsManager.Values) { try { if (addGroup.getGroup(group.Name) == null && !addGroup.Name.Equals(group.Name)) { group.add(addGroup); } else { Console.WriteLine("Can not add group " + addGroup.Name + " to " + group.Name); } } catch (Exception e) { Console.WriteLine(e.StackTrace); } } break; } //If its not a wild card then add the given property group to group try { nDataGroup addGroup = null; DataGroupsManager.TryGetValue(groupProperty.Trim(), out addGroup); if (addGroup != null) { if (!group.Name.Equals(addGroup.Name) && addGroup.getGroup(group.Name) == null) { group.add(addGroup); } else { Console.WriteLine("Can not add group " + addGroup.Name + " to " + group.Name); } } else { Console.WriteLine("Unknown Data Group: " + groupProperty); } } catch (Exception e) { Console.WriteLine(e.StackTrace); } } } } /* * Configure the given stream based on the property file */ private void configureStream(nDataStream stream) { //Check for a global add addStreamsToGroups("*@*", stream); //Check for specific add addStreamsToGroups(stream.Subject, stream); //Break the streams subject up so we can check for wildcard's String[] subject = stream.Subject.Split(new Char [] {'@'}); //Check for a username at wildcard host addStreamsToGroups(subject[0] + "@*", stream); //Check for wildcard username at specific host addStreamsToGroups("*@" + subject[1], stream); } private void addStreamsToGroups(String subject, nDataStream stream) { String pds = null; propertiesDataStreams.TryGetValue(subject, out pds); if (pds != null) { String[] allArr = pds.Split(new Char [] {','}); foreach (String tmp in allArr) { //Check for wildcard adds and if this is the case add this stream to all groups if (tmp.Equals("*")) { foreach (nDataGroup grp in DataGroupsManager.Values) { try { grp.add(stream); } catch (Exception e) { Console.WriteLine(e.StackTrace); } } break; } //If its not a wildcard at to all then just add this stream to the property files group nDataGroup group = null; DataGroupsManager.TryGetValue(tmp.Trim(), out group); if (group != null) { try { group.add(stream); } catch (Exception e) { Console.WriteLine(e.StackTrace); } } } } } protected override void processArgs(String[] args) { if (args.Length < 4) { Usage(); UsageEnv(); } } /** * Reads in a series of key value pairs from a properties file */ private Dictionary readPropertiesFile(string fileName) { Dictionary props = new Dictionary(); try { foreach (String line in File.ReadAllLines(fileName)) { if ((!String.IsNullOrEmpty(line)) && (!line.StartsWith(";")) && (!line.StartsWith("#")) && (!line.StartsWith("'")) && (line.Contains("="))) { int index = line.IndexOf('='); String key = line.Substring(0, index).Trim(); String value = line.Substring(index + 1).Trim(); if ((value.StartsWith("\"") && value.EndsWith("\"")) || (value.StartsWith("'") && value.EndsWith("'"))) { value = value.Substring(1, value.Length - 2); } props.Add(key, value); } } } catch (Exception e) { Console.WriteLine("Error in properties file loading"); Console.WriteLine(e.StackTrace); Environment.Exit(1); } return props; } public static void Main(String[] args) { //Create an instance for this class mySelf = new dataGroupsManager(); //Process command line arguments mySelf.processArgs(args); String autoRecreateStr = args[3]; String propertiesFileStreams = args[2]; String propertiesFileGroups = args[1]; String RNAME = args[0]; if (propertiesFileGroups == null || propertiesFileGroups.Length < 1 || propertiesFileStreams == null || propertiesFileStreams.Length < 1 || autoRecreateStr == null || autoRecreateStr.Length < 1 || RNAME == null || RNAME.Length < 1) { Usage(); Environment.Exit(1); } //Process Environment Variables nSampleApp.processEnvironmentVariables(); //Check the propertiesDataGroups file exists else create one if (!File.Exists(propertiesFileGroups)) { try { using (StreamWriter sw = File.CreateText(propertiesFileGroups)) { sw.Write("#This is the propertiesDataGroups file for sample dataGroupsManager data group mapping\n"); sw.Write("#This adds the data groups mentioned on the right of the equals sign to the data group mentioned on the left\n"); sw.Write("#dataGroupParent=dataGroupChild,dataGroupChild,dataGroupChild\n"); sw.Close(); } } catch (Exception e) { Console.WriteLine(e.StackTrace); } } //Check the propertiesDataGroups file exists else create one if (!File.Exists(propertiesFileStreams)) { try { using (StreamWriter sw = File.CreateText(propertiesFileStreams)) { sw.Write("#This is the propertiesDataGroups file for sample dataGroupsManager data stream mapping\n"); sw.Write("#This adds streams based on subject on the left to groups on the right. The default one adds all streams to all groups\n"); sw.Write("#dataStreamSubject=dataGroupName,dataGroupName,dataGroupName\n"); sw.Write("*@*=*"); sw.Close(); } } catch (Exception e) { Console.WriteLine(e.StackTrace); } } bool autoRecreate = Convert.ToBoolean(autoRecreateStr); //Process the local REALM RNAME details String[] rproperties = new String[4]; rproperties = parseRealmProperties(RNAME); //delete the data group specified mySelf.doit(rproperties, propertiesFileGroups, propertiesFileStreams, autoRecreate); } /** * Prints the usage message for this class */ private static void Usage() { Console.WriteLine("Usage ...\n"); Console.WriteLine("dataGroupsManager \n"); Console.WriteLine(" \n"); Console.WriteLine(" - the rname of the server to connect to"); Console.WriteLine(" - The location of the property file to use for mapping data groups to data groups"); Console.WriteLine(" - The location of the property file to use for mapping data streams to data groups"); Console.WriteLine(" - True or False to auto recreate data groups takes the data group property file and creates channels\n a group for every name mentioned on the left of equals sign"); Console.WriteLine("\n\nNote: -? provides help on environment variables \n"); } public void addedStream(nDataGroup group, nDataStream stream, int count) { //If readonly then its the default data group so we want to process it if (group.isReadOnly()) { configureStream(stream); } else { Console.WriteLine("Added Stream " + stream.Name + " to group " + group.Name); } } public void deletedStream(nDataGroup group, nDataStream stream, int count, bool serverRemoved) { Console.WriteLine("Deleted Stream " + stream.Name + " from group " + group.Name + " server removed " + serverRemoved); } public void createdGroup(nDataGroup group) { //When a group is created configure it if we have not already. nDataGroup grp = null; DataGroupsManager.TryGetValue(group.Name, out grp); if (grp == null) { DataGroupsManager.Add(group.Name, group); configureGroup(group); Console.WriteLine("Created and Configured Group " + group.Name); } else { Console.WriteLine("Already Had Created Group"); } } public void deletedGroup(nDataGroup group) { //Remove the group as it no longer exists DataGroupsManager.Remove(group.Name); Console.WriteLine("Deleted Group " + group.Name); } public void addedGroup(nDataGroup to, nDataGroup group, int count) { Console.WriteLine("Added Group " + group.Name + " from " + to.Name); } public void removedGroup(nDataGroup from, nDataGroup group, int count) { Console.WriteLine("Removed Group " + group.Name + " from " + from.Name); } } } //End of dataGroupsManager