Nirvana Java Client: Delete a Realm ACL Entry

This example demonstrates how to delete an ACL entry from a realm on a Nirvana Channel.

Usage

ndelrealmacl <user> <host> [-r] 

<Required Arguments> 

<user> - User name parameter to delete the realm ACL entry from
<host> - Host name parameter to delete the realm ACL entry from

[Optional Arguments] 

[-r] - Specifies whether recursive traversal of the namespace should be done


Note: -? provides help on environment variables

Application Source Code

/**
 *
 *  ----------------------------------------------------------------------------------
 *
 *  PCB Systems Limited License Version 1.1
 *  Copyright  PCB Systems Limited. All rights reserved
 *
 *  In the event that you should download or otherwise use this software
 *  ( the "Software" ) you hereby acknowledge and agree that:
 *
 *  1. The Software is the property of PCB Systems Limited: Title, Copyright and all
 *  other proprietary rights, interest and benefit in and to the Software is and
 *  shall be owned by PCB Systems Limited;
 *
 *  2. You will not make copies of the Software whatsoever other than, if you should
 *  so wish, a single copy for archival purposes only;
 *
 *  3. You will not modify, reverse assemble, decompile, reverse engineer or otherwise
 *  translate the Software;
 *
 *  4. You will not redistribute, copy, forward electronically or circulate the Software
 *  to any person for any purpose whatsoever without the prior written consent of
 *  PCB Systems Limited;
 *
 *  5. You will not charge for, market or provide any managed service or product that
 *  is based upon or includes the Software or any variant of it; and
 *
 *  6. You will not use the Software for any purpose apart from your own personal,
 *  noncommercial and lawful use;
 *
 *  You hereby agree that the software is used by you on an "as is" basis, without
 *  warranty of any kind. PCB Systems Limited hereby expressly disclaim all warranties
 *  and conditions, either expressed or implied, including but not limited to any
 *  implied warranties or conditions or merchantability and fitness for a particular
 *  purpose.
 *
 *  You agree that you are solely responsible for determining the appropriateness of
 *  using the Software and assume all risks associated with it including but not
 *  limited to the risks of program errors, damage to or loss of of data, programs or
 *  equipment and unavailability or interruption of operations.
 *
 *  PCB Systems Limited will not be liable for any direct damages or for any, special,
 *  incidental or indirect damages or for any economic consequential damages ( including
 *  lost profits or savings ), or any damage howsoever arising.
 *
 *  ----------------------------------------------------------------------------------
 */
 package com.pcbsys.nirvana.nAdminAPI.apps;
import com.pcbsys.foundation.utils.fEnvironment;
import com.pcbsys.nirvana.nAdminAPI.*;
import com.pcbsys.nirvana.client.*;
import java.util.*;
/**
 * This application can be used to remove a subject from a realm
 *
 * You can also specify to remove an acl entry from all known realms within a namespace, by
 * recursively searching through looking for other realm nodes and removing the acl entry
 *
 * This is achieved by specifying -r as a command line parameter.
 */
