Search in sources :

Example 16 with Descriptor

use of org.apache.felix.service.command.Descriptor in project karaf by apache.

the class CommandTracker method addingService.

@Override
public List<Command> addingService(final ServiceReference<Object> reference) {
    final String scope = reference.getProperty(CommandProcessor.COMMAND_SCOPE).toString();
    final Object function = reference.getProperty(CommandProcessor.COMMAND_FUNCTION);
    final List<String> names = new ArrayList<>();
    if (function.getClass().isArray()) {
        for (final Object f : ((Object[]) function)) {
            names.add(f.toString());
        }
    } else {
        names.add(function.toString());
    }
    List<Command> commands = new ArrayList<>();
    for (String name : names) {
        final Function target = new CommandProxy(context, reference, name);
        Command command = new Command() {

            @Override
            public String getScope() {
                return scope;
            }

            @Override
            public String getName() {
                return name;
            }

            @Override
            public String getDescription() {
                for (Method method : getMethods()) {
                    Descriptor d = method.getAnnotation(Descriptor.class);
                    if (d != null) {
                        return d.value();
                    }
                }
                Object property = reference.getProperty("osgi.command.description");
                if (property != null) {
                    return property.toString();
                } else {
                    return getName();
                }
            }

            @Override
            public Completer getCompleter(final boolean scoped) {
                return null;
            }

            @Override
            public Parser getParser() {
                return null;
            }

            @Override
            public Object execute(Session session, List<Object> arguments) throws Exception {
                // TODO: remove not really nice cast
                CommandSession commandSession = (CommandSession) session.get(".commandSession");
                return target.execute(commandSession, arguments);
            }

            private List<Method> getMethods() {
                Object target = context.getService(reference);
                List<Method> methods = new ArrayList<>();
                String func = name.substring(name.indexOf(':') + 1).toLowerCase();
                List<String> funcs = new ArrayList<>();
                funcs.add("is" + func);
                funcs.add("get" + func);
                funcs.add("set" + func);
                if (Reflective.KEYWORDS.contains(func)) {
                    funcs.add("_" + func);
                } else {
                    funcs.add(func);
                }
                for (Method method : target.getClass().getMethods()) {
                    if (funcs.contains(method.getName().toLowerCase())) {
                        methods.add(method);
                    }
                }
                context.ungetService(reference);
                return methods;
            }
        };
        sessionFactory.getRegistry().register(command);
        commands.add(command);
    }
    return commands;
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Function(org.apache.felix.service.command.Function) CommandProxy(org.apache.felix.gogo.runtime.CommandProxy) CommandSession(org.apache.felix.service.command.CommandSession) Descriptor(org.apache.felix.service.command.Descriptor) List(java.util.List) ArrayList(java.util.ArrayList) CommandSession(org.apache.felix.service.command.CommandSession)

Example 17 with Descriptor

use of org.apache.felix.service.command.Descriptor in project felix by apache.

the class ComponentCommands method disable.

@Descriptor("Disable an enabled component")
public boolean disable(@Descriptor("Name of the component") final String name) {
    boolean changed = false;
    for (ComponentDescriptionDTO comp : findComponents(name)) {
        if (scr.isComponentEnabled(comp)) {
            scr.disableComponent(comp);
            changed = true;
        }
    }
    return changed;
}
Also used : ComponentDescriptionDTO(org.osgi.service.component.runtime.dto.ComponentDescriptionDTO) Descriptor(org.apache.felix.service.command.Descriptor)

Example 18 with Descriptor

use of org.apache.felix.service.command.Descriptor in project felix by apache.

the class ObrGogoCommand method javadoc.

@Descriptor("retrieve resource JavaDoc from repository")
public void javadoc(@Descriptor("extract documentation") @Parameter(names = { "-x", "--extract" }, presentValue = "true", absentValue = "false") boolean extract, @Descriptor("local target directory") File localDir, @Descriptor("( <bundle-name> | <symbolic-name> | <bundle-id> )[@<version>] ...") String[] args) throws IOException, InvalidSyntaxException {
    Object svcObj = getRepositoryAdmin();
    if (svcObj == null) {
        return;
    }
    RepositoryAdmin ra = (RepositoryAdmin) svcObj;
    for (int argIdx = 0; argIdx < args.length; argIdx++) {
        // Find the target's bundle resource.
        String targetName = args[argIdx];
        String targetVersion = null;
        int idx = args[argIdx].indexOf(VERSION_SEPARATOR);
        if (idx > 0) {
            targetName = args[argIdx].substring(0, idx);
            targetVersion = args[argIdx].substring(idx + 1);
        }
        Resource resource = selectNewestVersion(searchRepository(ra, targetName, targetVersion));
        if (resource == null) {
            System.err.println("Unknown bundle and/or version: " + args[argIdx]);
        } else {
            URL docURL = (URL) resource.getProperties().get("javadoc");
            if (docURL != null) {
                downloadSource(System.out, System.err, docURL, localDir, extract);
            } else {
                System.err.println("Missing javadoc URL: " + args[argIdx]);
            }
        }
    }
}
Also used : URL(java.net.URL) Descriptor(org.apache.felix.service.command.Descriptor)

Example 19 with Descriptor

use of org.apache.felix.service.command.Descriptor in project felix by apache.

the class ObrGogoCommand method source.

@Descriptor("retrieve resource source code from repository")
public void source(@Descriptor("extract source code") @Parameter(names = { "-x", "--extract" }, presentValue = "true", absentValue = "false") boolean extract, @Descriptor("local target directory") File localDir, @Descriptor("( <bundle-name> | <symbolic-name> | <bundle-id> )[@<version>] ...") String[] args) throws IOException, InvalidSyntaxException {
    Object svcObj = getRepositoryAdmin();
    if (svcObj == null) {
        return;
    }
    RepositoryAdmin ra = (RepositoryAdmin) svcObj;
    for (int argIdx = 0; argIdx < args.length; argIdx++) {
        // Find the target's bundle resource.
        String targetName = args[argIdx];
        String targetVersion = null;
        int idx = args[argIdx].indexOf(VERSION_SEPARATOR);
        if (idx > 0) {
            targetName = args[argIdx].substring(0, idx);
            targetVersion = args[argIdx].substring(idx + 1);
        }
        Resource resource = selectNewestVersion(searchRepository(ra, targetName, targetVersion));
        if (resource == null) {
            System.err.println("Unknown bundle and/or version: " + args[argIdx]);
        } else {
            String srcURI = (String) resource.getProperties().get(Resource.SOURCE_URI);
            if (srcURI != null) {
                downloadSource(System.out, System.err, new URL(srcURI), localDir, extract);
            } else {
                System.err.println("Missing source URL: " + args[argIdx]);
            }
        }
    }
}
Also used : URL(java.net.URL) Descriptor(org.apache.felix.service.command.Descriptor)

Example 20 with Descriptor

use of org.apache.felix.service.command.Descriptor in project felix by apache.

the class Basic method bundlelevel.

@Descriptor("query bundle start level")
public void bundlelevel(@Descriptor("bundle to query") Bundle bundle) {
    // Keep track of service references.
    List<ServiceReference<?>> refs = new ArrayList<ServiceReference<?>>();
    // Get start level service.
    StartLevel sl = Util.getService(m_bc, StartLevel.class, refs);
    if (sl == null) {
        System.out.println("Start Level service is unavailable.");
    } else // Get the bundle start level.
    {
        if (bundle != null) {
            System.out.println(bundle + " is level " + sl.getBundleStartLevel(bundle));
        }
    }
    Util.ungetServices(m_bc, refs);
}
Also used : ArrayList(java.util.ArrayList) StartLevel(org.osgi.service.startlevel.StartLevel) ServiceReference(org.osgi.framework.ServiceReference) Descriptor(org.apache.felix.service.command.Descriptor)

Aggregations

Descriptor (org.apache.felix.service.command.Descriptor)31 ArrayList (java.util.ArrayList)13 ServiceReference (org.osgi.framework.ServiceReference)8 IOException (java.io.IOException)6 Bundle (org.osgi.framework.Bundle)6 StartLevel (org.osgi.service.startlevel.StartLevel)5 URL (java.net.URL)4 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)4 PrintStream (java.io.PrintStream)3 Method (java.lang.reflect.Method)3 MalformedURLException (java.net.MalformedURLException)3 List (java.util.List)3 TreeMap (java.util.TreeMap)3 HandlerFactory (org.apache.felix.ipojo.HandlerFactory)3 TypeDeclaration (org.apache.felix.ipojo.extender.TypeDeclaration)3 BundleException (org.osgi.framework.BundleException)3 File (java.io.File)2 InputStream (java.io.InputStream)2 URI (java.net.URI)2 Factory (org.apache.felix.ipojo.Factory)2