org.eclipse.osgi_3.7.2.v20120110-1415

    }
      }
    }
    else
    {
      for (multipleRequireSupplierMaps = multipleImportSupplierList.iterator(); ((Iterator)multipleRequireSupplierMaps).hasNext();)
      {
        ResolverConstraint constraint = (ResolverConstraint)((Iterator)multipleRequireSupplierMaps).next();
        results.add(new ResolverConstraint[] { constraint });
      }
      for (multipleRequireSupplierMaps = multipleRequireSupplierList.iterator(); ((Iterator)multipleRequireSupplierMaps).hasNext();)
      {
        ResolverConstraint constraint = (ResolverConstraint)((Iterator)multipleRequireSupplierMaps).next();
        results.add(new ResolverConstraint[] { constraint });
      }
      for (multipleRequireSupplierMaps = multipleGenericSupplierList.iterator(); ((Iterator)multipleRequireSupplierMaps).hasNext();)
      {
        ResolverConstraint constraint = (ResolverConstraint)((Iterator)multipleRequireSupplierMaps).next();
        results.add(new ResolverConstraint[] { constraint });
      }
    }
    return (ResolverConstraint[][])results.toArray(new ResolverConstraint[results.size()][]);
  }
  
  String getSystemBundle()
  {
    Dictionary[] platformProperties = state.getPlatformProperties();
    String systemBundle = platformProperties.length == 0 ? null : (String)platformProperties[0].get("osgi.system.bundle");
    if (systemBundle == null) {
      systemBundle = Constants.getInternalSymbolicName();
    }
    return systemBundle;
  }
  
  private void addMergedSuppliers(List<ResolverConstraint[]> mergedSuppliers, Map<String, List<List<ResolverConstraint>>> constraints)
  {
    Iterator localIterator2;
    for (Iterator localIterator1 = constraints.values().iterator(); localIterator1.hasNext(); localIterator2.hasNext())
    {
      List<List<ResolverConstraint>> mergedConstraintLists = (List)localIterator1.next();
      localIterator2 = mergedConstraintLists.iterator(); continue;List<ResolverConstraint> constraintList = (List)localIterator2.next();
      mergedSuppliers.add((ResolverConstraint[])constraintList.toArray(new ResolverConstraint[constraintList.size()]));
    }
  }
  
  private void addMutipleSupplierConstraint(Map<String, List<List<ResolverConstraint>>> constraints, ResolverConstraint constraint, String key)
  {
    List<List<ResolverConstraint>> mergedConstraintLists = (List)constraints.get(key);
    if (mergedConstraintLists == null)
    {
      mergedConstraintLists = new ArrayList(0);
      List<ResolverConstraint> constraintList = new ArrayList(1);
      constraintList.add(constraint);
      mergedConstraintLists.add(constraintList);
      constraints.put(key, mergedConstraintLists);
      return;
    }
    for (Iterator localIterator = mergedConstraintLists.iterator(); localIterator.hasNext();)
    {
      List<ResolverConstraint> constraintList = (List)localIterator.next();
      ResolverConstraint mergedConstraint = (ResolverConstraint)constraintList.get(0);
      VersionSupplier[] suppliers1 = constraint.getPossibleSuppliers();
      VersionSupplier[] suppliers2 = mergedConstraint.getPossibleSuppliers();
      if (suppliers1.length == suppliers2.length)
      {
        for (int i = 0; i < suppliers1.length; i++) {
          if (suppliers1[i] == suppliers2[i]) {}
        }
        constraintList.add(constraint);
        return;
      }
    }
    List<ResolverConstraint> constraintList = new ArrayList(1);
    constraintList.add(constraint);
    mergedConstraintLists.add(constraintList);
  }
  
  private void checkCycle(List<ResolverBundle> cycle)
  {
    int cycleSize = cycle.size();
    if (cycleSize == 0) {
      return;
    }
    for (Iterator<ResolverBundle> iCycle = cycle.iterator(); iCycle.hasNext();)
    {
      ResolverBundle cycleBundle = (ResolverBundle)iCycle.next();
      if (!cycleBundle.isResolvable())
      {
        iCycle.remove();
      }
      else
      {
        ResolverImport[] imports = cycleBundle.getImportPackages();
        for (int j = 0; j < imports.length; j++)
        {
          while (imports[j].getSelectedSupplier() != null)
          {
            ResolverExport importSupplier = (ResolverExport)imports[j].getSelectedSupplier();
            if (importSupplier.getSubstitute() == null) {
              break;
            }
            imports[j].selectNextSupplier();
          }
          if ((!imports[j].isDynamic()) && (!imports[j].isOptional()) && (imports[j].getSelectedSupplier() == null))
          {
            cycleBundle.setResolvable(false);
            state.addResolverError(imports[j].getVersionConstraint().getBundle(), 1, imports[j].getVersionConstraint().toString(), imports[j].getVersionConstraint());
            iCycle.remove();
            break;
          }
        }
      }
    }
    if (cycle.size() != cycleSize)
    {
      for (int i = 0; i < cycle.size(); i++)
      {
        ResolverBundle cycleBundle = (ResolverBundle)cycle.get(i);
        cycleBundle.clearWires();
      }
      List<ResolverBundle> innerCycle = new ArrayList(cycle.size());
      for (int i = 0; i < cycle.size(); i++) {
        resolveBundle((ResolverBundle)cycle.get(i), innerCycle);
      }
      checkCycle(innerCycle);
    }
    else
    {
      for (int i = 0; i < cycle.size(); i++)
      {
        if ((DEBUG) || (DEBUG_CYCLES)) {
          log("Pushing " + cycle.get(i) + " to RESOLVED");
        }
        setBundleResolved((ResolverBundle)cycle.get(i));
      }
    }
  }
  
  static Collection<BundleCapability> asCapabilities(Collection<? extends BundleCapability> capabilities)
  {
    return capabilities;
  }
  
  private void resolveFragment(ResolverBundle fragment)
  {
    if (!fragment.isFragment()) {
      return;
    }
    if ((fragment.getHost().getNumPossibleSuppliers() > 0) && (
      (!developmentMode) || (state.getResolverErrors(fragment.getBundleDescription()).length == 0))) {
      setBundleResolved(fragment);
    }
  }
  
  private boolean resolveBundle(ResolverBundle bundle, List<ResolverBundle> cycle)
  {
    if (bundle.isFragment()) {
      return false;
    }
    if (!bundle.isResolvable())
    {
      if (DEBUG) {
        log("  - " + bundle + " is unresolvable");
      }
      return false;
    }
    switch (bundle.getState())
    {
    case 2: 
      if (DEBUG) {
        log("  - " + bundle + " already resolved");
      }
      return true;
    case 0: 
      bundle.clearWires();
      setBundleResolving(bundle);
      break;
    case 1: 
      if (cycle.contains(bundle)) {
        return true;
      }
      break;
    }
    boolean failed = false;
    if (!failed)
    {
      GenericConstraint[] genericRequires = bundle.getGenericRequires();
      for (int i = 0; i < genericRequires.length; i++) {
        if (!resolveGenericReq(genericRequires[i], cycle))
        {
          if ((DEBUG) || (DEBUG_GENERICS)) {
            log("** GENERICS " + genericRequires[i].getVersionConstraint().getName() + "[" + genericRequires[i].getBundleDescription() + "] failed to resolve");
          }
          state.addResolverError(genericRequires[i].getVersionConstraint().getBundle(), 32768, genericRequires[i].getVersionConstraint().toString(), genericRequires[i].getVersionConstraint());
          if (genericRequires[i].isFromFragment())
          {
            if (!developmentMode) {
              bundle.detachFragment((ResolverBundle)bundleMapping.get(genericRequires[i].getVersionConstraint().getBundle()), null);
            }
          }
          else if (!developmentMode)
          {
            failed = true;
            break;
          }
        }
      }
    }
    if (!failed)
    {
      BundleConstraint[] requires = bundle.getRequires();
      for (int i = 0; i < requires.length; i++) {
        if (!resolveRequire(requires[i], cycle))
        {
          if ((DEBUG) || (DEBUG_REQUIRES)) {
            log("** REQUIRE " + requires[i].getVersionConstraint().getName() + "[" + requires[i].getBundleDescription() + "] failed to resolve");
          }
          state.addResolverError(requires[i].getVersionConstraint().getBundle(), 2, requires[i].getVersionConstraint().toString(), requires[i].getVersionConstraint());
          if (requires[i].isFromFragment())
          {
            if (!developmentMode) {
              bundle.detachFragment((ResolverBundle)bundleMapping.get(requires[i].getVersionConstraint().getBundle()), requires[i]);
            }
          }
          else if (!developmentMode)
          {
            failed = true;
            break;
          }
        }
      }
    }
    if (!failed)
    {
      ResolverImport[] imports = bundle.getImportPackages();
      for (int i = 0; i < imports.length; i++) {
        if ((!imports[i].isDynamic()) && (!resolveImport(imports[i], cycle)))
        {
          if ((DEBUG) || (DEBUG_IMPORTS)) {
            log("** IMPORT " + imports[i].getName() + "[" + imports[i].getBundleDescription() + "] failed to resolve");
          }
          state.addResolverError(imports[i].getVersionConstraint().getBundle(), 1, imports[i].getVersionConstraint().toString(), imports[i].getVersionConstraint());
          if (imports[i].isFromFragment())
          {
            if (!developmentMode) {
              bundle.detachFragment((ResolverBundle)bundleMapping.get(imports[i].getVersionConstraint().getBundle()), imports[i]);
            }
          }
          else if (!developmentMode)
          {
            failed = true;
            break;
          }
        }
      }
    }
    checkFragmentConstraints(bundle);
    if ((developmentMode) && (!failed) && (state.getResolverErrors(bundle.getBundleDescription()).length > 0)) {
      failed = true;
    }
    if (failed)
    {
      setBundleUnresolved(bundle, false, developmentMode);
      if (DEBUG) {
        log(bundle + " NOT RESOLVED");
      }
    }
    else if (!cycle.contains(bundle))
    {
      setBundleResolved(bundle);
      if (DEBUG) {
        log(bundle + " RESOLVED");
      }
    }
    if (bundle.getState() == 0) {
      bundle.setResolvable(false);
    }
    return bundle.getState() != 0;
  }
  
  private void checkFragmentConstraints(ResolverBundle bundle)
  {
    ResolverBundle[] fragments = bundle.getFragments();
    for (int i = 0; i < fragments.length; i++)
    {
      BundleDescription fragment = fragments[i].getBundleDescription();
      if ((bundle.constraintsConflict(fragment, fragment.getImportPackages(), fragment.getRequiredBundles(), fragment.getGenericRequires())) && (!developmentMode)) {
        bundle.detachFragment(fragments[i], null);
      }
    }
  }
  
  private boolean resolveGenericReq(GenericConstraint constraint, List<ResolverBundle> cycle)
  {
    if (DEBUG_GENERICS) {
      log("Trying to resolve: " + constraint.getBundle() + ", " + constraint.getVersionConstraint());
    }
    VersionSupplier matchingCapability = constraint.getSelectedSupplier();
    if (matchingCapability != null)
    {
      if (!cycle.contains(constraint.getBundle()))
      {
        cycle.add(constraint.getBundle());
        if (DEBUG_CYCLES) {
          log("generic cycle: " + constraint.getBundle() + " -> " + constraint.getSelectedSupplier());
        }
      }
      if (DEBUG_GENERICS) {
        log("  - already wired");
      }
      return true;
    }
    VersionHashMap<GenericCapability> namespace = (VersionHashMap)resolverGenerics.get(constraint.getNameSpace());
    String name = constraint.getName();
    List<GenericCapability> capabilities;
    List<GenericCapability> capabilities;
    if (namespace == null) {
      capabilities = Collections.EMPTY_LIST;
    } else {
      capabilities = (name == null) || ("*".equals(name)) ? namespace.getAllValues() : namespace.get(name);
    }
    List<GenericCapability> candidates = new ArrayList(capabilities);
    List<BundleCapability> genCapabilities = new ArrayList(candidates.size());
    for (Iterator<GenericCapability> iCandidates = candidates.iterator(); iCandidates.hasNext();)
    {
      GenericCapability capability = (GenericCapability)iCandidates.next();
      if (!constraint.isSatisfiedBy(capability)) {
        iCandidates.remove();
      } else {
        genCapabilities.add(capability.getCapability());
      }
    }
    if (hook != null) {
      hook.filterMatches(constraint.getRequirement(), asCapabilities(new ArrayMap(genCapabilities, candidates)));
    }
    boolean result = false;
    for (Iterator localIterator = candidates.iterator(); localIterator.hasNext();)
    {
      GenericCapability capability = (GenericCapability)localIterator.next();
      if (DEBUG_GENERICS) {
        log("CHECKING GENERICS: " + capability.getBaseDescription());
      }
      constraint.addPossibleSupplier(capability);
      if (constraint.getBundle() == capability.getResolverBundle())
      {
        result = true;
      }
      else
      {
        ResolverBundle supplier = capability.getResolverBundle();
        if ((supplier.getState() == 2) || (resolveBundle(supplier, cycle)) || (developmentMode))
        {
          if ((supplier.getState() == 1) && 
            (!cycle.contains(supplier))) {
            cycle.add(supplier);
          }
        }
        else
        {
          constraint.removePossibleSupplier(capability);
          continue;
        }
        if (DEBUG_GENERICS) {
          log("Found match: " + capability.getBaseDescription() + ". Wiring");
        }
        result = true;
      }
    }
    return result ? true : constraint.isOptional();
  }
  
  private boolean resolveRequire(BundleConstraint req, List<ResolverBundle> cycle)
  {
    if (DEBUG_REQUIRES) {
      log("Trying to resolve: " + req.getBundle() + ", " + req.getVersionConstraint());
    }
    if (req.getSelectedSupplier() != null)
    {
      if (!cycle.contains(req.getBundle()))
      {
        cycle.add(req.getBundle());
        if (DEBUG_CYCLES) {
          log("require-bundle cycle: " + req.getBundle() + " -> " + req.getSelectedSupplier());
        }
      }
      if (DEBUG_REQUIRES) {
        log("  - already wired");
      }
      return true;
    }
    List<ResolverBundle> bundles = resolverBundles.get(req.getVersionConstraint().getName());
    List<ResolverBundle> candidates = new ArrayList(bundles);
    List<BundleCapability> capabilities = new ArrayList(candidates.size());
    for (Iterator<ResolverBundle> iCandidates = candidates.iterator(); iCandidates.hasNext();)
    {
      ResolverBundle bundle = (ResolverBundle)iCandidates.next();
      if (!req.isSatisfiedBy(bundle)) {
        iCandidates.remove();
      } else {
        capabilities.add(bundle.getCapability());
      }
    }
    if (hook != null) {
      hook.filterMatches(req.getRequirement(), asCapabilities(new ArrayMap(capabilities, candidates)));
    }
    boolean result = false;
    for (Iterator localIterator = candidates.iterator(); localIterator.hasNext();)
    {
      ResolverBundle bundle = (ResolverBundle)localIterator.next();
      if (DEBUG_REQUIRES) {
        log("CHECKING: " + bundle.getBundleDescription());
      }
      req.addPossibleSupplier(bundle);
      if (req.getBundle() != bundle) {
        if ((bundle.getState() != 2) && (!resolveBundle(bundle, cycle)) && (!developmentMode))
        {
          req.removePossibleSupplier(bundle);
          continue;
        }
      }
      if ((req.getBundle() != bundle) && 
        (bundle.getState() == 1)) {
        if (!cycle.contains(req.getBundle()))
        {
          cycle.add(req.getBundle());
          if (DEBUG_CYCLES) {
            log("require-bundle cycle: " + req.getBundle() + " -> " + req.getSelectedSupplier());
          }
        }
      }
      if (DEBUG_REQUIRES) {
        log("Found match: " + bundle.getBundleDescription() + ". Wiring");
      }
      result = true;
    }
    if ((result) || (req.isOptional())) {
      return true;
    }
    return false;
  }
  
  private boolean resolveImport(ResolverImport imp, List<ResolverBundle> cycle)
  {
    if (DEBUG_IMPORTS) {
      log("Trying to resolve: " + imp.getBundle() + ", " + imp.getName());
    }
    if (imp.getSelectedSupplier() != null)
    {
      if (!cycle.contains(imp.getBundle()))
      {
        cycle.add(imp.getBundle());
        if (DEBUG_CYCLES) {
          log("import-package cycle: " + imp.getBundle() + " -> " + imp.getSelectedSupplier() + " from " + imp.getSelectedSupplier().getBundleDescription());
        }
      }
      if (DEBUG_IMPORTS) {
        log("  - already wired");
      }
      return true;
    }
    boolean result = false;
    ResolverExport[] substitutableExps = imp.getBundle().getExports(imp.getName());
    List<ResolverExport> exports = resolverExports.get(imp.getName());
    List<ResolverExport> candidates = new ArrayList(exports);
    List<BundleCapability> capabilities = new ArrayList(candidates.size());
    for (Iterator<ResolverExport> iCandidates = candidates.iterator(); iCandidates.hasNext();)
    {
      export = (ResolverExport)iCandidates.next();
      if (!imp.isSatisfiedBy(export)) {
        iCandidates.remove();
      } else {
        capabilities.add(export.getCapability());
      }
    }
    if (hook != null) {
      hook.filterMatches(imp.getRequirement(), asCapabilities(new ArrayMap(capabilities, candidates)));
    }
    for (ResolverExport export = candidates.iterator(); export.hasNext();)
    {
      ResolverExport export = (ResolverExport)export.next();
      if (DEBUG_IMPORTS) {
        log("CHECKING: " + export.getExporter().getBundleDescription() + ", " + export.getName());
      }
      int originalState = export.getExporter().getState();
      if ((!imp.isDynamic()) || (originalState == 2))
      {
        if ((imp.getSelectedSupplier() != null) && (((ResolverExport)imp.getSelectedSupplier()).getExporter() == imp.getBundle())) {
          break;
        }
        imp.addPossibleSupplier(export);
        if (imp.getBundle() != export.getExporter())
        {
          for (int j = 0; j < substitutableExps.length; j++) {
            if (substitutableExps[j].getSubstitute() == null) {
              substitutableExps[j].setSubstitute(export);
            }
          }
          if (((originalState != 2) && (!resolveBundle(export.getExporter(), cycle)) && (!developmentMode)) || (export.getSubstitute() != null))
          {
            imp.removePossibleSupplier(export);
            if (imp.getSelectedSupplier() != null) {
              continue;
            }
            for (int j = 0; j < substitutableExps.length; j++) {
              if (substitutableExps[j].getSubstitute() == export) {
                substitutableExps[j].setSubstitute(null);
              }
            }
            continue;
          }
        }
        else
        {
          if (export.getSubstitute() != null) {
            continue;
          }
        }
        if ((imp.getBundle() != export.getExporter()) && 
          (export.getExporter().getState() == 1)) {
          if (!cycle.contains(imp.getBundle()))
          {
            cycle.add(imp.getBundle());
            if (DEBUG_CYCLES) {
              log("import-package cycle: " + imp.getBundle() + " -> " + imp.getSelectedSupplier() + " from " + imp.getSelectedSupplier().getBundleDescription());
            }
          }
        }
        if (DEBUG_IMPORTS) {
          log("Found match: " + export.getExporter() + ". Wiring " + imp.getBundle() + ":" + imp.getName());
        }
        result = true;
      }
    }
    if (result) {
      return true;
    }
    if (imp.isOptional()) {
      return true;
    }
    if ((substitutableExps.length > 0) && (substitutableExps[0].getSubstitute() == null)) {
      return true;
    }
    return false;
  }
  
  private void setBundleUnresolved(ResolverBundle bundle, boolean removed, boolean keepFragsAttached)
  {
    if ((bundle.getState() == 0) && (!developmentMode)) {
      return;
    }
    if ((removed) || (!keepFragsAttached))
    {
      resolverExports.remove(bundle.getExportPackages());
      removeGenerics(bundle.getGenericCapabilities());
      bundle.detachAllFragments();
      bundle.initialize(false);
      if (!removed)
      {
        resolverExports.put(bundle.getExportPackages());
        addGenerics(bundle.getGenericCapabilities());
      }
    }
    if ((!removed) && ((!developmentMode) || (!unresolvedBundles.contains(bundle)))) {
      unresolvedBundles.add(bundle);
    }
    bundle.setState(0);
  }
  
  private void setBundleResolved(ResolverBundle bundle)
  {
    if (bundle.getState() == 2) {
      return;
    }
    unresolvedBundles.remove(bundle);
    bundle.setState(2);
  }
  
  private void setBundleResolving(ResolverBundle bundle)
  {
    if (bundle.getState() == 1) {
      return;
    }
    unresolvedBundles.remove(bundle);
    bundle.setState(1);
  }
  
  private void stateResolveBundles(ResolverBundle[] resolvedBundles)
  {
    for (int i = 0; i < resolvedBundles.length; i++) {
      if (!resolvedBundles[i].getBundleDescription().isResolved()) {
        stateResolveBundle(resolvedBundles[i]);
      }
    }
  }
  
  private void stateResolveConstraints(ResolverBundle rb)
  {
    ResolverImport[] imports = rb.getImportPackages();
    for (int i = 0; i < imports.length; i++)
    {
      ResolverExport export = (ResolverExport)imports[i].getSelectedSupplier();
      BaseDescription supplier = export == null ? null : export.getExportPackageDescription();
      state.resolveConstraint(imports[i].getVersionConstraint(), supplier);
    }
    BundleConstraint[] requires = rb.getRequires();
    for (int i = 0; i < requires.length; i++)
    {
      ResolverBundle bundle = (ResolverBundle)requires[i].getSelectedSupplier();
      BaseDescription supplier = bundle == null ? null : bundle.getBundleDescription();
      state.resolveConstraint(requires[i].getVersionConstraint(), supplier);
    }
    GenericConstraint[] genericRequires = rb.getGenericRequires();
    for (int i = 0; i < genericRequires.length; i++)
    {
      VersionSupplier[] matchingCapabilities = genericRequires[i].getMatchingCapabilities();
      if (matchingCapabilities == null) {
        state.resolveConstraint(genericRequires[i].getVersionConstraint(), null);
      } else {
        for (int j = 0; j < matchingCapabilities.length; j++) {
          state.resolveConstraint(genericRequires[i].getVersionConstraint(), matchingCapabilities[j].getBaseDescription());
        }
      }
    }
  }
  
  private void stateResolveFragConstraints(ResolverBundle rb)
  {
    ResolverBundle host = (ResolverBundle)rb.getHost().getSelectedSupplier();
    ImportPackageSpecification[] imports = rb.getBundleDescription().getImportPackages();
    for (int i = 0; i < imports.length; i++)
    {
      ResolverImport hostImport = host == null ? null : host.getImport(imports[i].getName());
      ResolverExport export = (ResolverExport)(hostImport == null ? null : hostImport.getSelectedSupplier());
      BaseDescription supplier = export == null ? null : export.getExportPackageDescription();
      state.resolveConstraint(imports[i], supplier);
    }
    BundleSpecification[] requires = rb.getBundleDescription().getRequiredBundles();
    for (int i = 0; i < requires.length; i++)
    {
      BundleConstraint hostRequire = host == null ? null : host.getRequire(requires[i].getName());
      ResolverBundle bundle = (ResolverBundle)(hostRequire == null ? null : hostRequire.getSelectedSupplier());
      BaseDescription supplier = bundle == null ? null : bundle.getBundleDescription();
      state.resolveConstraint(requires[i], supplier);
    }
  }
  
  private void stateResolveBundle(ResolverBundle rb)
  {
    if ((!rb.isResolved()) && (!developmentMode)) {
      return;
    }
    if (rb.isFragment()) {
      stateResolveFragConstraints(rb);
    } else {
      stateResolveConstraints(rb);
    }
    Map<String, List<StateWire>> stateWires = new HashMap();
    
    ResolverExport[] exports = rb.getSelectedExports();
    List<ExportPackageDescription> selectedExports = new ArrayList(exports.length);
    for (int i = 0; i < exports.length; i++) {
      if (permissionChecker.checkPackagePermission(exports[i].getExportPackageDescription())) {
        selectedExports.add(exports[i].getExportPackageDescription());
      }
    }
    ExportPackageDescription[] selectedExportsArray = (ExportPackageDescription[])selectedExports.toArray(new ExportPackageDescription[selectedExports.size()]);
    
    ResolverExport[] substituted = rb.getSubstitutedExports();
    List<ExportPackageDescription> substitutedExports = new ArrayList(substituted.length);
    for (int i = 0; i < substituted.length; i++) {
      substitutedExports.add(substituted[i].getExportPackageDescription());
    }
    ExportPackageDescription[] substitutedExportsArray = (ExportPackageDescription[])substitutedExports.toArray(new ExportPackageDescription[substitutedExports.size()]);
    
    ExportPackageDescription[] exportsWiredToArray = getExportsWiredTo(rb, stateWires);
    
    BundleConstraint[] requires = rb.getRequires();
    List<BundleDescription> bundlesWiredTo = new ArrayList(requires.length);
    List<StateWire> requireWires = new ArrayList(requires.length);
    for (int i = 0; i < requires.length; i++) {
      if (requires[i].getSelectedSupplier() != null)
      {
        BundleDescription supplier = (BundleDescription)requires[i].getSelectedSupplier().getBaseDescription();
        bundlesWiredTo.add(supplier);
        StateWire requireWire = newStateWire(rb.getBundleDescription(), requires[i].getVersionConstraint(), supplier, supplier);
        requireWires.add(requireWire);
      }
    }
    BundleDescription[] bundlesWiredToArray = (BundleDescription[])bundlesWiredTo.toArray(new BundleDescription[bundlesWiredTo.size()]);
    if (!requireWires.isEmpty()) {
      stateWires.put("osgi.wiring.bundle", requireWires);
    }
    GenericCapability[] capabilities = rb.getGenericCapabilities();
    List<GenericDescription> selectedCapabilities = new ArrayList(capabilities.length);
    GenericCapability[] arrayOfGenericCapability1;
    int j = (arrayOfGenericCapability1 = capabilities).length;
    for (int i = 0; i < j; i++)
    {
      GenericCapability capability = arrayOfGenericCapability1[i];
      if (permissionChecker.checkCapabilityPermission(capability.getGenericDescription())) {
        selectedCapabilities.add(capability.getGenericDescription());
      }
    }
    GenericDescription[] selectedCapabilitiesArray = (GenericDescription[])selectedCapabilities.toArray(new GenericDescription[selectedCapabilities.size()]);
    
    GenericConstraint[] genericRequires = rb.getGenericRequires();
    Object resolvedGenericRequires = new ArrayList(genericRequires.length);
    GenericConstraint[] arrayOfGenericConstraint1;
    int m = (arrayOfGenericConstraint1 = genericRequires).length;
    for (int k = 0; k < m; k++)
    {
      GenericConstraint genericConstraint = arrayOfGenericConstraint1[k];
      VersionSupplier[] matching = genericConstraint.getMatchingCapabilities();
      if (matching != null)
      {
        VersionSupplier[] arrayOfVersionSupplier1;
        int i1 = (arrayOfVersionSupplier1 = matching).length;
        for (int n = 0; n < i1; n++)
        {
          VersionSupplier capability = arrayOfVersionSupplier1[n];
          GenericDescription supplier = ((GenericCapability)capability).getGenericDescription();
          ((List)resolvedGenericRequires).add(supplier);
          StateWire genericWire = newStateWire(rb.getBundleDescription(), genericConstraint.getVersionConstraint(), supplier.getSupplier(), supplier);
          List<StateWire> genericWires = (List)stateWires.get(genericConstraint.getNameSpace());
          if (genericWires == null)
          {
            genericWires = new ArrayList();
            stateWires.put(genericConstraint.getNameSpace(), genericWires);
          }
          genericWires.add(genericWire);
        }
      }
    }
    GenericDescription[] capabilitiesWiredToArray = (GenericDescription[])((List)resolvedGenericRequires).toArray(new GenericDescription[((List)resolvedGenericRequires).size()]);
    
    BundleDescription[] hostBundles = (BundleDescription[])null;
    if (rb.isFragment())
    {
      VersionSupplier[] matchingBundles = rb.getHost().getPossibleSuppliers();
      if ((matchingBundles != null) && (matchingBundles.length > 0))
      {
        hostBundles = new BundleDescription[matchingBundles.length];
        Object hostWires = new ArrayList(matchingBundles.length);
        stateWires.put("osgi.wiring.host", hostWires);
        for (int i = 0; i < matchingBundles.length; i++)
        {
          hostBundles[i] = matchingBundles[i].getBundleDescription();
          StateWire hostWire = newStateWire(rb.getBundleDescription(), rb.getHost().getVersionConstraint(), hostBundles[i], hostBundles[i]);
          ((List)hostWires).add(hostWire);
          if (hostBundles[i].isResolved())
          {
            ExportPackageDescription[] newSelectedExports = (ExportPackageDescription[])null;
            GenericDescription[] newSelectedCapabilities = (GenericDescription[])null;
            if (rb.isNewFragmentExports())
            {
              ResolverExport[] hostExports = ((ResolverBundle)matchingBundles[i]).getSelectedExports();
              newSelectedExports = new ExportPackageDescription[hostExports.length];
              for (int j = 0; j < hostExports.length; j++) {
                newSelectedExports[j] = hostExports[j].getExportPackageDescription();
              }
            }
            if (rb.isNewFragmentCapabilities())
            {
              GenericCapability[] hostCapabilities = ((ResolverBundle)matchingBundles[i]).getGenericCapabilities();
              newSelectedCapabilities = new GenericDescription[hostCapabilities.length];
              for (int j = 0; j < hostCapabilities.length; j++) {
                newSelectedCapabilities[j] = hostCapabilities[j].getGenericDescription();
              }
            }
            if ((newSelectedCapabilities != null) || (newSelectedExports != null))
            {
              if (newSelectedCapabilities == null) {
                newSelectedCapabilities = hostBundles[i].getSelectedGenericCapabilities();
              }
              if (newSelectedExports == null) {
                newSelectedExports = hostBundles[i].getSelectedExports();
              }
              state.resolveBundle(hostBundles[i], true, null, newSelectedExports, hostBundles[i].getSubstitutedExports(), newSelectedCapabilities, hostBundles[i].getResolvedRequires(), hostBundles[i].getResolvedImports(), hostBundles[i].getResolvedGenericRequires(), ((BundleDescriptionImpl)hostBundles[i]).getWires());
            }
          }
        }
      }
    }
    state.resolveBundle(rb.getBundleDescription(), rb.isResolved(), hostBundles, selectedExportsArray, substitutedExportsArray, selectedCapabilitiesArray, bundlesWiredToArray, exportsWiredToArray, capabilitiesWiredToArray, stateWires);
  }
  
  private static ExportPackageDescription[] getExportsWiredTo(ResolverBundle rb, Map<String, List<StateWire>> stateWires)
  {
    ResolverImport[] imports = rb.getImportPackages();
    List<ExportPackageDescription> exportsWiredTo = new ArrayList(imports.length);
    List<StateWire> importWires = new ArrayList(imports.length);
    for (int i = 0; i < imports.length; i++) {
      if (imports[i].getSelectedSupplier() != null)
      {
        ExportPackageDescription supplier = (ExportPackageDescription)imports[i].getSelectedSupplier().getBaseDescription();
        exportsWiredTo.add(supplier);
        StateWire wire = newStateWire(rb.getBundleDescription(), imports[i].getVersionConstraint(), supplier.getExporter(), supplier);
        importWires.add(wire);
      }
    }
    if ((stateWires != null) && (!importWires.isEmpty())) {
      stateWires.put("osgi.wiring.package", importWires);
    }
    return (ExportPackageDescription[])exportsWiredTo.toArray(new ExportPackageDescription[exportsWiredTo.size()]);
  }
  
  private static StateWire newStateWire(BundleDescription requirementHost, VersionConstraint declaredRequirement, BundleDescription capabilityHost, BaseDescription declaredCapability)
  {
    BaseDescription fragDeclared = ((BaseDescriptionImpl)declaredCapability).getFragmentDeclaration();
    declaredCapability = fragDeclared != null ? fragDeclared : declaredCapability;
    return new StateWire(requirementHost, declaredRequirement, capabilityHost, declaredCapability);
  }
  
  public synchronized ExportPackageDescription resolveDynamicImport(BundleDescription importingBundle, String requestedPackage)
  {
    if (state == null) {
      throw new IllegalStateException("RESOLVER_NO_STATE");
    }
    if (!initialized) {
      initialize();
    }
    hook = ((state instanceof StateImpl) ? ((StateImpl)state).getResolverHook() : null);
    try
    {
      ResolverBundle rb = (ResolverBundle)bundleMapping.get(importingBundle);
      if (rb.getExport(requestedPackage) != null) {
        return null;
      }
      ResolverImport[] resolverImports = rb.getImportPackages();
      ExportPackageDescription localExportPackageDescription1;
      for (int j = 0; j < resolverImports.length; j++) {
        if (resolverImports[j].isDynamic())
        {
          ExportPackageDescription supplier = resolveDynamicImport(resolverImports[j], requestedPackage);
          if (supplier != null) {
            return supplier;
          }
        }
      }
      ImportPackageSpecification[] addedDynamicImports = importingBundle.getAddedDynamicImportPackages();
      ImportPackageSpecification[] arrayOfImportPackageSpecification1;
      int j = (arrayOfImportPackageSpecification1 = addedDynamicImports).length;
      for (int i = 0; i < j; i++)
      {
        ImportPackageSpecification addedDynamicImport = arrayOfImportPackageSpecification1[i];
        ResolverImport newImport = new ResolverImport(rb, addedDynamicImport);
        ExportPackageDescription supplier = resolveDynamicImport(newImport, requestedPackage);
        if (supplier != null) {
          return supplier;
        }
      }
      if ((DEBUG) || (DEBUG_IMPORTS)) {
        log("Failed to resolve dynamic import: " + requestedPackage);
      }
      return null;
    }
    finally
    {
      hook = null;
    }
  }
  
  private void addStateWire(BundleDescription importingBundle, VersionConstraint requirement, BundleDescription capabilityHost, ExportPackageDescription capability)
  {
    Map<String, List<StateWire>> wires = ((BundleDescriptionImpl)importingBundle).getWires();
    List<StateWire> imports = (List)wires.get("osgi.wiring.package");
    if (imports == null)
    {
      imports = new ArrayList();
      wires.put("osgi.wiring.package", imports);
    }
    imports.add(newStateWire(importingBundle, requirement, capabilityHost, capability));
  }
  
  private ExportPackageDescription resolveDynamicImport(ResolverImport dynamicImport, String requestedPackage)
  {
    String importName = dynamicImport.getName();
    if ((importName.equals("*")) || (
      (importName.endsWith(".*")) && (requestedPackage.startsWith(importName.substring(0, importName.length() - 1))))) {
      dynamicImport.setName(requestedPackage);
    }
    try
    {
      if (!requestedPackage.equals(dynamicImport.getName())) {
        return null;
      }
      if (resolveImport(dynamicImport, new ArrayList()))
      {
        groupingChecker.populateRoots(dynamicImport.getBundle());
        while (dynamicImport.getSelectedSupplier() != null) {
          if (groupingChecker.isDynamicConsistent(dynamicImport.getBundle(), (ResolverExport)dynamicImport.getSelectedSupplier()) != null)
          {
            dynamicImport.selectNextSupplier();
          }
          else
          {
            if (DEBUG_IMPORTS) {
              log("Resolved dynamic import: " + dynamicImport.getBundle() + ":" + dynamicImport.getName() + " -> " + ((ResolverExport)dynamicImport.getSelectedSupplier()).getExporter() + ":" + requestedPackage);
            }
            ResolverExport export = (ResolverExport)dynamicImport.getSelectedSupplier();
            groupingChecker.populateRoots(dynamicImport.getBundle(), export);
            
            ExportPackageDescription supplier = export.getExportPackageDescription();
            if (supplier != null) {
              addStateWire(dynamicImport.getBundleDescription(), dynamicImport.getVersionConstraint(), supplier.getExporter(), supplier);
            }
            return supplier;
          }
        }
        dynamicImport.clearPossibleSuppliers();
      }
    }
    finally
    {
      if (importName.endsWith("*")) {
        dynamicImport.clearPossibleSuppliers();
      }
      dynamicImport.setName(null);
    }
    if (importName.endsWith("*")) {
      dynamicImport.clearPossibleSuppliers();
    }
    dynamicImport.setName(null);
    
    return null;
  }
  
  public void bundleAdded(BundleDescription bundle)
  {
    if (!initialized) {
      return;
    }
    if (bundleMapping.get(bundle) != null) {
      return;
    }
    ResolverBundle rb = new ResolverBundle(bundle, this);
    bundleMapping.put(bundle, rb);
    unresolvedBundles.add(rb);
    resolverExports.put(rb.getExportPackages());
    resolverBundles.put(rb.getName(), rb);
    addGenerics(rb.getGenericCapabilities());
  }
  
  public void bundleRemoved(BundleDescription bundle, boolean pending)
  {
    ResolverBundle rb = initialized ? (ResolverBundle)bundleMapping.get(bundle) : null;
    if (rb != null) {
      rb.setUninstalled();
    }
    internalBundleRemoved(bundle, pending);
  }
  
  private void internalBundleRemoved(BundleDescription bundle, boolean pending)
  {
    if (pending) {
      removalPending.put(new Long(bundle.getBundleId()), bundle);
    }
    if (!initialized) {
      return;
    }
    ResolverBundle rb = (ResolverBundle)bundleMapping.get(bundle);
    if (rb == null) {
      return;
    }
    if (!pending)
    {
      bundleMapping.remove(bundle);
      groupingChecker.clear(rb);
    }
    if ((!pending) || (!bundle.isResolved()))
    {
      resolverExports.remove(rb.getExportPackages());
      resolverBundles.remove(rb);
      removeGenerics(rb.getGenericCapabilities());
    }
    unresolvedBundles.remove(rb);
  }
  
  private void unresolveBundle(ResolverBundle bundle, boolean removed)
  {
    if (bundle == null) {
      return;
    }
    List<BundleDescription> removedBundles = removalPending.remove(new Long(bundle.getBundleDescription().getBundleId()));
    for (Iterator localIterator = removedBundles.iterator(); localIterator.hasNext();)
    {
      BundleDescription removedDesc = (BundleDescription)localIterator.next();
      ResolverBundle re = (ResolverBundle)bundleMapping.get(removedDesc);
      unresolveBundle(re, true);
      state.removeBundleComplete(removedDesc);
      resolverExports.remove(re.getExportPackages());
      resolverBundles.remove(re);
      removeGenerics(re.getGenericCapabilities());
      bundleMapping.remove(removedDesc);
      groupingChecker.clear(re);
      if (removedDesc == bundle.getBundleDescription()) {
        removed = true;
      }
    }
    if ((!bundle.getBundleDescription().isResolved()) && (!developmentMode)) {
      return;
    }
    CompositeResolveHelperRegistry currentLinks = compositeHelpers;
    if (currentLinks != null)
    {
      CompositeResolveHelper helper = currentLinks.getCompositeResolveHelper(bundle.getBundleDescription());
      if (helper != null) {
        helper.giveExports(null);
      }
    }
    setBundleUnresolved(bundle, removed, false);
    
    BundleDescription[] dependents = bundle.getBundleDescription().getDependents();
    state.resolveBundle(bundle.getBundleDescription(), false, null, null, null, null, null, null, null, null);
    for (int i = 0; i < dependents.length; i++) {
      unresolveBundle((ResolverBundle)bundleMapping.get(dependents[i]), false);
    }
  }
  
  public void bundleUpdated(BundleDescription newDescription, BundleDescription existingDescription, boolean pending)
  {
    internalBundleRemoved(existingDescription, pending);
    bundleAdded(newDescription);
  }
  
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd