Search in sources :

Example 1 with Descriptor

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

the class Basic method headers.

@Descriptor("display bundle headers")
public void headers(@Descriptor("target bundles") Bundle[] bundles) {
    bundles = ((bundles == null) || (bundles.length == 0)) ? m_bc.getBundles() : bundles;
    for (Bundle bundle : bundles) {
        String title = Util.getBundleName(bundle);
        System.out.println("\n" + title);
        System.out.println(Util.getUnderlineString(title.length()));
        Dictionary<String, String> dict = bundle.getHeaders();
        Enumeration<String> keys = dict.keys();
        while (keys.hasMoreElements()) {
            String k = keys.nextElement();
            String v = dict.get(k);
            System.out.println(k + " = " + v);
        }
    }
}
Also used : Bundle(org.osgi.framework.Bundle) Descriptor(org.apache.felix.service.command.Descriptor)

Example 2 with Descriptor

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

the class Basic method bundlelevel.

@Descriptor("set bundle start level or initial bundle start level")
public void bundlelevel(@Descriptor("set the bundle's start level") @Parameter(names = { "-s", "--setlevel" }, presentValue = "true", absentValue = "false") boolean set, @Descriptor("set the initial bundle start level") @Parameter(names = { "-i", "--setinitial" }, presentValue = "true", absentValue = "false") boolean initial, @Descriptor("target level") int level, @Descriptor("target identifiers") Bundle[] bundles) {
    // 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 if (set && initial) {
        System.out.println("Cannot specify '-s' and '-i' at the same time.");
    } else if (!set && !initial) {
        System.out.println("Must specify either '-s' or '-i'.");
    } else if (level <= 0) {
        System.out.println("Specified start level must be greater than zero.");
    } else // Set the initial bundle start level.
    if (initial) {
        if ((bundles != null) && (bundles.length == 0)) {
            sl.setInitialBundleStartLevel(level);
        } else {
            System.out.println("Cannot specify bundles when setting initial start level.");
        }
    } else // Set the bundle start level.
    if (set) {
        if ((bundles != null) && (bundles.length != 0)) {
            for (Bundle bundle : bundles) {
                sl.setBundleStartLevel(bundle, level);
            }
        } else {
            System.out.println("Must specify target bundles.");
        }
    }
    Util.ungetServices(m_bc, refs);
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) StartLevel(org.osgi.service.startlevel.StartLevel) ServiceReference(org.osgi.framework.ServiceReference) Descriptor(org.apache.felix.service.command.Descriptor)

Example 3 with Descriptor

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

the class Basic method log.

@Descriptor("display some matching log entries")
public void log(@Descriptor("maximum number of entries") int maxEntries, @Descriptor("minimum log level [ debug | info | warn | error ]") String logLevel) {
    // Keep track of service references.
    List<ServiceReference<?>> refs = new ArrayList<ServiceReference<?>>();
    // Get start level service.
    LogReaderService lrs = Util.getService(m_bc, LogReaderService.class, refs);
    if (lrs == null) {
        System.out.println("Log reader service is unavailable.");
    } else {
        @SuppressWarnings("unchecked") Enumeration<LogEntry> entries = lrs.getLog();
        int minLevel = logLevelAsInt(logLevel);
        int index = 0;
        while (entries.hasMoreElements() && (maxEntries < 0 || index < maxEntries)) {
            LogEntry entry = (LogEntry) entries.nextElement();
            if (entry.getLevel() <= minLevel) {
                display(entry);
                index++;
            }
        }
        Util.ungetServices(m_bc, refs);
    }
}
Also used : ArrayList(java.util.ArrayList) LogReaderService(org.osgi.service.log.LogReaderService) LogEntry(org.osgi.service.log.LogEntry) ServiceReference(org.osgi.framework.ServiceReference) Descriptor(org.apache.felix.service.command.Descriptor)

Example 4 with Descriptor

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

the class Basic method help.

