Search in sources :

Example 1 with ComponentDependencyDeclaration

use of org.apache.felix.dm.ComponentDependencyDeclaration in project felix by apache.

the class DMCommand method dm.

/**
 * Dependency Manager "dm" command. We use gogo annotations, in order to automate documentation,
 * and also to automatically manage optional flags/options and parameters ordering.
 *
 * @param session the gogo command session, used to get some variables declared in the shell
 *        This parameter is automatically passed by the gogo runtime.
 * @param nodeps false means that dependencies are not displayed
 * @param compact true means informations are displayed in a compact format. This parameter can also be
 *        set using the "dependencymanager.compact" gogo shell variable.
 * @param notavail only unregistered components / unavailable dependencies are displayed
 * @param stats true means some statistics are displayed
 * @param services an osgi filter used to filter on some given osgi service properties.  This parameter can also be
 *        set using the "dependencymanager.services" gogo shell variable.
 * @param components a regular expression to match either component implementation class names.  This parameter can also be
 *        set using the "dependencymanager.components" gogo shell variable.
 * @param componentIds only components matching one of the specified components ids are displayed
 * @param bundleIds a list of bundle ids or symbolic names, used to filter on some given bundles
 */
@Descriptor("List dependency manager components")
public void dm(CommandSession session, @Descriptor("Hides component dependencies") @Parameter(names = { "nodeps", "nd" }, presentValue = "true", absentValue = "false") boolean nodeps, @Descriptor("Displays components using a compact form") @Parameter(names = { "compact", "cp" }, presentValue = "true", absentValue = "") String compact, @Descriptor("Only displays unavailable components") @Parameter(names = { "notavail", "na" }, presentValue = "true", absentValue = "false") boolean notavail, @Descriptor("Detects where are the root failures") @Parameter(names = { "wtf" }, presentValue = "true", absentValue = "false") boolean wtf, @Descriptor("Displays components statistics") @Parameter(names = { "stats", "stat", "st" }, presentValue = "true", absentValue = "false") boolean stats, @Descriptor("<OSGi filter used to filter some service properties>") @Parameter(names = { "services", "s" }, absentValue = "") String services, @Descriptor("<Regex(s) used to filter on component implementation class names (comma separated), can be negated using \"!\" prefix>") @Parameter(names = { "components", "c" }, absentValue = "") String components, @Descriptor("<List of component identifiers to display (comma separated)>") @Parameter(names = { "componentIds", "cid", "ci" }, absentValue = "") String componentIds, @Descriptor("<List of bundle ids or bundle symbolic names to display (comma separated)>") @Parameter(names = { "bundleIds", "bid", "bi", "b" }, absentValue = "") String bundleIds, @Descriptor("<Max number of top components to display (0=all)> This command displays components callbacks (init/start) times>") @Parameter(names = { "top" }, absentValue = "-1") int top) throws Throwable {
    boolean comp = Boolean.parseBoolean(getParam(session, ENV_COMPACT, compact));
    services = getParam(session, ENV_SERVICES, services);
    String[] componentsRegex = getParams(session, ENV_COMPONENTS, components);
    // list of bundle ids or bundle symbolic names
    ArrayList<String> bids = new ArrayList<String>();
    // list of component ids
    ArrayList<Long> cids = new ArrayList<Long>();
    // Parse and check componentIds option
    StringTokenizer tok = new StringTokenizer(componentIds, ", ");
    while (tok.hasMoreTokens()) {
        try {
            cids.add(Long.parseLong(tok.nextToken()));
        } catch (NumberFormatException e) {
            System.out.println("Invalid value for componentIds option");
            return;
        }
    }
    // Parse services filter
    Filter servicesFilter = null;
    try {
        if (services != null) {
            servicesFilter = m_context.createFilter(services);
        }
    } catch (InvalidSyntaxException e) {
        System.out.println("Invalid services OSGi filter: " + services);
        e.printStackTrace(System.err);
        return;
    }
    // Parse and check bundleIds option
    tok = new StringTokenizer(bundleIds, ", ");
    while (tok.hasMoreTokens()) {
        bids.add(tok.nextToken());
    }
    if (top != -1) {
        showTopComponents(top);
        return;
    }
    if (wtf) {
        wtf();
        return;
    }
    DependencyGraph graph = null;
    if (notavail) {
        graph = DependencyGraph.getGraph(ComponentState.UNREGISTERED, DependencyState.ALL_UNAVAILABLE);
    } else {
        graph = DependencyGraph.getGraph(ComponentState.ALL, DependencyState.ALL);
    }
    List<ComponentDeclaration> allComponents = graph.getAllComponents();
    Collections.sort(allComponents, COMPONENT_DECLARATION_COMPARATOR);
    long numberOfComponents = 0;
    long numberOfDependencies = 0;
    long lastBundleId = -1;
    for (ComponentDeclaration cd : allComponents) {
        Bundle bundle = cd.getBundleContext().getBundle();
        if (!matchBundle(bundle, bids)) {
            continue;
        }
        Component component = (Component) cd;
        String name = cd.getName();
        if (!mayDisplay(component, servicesFilter, componentsRegex, cids)) {
            continue;
        }
        numberOfComponents++;
        long bundleId = bundle.getBundleId();
        if (lastBundleId != bundleId) {
            lastBundleId = bundleId;
            if (comp) {
                System.out.println("[" + bundleId + "] " + compactName(bundle.getSymbolicName()));
            } else {
                System.out.println("[" + bundleId + "] " + bundle.getSymbolicName());
            }
        }
        if (comp) {
            System.out.print(" [" + cd.getId() + "] " + compactName(name) + " " + compactState(ComponentDeclaration.STATE_NAMES[cd.getState()]));
        } else {
            System.out.println(" [" + cd.getId() + "] " + name + " " + ComponentDeclaration.STATE_NAMES[cd.getState()]);
        }
        if (!nodeps) {
            List<ComponentDependencyDeclaration> dependencies = graph.getDependecies(cd);
            if (!dependencies.isEmpty()) {
                numberOfDependencies += dependencies.size();
                if (comp) {
                    System.out.print('(');
                }
                for (int j = 0; j < dependencies.size(); j++) {
                    ComponentDependencyDeclaration dep = dependencies.get(j);
                    String depName = dep.getName();
                    String depType = dep.getType();
                    int depState = dep.getState();
                    if (comp) {
                        if (j > 0) {
                            System.out.print(' ');
                        }
                        System.out.print(compactName(depName) + " " + compactState(depType) + " " + compactState(ComponentDependencyDeclaration.STATE_NAMES[depState]));
                    } else {
                        System.out.println("    " + depName + " " + depType + " " + ComponentDependencyDeclaration.STATE_NAMES[depState]);
                    }
                }
                if (comp) {
                    System.out.print(')');
                }
            }
        }
        if (comp) {
            System.out.println();
        }
    }
    if (stats) {
        System.out.println("Statistics:");
        System.out.println(" - Dependency managers: " + DependencyManager.getDependencyManagers().size());
        System.out.println(" - Components: " + numberOfComponents);
        if (!nodeps) {
            System.out.println(" - Dependencies: " + numberOfDependencies);
        }
    }
}
Also used : ComponentDeclaration(org.apache.felix.dm.ComponentDeclaration) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) ComponentDependencyDeclaration(org.apache.felix.dm.ComponentDependencyDeclaration) DependencyGraph(org.apache.felix.dm.diagnostics.DependencyGraph) StringTokenizer(java.util.StringTokenizer) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Component(org.apache.felix.dm.Component) Descriptor(org.apache.felix.service.command.Descriptor)

