@Override public Bundle addingBundle(final Bundle bundle, final BundleEvent event) { Collection<BundleRequirement> wiredRequirements = resolveWiredRequirements(bundle); if (wiredRequirements.size() == 0) { return null; } List<ComponentContainerInstance<?>> containers; try { containers = addBundleRequirements(bundle, wiredRequirements); } catch (RuntimeException e) { logService.log(LogService.LOG_ERROR, "Error during processing requirements of bundle: " + bundle.getSymbolicName() + ":" + bundle.getVersion(), e); return null; } activeComponentContainers.put(bundle, containers); return bundle; }
private Collection<BundleRequirement> resolveWiredRequirements(final Bundle bundle) { BundleWiring bundleWiring = bundle.adapt(BundleWiring.class); List<BundleRequirement> result = new ArrayList<>(); List<BundleWire> extenderWires = bundleWiring.getRequiredWires(ExtenderNamespace.EXTENDER_NAMESPACE); Bundle extenderBundle = context.getBundle(); for (BundleWire bundleWire : extenderWires) { if (extenderBundle.equals(bundleWire.getProviderWiring().getBundle())) { Map<String, Object> capabilityAttributes = bundleWire.getCapability().getAttributes(); if (ECMExtenderConstants.EXTENDER_SYMBOLIC_NAME .equals(capabilityAttributes.get(ExtenderNamespace.EXTENDER_NAMESPACE))) { BundleRequirement requirement = bundleWire.getRequirement(); result.add(requirement); } } } return result; }
private BundleInfo createMockBundle( long bundleId, BundleState bundleState, String[] missingDependencies, BundleContext bundleContext, BundleService bundleService ) { Bundle bundle = mock( Bundle.class ); when( bundle.getBundleId() ).thenReturn( bundleId ); String bundleName = WatchersTestUtils.getBundleName( bundleId ); when( bundle.getSymbolicName() ).thenReturn( bundleName ); org.apache.karaf.bundle.core.BundleInfo bundleInfo = mock( org.apache.karaf.bundle.core.BundleInfo.class ); when( bundleInfo.getState() ).thenReturn( bundleState ); when( bundleService.getInfo( bundle ) ).thenReturn( bundleInfo ); List<BundleRequirement> unsatisfiedRquirements = null; if ( missingDependencies != null ) { unsatisfiedRquirements = new ArrayList<BundleRequirement>(); for ( String missingDependency : missingDependencies ) { BundleRequirement requirement = mock( BundleRequirement.class ); when( requirement.toString() ).thenReturn( missingDependency ); unsatisfiedRquirements.add( requirement ); } } when( bundleService.getUnsatisfiedRquirements( bundle, null ) ).thenReturn( unsatisfiedRquirements ); BundleInfo featureBundleInfo = mock( BundleInfo.class ); when( featureBundleInfo.getLocation() ).thenReturn( bundleName ); when( bundleContext.getBundle( bundleName ) ).thenReturn( bundle ); return featureBundleInfo; }
/** * Generates a unique id for given bundle requirement * * @param bundleRequirement requirement * @return unique id in string */ public static String uniqueRequirementId(BundleRequirement bundleRequirement) { long bundleId = bundleRequirement.getRevision().getBundle().getBundleId(); String requirementString = ""; Object requirementName = bundleRequirement.getDirectives().get(REQUIREMENT_FILTER_ATTRIBUTE); if (requirementName != null) { // you never know requirementString = requirementName.toString(); } StringBuilder sb = new StringBuilder(); sb.append(bundleId); sb.append(PACKAGE_ID_SEPARATOR); sb.append(bundleRequirement.getNamespace()); sb.append(PACKAGE_ID_SEPARATOR); sb.append(requirementString); return sb.toString(); }
@Override public void filterMatches(BundleRequirement bundleRequirement, Collection<BundleCapability> bundleCapabilities) { Bundle bundle = bundleRequirement.getRevision().getBundle(); String symbolicName = bundle.getSymbolicName(); log.log(LogLevel.DEBUG, "Filtering matches for " + symbolicName); if (symbolicName.startsWith("com.fasterxml.jackson.jaxrs")) removeBundlesMatching(bundleCapabilities, JacksonJaxrsResolverHook::isJaxRs1Bundle); else if (symbolicName.equals("jackson-jaxrs") && bundle.getVersion().getMajor() == 1) { removeBundlesMatching(bundleCapabilities, JacksonJaxrsResolverHook::isJaxRs2Bundle); } }
private boolean hasRequirement(BundleWiring wiring, String packageName) { List<BundleRequirement> reqs = wiring.getRequirements(null); for (BundleRequirement req : reqs) { String filter = req.getDirectives().get(FILTER); if (filter.contains(packageName)) { return true; } } return false; }
private String getBundleReport( BundleService bundleService, List<BundleStateService> bundleStateServices, Bundle bundle ) { BundleState bundleState = bundleService.getInfo( bundle ).getState(); long bundleId = bundle.getBundleId(); String bundleName = bundle.getSymbolicName(); String bundleReport = "Bundle '" + bundleName + "':" + System.lineSeparator() + "\t Bundle State: " + bundleState + System .lineSeparator() + "\t Bundle ID: " + bundleId; // We loop through the available Bundle State Services and gather diagnostic information, if it exists. Usually, // there are two Bundle State Services, the BlueprintStateService and the SpringStateService. for ( BundleStateService bundleStateService : bundleStateServices ) { String part = bundleStateService.getDiag( bundle ); if ( part != null ) { bundleReport += bundleStateService.getName() + "\n"; bundleReport += part.replaceAll( "\n", "\n\t" ); } } // Unsatisfied Requirements for this bundle, includes optional requirements List<BundleRequirement> missingDependencies = bundleService.getUnsatisfiedRquirements( bundle, null ); if ( missingDependencies != null && missingDependencies.isEmpty() == false ) { bundleReport += System.lineSeparator() + "\t Unsatisfied Requirements:"; for ( BundleRequirement missDependency : missingDependencies ) { bundleReport += System.lineSeparator() + "\t\t" + missDependency; } } return bundleReport; }
/** * Write metadata for a bundle requirement * * @param metadataBuilder metadata builder object * @param bundleRequirement bundle requirement * @return the passed builder object with modifications */ public static ImmutableResourceMetadata.Builder metadataFrom(ImmutableResourceMetadata.Builder metadataBuilder, BundleRequirement bundleRequirement) { ImmutableResourceMetadata.Builder builder = new ImmutableResourceMetadata.Builder(); for (Map.Entry<String, Object> att : bundleRequirement.getAttributes().entrySet()) { builder.set(att.getKey(), att.getValue()); } metadataBuilder.set("attributes",builder.build()); builder = new ImmutableResourceMetadata.Builder(); for (Map.Entry<String, String> dir : bundleRequirement.getDirectives().entrySet()) { builder.set(dir.getKey(), dir.getValue()); } metadataBuilder.set("directives",builder.build()); return metadataBuilder; }
@Override public <A> A adaptTo(Class<A> clazz) { if (BundleRequirement.class.equals(clazz)) { return (A) m_requirement; } else if (BundleRequirementResource.class.equals(clazz)) { return (A) this; } else { return null; } }
private boolean requirementSatisfiable(final BundleRequirement requirement, final List<BundleCapability> availableCapabilities) { for (BundleCapability bundleCapability : availableCapabilities) { if (requirement.matches(bundleCapability)) { return true; } } return false; }
public MissingClassAttributeException(final BundleRequirement requirement) { super("Could not find required '" + ECMExtenderConstants.REQUIREMENT_ATTR_CLASS + "' attribute in requirement: " + requirement.toString()); }
public ComponentClassNotFoundException(final BundleRequirement requirement, final ClassNotFoundException cause) { super("The class defined in requirement could not be found: " + requirement.toString(), cause); }
private void printMissingRequirements(final Framework osgiContainer) { BundleContext systemBundleContext = osgiContainer.getBundleContext(); ServiceReference<PlatformAdmin> platformServiceSR = systemBundleContext .getServiceReference(PlatformAdmin.class); PlatformAdmin platformAdmin = systemBundleContext.getService(platformServiceSR); State state = platformAdmin.getState(); Bundle[] bundles = systemBundleContext.getBundles(); List<BundleCapability> availableCapabilities = getAllCapabilities(bundles, state); Map<Bundle, List<ImportPackageSpecification>> requiredMissingByBundle = new TreeMap<>(); Map<Bundle, List<ImportPackageSpecification>> optionalMissingByBundle = new TreeMap<>(); for (Bundle bundle : bundles) { if (bundle.getState() == Bundle.INSTALLED) { List<ImportPackageSpecification> requiredMissings = new ArrayList<>(); List<ImportPackageSpecification> optionalMissings = new ArrayList<>(); BundleDescription bundleDescription = state.getBundle(bundle.getBundleId()); ImportPackageSpecification[] allImports = bundleDescription.getImportPackages(); for (ImportPackageSpecification importPackage : allImports) { BundleRequirement requirement = importPackage.getRequirement(); if (!requirementSatisfiable(requirement, availableCapabilities)) { if (Constants.RESOLUTION_OPTIONAL .equals(requirement.getDirectives().get(Constants.RESOLUTION_DIRECTIVE))) { optionalMissings.add(importPackage); } else { requiredMissings.add(importPackage); } } } if (optionalMissings.size() > 0) { optionalMissingByBundle.put(bundle, optionalMissings); } if (requiredMissings.size() > 0) { requiredMissingByBundle.put(bundle, requiredMissings); } } } getLog().info("----- Missing required packages by bundles -----"); Set<String> requiredSum = printBundlesWithMissingImportsAndSummarize(requiredMissingByBundle); getLog().info(""); getLog().info(""); getLog().info("----- Missing optional packages by bundles -----"); Set<String> optionalSum = printBundlesWithMissingImportsAndSummarize(optionalMissingByBundle); getLog().info(""); getLog().info(""); getLog().info("----- Missing required packages (summary) -----"); printMissingSummary(requiredSum); getLog().info(""); getLog().info(""); getLog().info("----- Missing optional packages (summary) -----"); printMissingSummary(optionalSum); getLog().info(""); getLog().info(""); }