tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli; |
| 2 | |
| 3 | import org.apache.karaf.shell.console.OsgiCommandSupport; |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame^] | 4 | import org.onlab.osgi.DefaultServiceDirectory; |
| 5 | import org.onlab.osgi.ServiceNotFoundException; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 6 | |
| 7 | /** |
| 8 | * Base abstraction of Karaf shell commands. |
| 9 | */ |
| 10 | public abstract class AbstractShellCommand extends OsgiCommandSupport { |
| 11 | |
| 12 | /** |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 13 | * Returns the reference to the implementation of the specified service. |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 14 | * |
| 15 | * @param serviceClass service class |
| 16 | * @param <T> type of service |
| 17 | * @return service implementation |
| 18 | */ |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 19 | public static <T> T get(Class<T> serviceClass) { |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame^] | 20 | return DefaultServiceDirectory.getService(serviceClass); |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 21 | } |
| 22 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 23 | /** |
| 24 | * Prints the arguments using the specified format. |
| 25 | * |
| 26 | * @param format format string; see {@link String#format} |
| 27 | * @param args arguments |
| 28 | */ |
| 29 | public static void print(String format, Object... args) { |
| 30 | System.out.println(String.format(format, args)); |
| 31 | } |
| 32 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Prints the arguments using the specified format to error stream. |
| 35 | * |
| 36 | * @param format format string; see {@link String#format} |
| 37 | * @param args arguments |
| 38 | */ |
| 39 | public static void error(String format, Object... args) { |
| 40 | System.err.println(String.format(format, args)); |
| 41 | } |
| 42 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame^] | 43 | /** |
| 44 | * Executes this command. |
| 45 | */ |
| 46 | protected abstract void execute(); |
| 47 | |
| 48 | @Override |
| 49 | protected Object doExecute() throws Exception { |
| 50 | try { |
| 51 | execute(); |
| 52 | } catch (ServiceNotFoundException e) { |
| 53 | error(e.getMessage()); |
| 54 | } |
| 55 | return null; |
| 56 | } |
| 57 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 58 | } |