use of org.apache.felix.dm.diagnostics.DependencyGraph 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);
}
}
}
use of org.apache.felix.dm.diagnostics.DependencyGraph in project felix by apache.
the class DMCommand method wtf.
public void wtf() {
DependencyGraph graph = DependencyGraph.getGraph(ComponentState.UNREGISTERED, DependencyState.REQUIRED_UNAVAILABLE);
List<ComponentDeclaration> unregisteredComponents = graph.getAllComponents();
if (unregisteredComponents.isEmpty()) {
System.out.println("No unregistered components found");
} else {
String message = unregisteredComponents.size() + " unregistered components found";
System.out.println(message);
System.out.println("----------------------------------------------------".substring(0, message.length()));
}
listResolvedBundles();
listInstalledBundles();
List<CircularDependency> circularDependencies = graph.getCircularDependencies();
if (!circularDependencies.isEmpty()) {
System.out.println("Circular dependencies:");
printCircularDependencies(circularDependencies);
}
List<MissingDependency> missingConfigDependencies = graph.getMissingDependencies(CONFIGURATION);
if (!missingConfigDependencies.isEmpty()) {
System.out.println("The following configuration(s) are missing: ");
printMissingDependencies(missingConfigDependencies);
}
List<MissingDependency> missingServiceDependencies = graph.getMissingDependencies(SERVICE);
if (!missingServiceDependencies.isEmpty()) {
System.out.println("The following service(s) are missing: ");
printMissingDependencies(missingServiceDependencies);
}
List<MissingDependency> missingResourceDependencies = graph.getMissingDependencies(RESOURCE);
if (!missingResourceDependencies.isEmpty()) {
System.out.println("The following resource(s) are missing: ");
printMissingDependencies(missingResourceDependencies);
}
List<MissingDependency> missingBundleDependencies = graph.getMissingDependencies(BUNDLE);
if (!missingBundleDependencies.isEmpty()) {
System.out.println("The following bundle(s) are missing: ");
printMissingDependencies(missingBundleDependencies);
}
List<MissingDependency> missingCustomDependencies = graph.getMissingCustomDependencies();
if (!missingCustomDependencies.isEmpty()) {
System.out.println("The following custom dependency(ies) are missing: ");
printMissingCustomDependencies(missingCustomDependencies);
}
}
use of org.apache.felix.dm.diagnostics.DependencyGraph in project felix by apache.
the class DiagnosticsTest method testSingleComponent.
public void testSingleComponent() throws Exception {
DependencyManager dm = getDM();
Component component = dm.createComponent().setImplementation(Object.class);
dm.add(component);
DependencyGraph graph = DependencyGraph.getGraph(ComponentState.ALL, DependencyState.ALL);
assertTrue(checkComponentCount(1, graph.getAllComponents().size()));
assertTrue(graph.getAllDependencies().isEmpty());
graph = DependencyGraph.getGraph(ComponentState.UNREGISTERED, DependencyState.ALL_UNAVAILABLE);
assertTrue(graph.getAllComponents().isEmpty());
assertTrue(graph.getAllDependencies().isEmpty());
}
use of org.apache.felix.dm.diagnostics.DependencyGraph in project felix by apache.
the class DiagnosticsTest method testCircularDependencies.
public void testCircularDependencies() throws Exception {
DependencyManager dm = getDM();
Component component0 = dm.createComponent().setImplementation(C0.class).add(dm.createServiceDependency().setService(S1.class).setRequired(true));
Component component1 = dm.createComponent().setImplementation(S1Impl1.class).setInterface(S1.class.getName(), null).add(dm.createServiceDependency().setService(S2.class).setRequired(true));
Component component2 = dm.createComponent().setImplementation(S2Impl1.class).setInterface(S2.class.getName(), null).add(dm.createServiceDependency().setService(S1.class).setRequired(true));
m_dm.add(component0);
m_dm.add(component1);
m_dm.add(component2);
DependencyGraph graph = DependencyGraph.getGraph(ComponentState.UNREGISTERED, DependencyState.REQUIRED_UNAVAILABLE);
List<CircularDependency> circularDependencies = graph.getCircularDependencies();
assertEquals(1, circularDependencies.size());
List<ComponentDeclaration> circularDependencyComponents = circularDependencies.get(0).getComponents();
assertTrue(circularDependencyComponents.contains(component1));
assertTrue(circularDependencyComponents.contains(component2));
assertFalse(circularDependencyComponents.contains(component0));
}
use of org.apache.felix.dm.diagnostics.DependencyGraph in project felix by apache.
the class DiagnosticsTest method testConfigurationDependencyMissing.
public void testConfigurationDependencyMissing() throws Exception {
DependencyManager dm = getDM();
ConfigurationDependency configurationDependency1 = dm.createConfigurationDependency().setPid("missing.configuration.pid");
Component component1 = dm.createComponent().setImplementation(Object.class).add(configurationDependency1);
m_dm.add(component1);
DependencyGraph graph = DependencyGraph.getGraph(ComponentState.UNREGISTERED, DependencyState.REQUIRED_UNAVAILABLE);
assertEquals(1, graph.getAllComponents().size());
assertEquals(1, graph.getAllDependencies().size());
List<MissingDependency> missingServiceDependencies = graph.getMissingDependencies("service");
assertTrue(missingServiceDependencies.isEmpty());
List<MissingDependency> missingConfigDependencies = graph.getMissingDependencies("configuration");
assertEquals(1, missingConfigDependencies.size());
MissingDependency missingConfigDependency = missingConfigDependencies.get(0);
assertEquals("missing.configuration.pid", missingConfigDependency.getName());
}
Aggregations