Example 2 with ComponentDependencyDeclaration

use of org.apache.felix.dm.ComponentDependencyDeclaration in project felix by apache.

the class FELIX4158_DependencyDeclarationTest method testResourceDependencyDeclarationWithFilter.

public void testResourceDependencyDeclarationWithFilter() {
    DependencyManager m = getDM();
    Component c = m.createComponent().setImplementation(new Object()).add(m.createResourceDependency().setFilter("(&(path=/path/to/*.txt)(host=localhost))"));
    ComponentDeclaration cd = c.getComponentDeclaration();
    ComponentDependencyDeclaration[] cdds = cd.getComponentDependencies();
    Assert.assertNotNull(cdds);
    Assert.assertNotNull(cdds.length == 1);
    Assert.assertEquals(cdds[0].getName(), ("(&(path=/path/to/*.txt)(host=localhost))"));
    Assert.assertNull(cdds[0].getSimpleName());
    Assert.assertNotNull(cdds[0].getFilter());
    Assert.assertEquals(cdds[0].getFilter(), "(&(path=/path/to/*.txt)(host=localhost))");
    m.clear();
}
Also used : ComponentDeclaration(org.apache.felix.dm.ComponentDeclaration) DependencyManager(org.apache.felix.dm.DependencyManager) ComponentDependencyDeclaration(org.apache.felix.dm.ComponentDependencyDeclaration) Component(org.apache.felix.dm.Component)

Example 3 with ComponentDependencyDeclaration

use of org.apache.felix.dm.ComponentDependencyDeclaration in project felix by apache.

the class FELIX4158_DependencyDeclarationTest method testBundleDependencyDeclarationWithFilter.

