use of org.apache.karaf.shell.support.converter.GenericType in project karaf by apache.
the class DefaultActionPreparator method prepare.
public boolean prepare(Action action, Session session, List<Object> params) throws Exception {
Command command = action.getClass().getAnnotation(Command.class);
Map<Option, Field> options = new HashMap<>();
Map<Argument, Field> arguments = new HashMap<>();
List<Argument> orderedArguments = new ArrayList<>();
for (Class<?> type = action.getClass(); type != null; type = type.getSuperclass()) {
for (Field field : type.getDeclaredFields()) {
Option option = field.getAnnotation(Option.class);
if (option != null) {
options.put(option, field);
}
Argument argument = field.getAnnotation(Argument.class);
if (argument != null) {
argument = replaceDefaultArgument(field, argument);
arguments.put(argument, field);
int index = argument.index();
while (orderedArguments.size() <= index) {
orderedArguments.add(null);
}
if (orderedArguments.get(index) != null) {
throw new IllegalArgumentException("Duplicate argument index: " + index + " on Action " + action.getClass().getName());
}
orderedArguments.set(index, argument);
}
}
}
assertIndexesAreCorrect(action.getClass(), orderedArguments);
String commandErrorSt = COLOR_RED + "Error executing command " + command.scope() + ":" + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL + COLOR_DEFAULT + ": ";
for (Object param : params) {
if (HelpOption.HELP.name().equals(param)) {
int termWidth = session.getTerminal() != null ? session.getTerminal().getWidth() : 80;
boolean globalScope = NameScoping.isGlobalScope(session, command.scope());
printUsage(action, options, arguments, System.out, globalScope, termWidth);
return false;
}
}
// Populate
Map<Option, Object> optionValues = new HashMap<Option, Object>();
Map<Argument, Object> argumentValues = new HashMap<Argument, Object>();
boolean processOptions = true;
int argIndex = 0;
for (Iterator<Object> it = params.iterator(); it.hasNext(); ) {
Object param = it.next();
String paramValue = null;
if (param instanceof String) {
paramValue = (String) param;
}
if (param instanceof Token) {
paramValue = param.toString();
}
if (processOptions && paramValue != null && paramValue.startsWith("-")) {
boolean isKeyValuePair = paramValue.indexOf('=') != -1;
String name;
Object value = null;
if (isKeyValuePair) {
name = paramValue.substring(0, paramValue.indexOf('='));
value = paramValue.substring(paramValue.indexOf('=') + 1);
} else {
name = paramValue;
}
Option option = null;
for (Option opt : options.keySet()) {
if (name.equals(opt.name()) || Arrays.asList(opt.aliases()).contains(name)) {
option = opt;
break;
}
}
if (option == null) {
throw new CommandException(commandErrorSt + "undefined option " + INTENSITY_BOLD + paramValue + INTENSITY_NORMAL + "\n" + "Try <command> --help' for more information.", "Undefined option: " + paramValue);
}
Field field = options.get(option);
if (value == null && (field.getType() == boolean.class || field.getType() == Boolean.class)) {
value = Boolean.TRUE;
}
if (value == null && it.hasNext()) {
value = it.next();
}
if (value == null) {
throw new CommandException(commandErrorSt + "missing value for option " + INTENSITY_BOLD + paramValue + INTENSITY_NORMAL, "Missing value for option: " + paramValue);
}
if (option.multiValued()) {
@SuppressWarnings("unchecked") List<Object> l = (List<Object>) optionValues.get(option);
if (l == null) {
l = new ArrayList<Object>();
optionValues.put(option, l);
}
l.add(value);
} else {
optionValues.put(option, value);
}
} else {
processOptions = false;
if (argIndex >= orderedArguments.size()) {
throw new CommandException(commandErrorSt + "too many arguments specified", "Too many arguments specified");
}
Argument argument = orderedArguments.get(argIndex);
if (!argument.multiValued()) {
argIndex++;
}
if (argument.multiValued()) {
@SuppressWarnings("unchecked") List<Object> l = (List<Object>) argumentValues.get(argument);
if (l == null) {
l = new ArrayList<Object>();
argumentValues.put(argument, l);
}
l.add(param);
} else {
argumentValues.put(argument, param);
}
}
}
// Check required arguments / options
for (Option option : options.keySet()) {
if (option.required() && optionValues.get(option) == null) {
throw new CommandException(commandErrorSt + "option " + INTENSITY_BOLD + option.name() + INTENSITY_NORMAL + " is required", "Option " + option.name() + " is required");
}
}
for (Argument argument : orderedArguments) {
if (argument.required() && argumentValues.get(argument) == null) {
throw new CommandException(commandErrorSt + "argument " + INTENSITY_BOLD + argument.name() + INTENSITY_NORMAL + " is required", "Argument " + argument.name() + " is required");
}
}
// Convert and inject values
for (Map.Entry<Option, Object> entry : optionValues.entrySet()) {
Field field = options.get(entry.getKey());
Object value;
try {
value = convert(action, entry.getValue(), field.getGenericType());
} catch (Exception e) {
throw new CommandException(commandErrorSt + "unable to convert option " + INTENSITY_BOLD + entry.getKey().name() + INTENSITY_NORMAL + " with value '" + entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(), "Unable to convert option " + entry.getKey().name() + " with value '" + entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(), e);
}
field.setAccessible(true);
field.set(action, value);
}
for (Map.Entry<Argument, Object> entry : argumentValues.entrySet()) {
Field field = arguments.get(entry.getKey());
Object value;
try {
value = convert(action, entry.getValue(), field.getGenericType());
} catch (Exception e) {
throw new CommandException(commandErrorSt + "unable to convert argument " + INTENSITY_BOLD + entry.getKey().name() + INTENSITY_NORMAL + " with value '" + entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(), "Unable to convert argument " + entry.getKey().name() + " with value '" + entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(), e);
}
field.setAccessible(true);
field.set(action, value);
}
return true;
}
use of org.apache.karaf.shell.support.converter.GenericType in project karaf by apache.
the class ManagerImpl method instantiate.
public <T> T instantiate(Class<? extends T> clazz, Registry registry) throws Exception {
if (!allowCustomServices) {
Service reg = clazz.getAnnotation(Service.class);
if (reg == null) {
throw new IllegalArgumentException("Class " + clazz.getName() + " is not annotated with @Service");
}
}
T instance = clazz.newInstance();
// Inject services
for (Class<?> cl = clazz; cl != Object.class; cl = cl.getSuperclass()) {
for (Field field : cl.getDeclaredFields()) {
Reference ref = field.getAnnotation(Reference.class);
if (ref != null) {
GenericType type = new GenericType(field.getGenericType());
Object value;
if (type.getRawClass() == List.class) {
Set<Object> set = new HashSet<>();
set.addAll(registry.getServices(type.getActualTypeArgument(0).getRawClass()));
if (registry != this.dependencies) {
set.addAll(this.dependencies.getServices(type.getActualTypeArgument(0).getRawClass()));
}
value = new ArrayList<>(set);
} else {
value = registry.getService(type.getRawClass());
if (value == null && registry != this.dependencies) {
value = this.dependencies.getService(type.getRawClass());
}
}
if (!allowCustomServices && value == null && !ref.optional()) {
throw new IllegalStateException("No service matching " + field.getType().getName());
}
field.setAccessible(true);
field.set(instance, value);
}
}
}
for (Method method : clazz.getDeclaredMethods()) {
Init ann = method.getAnnotation(Init.class);
if (ann != null && method.getParameterTypes().length == 0 && method.getReturnType() == void.class) {
method.setAccessible(true);
method.invoke(instance);
}
}
return instance;
}
use of org.apache.karaf.shell.support.converter.GenericType in project karaf by apache.
the class CommandExtension method inspectClass.
private void inspectClass(final Class<?> clazz) throws Exception {
Service reg = clazz.getAnnotation(Service.class);
if (reg == null) {
return;
}
// Create trackers
for (Class<?> cl = clazz; cl != Object.class; cl = cl.getSuperclass()) {
for (Field field : cl.getDeclaredFields()) {
Reference ref = field.getAnnotation(Reference.class);
if (ref != null) {
GenericType type = new GenericType(field.getGenericType());
Class clazzRef = type.getRawClass() == List.class ? type.getActualTypeArgument(0).getRawClass() : type.getRawClass();
if (clazzRef != BundleContext.class && clazzRef != Session.class && clazzRef != Terminal.class && clazzRef != History.class && clazzRef != Registry.class && clazzRef != SessionFactory.class && !registry.hasService(clazzRef)) {
track(type, ref.optional());
}
}
}
}
classes.add(clazz);
}
use of org.apache.karaf.shell.support.converter.GenericType in project karaf by apache.
the class ArgumentCompleter method getDefaultCompleter.
@SuppressWarnings({ "unchecked", "rawtypes" })
private Completer getDefaultCompleter(Field field, boolean multi) {
Completer completer = null;
Class<?> type = field.getType();
GenericType genericType = new GenericType(field.getGenericType());
if (Collection.class.isAssignableFrom(genericType.getRawClass()) && multi) {
type = genericType.getActualTypeArgument(0).getRawClass();
}
if (type.isAssignableFrom(URI.class)) {
completer = new UriCompleter();
} else if (type.isAssignableFrom(File.class)) {
completer = new FileCompleter();
} else if (type.isAssignableFrom(Boolean.class) || type.isAssignableFrom(boolean.class)) {
completer = new StringsCompleter(Arrays.asList("false", "true"));
} else if (Enum.class.isAssignableFrom(type)) {
Set<String> values = new HashSet<>();
for (Object o : EnumSet.allOf((Class<Enum>) type)) {
values.add(o.toString());
}
completer = new StringsCompleter(values);
}
return completer;
}
Aggregations