org.eclipse.osgi_3.7.2.v20120110-1415

t(1);
      }
      results.add(new PackageRoots[] { this, bundleUsedRoots });
    }
    return isConsistentClassSpace(bundleUsedRoots, bundle, visited, results);
  }
  
  private boolean subSet(ResolverExport[] superSet, ResolverExport[] subSet)
  {
    for (int i = 0; i < subSet.length; i++)
    {
      boolean found = false;
      for (int j = 0; j < superSet.length; j++) {
        if (subSet[i].getExporter() == superSet[j].getExporter())
        {
          found = true;
          break;
        }
      }
      if (!found) {
        return false;
      }
    }
    return true;
  }
  
  public boolean superSet(PackageRoots subSet)
  {
    return subSet(roots, roots);
  }
  
  public String getName()
  {
    return name;
  }
  
  public ResolverExport[] getRoots()
  {
    return roots;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.osgi.internal.module.GroupingChecker.PackageRoots
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.osgi.internal.module;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.BundleSpecification;
import org.eclipse.osgi.service.resolver.ExportPackageDescription;

public class GroupingChecker
{
  final PackageRoots nullPackageRoots = new PackageRoots(null);
  private Map<ResolverBundle, Map<String, PackageRoots>> bundles = new HashMap();
  
  public void populateRoots(ResolverBundle bundle)
  {
    if (bundles.containsKey(bundle)) {
      return;
    }
    BundleConstraint[] requires = bundle.getRequires();
    for (int j = 0; j < requires.length; j++)
    {
      ResolverBundle selectedSupplier = (ResolverBundle)requires[j].getSelectedSupplier();
      if (selectedSupplier != null) {
        isConsistentInternal(bundle, selectedSupplier, new ArrayList(1), true, null);
      }
    }
    ExportPackageDescription[] imports = bundle.getBundleDescription().getResolvedImports();
    ExportPackageDescription[] arrayOfExportPackageDescription1;
    int j = (arrayOfExportPackageDescription1 = imports).length;
    for (int i = 0; i < j; i++)
    {
      ExportPackageDescription importPkg = arrayOfExportPackageDescription1[i];
      List<ResolverExport> exports = bundle.getResolver().getResolverExports().get(importPkg.getName());
      for (Iterator localIterator = exports.iterator(); localIterator.hasNext();)
      {
        ResolverExport export = (ResolverExport)localIterator.next();
        if (export.getExportPackageDescription() == importPkg) {
          isConsistentInternal(bundle, export, true, null);
        }
      }
    }
  }
  
  public void populateRoots(ResolverBundle importingBundle, ResolverExport export)
  {
    Map<String, PackageRoots> packageRoots = (Map)bundles.get(importingBundle);
    if (packageRoots != null) {
      packageRoots.remove(export.getName());
    }
    PackageRoots roots = getPackageRoots(export.getExporter(), export.getName(), null);
    packageRoots.put(export.getName(), roots);
  }
  
  public PackageRoots[][] isConsistent(ResolverBundle requiringBundle, ResolverBundle matchingBundle)
  {
    List<PackageRoots[]> results = isConsistentInternal(requiringBundle, matchingBundle, new ArrayList(1), false, null);
    return results == null ? null : (PackageRoots[][])results.toArray(new PackageRoots[results.size()][]);
  }
  
  private List<PackageRoots[]> isConsistentInternal(ResolverBundle requiringBundle, ResolverBundle matchingBundle, List<ResolverBundle> visited, boolean dynamicImport, List<PackageRoots[]> results)
  {
    if (visited.contains(matchingBundle)) {
      return results;
    }
    visited.add(matchingBundle);
    
    ResolverExport[] matchingExports = matchingBundle.getExportPackages();
    for (int i = 0; i < matchingExports.length; i++)
    {
      ResolverExport matchingExport = matchingExports[i];
      if (matchingExports[i].getSubstitute() != null) {
        matchingExport = (ResolverExport)matchingExports[i].getSubstitute();
      }
      results = isConsistentInternal(requiringBundle, matchingExport, dynamicImport, results);
    }
    BundleConstraint[] supplierRequires = matchingBundle.getRequires();
    for (int j = 0; j < supplierRequires.length; j++)
    {
      ResolverBundle reexported = (ResolverBundle)supplierRequires[j].getSelectedSupplier();
      if ((reexported != null) && (((BundleSpecification)supplierRequires[j].getVersionConstraint()).isExported())) {
        results = isConsistentInternal(requiringBundle, reexported, visited, dynamicImport, results);
      }
    }
    return results;
  }
  
  public PackageRoots[][] isConsistent(ResolverBundle importingBundle, ResolverExport matchingExport)
  {
    List<PackageRoots[]> results = isConsistentInternal(importingBundle, matchingExport, false, null);
    return results == null ? null : (PackageRoots[][])results.toArray(new PackageRoots[results.size()][]);
  }
  
  public PackageRoots[][] isConsistent(ResolverBundle requiringBundle, GenericCapability matchingCapability)
  {
    String[] uses = matchingCapability.getUsesDirective();
    if (uses == null) {
      return null;
    }
    ArrayList<PackageRoots[]> results = new ArrayList(0);
    String[] arrayOfString1;
    int j = (arrayOfString1 = uses).length;
    for (int i = 0; i < j; i++)
    {
      String usedPackage = arrayOfString1[i];
      PackageRoots providingRoots = getPackageRoots(matchingCapability.getResolverBundle(), usedPackage, null);
      providingRoots.addConflicts(requiringBundle, usedPackage, null, results);
    }
    return results.size() == 0 ? null : (PackageRoots[][])results.toArray(new PackageRoots[results.size()][]);
  }
  
  public PackageRoots[][] isDynamicConsistent(ResolverBundle importingBundle, ResolverExport matchingExport)
  {
    List<PackageRoots[]> results = isConsistentInternal(importingBundle, matchingExport, true, null);
    return results == null ? null : (PackageRoots[][])results.toArray(new PackageRoots[results.size()][]);
  }
  
  private List<PackageRoots[]> isConsistentInternal(ResolverBundle importingBundle, ResolverExport matchingExport, boolean dyanamicImport, List<PackageRoots[]> results)
  {
    PackageRoots exportingRoots = getPackageRoots(matchingExport.getExporter(), matchingExport.getName(), null);
    
    results = exportingRoots.isConsistentClassSpace(importingBundle, null, results);
    if (!dyanamicImport) {
      return results;
    }
    PackageRoots importingRoots = getPackageRoots(importingBundle, matchingExport.getName(), null);
    Map<String, PackageRoots> importingPackages = (Map)bundles.get(importingBundle);
    if (importingPackages != null) {
      for (Iterator<PackageRoots> allImportingPackages = importingPackages.values().iterator(); allImportingPackages.hasNext();)
      {
        PackageRoots roots = (PackageRoots)allImportingPackages.next();
        if (roots != importingRoots) {
          results = roots.isConsistentClassSpace(exportingRoots, matchingExport.getExporter(), null, results);
        }
      }
    }
    GenericConstraint[] genericRequires = importingBundle.getGenericRequires();
    GenericConstraint[] arrayOfGenericConstraint1;
    int j = (arrayOfGenericConstraint1 = genericRequires).length;
    for (int i = 0; i < j; i++)
    {
      GenericConstraint constraint = arrayOfGenericConstraint1[i];
      if (constraint.supplierHasUses())
      {
        GenericCapability supplier = (GenericCapability)constraint.getSelectedSupplier();
        String[] uses = supplier.getUsesDirective();
        if (uses != null)
        {
          String[] arrayOfString1;
          int m = (arrayOfString1 = uses).length;
          for (int k = 0; k < m; k++)
          {
            String usedPackage = arrayOfString1[k];
            if (usedPackage.equals(matchingExport.getName())) {
              results = exportingRoots.addConflicts(supplier.getResolverBundle(), usedPackage, null, results);
            }
          }
        }
      }
    }
    return results;
  }
  
  PackageRoots getPackageRoots(ResolverBundle bundle, String packageName, List<ResolverBundle> visited)
  {
    Map<String, PackageRoots> packages = (Map)bundles.get(bundle);
    if (packages == null)
    {
      packages = new HashMap(5);
      bundles.put(bundle, packages);
    }
    PackageRoots packageRoots = (PackageRoots)packages.get(packageName);
    if (packageRoots == null)
    {
      packageRoots = createPackageRoots(bundle, packageName, visited == null ? new ArrayList(1) : visited);
      packages.put(packageName, packageRoots);
    }
    return packageRoots != null ? packageRoots : nullPackageRoots;
  }
  
  private PackageRoots createPackageRoots(ResolverBundle bundle, String packageName, List<ResolverBundle> visited)
  {
    if (visited.contains(bundle)) {
      return null;
    }
    visited.add(bundle);
    if (bundle.getBundleDescription().isResolved())
    {
      ExportPackageDescription[] imports = bundle.getBundleDescription().getResolvedImports();
      ExportPackageDescription[] arrayOfExportPackageDescription1;
      int j = (arrayOfExportPackageDescription1 = imports).length;
      for (int i = 0; i < j; i++)
      {
        ExportPackageDescription importPkg = arrayOfExportPackageDescription1[i];
        if ((importPkg.getExporter() != bundle.getBundleDescription()) && (importPkg.getName().equals(packageName)))
        {
          List<ResolverExport> exports = bundle.getResolver().getResolverExports().get(packageName);
          for (Iterator localIterator = exports.iterator(); localIterator.hasNext();)
          {
            ResolverExport export = (ResolverExport)localIterator.next();
            if (export.getExportPackageDescription() == importPkg) {
              return getPackageRoots(export.getExporter(), packageName, visited);
            }
          }
        }
      }
    }
    else
    {
      ResolverImport imported = bundle.getImport(packageName);
      if ((imported != null) && (imported.getSelectedSupplier() != null))
      {
        ResolverExport selectedExport = (ResolverExport)imported.getSelectedSupplier();
        if (selectedExport.getExporter() != bundle) {
          return getPackageRoots(selectedExport.getExporter(), packageName, visited);
        }
      }
    }
    ResolverExport[] exports = bundle.getExports(packageName);
    List<PackageRoots> roots = new ArrayList(0);
    
    BundleConstraint[] requires = bundle.getRequires();
    for (int i = 0; i < requires.length; i++)
    {
      ResolverBundle supplier = (ResolverBundle)requires[i].getSelectedSupplier();
      if (supplier != null) {
        if (supplier.getExport(packageName) != null)
        {
          PackageRoots requiredRoots = getPackageRoots(supplier, packageName, visited);
          if (requiredRoots != nullPackageRoots) {
            roots.add(requiredRoots);
          }
        }
        else
        {
          BundleConstraint[] supplierRequires = supplier.getRequires();
          for (int j = 0; j < supplierRequires.length; j++)
          {
            ResolverBundle reexported = (ResolverBundle)supplierRequires[j].getSelectedSupplier();
            if ((reexported != null) && (((BundleSpecification)supplierRequires[j].getVersionConstraint()).isExported())) {
              if (reexported.getExport(packageName) != null)
              {
                PackageRoots reExportedRoots = getPackageRoots(reexported, packageName, visited);
                if (reExportedRoots != nullPackageRoots) {
                  roots.add(reExportedRoots);
                }
              }
            }
          }
        }
      }
    }
    if ((exports.length > 0) || (roots.size() > 1))
    {
      PackageRoots[] requiredRoots = (PackageRoots[])roots.toArray(new PackageRoots[roots.size()]);
      if (exports.length == 0)
      {
        PackageRoots superSet = requiredRoots[0];
        for (int i = 1; i < requiredRoots.length; i++) {
          if (requiredRoots[i].superSet(superSet))
          {
            superSet = requiredRoots[i];
          }
          else if (!superSet.superSet(requiredRoots[i]))
          {
            superSet = null;
            break;
          }
        }
        if (superSet != null) {
          return superSet;
        }
      }
      PackageRoots result = new PackageRoots(packageName);
      for (int i = 0; i < requiredRoots.length; i++) {
        result.merge(requiredRoots[i]);
      }
      for (int i = 0; i < exports.length; i++) {
        result.addRoot(exports[i]);
      }
      return result;
    }
    return roots.size() == 0 ? nullPackageRoots : (PackageRoots)roots.get(0);
  }
  
  public void clear()
  {
    bundles.clear();
  }
  
  public void clear(ResolverBundle rb)
  {
    bundles.remove(rb);
  }
  
  class PackageRoots
  {
    private String name;
    private ResolverExport[] roots;
    
    PackageRoots(String name)
    {
      this.name = name;
    }
    
    public boolean hasRoots()
    {
      return (roots != null) && (roots.length > 0);
    }
    
    public void addRoot(ResolverExport export)
    {
      if (roots == null)
      {
        roots = new ResolverExport[] { export };
        return;
      }
      String exportBSN = export.getExporter().getName();
      if (exportBSN != null) {
        for (int i = 0; i < roots.length; i++) {
          if ((export.getExporter() != roots[i].getExporter()) && (exportBSN.equals(roots[i].getExporter().getName()))) {
            return;
          }
        }
      }
      if (!contains(export, roots))
      {
        ResolverExport[] newRoots = new ResolverExport[roots.length + 1];
        System.arraycopy(roots, 0, newRoots, 0, roots.length);
        newRoots[roots.length] = export;
        roots = newRoots;
      }
    }
    
    private boolean contains(ResolverExport export, ResolverExport[] exports)
    {
      for (int i = 0; i < exports.length; i++) {
        if (exports[i] == export) {
          return true;
        }
      }
      return false;
    }
    
    public void merge(PackageRoots packageRoots)
    {
      if ((packageRoots == null) || (roots == null)) {
        return;
      }
      int size = roots.length;
      for (int i = 0; i < size; i++) {
        addRoot(roots[i]);
      }
    }
    
    public List<PackageRoots[]> isConsistentClassSpace(ResolverBundle importingBundle, List<PackageRoots> visited, List<PackageRoots[]> results)
    {
      if (roots == null) {
        return results;
      }
      if (visited == null) {
        visited = new ArrayList(1);
      }
      if (visited.contains(this)) {
        return results;
      }
      visited.add(this);
      int size = roots.length;
      for (int i = 0; i < size; i++)
      {
        ResolverExport root = roots[i];
        String[] uses = root.getUsesDirective();
        if (uses != null) {
          for (int j = 0; j < uses.length; j++) {
            if (!uses[j].equals(root.getName()))
            {
              PackageRoots thisUsedRoots = getPackageRoots(root.getExporter(), uses[j], null);
              PackageRoots importingUsedRoots = getPackageRoots(importingBundle, uses[j], null);
              if (thisUsedRoots != importingUsedRoots)
              {
                if ((thisUsedRoots != nullPackageRoots) && (importingUsedRoots != nullPackageRoots) && 
                  (!subSet(roots, roots)) && (!subSet(roots, roots)))
                {
                  if (results == null) {
                    results = new ArrayList(1);
                  }
                  results.add(new PackageRoots[] { this, importingUsedRoots });
                }
                results = thisUsedRoots.isConsistentClassSpace(importingBundle, visited, results);
              }
            }
          }
        }
      }
      return results;
    }
    
    public List<PackageRoots[]> isConsistentClassSpace(PackageRoots exportingRoots, ResolverBundle exporter, List<PackageRoots> visited, List<PackageRoots[]> results)
    {
      if (roots == null) {
        return results;
      }
      int size = roots.length;
      for (int i = 0; i < size; i++)
      {
        ResolverExport root = roots[i];
        String[] uses = root.getUsesDirective();
        if (uses != null)
        {
          if (visited == null) {
            visited = new ArrayList(1);
          }
          if (visited.contains(this)) {
            return results;
          }
          visited.add(this);
          for (int j = 0; j < uses.length; j++) {
            if ((!uses[j].equals(root.getName())) && (uses[j].equals(name)))
            {
              PackageRoots thisUsedRoots = getPackageRoots(root.getExporter(), uses[j], null);
              PackageRoots exportingUsedRoots = getPackageRoots(exporter, uses[j], null);
              if (thisUsedRoots == exportingRoots) {
                return results;
              }
              if ((thisUsedRoots != nullPackageRoots) && (exportingUsedRoots != nullPackageRoots) && 
                (!subSet(roots, roots)) && (!subSet(roots, roots)))
              {
                if (results == null) {
                  results = new ArrayList(1);
                }
                results.add(new PackageRoots[] { this, exportingUsedRoots });
              }
              results = thisUsedRoots.isConsistentClassSpace(exportingRoots, exporter, visited, results);
            }
          }
        }
      }
      return results;
    }
    
    List<PackageRoots[]> addConflicts(ResolverBundle bundle, String usedPackage, List<PackageRoots> visited, List<PackageRoots[]> results)
    {
      PackageRoots bundleUsedRoots = getPackageRoots(bundle, usedPackage, null);
      if (this == bundleUsedRoots) {
        return results;
      }
      if ((this != nullPackageRoots) && (bundleUsedRoots != nullPackageRoots) && 
        (!subSet(roots, roots)) && (!subSet(roots, roots)))
      {
        if (results == null) {
          results = new ArrayList(1);
        }
        results.add(new PackageRoots[] { this, bundleUsedRoots });
      }
      return isConsistentClassSpace(bundleUsedRoots, bundle, visited, results);
    }
    
    private boolean subSet(ResolverExport[] superSet, ResolverExport[] subSet)
    {
      for (int i = 0; i < subSet.length; i++)
      {
        boolean found = false;
        for (int j = 0; j < superSet.length; j++) {
          if (subSet[i].getExporter() == superSet[j].getExporter())
          {
            found = true;
            break;
          }
        }
        if (!found) {
          return false;
        }
      }
      return true;
    }
    
    public boolean superSet(PackageRoots subSet)
    {
      return subSet(roots, roots);
    }
    
    public String getName()
    {
      return name;
    }
    
    public ResolverExport[] getRoots()
    {
      return roots;
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.osgi.internal.module.GroupingChecker
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.osgi.internal.module;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class MappedList<K, V>
{
  protected final HashMap<K, List<V>> internal = new HashMap();
  protected final List<V> empty = Collections.EMPTY_LIST;
  
  public void put(K key, V value)
  {
    List<V> existing = (List)internal.get(key);
    if (existing == null)
    {
      existing = new ArrayList(1);
      existing.add(value);
      internal.put(key, existing);
      return;
    }
    int index = insertionIndex(existing, value);
    existing.add(index, value);
  }
  
  protected int insertionIndex(List<V> existing, V value)
  {
    return existing.size();
  }
  
  public List<V> remove(K key)
  {
    return get(key, true);
  }
  
  public List<V> get(K key)
  {
    return get(key, false);
  }
  
  private List<V> get(K key, boolean remove)
  {
    List<V> result = remove ? (List)internal.remove(key) : (List)internal.get(key);
    return result == null ? empty : result;
  }
  
  public int getSize()
  {
    return internal.size();
  }
  
  public List<V> getAllValues()
  {
    if (getSize() == 0) {
      return empty;
    }
    ArrayList<V> results = new ArrayList(getSize());
    Iterator<List<V>> iter = internal.values().iterator();
    while (iter.hasNext()) {
      results.addAll((Collection)iter.next());
    }
    return results;
  }
  
  public void clear()
  {
    internal.clear();
  }
}

/* Location:
 * Qualified Name:     org.eclipse.osgi.internal.module.MappedList
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.osgi.internal.module;

import java.security.Permission;
import org.eclipse.osgi.service.resolver.BaseDescription;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.BundleSpecification;
import org.eclipse.osgi.service.resolver.ExportPackageDescription;
import org.eclipse.osgi.service.resolver.GenericDescription;
import org.eclipse.osgi.service.resolver.GenericSpecification;
import org.eclipse.osgi.service.resolver.HostSpecification;
import org.eclipse.osgi.service.resolver.ImportPackageSpecification;
import org.eclipse.osgi.service.resolver.State;
import org.eclipse.osgi.service.resolver.VersionConstraint;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundlePermission;
import org.osgi.framework.CapabilityPermission;
import org.osgi.framework.PackagePermission;

public class PermissionChecker
{
  private boolean checkPermissions = false;
  private ResolverImpl resolver;
  
  public PermissionChecker(boolean checkPermissions, ResolverImpl resolver)
  {
    this.checkPermissions = checkPermissions;
    this.resolver = resolver;
  }
  
  public boolean checkPermission(VersionConstraint vc, BaseDescription bd)
  {
    if (!checkPermissions) {
      return true;
    }
    boolean success = false;
    Permission producerPermission = null;Permission consumerPermission = null;
    Bundle consumer = null;
    Bundle producer = bd.getSupplier().getBundle();
    int errorType = 0;
    if ((vc instanceof ImportPackageSpecification))
    {
      errorType = 128;
      producerPermission = new PackagePermission(bd.getName(), "exportonly");
      consumerPermission = producer != null ? new PackagePermission(vc.getName(), producer, "import") : new PackagePermission(vc.getName(), "import");
    }
    else if (((vc instanceof BundleSpecification)) || ((vc instanceof HostSpecification)))
    {
      boolean requireBundle = vc instanceof BundleSpecification;
      errorType = requireBundle ? 512 : 4096;
      producerPermission = new BundlePermission(bd.getName(), requireBundle ? "provide" : "host");
      consumerPermission = new BundlePermission(vc.getName(), requireBundle ? "require" : "fragment");
    }
    else if ((vc instanceof GenericSpecification))
    {
      errorType = 524288;
      GenericDescription gd = (GenericDescription)bd;
      producerPermission = new CapabilityPermission(gd.getType(), "provide");
      consumerPermission = new CapabilityPermission(gd.getType(), gd.getDeclaredAttributes(), producer, "require");
    }
    consumer = vc.getBundle().getBundle();
    if ((producer != null) && ((producer.getState() & 0x1) == 0))
    {
      success = producer.hasPermission(producerPermission);
      if (!success)
      {
        switch (errorType)
        {
        case 128: 
          errorType = 256;
          break;
        case 512: 
        case 4096: 
          errorType = errorType == 512 ? 1024 : 2048;
          break;
        case 524288: 
          errorType = 1024;
        }
        resolver.getState().addResolverError(vc.getBundle(), errorType, producerPermission.toString(), vc);
      }
    }
    if ((success) && (consumer != null) && ((consumer.getState() & 0x1) == 0))
    {
      success = consumer.hasPermission(consumerPermission);
      if (!success) {
        resolver.getState().addResolverError(vc.getBundle(), errorType, consumerPermission.toString(), vc);
      }
    }
    return success;
  }
  
  boolean checkPackagePermission(ExportPackageDescription export)
  {
    if (!checkPermissions) {
      return true;
    }
    export.getSupplier().getBundle();
    Bundle bundle = export.getSupplier().getBundle();
    return bundle == null ? false : bundle.hasPermission(new PackagePermission(export.getName(), "exportonly"));
  }
  
  boolean checkCapabilityPermission(GenericDescription capability)
  {
    if (!checkPermissions) {
      return true;
    }
    Bundle bundle = capability.getSupplier().getBundle();
    return bundle == null ? false : bundle.hasPermission(new CapabilityPermission(capability.getType(), "provide"));
  }
}

/* Location:
 * Qualified Name:     org.eclipse.osgi.internal.module.PermissionChecker
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.osgi.internal.module;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.osgi.internal.resolver.ExportPackageDescriptionImpl;
import org.eclipse.osgi.internal.resolver.GenericDescriptionImpl;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.BundleSpecification;
import org.eclipse.osgi.service.resolver.ExportPackageDescription;
import org.eclipse.osgi.service.resolver.GenericDescription;
import org.eclipse.osgi.service.resolver.GenericSpecification;
import org.eclipse.osgi.service.resolver.HostSpecification;
import org.eclipse.osgi.service.resolver.ImportPackageSpecification;
import org.eclipse.osgi.service.resolver.State;
import org.eclipse.osgi.service.resolver.VersionConstraint;
import org.osgi.framework.Version;

public class ResolverBundle
  extends VersionSupplier
  implements Comparable<ResolverBundle>
{
  public static final int UNRESOLVED = 0;
  public static final int RESOLVING = 1;
  public static final int RESOLVED = 2;
  private final Long bundleID;
  private BundleConstraint host;
  private ResolverImport[] imports;
  private ResolverExport[] exports;
  private BundleConstraint[] requires;
  private GenericCapability[] genericCapabilities;
  private GenericConstraint[] genericReqiures;
  private ArrayList<ResolverBundle> fragments;
  private HashMap<Long, List<ResolverExport>> fragmentExports;
  private HashMap<Long, List<ResolverImport>> fragmentImports;
  private HashMap<Long, List<BundleConstraint>> fragmentRequires;
  private HashMap<Long, List<GenericCapability>> fragmentGenericCapabilities;
  private HashMap<Long, List<GenericConstraint>> fragmentGenericRequires;
  private boolean resolvable = true;
  private int state = 0;
  private boolean uninstalled = false;
  private final ResolverImpl resolver;
  private boolean newFragmentExports;
  private boolean newFragmentCapabilities;
  
  ResolverBundle(BundleDescription bundle, ResolverImpl resolver)
  {
    super(bundle);
    bundleID = new Long(bundle.getBundleId());
    this.resolver = resolver;
    initialize(bundle.isResolved());
  }
  
  void initialize(boolean useSelectedExports)
  {
    if (getBundleDescription().getHost() != null)
    {
      host = new BundleConstraint(this, getBundleDescription().getHost());
      exports = new ResolverExport[0];
      imports = new ResolverImport[0];
      requires = new BundleConstraint[0];
      genericReqiures = new GenericConstraint[0];
      genericCapabilities = new GenericCapability[0];
      return;
    }
    ImportPackageSpecification[] actualImports = getBundleDescription().getImportPackages();
    
    List<ResolverImport> importList = new ArrayList(actualImports.length);
    for (int i = actualImports.length - 1; i >= 0; i--) {
      if ("optional".equals(actualImports[i].getDirective("resolution"))) {
        importList.add(new ResolverImport(this, actualImports[i]));
      } else {
        importList.add(0, new ResolverImport(this, actualImports[i]));
      }
    }
    imports = ((ResolverImport[])importList.toArray(new ResolverImport[importList.size()]));
    
    ExportPackageDescription[] actualExports = useSelectedExports ? getBundleDescription().getSelectedExports() : getBundleDescription().getExportPackages();
    exports = new ResolverExport[actualExports.length];
    for (int i = 0; i < actualExports.length; i++) {
      exports[i] = new ResolverExport(this, actualExports[i]);
    }
    BundleSpecification[] actualRequires = getBundleDescription().getRequiredBundles();
    requires = new BundleConstraint[actualRequires.length];
    for (int i = 0; i < requires.length; i++) {
      requires[i] = new BundleConstraint(this, actualRequires[i]);
    }
    GenericSpecification[] actualGenericRequires = getBundleDescription().getGenericRequires();
    genericReqiures = new GenericConstraint[actualGenericRequires.length];
    for (int i = 0; i < genericReqiures.length; i++) {
      genericReqiures[i] = new GenericConstraint(this, actualGenericRequires[i]);
    }
    GenericDescription[] actualCapabilities = useSelectedExports ? getBundleDescription().getSelectedGenericCapabilities() : getBundleDescription().getGenericCapabilities();
    genericCapabilities = new GenericCapability[actualCapabilities.length];
    for (int i = 0; i < genericCapabilities.length; i++) {
      genericCapabilities[i] = new GenericCapability(this, actualCapabilities[i]);
    }
    fragments = null;
    fragmentExports = null;
    fragmentImports = null;
    fragmentRequires = null;
    fragmentGenericCapabilities = null;
    fragmentGenericRequires = null;
  }
  
  ResolverExport getExport(String name)
  {
    ResolverExport[] allExports = getExports(name);
    return allExports.length == 0 ? null : allExports[0];
  }
  
  ResolverExport[] getExports(String name)
  {
    List<ResolverExport> results = new ArrayList(1);
    
    List<ResolverExport> resolverExports = resolver.getResolverExports().get(name);
    for (Iterator localIterator = resolverExports.iterator(); localIterator.hasNext();)
    {
      ResolverExport export = (ResolverExport)localIterator.next();
      if (export.getExporter() == this) {
        results.add(export);
      }
    }
    return (ResolverExport[])results.toArray(new ResolverExport[results.size()]);
  }
  
  void clearWires()
  {
    ResolverImport[] allImports = getImportPackages();
    for (int i = 0; i < allImports.length; i++) {
      allImports[i].clearPossibleSuppliers();
    }
    if (host != null) {
      host.clearPossibleSuppliers();
    }
    BundleConstraint[] allRequires = getRequires();
    for (int i = 0; i < allRequires.length; i++) {
      allRequires[i].clearPossibleSuppliers();
    }
    GenericConstraint[] allGenericRequires = getGenericRequires();
    for (int i = 0; i < allGenericRequires.length; i++) {
      allGenericRequires[i].clearPossibleSuppliers();
    }
    ResolverExport[] allExports = getExportPackages();
    for (int i = 0; i < allExports.length; i++) {
      allExports[i].setSubstitute(null);
    }
  }
  
  boolean isResolved()
  {
    return getState() == 2;
  }
  
  boolean isFragment()
  {
    return host != null;
  }
  
  int getState()
  {
    return state;
  }
  
  void setState(int state)
  {
    this.state = state;
  }
  
  private <T> List<T> getAll(T[] hostEntries, Map<Long, List<T>> fragmentMap)
  {
    List<T> result = new ArrayList(hostEntries.length);
    Object[] arrayOfObject;
    int j = (arrayOfObject = hostEntries).length;
    for (int i = 0; i < j; i++)
    {
      T entry = arrayOfObject[i];
      result.add(entry);
    }
    for (Iterator localIterator = fragments.iterator(); localIterator.hasNext();)
    {
      ResolverBundle fragment = (ResolverBundle)localIterator.next();
      Object fragEntries = (List)fragmentMap.get(bundleID);
      if (fragEntries != null) {
        result.addAll((Collection)fragEntries);
      }
    }
    return result;
  }
  
  ResolverImport[] getImportPackages()
  {
    if ((isFragment()) || (fragments == null) || (fragments.size() == 0)) {
      return imports;
    }
    List<ResolverImport> result = getAll(imports, fragmentImports);
    return (ResolverImport[])result.toArray(new ResolverImport[result.size()]);
  }
  
  ResolverExport[] getExportPackages()
  {
    if ((isFragment()) || (fragments == null) || (fragments.size() == 0)) {
      return exports;
    }
    List<ResolverExport> result = getAll(exports, fragmentExports);
    return (ResolverExport[])result.toArray(new ResolverExport[result.size()]);
  }
  
  ResolverExport[] getSelectedExports()
  {
    return getExports(true);
  }
  
  ResolverExport[] getSubstitutedExports()
  {
    return getExports(false);
  }
  
  private ResolverExport[] getExports(boolean selected)
  {
    ResolverExport[] results = getExportPackages();
    int removedExports = 0;
    for (int i = 0; i < results.length; i++) {
      if (selected ? results[i].getSubstitute() != null : results[i].getSubstitute() == null) {
        removedExports++;
      }
    }
    if (removedExports == 0) {
      return results;
    }
    ResolverExport[] selectedExports = new ResolverExport[results.length - removedExports];
    int index = 0;
    for (int i = 0; i < results.length; i++) {
      if (selected ? results[i].getSubstitute() == null : results[i].getSubstitute() != null)
      {
        selectedExports[index] = results[i];
        index++;
      }
    }
    return selectedExports;
  }
  
  BundleConstraint getHost()
  {
    return host;
  }
  
  GenericCapability[] getGenericCapabilities()
  {
    if ((isFragment()) || (fragments == null) || (fragments.size() == 0)) {
      return genericCapabilities;
    }
    List<GenericCapability> result = getAll(genericCapabilities, fragmentGenericCapabilities);
    return (GenericCapability[])result.toArray(new GenericCapability[result.size()]);
  }
  
  BundleConstraint[] getRequires()
  {
    if ((isFragment()) || (fragments == null) || (fragments.size() == 0)) {
      return requires;
    }
    List<BundleConstraint> result = getAll(requires, fragmentRequires);
    return (BundleConstraint[])result.toArray(new BundleConstraint[result.size()]);
  }
  
  GenericConstraint[] getGenericRequires()
  {
    if ((isFragment()) || (fragments == null) || (fragments.size() == 0)) {
      return genericReqiures;
    }
    List<GenericConstraint> result = getAll(genericReqiures, fragmentGenericRequires);
    return (GenericConstraint[])result.toArray(new GenericConstraint[result.size()]);
  }
  
  BundleConstraint getRequire(String name)
  {
    BundleConstraint[] allRequires = getRequires();
    for (int i = 0; i < allRequires.length; i++) {
      if (allRequires[i].getVersionConstraint().getName().equals(name)) {
        return allRequires[i];
      }
    }
    return null;
  }
  
  public BundleDescription getBundleDescription()
  {
    return (BundleDescription)getBaseDescription();
  }
  
  public ResolverBundle getResolverBundle()
  {
    return this;
  }
  
  ResolverImport getImport(String name)
  {
    ResolverImport[] allImports = getImportPackages();
    for (int i = 0; i < allImports.length; i++) {
      if (allImports[i].getName().equals(name)) {
        return allImports[i];
      }
    }
    return null;
  }
  
  public String toString()
  {
    return "[" + getBundleDescription() + "]";
  }
  
  private void initFragments()
  {
    if (fragments == null) {
      fragments = new ArrayList(1);
    }
    if (fragmentExports == null) {
      fragmentExports = new HashMap(1);
    }
    if (fragmentImports == null) {
      fragmentImports = new HashMap(1);
    }
    if (fragmentRequires == null) {
      fragmentRequires = new HashMap(1);
    }
    if (fragmentGenericCapabilities == null) {
      fragmentGenericCapabilities = new HashMap(1);
    }
    if (fragmentGenericRequires == null) {
      fragmentGenericRequires = new HashMap(1);
    }
  }
  
  private boolean isImported(String packageName)
  {
    ResolverImport[] allImports = getImportPackages();
    for (int i = 0; i < allImports.length; i++) {
      if (packageName.equals(allImports[i].getName())) {
        return true;
      }
    }
    return false;
  }
  
  private boolean isRequired(String bundleName)
  {
    return getRequire(bundleName) != null;
  }
  
  void attachFragment(ResolverBundle fragment, boolean dynamicAttach)
  {
    if (isFragment()) {
      return;
    }
    if ((!getBundleDescription().attachFragments()) || ((isResolved()) && (!getBundleDescription().dynamicFragments()))) {
      return;
    }
    if ((fragment.getHost().getNumPossibleSuppliers() > 0) && (!((HostSpecification)fragment.getHost().getVersionConstraint()).isMultiHost())) {
      return;
    }
    ImportPackageSpecification[] newImports = fragment.getBundleDescription().getImportPackages();
    BundleSpecification[] newRequires = fragment.getBundleDescription().getRequiredBundles();
    ExportPackageDescription[] newExports = fragment.getBundleDescription().getExportPackages();
    GenericDescription[] newGenericCapabilities = fragment.getBundleDescription().getGenericCapabilities();
    GenericSpecification[] newGenericRequires = fragment.getBundleDescription().getGenericRequires();
    if ((dynamicAttach) && (constraintsConflict(fragment.getBundleDescription(), newImports, newRequires, newGenericRequires))) {
      return;
    }
    if ((isResolved()) && (newExports.length > 0)) {
      fragment.setNewFragmentExports(true);
    }
    if ((isResolved()) && (newGenericCapabilities.length > 0)) {
      fragment.setNewFragmentCapabilities(true);
    }
    initFragments();
    for (Iterator<ResolverBundle> iFragments = fragments.iterator(); iFragments.hasNext();)
    {
      ResolverBundle existingFragment = (ResolverBundle)iFragments.next();
      String bsn = existingFragment.getName();
      if ((bsn != null) && (bsn.equals(fragment.getName()))) {
        return;
      }
    }
    if (fragments.contains(fragment)) {
      return;
    }
    fragments.add(fragment);
    fragment.getHost().addPossibleSupplier(this);
    if (newImports.length > 0)
    {
      ArrayList<ResolverImport> hostImports = new ArrayList(newImports.length);
      for (int i = 0; i < newImports.length; i++) {
        if (!isImported(newImports[i].getName())) {
          hostImports.add(new ResolverImport(this, newImports[i]));
        }
      }
      fragmentImports.put(bundleID, hostImports);
    }
    if (newRequires.length > 0)
    {
      ArrayList<BundleConstraint> hostRequires = new ArrayList(newRequires.length);
      for (int i = 0; i < newRequires.length; i++) {
        if (!isRequired(newRequires[i].getName())) {
          hostRequires.add(new BundleConstraint(this, newRequires[i]));
        }
      }
      fragmentRequires.put(bundleID, hostRequires);
    }
    if (newGenericRequires.length > 0)
    {
      ArrayList<GenericConstraint> hostGenericRequires = new ArrayList(newGenericRequires.length);
      for (int i = 0; i < newGenericRequires.length; i++) {
        hostGenericRequires.add(new GenericConstraint(this, newGenericRequires[i]));
      }
      fragmentGenericRequires.put(bundleID, hostGenericRequires);
    }
    ArrayList<ResolverExport> hostExports = new ArrayList(newExports.length);
    boolean foundEquivalent;
    ExportPackageDescription hostExport;
    if ((newExports.length > 0) && (dynamicAttach))
    {
      for (int i = 0; i < newExports.length; i++)
      {
        ResolverExport[] currentExports = getExports(newExports[i].getName());
        foundEquivalent = false;
        for (int j = 0; (j < currentExports.length) && (!foundEquivalent); j++) {
          if (equivalentExports(currentExports[j], newExports[i])) {
            foundEquivalent = true;
          }
        }
        if (!foundEquivalent)
        {
          hostExpo
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-2017. Infinite Loop Ltd