/* Copyright 1999-2011 (c) My-Channels Copyright (c) 2012-2015 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.nAdminAPI.apps; import com.pcbsys.foundation.utils.fEnvironment; import com.pcbsys.nirvana.client.*; import com.pcbsys.nirvana.nAdminAPI.*; import java.util.Enumeration; /** * This application can be used to copy channels from one realm to another. * */ public class nCopyChannel { /** * Private variables used in this application */ private String fromRealm = null; private String toRealm = null; private nSessionAttributes attr = null; private String fromChannelName = null; private String toChannelName = null; private nLeafNode fromChan = null; private nRealmNode from = null; private nRealmNode to = null; private String copyArgs[] = null; private boolean localCopy = true; private int ttl = -10; private int capacity = -10; private int type = -10; /** * Consruct an instance of this class using the command line arguments passed * when it is executed. */ public nCopyChannel(String args[]) { try { copyArgs = args; // set the parameters required for this operation processArgs(copyArgs); System.out.println( "Connecting to " + fromRealm ); // construct the session attributes from the realm attr = new nSessionAttributes( fromRealm ); // get the root realm node from the realm admin from = new nRealmNode(attr); if(!from.isAuthorised()){ System.out.println("User not authorised on this node "+fromRealm); return; } // wait for the entire node namespace to be constructed if // the operation is recursive System.out.print( "waiting for namepsace construction..... " ); from.waitForEntireNameSpace(); System.out.println( "finished" ); // call the method to search from the root realm node searchNode(from); // close the realm node which will close all connections to child nodes if (!localCopy) { if (to != null) { if (fromChan != null) { try { nChannelAttributes newAttr = new nChannelAttributes(); fromChan.copy(to, true, checkAttributes(fromChan.getAttributes(), newAttr), null); } catch (nBaseAdminException e) { e.printStackTrace(); } } else { System.out.println("Could not find channel to copy : "+fromChannelName); } } else { System.out.println("Could not find realm to copy to : "+toRealm); } } from.close(); } catch (Exception e) { e.printStackTrace(); } } /** * Set new channel attributes for the channel copy */ private nChannelAttributes checkAttributes(nChannelAttributes p_src, nChannelAttributes p_dest) { try { if (ttl != -10) { p_dest.setTTL(ttl); } else { p_dest.setTTL(p_src.getTTL()); } if (capacity != -10) { p_dest.setMaxEvents(capacity); } else { p_dest.setMaxEvents(p_src.getMaxEvents()); } if (type != -10) { p_dest.setType(type); } else { p_dest.setType(p_src.getType()); } p_dest.setName(toChannelName); if (p_src.isJMSEngine()) { p_dest.useJMSEngine(p_src.isJMSEngine()); p_dest.useMergeEngine(false); } else if (p_src.isMergeEngine()) { p_dest.useJMSEngine(false); p_dest.useMergeEngine(p_src.isMergeEngine()); } //Publish keys we need to copy only in case of channel if (p_src.getPublishKeys() != null) { p_dest.setPublishKeys(p_src.getPublishKeys()); } //Copy Store properties p_dest.getProperties().copy(p_src.getProperties()); } catch (nBaseClientException e) { e.printStackTrace(); } return p_dest; } /** * search the enumeration of child nodes for other realms and channels */ private void searchNodes( nRealmNode p_node, Enumeration enum1) { while ( enum1.hasMoreElements() ) { Object obj = enum1.nextElement(); if ( obj instanceof nLeafNode ) { nLeafNode leaf = (nLeafNode)obj; String fullyQualifiedName = leaf.getAbsolutePath(); if (leaf.isChannel()) { System.out.println("Found channel "+fullyQualifiedName); } else { System.out.println("Found queue "+fullyQualifiedName); } if ((leaf.isChannel()) && (fullyQualifiedName.equals(fromChannelName))) { // we have found a channel that matches the name specified, so copy the channel if (localCopy) { try { nChannelAttributes newAttr = new nChannelAttributes(); leaf.copy(true, checkAttributes(leaf.getAttributes(), newAttr), leaf.getACLs(), null); } catch (nBaseAdminException e) { e.printStackTrace(); } } else { fromChan = leaf; } } } else if ( obj instanceof nRealmNode ) { // we have found another realm node, so search this node for channels if (!localCopy) { nRealmNode realm = (nRealmNode)obj; if (realm.getName().equalsIgnoreCase(toRealm)) { to = realm; } } } else if ( obj instanceof nContainer ) { nContainer cont = (nContainer)obj; searchNodes(p_node, cont.getNodes()); } } } /** * Search the children of the realm passed as a paremeter */ private void searchNode( nRealmNode p_node ) { try { System.out.println("Found node "+p_node.getName()); searchNodes( p_node, p_node.getNodes() ); } catch ( Exception ex ) { ex.printStackTrace(); } } /** * Set the program variables and flags based on command line args */ public void getOptions(String args[]) { fromRealm = System.getProperty("RNAME", null); if (fromRealm==null) { Usage(); System.exit(1); } fromChannelName = System.getProperty("FROMCHANNEL", null); if (fromChannelName==null) { Usage(); System.exit(1); } // set the channel name to lower case as the nirvana namespace is all lowercase for (int i = 0; i < args.length; i++) { if (args[i].equalsIgnoreCase("-r")) { try { i++; toRealm = args[i]; } catch (Exception e) { System.out.println("Invalid parameter passed for toRealm"); } } else if (args[i].equalsIgnoreCase("-n")) { try { i++; toChannelName = args[i]; } catch (Exception e) { System.out.println("Invalid parameter passed for channel name for copied channel"); } } else if (args[i].equalsIgnoreCase("-a")) { try { i++; ttl = Integer.parseInt(args[i]); } catch (Exception e) { System.out.println("Invalid parameter passed for channel age"); } } else if (args[i].equalsIgnoreCase("-c")) { try { i++; capacity = Integer.parseInt(args[i]); } catch (Exception e) { System.out.println("Invalid parameter passed for channel capacity"); } } else if (args[i].equalsIgnoreCase("-t")) { try { i++; String sType = args[i]; if (sType.equals("P")) { type = nChannelAttributes.PERSISTENT_TYPE; } else if (sType.equals("R")) { type = nChannelAttributes.RELIABLE_TYPE; } else if (sType.equals("M")) { type = nChannelAttributes.MIXED_TYPE; } else if (sType.equals("S")) { type = nChannelAttributes.SIMPLE_TYPE; } else if (sType.equals("T")) { type = nChannelAttributes.TRANSIENT_TYPE; } } catch (Exception e) { System.out.println("Invalid parameter passed for channel type"); } } } if (toRealm != null) { localCopy = false; } if (toChannelName == null) { if (fromChannelName.startsWith("/")) { toChannelName = "copyof"+fromChannelName.substring(1); } else { toChannelName = "copyof"+fromChannelName; } } } private void processArgs(String[] args){ if (args.length == 0) { Usage(); System.exit(1); } switch (args.length){ case 1: if (args[0].equals("-?")) { UsageEnv(); getOptions(null); break; } else { System.setProperty("FROMCHANNEL",args[0]); getOptions(args); break; } default: System.setProperty("FROMCHANNEL",args[0]); 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 * FROMCHANNEL * * as system properties, and pass in a list of permissions in the constructor * */ 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(); nCopyChannel copy = new nCopyChannel( args ); } /** * Prints the usage message for this class */ private static void Usage() { System.out.println( "Usage ...\n" ); System.out.println("nadmincopychan [-r toRealm] [-n toChannelName] [-a channel ttl] [-c channel capacity] [-t channel type]\n"); System.out.println( " \n"); System.out.println( " - Channel name parameter for the channel to copy" ); System.out.println( "\n[Optional Arguments] \n"); System.out.println( "<-r toRealm> - The RNAME of a remote realm to copy the channel to" ); System.out.println( "<-n toChannelName> - The name you wish to give the copied channel" ); System.out.println( "<-a channel ttl> - The ttl you wish to give the copied channel" ); System.out.println( "<-c channel capacity> - The capacity you wish to give the copied channel" ); System.out.println( "<-t channel type> - The channel you wish the copied channel to be any of (P | R | M | S | T)" ); 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); } }