public void testBundleDependencyDeclarationWithFilter() throws MalformedURLException {
    DependencyManager m = getDM();
    Component c = m.createComponent().setImplementation(new Object()).add(m.createBundleDependency().setStateMask(Bundle.ACTIVE).setFilter("(DependencyManager-Component=*)"));
    ComponentDeclaration cd = c.getComponentDeclaration();
    ComponentDependencyDeclaration[] cdds = cd.getComponentDependencies();
    Assert.assertNotNull(cdds);
    Assert.assertNotNull(cdds.length == 1);
    Assert.assertEquals(cdds[0].getName(), "active (DependencyManager-Component=*)");
    Assert.assertNotNull(cdds[0].getSimpleName());
    Assert.assertEquals(cdds[0].getSimpleName(), "active");
    Assert.assertNotNull(cdds[0].getFilter());
    Assert.assertEquals(cdds[0].getFilter(), "(DependencyManager-Component=*)");
    m.clear();
}
Also used : ComponentDeclaration(org.apache.felix.dm.ComponentDeclaration) DependencyManager(org.apache.felix.dm.DependencyManager) ComponentDependencyDeclaration(org.apache.felix.dm.ComponentDependencyDeclaration) Component(org.apache.felix.dm.Component)

Example 4 with ComponentDependencyDeclaration

use of org.apache.felix.dm.ComponentDependencyDeclaration in project felix by apache.

the class FELIX4158_DependencyDeclarationTest method testBundleDependencyDeclaration.

public void testBundleDependencyDeclaration() throws MalformedURLException {
    DependencyManager m = getDM();
    Component c = m.createComponent().setImplementation(new Object()).add(m.createBundleDependency());
    ComponentDeclaration cd = c.getComponentDeclaration();
    ComponentDependencyDeclaration[] cdds = cd.getComponentDependencies();
    Assert.assertNotNull(cdds);
    Assert.assertNotNull(cdds.length == 1);
    Assert.assertEquals(cdds[0].getName(), "active installed resolved");
    Assert.assertNotNull(cdds[0].getSimpleName());
    Assert.assertEquals(cdds[0].getSimpleName(), "active installed resolved");
    Assert.assertNull(cdds[0].getFilter());
    m.clear();
}
Also used : ComponentDeclaration(org.apache.felix.dm.ComponentDeclaration) DependencyManager(org.apache.felix.dm.DependencyManager) ComponentDependencyDeclaration(org.apache.felix.dm.ComponentDependencyDeclaration) Component(org.apache.felix.dm.Component)

Example 5 with ComponentDependencyDeclaration

use of org.apache.felix.dm.ComponentDependencyDeclaration in project felix by apache.

the class FELIX4158_DependencyDeclarationTest method testBundleDependencyDeclarationWithMask.

public void testBundleDependencyDeclarationWithMask() throws MalformedURLException {
    DependencyManager m = getDM();
    Component c = m.createComponent().setImplementation(new Object()).add(m.createBundleDependency().setStateMask(Bundle.ACTIVE | Bundle.RESOLVED));
    ComponentDeclaration cd = c.getComponentDeclaration();
    ComponentDependencyDeclaration[] cdds = cd.getComponentDependencies();
    Assert.assertNotNull(cdds);
    Assert.assertNotNull(cdds.length == 1);
    Assert.assertEquals(cdds[0].getName(), "active resolved");
    Assert.assertNotNull(cdds[0].getSimpleName());
    Assert.assertEquals(cdds[0].getSimpleName(), "active resolved");
    Assert.assertNull(cdds[0].getFilter());
    m.clear();
}
Also used : ComponentDeclaration(org.apache.felix.dm.ComponentDeclaration) DependencyManager(org.apache.felix.dm.DependencyManager) ComponentDependencyDeclaration(org.apache.felix.dm.ComponentDependencyDeclaration) Component(org.apache.felix.dm.Component)

Aggregations

ComponentDependencyDeclaration (org.apache.felix.dm.ComponentDependencyDeclaration)10 Component (org.apache.felix.dm.Component)8 ComponentDeclaration (org.apache.felix.dm.ComponentDeclaration)8 DependencyManager (org.apache.felix.dm.DependencyManager)7 ArrayList (java.util.ArrayList)2 URL (java.net.URL)1 StringTokenizer (java.util.StringTokenizer)1 DependencyGraph (org.apache.felix.dm.diagnostics.DependencyGraph)1 Descriptor (org.apache.felix.service.command.Descriptor)1 Bundle (org.osgi.framework.Bundle)1 Filter (org.osgi.framework.Filter)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 LogService (org.osgi.service.log.LogService)1