1 /* 2 * Copyright (C) 2009 Christian Schulte <cs@schulte.it> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * o Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * o Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 19 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $JOMC: Command.java 5215 2016-04-24 06:54:04Z schulte $ 29 * 30 */ 31 package org.jomc.cli; 32 33 import java.util.List; 34 import java.util.Locale; 35 import java.util.logging.Level; 36 import org.apache.commons.cli.CommandLine; 37 import org.apache.commons.cli.Options; 38 39 /** 40 * Command. 41 * 42 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 43 */ 44 public interface Command 45 { 46 47 /** 48 * Listener interface. 49 */ 50 public interface Listener 51 { 52 53 /** 54 * Gets called on logging. 55 * 56 * @param level The level of the event. 57 * @param message The message of the event or {@code null}. 58 * @param t The throwable of the event or {@code null}. 59 * 60 * @throws NullPointerException if {@code level} is {@code null}. 61 */ 62 void onLog( Level level, String message, Throwable t ); 63 64 } 65 66 /** 67 * Status code when the command completed successfully. 68 */ 69 int STATUS_SUCCESS = 0; 70 71 /** 72 * Status code when the command failed. 73 */ 74 int STATUS_FAILURE = 1; 75 76 /** 77 * Gets the list of registered listeners. 78 * 79 * @return The list of registered listeners. 80 */ 81 List<Listener> getListeners(); 82 83 /** 84 * Gets the log level of the instance. 85 * 86 * @return The log level of the instance. 87 * 88 * @see #setLogLevel(java.util.logging.Level) 89 */ 90 Level getLogLevel(); 91 92 /** 93 * Sets the log level of the instance. 94 * 95 * @param value The new log level of the instance or {@code null}. 96 * 97 * @see #getLogLevel() 98 */ 99 void setLogLevel( Level value ); 100 101 /** 102 * Gets the name of the command. 103 * 104 * @return The name of the command. 105 */ 106 String getName(); 107 108 /** 109 * Gets the abbreviated name of the command. 110 * 111 * @return The abbreviated name of the command. 112 */ 113 String getAbbreviatedName(); 114 115 /** 116 * Gets the short description of the command. 117 * 118 * @param locale The locale of the short description to return. 119 * 120 * @return The short description of the command. 121 * 122 * @throws NullPointerException if {@code locale} is {@code null}. 123 */ 124 String getShortDescription( Locale locale ) throws NullPointerException; 125 126 /** 127 * Gets the long description of the command. 128 * 129 * @param locale The locale of the long description to return. 130 * 131 * @return The long description of the command. 132 * 133 * @throws NullPointerException if {@code locale} is {@code null}. 134 */ 135 String getLongDescription( Locale locale ) throws NullPointerException; 136 137 /** 138 * Gets the options of the command. 139 * 140 * @return The options of the command. 141 */ 142 Options getOptions(); 143 144 /** 145 * Executes the command. 146 * 147 * @param commandLine Command line to execute. 148 * 149 * @return The status code to report. 150 * 151 * @throws NullPointerException if {@code commandLine} is {@code null}. 152 * 153 * @see #STATUS_SUCCESS 154 * @see #STATUS_FAILURE 155 */ 156 int execute( CommandLine commandLine ) throws NullPointerException; 157 158 }