/* Copyright 1999-2011 (c) My-Channels Copyright (c) 2012-2014 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.*; /** * Creates a nirvana channel */ public class makeChannel extends nSampleApp { private static makeChannel mySelf = null; /** * This method demonstrates the Nirvana API calls necessary to create a * channel. It is called after all command line arguments have been received * and validated * * @param realmDetails * a String[] containing the possible RNAME values * @param achannelName * the channel name to create */ private void doit(String[] realmDetails, String achannelName) { mySelf.constructSession(realmDetails); long initialEid = 0; // Check if the optional starteid is specified if (System.getProperty("STARTEID") != null) { try { Integer i = new Integer(System.getProperty("STARTEID")); initialEid = i.intValue(); } catch (Exception num) { initialEid = 0; } } boolean clusterWide = false; // Check if the channel is to be created across a cluster if (System.getProperty("CLUSTERWIDE") != null) { try { Boolean b = new Boolean(System.getProperty("CLUSTERWIDE")); clusterWide = b.booleanValue(); } catch (Exception ex) { } } // Creates the specified channel try { // Create a channel attributes object nChannelAttributes nca = createChannelAttributes(achannelName); nca.setClusterWide(clusterWide); // Create the channel nChannel myChannel = mySession.createChannel(nca, initialEid); } // Handle errors catch (nChannelNotFoundException cnfe) { System.out.println("The channel specified could not be found."); System.out .println("Please ensure that the channel exists in the REALM you connect to."); cnfe.printStackTrace(); System.exit(1); } catch (nSecurityException se) { System.out .println("Insufficient permissions for the requested operation."); System.out.println("Please check the ACL settings on the server."); se.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 (nUnknownRemoteRealmException urre) { System.out .println("The channel specified resided in a remote realm which could not be found."); System.out .println("Please ensure the channel name specified is correct."); urre.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 (nChannelAlreadyExistsException caee) { System.out .println("The channel specified already exists on the REALM."); caee.printStackTrace(); System.exit(1); } catch (nBaseClientException nbce) { System.out .println("An error occured while creating the Channel Attributes object."); nbce.printStackTrace(); System.exit(1); } // 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(); } protected void processArgs(String[] args) { switch (args.length) { case 6: System.getProperties().put("STARTEID", args[5]); case 5: System.getProperties().put("CLUSTERWIDE", args[4]); case 4: System.getProperties().put("TYPE", args[3]); case 3: System.getProperties().put("CAPACITY", args[2]); case 2: System.getProperties().put("TTL", args[1]); case 1: if (args[0].equals("-?")) { Usage(); UsageEnv(); } System.getProperties().put("CHANNAME", args[0]); break; } } public static void main(String[] args) { // Create an instance for this class mySelf = new makeChannel(); // Process command line arguments mySelf.processArgs(args); // Process Environment Variables nSampleApp.processEnvironmentVariables(); // Check the channel name specified String channelName = null; if (System.getProperty("CHANNAME") != null) channelName = System.getProperty("CHANNAME"); else { Usage(); System.exit(1); } // Check the local realm details 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); // create the channel specified mySelf.doit(rproperties, channelName); } private nChannelAttributes createChannelAttributes(String achannelName) throws nBaseClientException { // Create a channel attributes object nChannelAttributes nca = new nChannelAttributes(); // Set the channel name nca.setName(achannelName); // Set the channel type parameter (reliable or guaranteed) if (System.getProperty("TYPE") != null) { if (System.getProperty("TYPE").equalsIgnoreCase("R")) { nca.setType(nChannelAttributes.RELIABLE_TYPE); } else if (System.getProperty("TYPE").equalsIgnoreCase("P")) { nca.setType(nChannelAttributes.PERSISTENT_TYPE); } else if (System.getProperty("TYPE").equalsIgnoreCase("M")) { nca.setType(nChannelAttributes.MIXED_TYPE); } else if (System.getProperty("TYPE").equalsIgnoreCase("S")) { nca.setType(nChannelAttributes.SIMPLE_TYPE); } else if (System.getProperty("TYPE").equalsIgnoreCase("T")) { nca.setType(nChannelAttributes.TRANSIENT_TYPE); } } else { nca.setType(nChannelAttributes.SIMPLE_TYPE); } // Set the channel capacity parameter if (System.getProperty("CAPACITY") != null) { Integer i = new Integer(System.getProperty("CAPACITY")); nca.setMaxEvents(i.intValue()); } // Set the channel Time to Live parameter if (System.getProperty("TTL") != null) { Integer i = new Integer(System.getProperty("TTL")); nca.setTTL(i.intValue()); } return nca; } /** * Prints the usage message for this class */ private static void Usage() { System.out.println("Usage ...\n"); System.out .println("nmakechan [time to live] [capacity] [type] [cluster wide] [start eid] \n"); System.out.println(" \n"); System.out .println(" - Channel name parameter for the channel to be created"); System.out.println("\n[Optional Arguments] \n"); System.out .println("[time to live] - The Time To Live parameter for the new channel (default: 0)"); System.out .println("[capacity] - The Capacity parameter for the new channel (default: 0)"); System.out .println("[type] - The type parameter for the new channel (default: S)"); System.out .println(" R - For a reliable (stored in memory) channel with persistent eids"); System.out.println(" P - For a persistent (stored on disk) channel"); System.out .println(" S - For a simple (stored in memory) channel with non-persistent eids"); System.out.println(" T - For a transient (no server based storage)"); System.out .println(" M - For a Mixed (allows both memory and persistent events) channel\n"); System.out .println("[cluster wide] - Whether the channel is cluster wide. Will only work if the realm is part of a cluster"); System.out .println("[start eid] - The initial start event id for the new channel (default: 0)"); System.out .println("\n\nNote: -? provides help on environment variables \n"); } } // End of makeChannel Class