Ejemplo n.º 1
0
 public static boolean isBundleFragment(Bundle bundle) {
   boolean result = false;
   final BundleRevision revision = bundle.adapt(BundleRevision.class);
   if (revision != null) {
     result = (revision.getTypes() & BundleRevision.TYPE_FRAGMENT) != 0;
   }
   return result;
 }
Ejemplo n.º 2
0
 public static BundleWiringDTO newBundleWiringDTO(BundleRevision revision) {
   if (revision == null) {
     return null;
   }
   BundleWiringDTO dto = new DTOBuilder().getBundleWiringDTO(revision.getWiring());
   return dto;
 }
 private List<XPackageCapability> getPackageCapabilities(BundleRevision brev) {
   List<XPackageCapability> result = new ArrayList<XPackageCapability>();
   for (Capability aux : brev.getCapabilities(WIRING_PACKAGE_NAMESPACE)) {
     XPackageCapability cap = (XPackageCapability) aux;
     result.add(cap);
   }
   return result;
 }
 private List<XPackageRequirement> getDynamicPackageRequirements(BundleRevision brev) {
   List<XPackageRequirement> result = new ArrayList<XPackageRequirement>();
   for (Requirement aux : brev.getRequirements(WIRING_PACKAGE_NAMESPACE)) {
     XPackageRequirement req = (XPackageRequirement) aux;
     if (req.isDynamic()) {
       result.add(req);
     }
   }
   return result;
 }
  @Test
  public void testUpdateHostToFragment() throws Exception {
    Bundle bundleA = installBundle(getHostA());
    try {
      bundleA.start();
      assertBundleState(Bundle.ACTIVE, bundleA.getState());
      BundleRevisions brevs = bundleA.adapt(BundleRevisions.class);
      assertEquals(1, brevs.getRevisions().size());
      BundleRevision brev = brevs.getRevisions().get(0);
      assertEquals(0, brev.getTypes());

      bundleA.update(toInputStream(getFragmentA()));
      brevs = bundleA.adapt(BundleRevisions.class);
      Assert.assertNotSame(brev, brevs.getRevisions().get(0));
      brev = brevs.getRevisions().get(0);
      assertEquals(BundleRevision.TYPE_FRAGMENT, brev.getTypes());
    } finally {
      bundleA.uninstall();
    }
  }
