/* 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.nAdminAPI.apps; import com.pcbsys.nirvana.nAdminAPI.*; import com.pcbsys.nirvana.client.*; import java.io.*; /** * Reads in a file containing a schedule and adds this schedule to the realm */ public class nAddScheduler { private static nAddScheduler myself = null; //default values for subject and clusterwide private String subject = System.getProperty("user.name"); private boolean clusterwide = false; /** * This function creates a session based on RNAME and establishes the nRealmNode for that realm * It returns the nScheduleManager for the realm specified by RNAME * * @param RNAME The realm identifier including protocol e.g. nsp://localhost:9000 * @return The nScheduleManager for the realm specified by RNAME */ public nSchedulerManager getSchedulerMan(String RNAME) { try { System.out.print("Establishing realm node..."); nSessionAttributes nsa = new nSessionAttributes(RNAME); nRealmNode rNode = new nRealmNode(nsa); if (!rNode.isAuthorised()) { System.out.println("User not authorised on this node " + nsa); System.exit(1); } System.out.println("done"); return rNode.getSchedulerManager(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } return null; } /** * This method reads the file at location specified by source into a String so that it can be passed * as a parameter to the add nScheduleManager.add method * * @param source The location of the file containing the schedule * @return The String read from the source file */ private String readFile(String source) { StringBuilder schedule = new StringBuilder(); String buffer; System.out.print("Reading from source file..."); BufferedReader in=null; try { in = new BufferedReader(new FileReader(source)); //read file into a string while ((buffer = in.readLine()) != null) { schedule.append(buffer).append('\n'); } in.close(); System.out.println("done"); } catch (IOException e) { System.out.println("An error occured while trying to read from the schedule file. Please" + "check the file location specified.\n\n"); e.printStackTrace(); System.exit(1); } finally { if (in!=null){ try { in.close(); } catch (IOException e) { //explicit ignore } in=null; } } return schedule.toString(); } /** * Takes the program parameters and adds them to the system properties. * * @param args The program arguments */ private void processArgs(String[] args) { //If the user has not specified any parameters then the usage function is called if (args.length == 0) { usage(); System.exit(1); } //If the user specifies -? as the first parameter then the environment variable information is printed if (args[0].equals("-?")) { usageEnv(); System.exit(1); } switch (args.length) { default: { usage(); break; } case 1: System.getProperties().put("source", args[0]); break; case 2: System.getProperties().put("source", args[0]); System.getProperties().put("subject", args[1]); break; case 3: System.getProperties().put("source", args[0]); System.getProperties().put("subject", args[1]); System.getProperties().put("clusterwide", args[2]); } } public static void main(String[] args) { myself = new nAddScheduler(); myself.processArgs(args); /** *The RNAME variable is required as an environment variable, if the RNAME is not specifed then the *program cannot proceed. In this situation the usageEnv function is called and the system exits. */ String RNAME = null; if (System.getProperty("RNAME") != null) { RNAME = System.getProperty("RNAME"); } else { usageEnv(); System.exit(1); } //source is a required parameter so if it is not present, the usage function is called and the system exits String source = null; if (System.getProperty("source") != null) { source = System.getProperty("source"); } else { usage(); System.exit(1); } if (System.getProperty("subject") != null) { myself.subject = System.getProperty("subject"); } if (System.getProperty("clusterwide") != null) { if (System.getProperty("clusterwide").equalsIgnoreCase("true")) { myself.clusterwide = true; } } String schedule = myself.readFile(source); nSchedulerManager schedulerMan = myself.getSchedulerMan(RNAME); try { System.out.print("Adding schedule to realm..."); schedulerMan.add(schedule, myself.subject, myself.clusterwide); System.out.println("done"); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } /** * This method will be called if the user does not specify the correct program parameters. It prints information * to the output regarding the correct usage of the program parameters. */ private static void usage() { System.out.println("Usage ...\n"); System.out.println("naddschedule [subject] [clusterwide] \n"); System.out.println( " \n"); System.out.println( " - location of the schedule script file"); System.out.println( "\n[Optional Arguments] \n"); System.out.println( "[subject] - The subject of the schedule (default : os username)"); System.out.println( "[clusterwide] - Whether or not the schedule is cluster wide (default : false)"); System.out.println( "\n\nNote: -? provides help on environment variables \n"); } /** * If the user does not specify the correct environment variables then this method will be called to print * the correct usage of the environment variables */ 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); } }