public class nDelRealmAclEntry {
  /**
   * Private variables used in this application
   */
  private String realm = null;
  private String name = null;
  private String host = null;
  private boolean recursive = false;
  private nSessionAttributes attr = null;
  private nRealmNode node = null;
  /**
   * Construct and instance of this class using the command line arguments passed
   * when it is executed.
   */
  public nDelRealmAclEntry(String args[]) {
    try {
      // set the parameters required for this operation
      processArgs(args);
      System.out.println( "Connecting to " + realm );
      // construct the session attributes from the realm
      attr = new nSessionAttributes( realm );
      // get the root realm node from the realm admin
      node = new nRealmNode(attr);
      if(!node.isAuthorised()){
        System.out.println("User not authorised on this node "+realm);
        return;
      }
      
      // wait for the entire node namespace to be constructed if
      // the operation is recursive
        node.waitForEntireNameSpace();
      System.out.println( "Removing entry for " + name + "@" + host );
      // remove the entry from the acl
      node.removeRealmACLEntry(new nRealmACLEntry(name+"@"+host));
      System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
      System.out.println( "Removed ACL entry for "+node.getName() );
      System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
      dump();
      // if you specify -r as a command line parameter, you can choose to traverse the entire
      // realm namespace, including any realms that have been added to the root realm node
      if (recursive) {
        traverse(node);
      }
      node.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * recursively search through the nodes from a realm node looking for other realm nodes
   */
  public nRealmNode traverse(nRealmNode p_node) {
    // get the enumeration of child nodes from p_node
    Enumeration enum1 = p_node.getNodes();
    while ( enum1.hasMoreElements() ) {
      Object obj = enum1.nextElement();
      // only deal with realm nodes
      if ( obj instanceof nRealmNode ) {
        nRealmNode node = (nRealmNode)obj;
        try {
          node.removeRealmACLEntry(new nRealmACLEntry(name+"@"+host));
        } catch (Exception e) {
          e.printStackTrace();
        }
        System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
        System.out.println( "Removed ACL entry for "+node.getName() );
        System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
        dump();
        // now traverse the child nodes
        return traverse(node);
      } else if ( obj instanceof nContainer ) {
        nContainer cont = (nContainer)obj;
        searchNode(cont.getNodes());
      }
    }
    return null;
  }
  /**
   * Search the enumeration of nodes passed as a parameter
   */
  private void searchNode( Enumeration p_nodes ) {
    try {
      while (p_nodes.hasMoreElements()) {
        Object obj = p_nodes.nextElement();
        if (obj instanceof nRealmNode) {
          nRealmNode node = (nRealmNode)obj;
          try {
            // get the acl for this realm node
            nACL acl = node.getACLs();
            // remove the entry from the acl
            acl.remove(name +"@"+host);
            // set the node acl to the acl list with the removed entry
            node.setACLs(acl);
          } catch (Exception e) {
            e.printStackTrace();
          }
          System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
          System.out.println( "Removed ACL entry for "+node.getName() );
          System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
          dump();
        } else if (obj instanceof nContainer) {
          searchNode(((nContainer)obj).getNodes());
        }
      }
    }
    catch ( Exception ex ) {
      ex.printStackTrace();
    }
  }
  /**
   * If you construct an instance of this class from another class, you can set the name
   * and host for the subject to remove.
   */
  public void setSubject(String p_name, String p_host) {
    name = p_name;
    host = p_host;
  }
  /**
   * Output to system.out the permissions that have been set
   */
  public void dump() {
    System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
    System.out.println( "NAME                   : "+name );
    System.out.println( "HOST                   : "+host );
    System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
  }
  /**
   * Set the program variables and flags based on command line args
   */
  public void getOptions(String args[]) {
    realm = System.getProperty("RNAME", null);
    if (realm==null) {
      Usage();
      System.exit(1);
    }
    name = System.getProperty("NAME", null);
    if (name==null) {
      Usage();
      System.exit(1);
    }
    host = System.getProperty("HOST", null);
    if (host==null) {
      Usage();
      System.exit(1);
    }
    for (int i = 0; i < args.length; i++) {
      if (args[i].equalsIgnoreCase("-r")) {
        recursive = true;
      }
    }
  }
  private void processArgs(String[] args){
    if (args.length == 0) {
      Usage();
      System.exit(1);
    }
    switch (args.length){
      case 1:
        if (args[0].equals("-?")) UsageEnv();
        System.setProperty("NAME",args[0]);
        getOptions(null);
        break;
      default:
        System.setProperty("NAME",args[0]);
        System.setProperty("HOST",args[1]);
        getOptions(args);
    }
  }
  private static void processEnvironmentVariable(String variable){
    String laxVAR=System.getProperty("lax.nl.env."+variable);
    if (laxVAR!=null) System.setProperty(variable,laxVAR);
  }
  /**
   * Run this as a command line program passing the command line args.
   *
   * Or construct one of these classes from another class ensuring you have added :
   *
   *    RNAME
   *    NAME
   *    HOST
   *
   * as system properties
   *
   */
  public static void main( String[] args ) {
    //Process Environment Variables
    processEnvironmentVariable("RNAME");
    processEnvironmentVariable("LOGLEVEL");
    processEnvironmentVariable("HPROXY");
    processEnvironmentVariable("HAUTH");
    processEnvironmentVariable("CKEYSTORE");
    processEnvironmentVariable("CKEYSTOREPASSWD");
    processEnvironmentVariable("CAKEYSTORE");
    processEnvironmentVariable("CAKEYSTOREPASSWD");
    // Install any proxy server settings
    fEnvironment.setProxyEnvironments();
    // Install JSSE SSL Environement settings
    fEnvironment.setSSLEnvironments();
    nDelRealmAclEntry setAcl = new nDelRealmAclEntry( args );
    System.exit(0);
  }
  /**
   * Prints the usage message for this class
   */
  private static void Usage() {
    System.out.println( "Usage ...\n" );
    System.out.println("ndelrealmacl <user> <host> [-r] \n");
    System.out.println(
      "<Required Arguments> \n");
    System.out.println(
      "<user> - User name parameter to delete the realm ACL entry from" );
    System.out.println(
      "<host> - Host name parameter to delete the realm ACL entry from" );
    System.out.println(
      "\n[Optional Arguments] \n");
    System.out.println(
      "[-r] - Specifies whether recursive traversal of the namespace should be done" );
    System.out.println(
      "\n\nNote: -? provides help on environment variables \n");
  }
  private static void UsageEnv() {
    System.out.println(
      "\n\n(Environment Variables) \n");
    System.out.println(
      "(RNAME) - One or more RNAME entries in the form protocol://host:port" );
    System.out.println(
      "   protocol - Can be one of nsp, nhp, nsps, or nhps, where:" );
    System.out.println(
      "   nsp - Specifies Nirvana Socket Protocol (nsp)" );
    System.out.println(
      "   nhp - Specifies Nirvana HTTP Protocol (nhp)" );
    System.out.println(
      "   nsps - Specifies Nirvana Socket Protocol Secure (nsps), i.e. using SSL/TLS" );
    System.out.println(
      "   nhps - Specifies Nirvana HTTP Protocol Secure (nhps), i.e. using SSL/TLS" );
    System.out.println(
      "   port - The port number of the server" );
    System.out.println(
      "\nHint: - For multiple RNAME entries, use comma separated values which will be attempted in connection weight order\n" );
    System.out.println(
      "(LOGLEVEL) - This determines how much information the nirvana api will output 0 = verbose 7 = quiet\n" );
    System.out.println(
      "(CKEYSTORE) - If using SSL, the location of the keystore containing the client cert\n");
    System.out.println(
      "(CKEYSTOREPASSWD) - If using SSL, the password for the keystore containing the client cert\n");
    System.out.println(
      "(CAKEYSTORE) - If using SSL, the location of the ca truststore\n");
    System.out.println(
      "(CAKEYSTOREPASSWD) - If using SSL, the password for the ca truststore\n");
    System.out.println(
      "(HPROXY) - HTTP Proxy details in the form proxyhost:proxyport, where:" );
    System.out.println(
      "   proxyhost - The HTTP proxy host" );
    System.out.println(
      "   proxyport - The HTTP proxy port\n" );
    System.out.println(
      "(HAUTH) - HTTP Proxy authentication details in the form user:pass, where:" );
    System.out.println(
      "   user - The HTTP proxy authentication username" );
    System.out.println(
      "   pass - The HTTP proxy authentication password\n" );
    System.exit(1);
  }

}
EXAMPLE_SOURCE_END
Share this page with others:
Tell Your Tweets Facebook It! Add to Delicious Reddit! Digg It! Stumble Upon Add to Your Faves Mixx it
Follow Us:
Keep up with my-Channels on Twitter Become a fan on Facebook LinkedIn Profile Recent Highlights RSS Feed