import java.util.Map;
import java.util.logging.Logger;
import javax.inject.Inject;
import oracle.dbtools.plugin.api.cmdline.CommandProvider;
import oracle.dbtools.plugin.api.cmdline.annotations.Argument;
import oracle.dbtools.plugin.api.cmdline.annotations.Command;
import oracle.dbtools.plugin.api.cmdline.annotations.Option;
import oracle.dbtools.plugin.api.di.annotations.Provides;
import oracle.dbtools.plugin.api.i18n.annotations.TranslatableText;
/**
* This example plugin demonstrates how to create a command line plugin.
*
*
* - This example command echoes the arguments passed to the console.
* - This example shows how to use the
java.util.logging.Logger
* API.
* - This example shows to define internationalized help documentation for the
* command.
*
*
* Testing the command plugin
*
* - Build this project:
*
*
* $ ant
*
*
*
* - Add the compiled jar to
ords.war
*
*
* $ java -jar ../../../ords.war plugin built/plugin-echo-cmd.jar
*
*
*
* - Invoke the command:
*
*
* $ java -jar ../../../ords.war echo "Hello World"
*
*
* This should produce output like:
*
*
* Jun 17, 2007 10:33:00 AM EchoCommand execute
* INFO: Hello World
*
*
*
* - Review the help for the command:
*
*
* $ java -jar ../../../ords.war help echo
*
* This should produce output like:
*
* java -jar ords.war echo [--case] <text>
*
* Options:
* [--case] Modify the case of the text,
* values: upper|lower|preserve,
* default value: preserve
*
* Arguments:
* <text>* The text to echo to the
* console
*
*
*
*
* @author cdivilly
* @see EchoMessages
*/
@Provides
@Command(name = "echo", description = @TranslatableText(
type = EchoMessages.class, id = EchoMessages.DESCRIPTION),
arguments = { @Argument(name = "text", type = String[].class,
description = @TranslatableText(type = EchoMessages.class,
id = EchoMessages.TEXT)) }, options = @Option(name = "case",
description = @TranslatableText(type = EchoMessages.class,
id = EchoMessages.CASE)))
class EchoCommand implements CommandProvider {
@Inject
EchoCommand(Logger logger) {
this.logger = logger;
}
@Override
public void execute(Map values) throws Exception {
/* Get the value of the --case option */
final TextCase textCase = TextCase.parse((String) values.get("case"));
/* Get the values of the text argument */
final String[] text = (String[]) values.get("text");
/* Build the message */
StringBuilder msg = new StringBuilder();
for (String segment : text) {
msg.append(segment);
msg.append(' ');
}
String fullMsg = msg.toString();
/* Adjust the case of the message */
switch (textCase) {
case UPPER:
fullMsg = fullMsg.toUpperCase();
break;
case LOWER:
fullMsg = fullMsg.toLowerCase();
default:
}
/* Echo the message */
logger.info(fullMsg);
}
private final Logger logger;
/**
* Enum representing the legal values of the --case option
*
* @author cdivilly
*
*/
private enum TextCase {
LOWER, PRESERVE, UPPER;
static TextCase parse(String value) {
TextCase textCase = PRESERVE;
if (value != null) {
try {
textCase = valueOf(value.toUpperCase());
} catch (IllegalArgumentException e) {
}
}
return textCase;
}
};
}