/* * * 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. * */ package com.pcbsys.nirvana.apps; import com.pcbsys.nirvana.client.*; import java.io.*; import java.util.*; public class dataGroupsManager extends nSampleApp implements nDataGroupListener { private static dataGroupsManager mySelf = null; private Properties propertiesDataGroups; private Properties propertiesDataStreams; private HashMap dataGroupsManager = new HashMap(); private void doit(String[] realmDetails, String propertiesFileGroup, String propertiesFileStreams, boolean recreateGroups) { mySelf.constructSession(realmDetails); //Manages all data groups in a realm try { //Load the propertiesFile propertiesDataGroups = new Properties(); try { propertiesDataGroups.load(new FileInputStream(propertiesFileGroup)); } catch (Exception e) { System.out.println("Error in propertiesDataGroups file loading"); e.printStackTrace(); System.exit(1); } propertiesDataStreams = new Properties(); try { propertiesDataStreams.load(new FileInputStream(propertiesFileStreams)); } catch (Exception e) { System.out.println("Error in propertiesDataStreams file loading"); e.printStackTrace(); System.exit(1); } //Create all the data groups if requested if (recreateGroups) { Enumeration enume = propertiesDataGroups.propertyNames(); Vector arr = new Vector(); while (enume.hasMoreElements()) { String dg = (String) enume.nextElement(); if (!dg.equals("*")) { arr.add(dg); } } String[] arrS = new String[arr.size()]; arr.toArray(arrS); mySession.deleteDataGroup(arrS); Thread.sleep(5000); mySession.createDataGroups(arrS); } //Load up our data groups nDataGroup[] allDataGroup = mySession.getDataGroups(this); for (nDataGroup grp : allDataGroup) { dataGroupsManager.put(grp.getName(), grp); } //Setup all groups as per the propertiesDataGroups file for (nDataGroup grp : allDataGroup) { configureGroup(grp); } //Setup all data streams as per the propertiesDataStream file Iterator itr = mySession.getDefaultDataGroup().getStreams(); while (itr.hasNext()) { nDataStream stream = itr.next(); configureStream(stream); } // Manage groups and streams until the user presses a key System.out.println("Press any key to quit !"); BufferedInputStream bis = new BufferedInputStream(System.in); try { bis.read(); } catch (Exception read) { } // Ignore this } catch (nSecurityException e) { System.out.println("Insufficient permissions for the requested operation."); System.out.println("Please check the ACL settings on the server."); e.printStackTrace(); System.exit(1); } catch (nSessionNotConnectedException snce) { System.out.println("The session object used is not physically connected to the Nirvana realm."); System.out.println("Please ensure the realm is up and check your RNAME value."); snce.printStackTrace(); System.exit(1); } catch (nUnexpectedResponseException ure) { System.out.println("The Nirvana REALM has returned an unexpected response."); System.out.println("Please ensure the Nirvana REALM and client API used are compatible."); ure.printStackTrace(); System.exit(1); } catch (nRequestTimedOutException rtoe) { System.out.println("The requested operation has timed out waiting for a response from the REALM."); System.out.println("If this is a very busy REALM ask your administrator to increase the client timeout values."); rtoe.printStackTrace(); System.exit(1); } catch (nSessionPausedException e) { System.out.println("Session has been paused, please resume the session"); e.printStackTrace(); System.exit(1); } catch (nIllegalArgumentException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (nDataGroupDeletedException e) { e.printStackTrace(); } finally { //Close the session we opened try { nSessionFactory.close(mySession); } catch (Exception ex) { } //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.getName(), grp); } private void addGroupToGroup(String subject, nDataGroup group) { //Check if there is any properties for this group String groupProperties = propertiesDataGroups.getProperty(subject); if (groupProperties != null && groupProperties.length() > 0) { String[] groupPropertiesArray = groupProperties.split(","); for (String groupProperty : groupPropertiesArray) { //Check this is not a wild card of wildcard's 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("*")) { for (nDataGroup addGroup : dataGroupsManager.values()) { try { if (addGroup.getGroup(group.getName()) == null && !addGroup.getName().equals(group.getName())) { group.add(addGroup); } else { System.out.println("Can not add group " + addGroup.getName() + " to " + group.getName()); } } catch (Exception e) { e.printStackTrace(); } } break; } //If its not a wild card then add the given property group to group try { nDataGroup addGroup = dataGroupsManager.get(groupProperty.trim()); if (addGroup != null) { if (!group.getName().equals(addGroup.getName()) && addGroup.getGroup(group.getName()) == null) { group.add(addGroup); } else { System.out.println("Can not add group " + addGroup.getName() + " to " + group.getName()); } } else { System.out.println("Unknown Data Group: " + groupProperty); } } catch (Exception e) { e.printStackTrace(); } } } } /* * 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.getSubject(), stream); //Break the streams subject up so we can check for wildcard's String[] subject = stream.getSubject().split("@"); //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 = propertiesDataStreams.getProperty(subject); if (pds != null) { String[] allArr = pds.split(","); for (String tmp : allArr) { //Check for wildcard adds and if this is the case add this stream to all groups if (tmp.equals("*")) { for (nDataGroup grp : dataGroupsManager.values()) { try { grp.add(stream); } catch (Exception e) { e.printStackTrace(); } } break; } //If its not a wildcard at to all then just add this stream to the property files group nDataGroup grp = dataGroupsManager.get(tmp.trim()); if (grp != null) { try { grp.add(stream); } catch (Exception e) { e.printStackTrace(); } } } } } protected void processArgs(String[] args) { switch (args.length) { case 3: { System.getProperties().put("AutoRecreate", args[2]); } case 2: { System.getProperties().put("PropertiesFileLocationStreams", args[1]); } case 1: { if (args[0].equals("-?")) { Usage(); UsageEnv(); } System.getProperties().put("PropertiesFileLocationGroups", args[0]); break; } default: { Usage(); } } } public static void main(String[] args) { //Create an instance for this class mySelf = new dataGroupsManager(); //Process command line arguments mySelf.processArgs(args); //Process Environment Variables nSampleApp.processEnvironmentVariables(); //Check the propertiesDataGroups file exists String propertiesFileGroups = System.getProperty("PropertiesFileLocationGroups"); String propertiesFileStreams = System.getProperty("PropertiesFileLocationStreams"); String autoRecreateStr = System.getProperty("AutoRecreate"); if (propertiesFileGroups == null || propertiesFileGroups.length() < 1 || propertiesFileStreams == null || propertiesFileStreams.length() < 1 || autoRecreateStr == null || autoRecreateStr.length() < 1) { Usage(); System.exit(1); } //Check the propertiesDataGroups file exists else create one File file = new File(propertiesFileGroups); if (!file.exists()) { try { file.createNewFile(); FileWriter fstream = new FileWriter(file); BufferedWriter out = new BufferedWriter(fstream); out.write("#This is the propertiesDataGroups file for sample dataGroupsManager data group mapping\n"); out.write( "#This adds the data groups mentioned on the right of the equals sign to the data group mentioned on the left\n"); out.write("#dataGroupParent=dataGroupChild,dataGroupChild,dataGroupChild\n"); out.close(); } catch (IOException e) { e.printStackTrace(); } } //Check the propertiesDataGroups file exists else create one File file2 = new File(propertiesFileStreams); if (!file2.exists()) { try { file2.createNewFile(); FileWriter fstream = new FileWriter(file2); BufferedWriter out = new BufferedWriter(fstream); out.write("#This is the propertiesDataGroups file for sample dataGroupsManager data stream mapping\n"); out.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"); out.write("#dataStreamSubject=dataGroupName,dataGroupName,dataGroupName\n"); out.write("*@*=*"); out.close(); } catch (IOException e) { e.printStackTrace(); } } Boolean autoRecreate = Boolean.parseBoolean(autoRecreateStr); //Check the local realm details int idx = 0; String RNAME = null; if (System.getProperty("RNAME") != null) { RNAME = System.getProperty("RNAME"); } else { Usage(); System.exit(1); } //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() { System.out.println("Usage ...\n"); System.out.println("dataGroupsManager \n"); System.out.println(" \n"); System.out.println( " - The location of the property file to use for mapping data groups to data groups"); System.out.println( " - The location of the property file to use for mapping data streams to data groups"); System.out.println( " - 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"); System.out.println("\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 { System.out.println("Added Stream " + stream.getName() + " to group " + group.getName()); } } public void deletedStream(nDataGroup group, nDataStream stream, int count, boolean serverRemoved) { System.out.println( "Deleted Stream " + stream.getName() + " from group " + group.getName() + " server removed " + serverRemoved); } public void createdGroup(nDataGroup group) { //When a group is created configure it if we have not already. if (dataGroupsManager.get(group.getName()) == null) { dataGroupsManager.put(group.getName(), group); configureGroup(group); System.out.println("Created and Configured Group " + group.getName()); } else { System.out.println("Already Had Created Group"); } } public void deletedGroup(nDataGroup group) { //Remove the group as it no longer exists dataGroupsManager.remove(group.getName()); System.out.println("Deleted Group " + group.getName()); } public void addedGroup(nDataGroup to, nDataGroup group, int count) { System.out.println("Added Group " + group.getName() + " from " + to.getName()); } public void removedGroup(nDataGroup from, nDataGroup group, int count) { System.out.println("Removed Group " + group.getName() + " from " + from.getName()); } } //End of dataGroupsManager