tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli; |
| 2 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame^] | 3 | import org.apache.karaf.shell.commands.Option; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 4 | import org.apache.karaf.shell.console.OsgiCommandSupport; |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 5 | import org.onlab.osgi.DefaultServiceDirectory; |
| 6 | import org.onlab.osgi.ServiceNotFoundException; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 7 | |
| 8 | /** |
| 9 | * Base abstraction of Karaf shell commands. |
| 10 | */ |
| 11 | public abstract class AbstractShellCommand extends OsgiCommandSupport { |
| 12 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame^] | 13 | @Option(name = "-j", aliases = "--json", description = "Output JSON", |
| 14 | required = false, multiValued = false) |
| 15 | private boolean json = false; |
| 16 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 17 | /** |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 18 | * Returns the reference to the implementation of the specified service. |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 19 | * |
| 20 | * @param serviceClass service class |
| 21 | * @param <T> type of service |
| 22 | * @return service implementation |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 23 | * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 24 | */ |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 25 | public static <T> T get(Class<T> serviceClass) { |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 26 | return DefaultServiceDirectory.getService(serviceClass); |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 27 | } |
| 28 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Prints the arguments using the specified format. |
| 31 | * |
| 32 | * @param format format string; see {@link String#format} |
| 33 | * @param args arguments |
| 34 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 35 | public void print(String format, Object... args) { |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 36 | System.out.println(String.format(format, args)); |
| 37 | } |
| 38 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Prints the arguments using the specified format to error stream. |
| 41 | * |
| 42 | * @param format format string; see {@link String#format} |
| 43 | * @param args arguments |
| 44 | */ |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 45 | public void error(String format, Object... args) { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 46 | System.err.println(String.format(format, args)); |
| 47 | } |
| 48 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Executes this command. |
| 51 | */ |
| 52 | protected abstract void execute(); |
| 53 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame^] | 54 | /** |
| 55 | * Indicates whether JSON format should be output. |
| 56 | * |
| 57 | * @return true if JSON is requested |
| 58 | */ |
| 59 | protected boolean outputJson() { |
| 60 | return json; |
| 61 | } |
| 62 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 63 | @Override |
| 64 | protected Object doExecute() throws Exception { |
| 65 | try { |
| 66 | execute(); |
| 67 | } catch (ServiceNotFoundException e) { |
| 68 | error(e.getMessage()); |
| 69 | } |
| 70 | return null; |
| 71 | } |
| 72 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 73 | } |