@Descriptor("displays information about a specific command")
public void help(@Descriptor("target command") String name) {
    Map<String, List<Method>> commands = getCommands();
    List<Method> methods = null;
    // If the specified command doesn't have a scope, then
    // search for matching methods by ignoring the scope.
    int scopeIdx = name.indexOf(':');
    if (scopeIdx < 0) {
        for (Entry<String, List<Method>> entry : commands.entrySet()) {
            String k = entry.getKey().substring(entry.getKey().indexOf(':') + 1);
            if (name.equals(k)) {
                name = entry.getKey();
                methods = entry.getValue();
                break;
            }
        }
    } else // Otherwise directly look up matching methods.
    {
        methods = commands.get(name);
    }
    if ((methods != null) && (methods.size() > 0)) {
        for (Method m : methods) {
            Descriptor d = m.getAnnotation(Descriptor.class);
            if (d == null) {
                System.out.println("\n" + m.getName());
            } else {
                System.out.println("\n" + m.getName() + " - " + d.value());
            }
            System.out.println("   scope: " + name.substring(0, name.indexOf(':')));
            // Get flags and options.
            Class<?>[] paramTypes = m.getParameterTypes();
            Map<String, Parameter> flags = new TreeMap<String, Parameter>();
            Map<String, String> flagDescs = new TreeMap<String, String>();
            Map<String, Parameter> options = new TreeMap<String, Parameter>();
            Map<String, String> optionDescs = new TreeMap<String, String>();
            List<String> params = new ArrayList<String>();
            Annotation[][] anns = m.getParameterAnnotations();
            for (int paramIdx = 0; paramIdx < anns.length; paramIdx++) {
                Class<?> paramType = m.getParameterTypes()[paramIdx];
                if (paramType == CommandSession.class) {
                    /* Do not bother the user with a CommandSession. */
                    continue;
                }
                Parameter p = findAnnotation(anns[paramIdx], Parameter.class);
                d = findAnnotation(anns[paramIdx], Descriptor.class);
                if (p != null) {
                    if (p.presentValue().equals(Parameter.UNSPECIFIED)) {
                        options.put(p.names()[0], p);
                        if (d != null) {
                            optionDescs.put(p.names()[0], d.value());
                        }
                    } else {
                        flags.put(p.names()[0], p);
                        if (d != null) {
                            flagDescs.put(p.names()[0], d.value());
                        }
                    }
                } else if (d != null) {
                    params.add(paramTypes[paramIdx].getSimpleName());
                    params.add(d.value());
                } else {
                    params.add(paramTypes[paramIdx].getSimpleName());
                    params.add("");
                }
            }
            // Print flags and options.
            if (flags.size() > 0) {
                System.out.println("   flags:");
                for (Entry<String, Parameter> entry : flags.entrySet()) {
                    // Print all aliases.
                    String[] names = entry.getValue().names();
                    System.out.print("      " + names[0]);
                    for (int aliasIdx = 1; aliasIdx < names.length; aliasIdx++) {
                        System.out.print(", " + names[aliasIdx]);
                    }
                    System.out.println("   " + flagDescs.get(entry.getKey()));
                }
            }
            if (options.size() > 0) {
                System.out.println("   options:");
                for (Entry<String, Parameter> entry : options.entrySet()) {
                    // Print all aliases.
                    String[] names = entry.getValue().names();
                    System.out.print("      " + names[0]);
                    for (int aliasIdx = 1; aliasIdx < names.length; aliasIdx++) {
                        System.out.print(", " + names[aliasIdx]);
                    }
                    System.out.println("   " + optionDescs.get(entry.getKey()) + ((entry.getValue().absentValue() == null) ? "" : " [optional]"));
                }
            }
            if (params.size() > 0) {
                System.out.println("   parameters:");
                for (Iterator<String> it = params.iterator(); it.hasNext(); ) {
                    System.out.println("      " + it.next() + "   " + it.next());
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) Descriptor(org.apache.felix.service.command.Descriptor) Parameter(org.apache.felix.service.command.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) Descriptor(org.apache.felix.service.command.Descriptor)

Example 5 with Descriptor

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

the class Basic method lb.

@Descriptor("list installed bundles matching a substring")
public void lb(@Descriptor("show location") @Parameter(names = { "-l", "--location" }, presentValue = "true", absentValue = "false") boolean showLoc, @Descriptor("show symbolic name") @Parameter(names = { "-s", "--symbolicname" }, presentValue = "true", absentValue = "false") boolean showSymbolic, @Descriptor("show update location") @Parameter(names = { "-u", "--updatelocation" }, presentValue = "true", absentValue = "false") boolean showUpdate, @Descriptor("subtring matched against name or symbolic name") String pattern) {
    // 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.");
    }
    List<Bundle> found = new ArrayList<Bundle>();
    if (pattern == null) {
        found.addAll(Arrays.asList(m_bc.getBundles()));
    } else {
        Bundle[] bundles = m_bc.getBundles();
        for (int i = 0; i < bundles.length; i++) {
            Bundle bundle = bundles[i];
            String name = (String) bundle.getHeaders().get(Constants.BUNDLE_NAME);
            if (matchBundleName(bundle.getSymbolicName(), pattern) || matchBundleName(name, pattern)) {
                found.add(bundle);
            }
        }
    }
    if (found.size() > 0) {
        printBundleList((Bundle[]) found.toArray(new Bundle[found.size()]), sl, showLoc, showSymbolic, showUpdate);
    } else {
        System.out.println("No matching bundles found");
    }
    Util.ungetServices(m_bc, refs);
}
Also used : Bundle(org.osgi.framework.Bundle) 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