Ejemplo n.º 6
0
 private BundleRevisionDTO getBundleRevisionDTO(BundleRevision revision) {
   if (revision == null) {
     return null;
   }
   BundleRevisionDTO dto = resources.get(revision);
   if (dto != null) {
     return dto;
   }
   dto = new BundleRevisionDTO();
   dto.id = identifier(revision);
   resources.put(revision, dto);
   dto.bundle = revision.getBundle().getBundleId();
   dto.symbolicName = revision.getSymbolicName();
   dto.type = revision.getTypes();
   dto.version = revision.getVersion().toString();
   dto.capabilities = getListCapabilityDTO(revision.getDeclaredCapabilities(null));
   dto.requirements = getListRequirementDTO(revision.getDeclaredRequirements(null));
   return dto;
 }
    public boolean isAssignableTo(Bundle requester, String className) {
      // Always return true if the requester is the same as the provider.
      if (requester == m_bundle) {
        return true;
      }

      // Boolean flag.
      boolean allow = true;
      // Get the package.
      String pkgName = Util.getClassPackage(className);
      // Get package wiring from service requester.
      BundleRevision requesterRevision = requester.adapt(BundleRevision.class);
      BundleWire requesterWire = Util.getWire(requesterRevision, pkgName);
      BundleCapability requesterCap = Util.getPackageCapability(requesterRevision, pkgName);
      // Get package wiring from service provider.
      BundleRevision providerRevision = m_bundle.adapt(BundleRevision.class);
      BundleWire providerWire = Util.getWire(providerRevision, pkgName);
      BundleCapability providerCap = Util.getPackageCapability(providerRevision, pkgName);

      // There are four situations that may occur here:
      //   1. Neither the requester, nor provider have wires for the package.
      //   2. The requester does not have a wire for the package.
      //   3. The provider does not have a wire for the package.
      //   4. Both the requester and provider have a wire for the package.
      // For case 1, if the requester does not have access to the class at
      // all, we assume it is using reflection and do not filter. If the
      // requester does have access to the class, then we make sure it is
      // the same class as the service. For case 2, we do not filter if the
      // requester is the exporter of the package to which the provider of
      // the service is wired. Otherwise, as in case 1, if the requester
      // does not have access to the class at all, we do not filter, but if
      // it does have access we check if it is the same class accessible to
      // the providing revision. For case 3, the provider will not have a wire
      // if it is exporting the package, so we determine if the requester
      // is wired to it or somehow using the same class. For case 4, we
      // simply compare the exporting revisions from the package wiring to
      // determine if we need to filter the service reference.

      // Case 1: Both requester and provider have no wire.
      if ((requesterWire == null) && (providerWire == null)) {
        // If requester has no access then true, otherwise service
        // registration must have same class as requester.
        try {
          Class requestClass =
              ((BundleWiringImpl) requesterRevision.getWiring()).getClassByDelegation(className);
          allow = getRegistration().isClassAccessible(requestClass);
        } catch (Exception ex) {
          // Requester has no access to the class, so allow it, since
          // we assume the requester is using reflection.
          allow = true;
        }
      }
      // Case 2: Requester has no wire, but provider does.
      else if ((requesterWire == null) && (providerWire != null)) {
        // If the requester exports the package, then the provider must
        // be wired to it.
        if (requesterCap != null) {
          allow = providerWire.getProviderWiring().getRevision().equals(requesterRevision);
        }
        // Otherwise, check if the requester has access to the class and,
        // if so, if it is the same class as the provider.
        else {
          try {
            // Try to load class from requester.
            Class requestClass =
                ((BundleWiringImpl) requesterRevision.getWiring()).getClassByDelegation(className);
            try {
              // If requester has access to the class, verify it is the
              // same class as the provider.
              allow =
                  (((BundleWiringImpl) providerRevision.getWiring()).getClassByDelegation(className)
                      == requestClass);
            } catch (Exception ex) {
              allow = false;
            }
          } catch (Exception ex) {
            // Requester has no access to the class, so allow it, since
            // we assume the requester is using reflection.
            allow = true;
          }
        }
      }
      // Case 3: Requester has a wire, but provider does not.
      else if ((requesterWire != null) && (providerWire == null)) {
        // If the provider exports the package, then the requester must
        // be wired to it.
        if (providerCap != null) {
          allow = requesterWire.getProviderWiring().getRevision().equals(providerRevision);
        }
        // If the provider is not the exporter of the requester's package,
        // then try to use the service registration to see if the requester's
        // class is accessible.
        else {
          try {
            // Load the class from the requesting bundle.
            Class requestClass =
                ((BundleWiringImpl) requesterRevision.getWiring()).getClassByDelegation(className);
            // Get the service registration and ask it to check
            // if the service object is assignable to the requesting
            // bundle's class.
            allow = getRegistration().isClassAccessible(requestClass);
          } catch (Exception ex) {
            // Filter to be safe.
            allow = false;
          }
        }
      }
      // Case 4: Both requester and provider have a wire.
      else {
        // Include service reference if the wires have the
        // same source revision.
        allow =
            providerWire
                .getProviderWiring()
                .getRevision()
                .equals(requesterWire.getProviderWiring().getRevision());
      }

      return allow;
    }
