Search in sources :

Example 1 with Service

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

the class ManagerImpl method register.

@SuppressWarnings("unchecked")
@Override
public void register(Class<?> clazz) {
    if (!allowCustomServices) {
        Service reg = clazz.getAnnotation(Service.class);
        if (reg == null) {
            throw new IllegalArgumentException("Class " + clazz.getName() + " is not annotated with @Service");
        }
    }
    if (Action.class.isAssignableFrom(clazz)) {
        final Command cmd = clazz.getAnnotation(Command.class);
        if (cmd == null) {
            throw new IllegalArgumentException("Command " + clazz.getName() + " is not annotated with @Command");
        }
        Object command = new ActionCommand(this, (Class<? extends Action>) clazz);
        synchronized (instances) {
            instances.put(clazz, command);
        }
        registrations.register(command);
    }
    if (allowCustomServices || Completer.class.isAssignableFrom(clazz) || Parser.class.isAssignableFrom(clazz)) {
        try {
            // Create completer
            Object completer = instantiate(clazz);
            synchronized (instances) {
                instances.put(clazz, completer);
            }
            registrations.register(completer);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : Command(org.apache.karaf.shell.api.action.Command) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Parser(org.apache.karaf.shell.api.console.Parser)

Example 2 with Service

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

the class ManagerImpl method release.

public void release(Object instance) throws Exception {
    Class<?> clazz = instance.getClass();
    if (!allowCustomServices) {
        Service reg = clazz.getAnnotation(Service.class);
        if (reg == null) {
            throw new IllegalArgumentException("Class " + clazz.getName() + " is not annotated with @Service");
        }
    }
    for (Method method : clazz.getDeclaredMethods()) {
        Destroy ann = method.getAnnotation(Destroy.class);
        if (ann != null && method.getParameterTypes().length == 0 && method.getReturnType() == void.class) {
            method.setAccessible(true);
            method.invoke(instance);
        }
    }
}
Also used : Destroy(org.apache.karaf.shell.api.action.lifecycle.Destroy) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Method(java.lang.reflect.Method)

Example 3 with Service

use of org.apache.karaf.shell.api.action.lifecycle.Service 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)

Example 4 with Service

use of org.apache.karaf.shell.api.action.lifecycle.Service 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);
}
Also used : Field(java.lang.reflect.Field) 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) ArrayList(java.util.ArrayList) List(java.util.List) Registry(org.apache.karaf.shell.api.console.Registry) Terminal(org.apache.karaf.shell.api.console.Terminal)

Example 5 with Service

use of org.apache.karaf.shell.api.action.lifecycle.Service in project fuse-karaf by jboss-fuse.

the class DeleteCommand method doExecute.

@Override
protected void doExecute(PatchService service) throws Exception {
    Patch patch = patchManagement.loadPatch(new PatchDetailsRequest(patchId));
    if (patch == null) {
        throw new PatchException("Patch '" + patchId + "' not found");
    }
    if (patch.getResult() != null && patch.getResult().getKarafBases().size() > 0) {
        throw new PatchException("Patch '" + patchId + "' can't be deleted, as it's installed in these containers: " + patch.getResult().getKarafBases().stream().map(kb -> kb.contains("|") ? kb.split("\\s*\\|\\s*")[0] : kb).collect(Collectors.joining(", ")));
    }
    patchManagement.delete(patch);
    System.out.println("Patch '" + patchId + "' was successfully deleted");
}
Also used : UninstallPatchCompleter(org.jboss.fuse.patch.commands.completers.UninstallPatchCompleter) Argument(org.apache.karaf.shell.api.action.Argument) PatchDetailsRequest(org.jboss.fuse.patch.management.PatchDetailsRequest) Collectors(java.util.stream.Collectors) PatchException(org.jboss.fuse.patch.management.PatchException) BundleContext(org.osgi.framework.BundleContext) Command(org.apache.karaf.shell.api.action.Command) Reference(org.apache.karaf.shell.api.action.lifecycle.Reference) Patch(org.jboss.fuse.patch.management.Patch) Service(org.apache.karaf.shell.api.action.lifecycle.Service) PatchService(org.jboss.fuse.patch.PatchService) PatchManagement(org.jboss.fuse.patch.management.PatchManagement) Completion(org.apache.karaf.shell.api.action.Completion) PatchException(org.jboss.fuse.patch.management.PatchException) Patch(org.jboss.fuse.patch.management.Patch) PatchDetailsRequest(org.jboss.fuse.patch.management.PatchDetailsRequest)

Aggregations

Service (org.apache.karaf.shell.api.action.lifecycle.Service)5 Reference (org.apache.karaf.shell.api.action.lifecycle.Reference)3 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 Command (org.apache.karaf.shell.api.action.Command)2 GenericType (org.apache.karaf.shell.support.converter.GenericType)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Argument (org.apache.karaf.shell.api.action.Argument)1 Completion (org.apache.karaf.shell.api.action.Completion)1 Destroy (org.apache.karaf.shell.api.action.lifecycle.Destroy)1 Init (org.apache.karaf.shell.api.action.lifecycle.Init)1 Parser (org.apache.karaf.shell.api.console.Parser)1 Registry (org.apache.karaf.shell.api.console.Registry)1 Terminal (org.apache.karaf.shell.api.console.Terminal)1 PatchService (org.jboss.fuse.patch.PatchService)1 UninstallPatchCompleter (org.jboss.fuse.patch.commands.completers.UninstallPatchCompleter)1 Patch (org.jboss.fuse.patch.management.Patch)1