Search in sources :

Example 1 with Init

use of org.apache.karaf.shell.api.action.lifecycle.Init in project karaf by apache.

the class ConfigurationCompleter method init.

@Init
public void init() {
    registration = bundleContext.registerService(ConfigurationListener.class, this, null);
    Configuration[] configs;
    try {
        configs = admin.listConfigurations(null);
        if (configs == null) {
            return;
        }
    } catch (Exception e) {
        return;
    }
    Collection<String> pids = new ArrayList<>();
    for (Configuration config : configs) {
        pids.add(config.getPid());
    }
    delegate.getStrings().addAll(pids);
}
Also used : ConfigurationListener(org.osgi.service.cm.ConfigurationListener) Configuration(org.osgi.service.cm.Configuration) ArrayList(java.util.ArrayList) Init(org.apache.karaf.shell.api.action.lifecycle.Init)

Example 2 with Init

use of org.apache.karaf.shell.api.action.lifecycle.Init 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;
}
Also used : GenericType(org.apache.karaf.shell.support.converter.GenericType) Reference(org.apache.karaf.shell.api.action.lifecycle.Reference) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) Init(org.apache.karaf.shell.api.action.lifecycle.Init) HashSet(java.util.HashSet)

Aggregations

Init (org.apache.karaf.shell.api.action.lifecycle.Init)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Reference (org.apache.karaf.shell.api.action.lifecycle.Reference)1 Service (org.apache.karaf.shell.api.action.lifecycle.Service)1 GenericType (org.apache.karaf.shell.support.converter.GenericType)1 Configuration (org.osgi.service.cm.Configuration)1 ConfigurationListener (org.osgi.service.cm.ConfigurationListener)1