Ejemplo n.º 8
0
  /**
   * Add an extension bundle. The bundle will be added to the parent classloader and it's exported
   * packages will be added to the module definition exports of this instance. Subsequently, they
   * are available form the instance in it's role as content loader.
   *
   * @param felix the framework instance the given extension bundle comes from.
   * @param bundle the extension bundle to add.
   * @throws BundleException if extension bundles are not supported or this is not a framework
   *     extension.
   * @throws SecurityException if the caller does not have the needed
   *     AdminPermission.EXTENSIONLIFECYCLE and security is enabled.
   * @throws Exception in case something goes wrong.
   */
  synchronized void addExtensionBundle(Felix felix, BundleImpl bundle)
      throws SecurityException, BundleException, Exception {
    Object sm = System.getSecurityManager();
    if (sm != null) {
      ((SecurityManager) sm)
          .checkPermission(new AdminPermission(bundle, AdminPermission.EXTENSIONLIFECYCLE));
    }

    if (!((BundleProtectionDomain) bundle.getProtectionDomain())
        .impliesDirect(new AllPermission())) {
      throw new SecurityException("Extension Bundles must have AllPermission");
    }

    String directive =
        ManifestParser.parseExtensionBundleHeader(
            (String)
                ((BundleRevisionImpl) bundle.adapt(BundleRevision.class))
                    .getHeaders()
                    .get(Constants.FRAGMENT_HOST));

    // We only support classpath extensions (not bootclasspath).
    if (!Constants.EXTENSION_FRAMEWORK.equals(directive)) {
      throw new BundleException(
          "Unsupported Extension Bundle type: " + directive,
          new UnsupportedOperationException("Unsupported Extension Bundle type!"));
    }

    try {
      // Merge the exported packages with the exported packages of the systembundle.
      List<BundleCapability> exports = null;
      try {
        exports =
            ManifestParser.parseExportHeader(
                m_logger,
                m_systemBundleRevision,
                (String)
                    ((BundleRevisionImpl) bundle.adapt(BundleRevision.class))
                        .getHeaders()
                        .get(Constants.EXPORT_PACKAGE),
                m_systemBundleRevision.getSymbolicName(),
                m_systemBundleRevision.getVersion());
        exports = aliasSymbolicName(exports);
      } catch (Exception ex) {
        m_logger.log(
            bundle,
            Logger.LOG_ERROR,
            "Error parsing extension bundle export statement: "
                + ((BundleRevisionImpl) bundle.adapt(BundleRevision.class))
                    .getHeaders()
                    .get(Constants.EXPORT_PACKAGE),
            ex);
        return;
      }

      // Add the bundle as extension if we support extensions
      if (m_extensionManager != null) {
        // This needs to be the private instance.
        m_extensionManager.addExtension(felix, bundle);
      } else {
        // We don't support extensions (i.e., the parent is not an URLClassLoader).
        m_logger.log(
            bundle,
            Logger.LOG_WARNING,
            "Unable to add extension bundle to FrameworkClassLoader - Maybe not an URLClassLoader?");
        throw new UnsupportedOperationException(
            "Unable to add extension bundle to FrameworkClassLoader - Maybe not an URLClassLoader?");
      }
      appendCapabilities(exports);
    } catch (Exception ex) {
      throw ex;
    }

    BundleRevisionImpl bri = (BundleRevisionImpl) bundle.adapt(BundleRevision.class);
    List<BundleRequirement> reqs = bri.getDeclaredRequirements(BundleRevision.HOST_NAMESPACE);
    List<BundleCapability> caps = getCapabilities(BundleRevision.HOST_NAMESPACE);
    BundleWire bw = new BundleWireImpl(bri, reqs.get(0), m_systemBundleRevision, caps.get(0));
    bri.resolve(
        new BundleWiringImpl(
            m_logger,
            m_configMap,
            null,
            bri,
            null,
            Collections.singletonList(bw),
            Collections.EMPTY_MAP,
            Collections.EMPTY_MAP));
    felix.getDependencies().addDependent(bw);
    felix.setBundleStateAndNotify(bundle, Bundle.RESOLVED);
  }
Ejemplo n.º 9
0
  private List<Definition> resolveAspectsForBundle(
      final List<String> fingerprintElements,
      final Bundle bundle,
      final BundleRevision bundleRevision) {
    final List<Definition> result = new ArrayList<Definition>();
    final BundleWiring wiring = bundleRevision.getWiring();

    if (wiring != null && weavingBundleContext != null) {

      Definition aspects = null;

      // fragments
      for (final BundleWire hostWire : wiring.getProvidedWires(HostNamespace.HOST_NAMESPACE)) {
        final Bundle fragmentBundle = hostWire.getRequirer().getBundle();
        if (fragmentBundle != null) {
          aspects = aspectAdmin.getAspectDefinition(fragmentBundle);
          if (aspects != null) {
            result.add(aspects);
            fingerprintElements.add(
                fragmentBundle.getSymbolicName()
                    + ":" //$NON-NLS-1$
                    + hostWire.getRequirer().getVersion().toString());
          }
        }
      }

      // required bundles
      final List<BundleWire> requiredBundles =
          wiring.getRequiredWires(BundleNamespace.BUNDLE_NAMESPACE);
      ManifestElement[] requireHeaders = null;
      if (!requiredBundles.isEmpty()) {
        try {
          requireHeaders =
              ManifestElement.parseHeader(
                  Constants.REQUIRE_BUNDLE,
                  bundle.getHeaders("").get(Constants.REQUIRE_BUNDLE)); // $NON-NLS-1$
        } catch (final BundleException e) {
        }
      }
      for (final BundleWire requiredBundleWire : requiredBundles) {
        final Bundle requiredBundle = requiredBundleWire.getProvider().getBundle();
        if (requiredBundle != null) {
          final int applyPolicy =
              getApplyAspectsPolicy(requireHeaders, requiredBundle.getSymbolicName());

          aspects = aspectAdmin.resolveRequiredBundle(requiredBundle, applyPolicy);

          if (aspects != null) {
            result.add(aspects);
            fingerprintElements.add(
                requiredBundle.getSymbolicName()
                    + ":" //$NON-NLS-1$
                    + requiredBundleWire.getProvider().getVersion().toString());
          }
        }
      }

      // imported packages
      final List<BundleWire> importedPackages =
          wiring.getRequiredWires(PackageNamespace.PACKAGE_NAMESPACE);
      ManifestElement[] importHeaders = null;
      if (!importedPackages.isEmpty()) {
        try {
          importHeaders =
              ManifestElement.parseHeader(
                  Constants.IMPORT_PACKAGE,
                  bundle.getHeaders("").get(Constants.IMPORT_PACKAGE)); // $NON-NLS-1$
        } catch (final BundleException e) {
        }
      }
      for (final BundleWire importPackageWire : importedPackages) {
        final Bundle exportingBundle = importPackageWire.getProvider().getBundle();
        if (exportingBundle != null) {
          final String importedPackage =
              (String)
                  importPackageWire
                      .getCapability()
                      .getAttributes()
                      .get(PackageNamespace.PACKAGE_NAMESPACE);

          final int applyPolicy = getApplyAspectsPolicy(importHeaders, importedPackage);

          aspects =
              aspectAdmin.resolveImportedPackage(exportingBundle, importedPackage, applyPolicy);

          if (aspects != null) {
            result.add(aspects);
            final Object v =
                importPackageWire
                    .getCapability()
                    .getAttributes()
                    .get(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
            final String version = v == null ? Version.emptyVersion.toString() : v.toString();
            fingerprintElements.add(
                importedPackage
                    + ":" //$NON-NLS-1$
                    + version);
          }
        }
      }

      // supplementers
      final Supplementer[] supplementers =
          this.supplementerRegistry.getSupplementers(bundleRevision.getBundle().getBundleId());

      for (int i = 0; i < supplementers.length; i++) {
        aspects =
            aspectAdmin.getExportedAspectDefinitions(supplementers[i].getSupplementerBundle());
        if (aspects != null) {
          result.add(aspects);
          fingerprintElements.add(
              supplementers[i].getSymbolicName()
                  + ":" //$NON-NLS-1$
                  + getBundleVersion(supplementers[i].getSupplementerBundle()));
        }
      }

      // this bundle
      aspects = aspectAdmin.getAspectDefinition(bundle);
      if (aspects != null) {
        final String finishedValue =
            bundle
                .getHeaders("")
                .get( //$NON-NLS-1$
                    AspectAdmin.AOP_BUNDLE_FINISHED_HEADER);
        if (finishedValue == null || !AspectAdmin.AOP_BUNDLE_FINISHED_VALUE.equals(finishedValue)) {
          result.add(aspects);
          fingerprintElements.add(
              bundle.getSymbolicName()
                  + ":" //$NON-NLS-1$
                  + bundleRevision.getVersion().toString());
        }
      }
    }

    return result;
  }