-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Andyk/sample update with CommonsCLI (#1418)
* standardize on command-line arguments (replace env variables) * Updates based on PR feedbacks * updated with TimeUnit and log based on feedback. * update sample using commonsCli for command line argument processing * update sample using commonsCli for command line argument processing * updates from PR feedback * updates from PR feedback #2 Co-authored-by: Andy Kwong <andykwong@microsoft.com>
- Loading branch information
Showing
3 changed files
with
225 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192 changes: 192 additions & 0 deletions
192
...connection-sample/src/main/java/samples/com/microsoft/azure/sdk/iot/SampleParameters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
package samples.com.microsoft.azure.sdk.iot; | ||
|
||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.CommandLineParser; | ||
import org.apache.commons.cli.DefaultParser; | ||
import org.apache.commons.cli.HelpFormatter; | ||
import org.apache.commons.cli.Option; | ||
import org.apache.commons.cli.Options; | ||
import org.apache.commons.cli.ParseException; | ||
|
||
import java.net.URISyntaxException; | ||
import java.io.*; | ||
|
||
|
||
public class SampleParameters { | ||
|
||
private static final String DEVICE_CONNECTION_STRING = System.getenv("IOTHUB_DEVICE_CONNECTION_STRING"); | ||
private static final String TRANSPORT = "mqtt"; | ||
private static final String NUM_REQUESTS = "3"; | ||
private static final String SLEEP_DURATION_IN_SECONDS = "10"; | ||
private static final String TIMEOUT_IN_MINUTES = "1"; | ||
|
||
private static final String FOOTER = "\nFor more info, please refer to https://github.com/Azure/azure-iot-sdks"; | ||
private static final String APPEXE = "java -jar "; | ||
|
||
private static CommandLine cmd = null; | ||
private static String[] connections = new String[2]; | ||
|
||
/** | ||
* Setup parameters from command line arguments | ||
* @param string array from main() | ||
*/ | ||
public SampleParameters(String[] args) { | ||
|
||
//create options for cli | ||
Options options = new Options() | ||
.addOption( | ||
Option.builder("h") | ||
.longOpt("help") | ||
.hasArg(false) | ||
.desc("Prints this message") | ||
.build() | ||
) | ||
.addOption( | ||
Option.builder("p") | ||
.longOpt("primaryConnectionString") | ||
.hasArg() | ||
.desc("Primary device connection string; required argument unless setup with environment variable \"IOTHUB_DEVICE_CONNECTION_STRING\"") | ||
.build() | ||
) | ||
.addOption( | ||
Option.builder("t") | ||
.longOpt("transportProtocol") | ||
.hasArg() | ||
.desc("Transport protocol [mqtt | https | amqps| amqps_ws | mqtt_ws] (optional); defaults to \"mqtt\"") | ||
.build() | ||
) | ||
.addOption( | ||
Option.builder("r") | ||
.longOpt("requests") | ||
.hasArg() | ||
.desc("Number of requests (optional); defaults to \"3\"") | ||
.build() | ||
) | ||
.addOption( | ||
Option.builder("d") | ||
.longOpt("sleepDuration") | ||
.hasArg() | ||
.desc("Sleep duration between requests in seconds (optional); defaults to \"10\"") | ||
.build() | ||
) | ||
.addOption( | ||
Option.builder("o") | ||
.longOpt("timeout") | ||
.hasArg() | ||
.desc("Timeout for each request in minutes (optional); defaults to \"1\"") | ||
.build() | ||
); | ||
|
||
//Get command line string | ||
String cmdLine = APPEXE; | ||
try { | ||
String jarPath = Options.class | ||
.getProtectionDomain() | ||
.getCodeSource() | ||
.getLocation() | ||
.toURI() | ||
.getPath(); | ||
String jarName = jarPath.substring(jarPath.lastIndexOf("/") + 1); | ||
cmdLine += jarName; | ||
} catch (URISyntaxException e) { | ||
e.printStackTrace(); | ||
cmdLine += "<sample.jar>"; | ||
} | ||
|
||
//Parsing command line | ||
CommandLineParser parser = new DefaultParser(); | ||
HelpFormatter formatter = new HelpFormatter(); | ||
|
||
try { | ||
//parse the command line arguments | ||
cmd = parser.parse(options, args); | ||
|
||
//Help option | ||
if (cmd.hasOption("h")) | ||
{ | ||
formatter.printHelp(cmdLine, "\nHelp:\n\n", options, FOOTER, true); | ||
System.exit(0); | ||
} | ||
|
||
//Connection String, required unless there is environment variable | ||
if (cmd.hasOption("p")) | ||
{ | ||
connections[0] = cmd.getOptionValue("p"); | ||
if (connections[0] == null || connections[0].trim().isEmpty()) | ||
{ | ||
formatter.printHelp(cmdLine, "\nError: Connection is empty\n\n", options, FOOTER, true); | ||
System.exit(0); | ||
} | ||
} | ||
else | ||
{ | ||
connections[0] = DEVICE_CONNECTION_STRING; | ||
if (connections[0] == null || connections[0].trim().isEmpty()) | ||
{ | ||
formatter.printHelp(cmdLine, "\nError: Connection is required as argument or as environment variable\n\n", options, FOOTER, true); | ||
System.exit(0); | ||
} | ||
} | ||
|
||
//Connection String (secondary), optional | ||
if (cmd.hasOption("s")) | ||
{ | ||
connections[1] = cmd.getOptionValue("s"); | ||
} | ||
|
||
} catch (ParseException e) { | ||
//wrong parameters | ||
formatter.printHelp(cmdLine, "\nError: "+e.getMessage()+"\n\n", options, FOOTER, true); | ||
|
||
System.exit(0); | ||
} | ||
} | ||
|
||
/** | ||
* get connection string argument from command line | ||
* @return string array of 2 (primary and secondary) | ||
*/ | ||
public String[] getConnectionStrings() | ||
{ | ||
return connections; | ||
} | ||
|
||
/** | ||
* get transport argument from command line | ||
* @return string value | ||
*/ | ||
public String getTransport() | ||
{ | ||
return cmd.getOptionValue("t", TRANSPORT); | ||
} | ||
|
||
/** | ||
* get number of requests argument from command line | ||
* @return string value | ||
*/ | ||
public String getNumRequests() | ||
{ | ||
return cmd.getOptionValue("r", NUM_REQUESTS); | ||
} | ||
|
||
/** | ||
* get sleep duration between requests argument from command line | ||
* @return string value (seconds) | ||
*/ | ||
public String getSleepDuration() | ||
{ | ||
return cmd.getOptionValue("d", SLEEP_DURATION_IN_SECONDS); | ||
} | ||
|
||
/** | ||
* get timeout argument from command line | ||
* @return string value (minutes) | ||
*/ | ||
public String getTimeout() | ||
{ | ||
return cmd.getOptionValue("o", TIMEOUT_IN_MINUTES); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters