@Override
  public Map<X509Certificate, List<X509Certificate>> getSignerCertificates(int signersType) {
    SignedContentFactory factory = equinoxContainer.getSignedContentFactory();
    if (factory == null) {
      return Collections.emptyMap();
    }

    try {
      SignerInfo[] infos = signerInfos;
      if (infos == null) {
        SignedContent signedContent = factory.getSignedContent(this);
        infos = signedContent.getSignerInfos();
        signerInfos = infos;
      }
      if (infos.length == 0) return Collections.emptyMap();
      Map<X509Certificate, List<X509Certificate>> results =
          new HashMap<X509Certificate, List<X509Certificate>>(infos.length);
      for (int i = 0; i < infos.length; i++) {
        if (signersType == SIGNERS_TRUSTED && !infos[i].isTrusted()) continue;
        Certificate[] certs = infos[i].getCertificateChain();
        if (certs == null || certs.length == 0) continue;
        List<X509Certificate> certChain = new ArrayList<X509Certificate>();
        for (int j = 0; j < certs.length; j++) certChain.add((X509Certificate) certs[j]);
        results.put((X509Certificate) certs[0], certChain);
      }
      return results;
    } catch (Exception e) {
      return Collections.emptyMap();
    }
  }
 private ModuleClassLoader getModuleClassLoader(boolean logResolveError) {
   ResolutionReport report = resolve();
   if (logResolveError && !Module.RESOLVED_SET.contains(module.getState())) {
     String reportMessage = report.getResolutionReportMessage(module.getCurrentRevision());
     equinoxContainer
         .getEventPublisher()
         .publishFrameworkEvent(
             FrameworkEvent.ERROR,
             this,
             new BundleException(reportMessage, BundleException.RESOLVE_ERROR));
   }
   return AccessController.doPrivileged(
       new PrivilegedAction<ModuleClassLoader>() {
         @Override
         public ModuleClassLoader run() {
           ModuleWiring wiring = getModule().getCurrentRevision().getWiring();
           if (wiring != null) {
             ModuleLoader moduleLoader = wiring.getModuleLoader();
             if (moduleLoader instanceof BundleLoader) {
               return ((BundleLoader) moduleLoader).getModuleClassLoader();
             }
           }
           return null;
         }
       });
 }
 @Override
 public ServiceReference<?>[] getRegisteredServices() {
   checkValid();
   BundleContextImpl current = getBundleContextImpl();
   return current == null
       ? null
       : equinoxContainer.getServiceRegistry().getRegisteredServices(current);
 }
 @Override
 public void uninstall() throws BundleException {
   // be sure to prime the headers with default local; calling priv method to avoid permission
   // check
   privGetHeaders(null);
   Storage storage = equinoxContainer.getStorage();
   storage.getModuleContainer().uninstall(module);
 }
 @Override
 public void stop(int options) throws BundleException {
   if (options == 0 && equinoxContainer.getConfiguration().getDebug().MONITOR_ACTIVATION) {
     Debug.printStackTrace(
         new Exception("A persistent stop has been called on bundle: " + this)); // $NON-NLS-1$
   }
   module.stop(getStopOptions(options));
 }
 @Override
 public void update(InputStream input) throws BundleException {
   try {
     Storage storage = equinoxContainer.getStorage();
     storage.update(module, storage.getContentConnection(module, null, input));
     signerInfos = null;
   } catch (IOException e) {
     throw new BundleException("Error reading bundle content.", e); // $NON-NLS-1$
   }
 }
 @Override
 public URL getEntry(String path) {
   try {
     equinoxContainer.checkAdminPermission(this, AdminPermission.RESOURCE);
   } catch (SecurityException e) {
     return null;
   }
   checkValid();
   Generation current = (Generation) getModule().getCurrentRevision().getRevisionInfo();
   return current.getEntry(path);
 }
 @Override
 public Enumeration<URL> findEntries(String path, String filePattern, boolean recurse) {
   try {
     equinoxContainer.checkAdminPermission(this, AdminPermission.RESOURCE);
   } catch (SecurityException e) {
     return null;
   }
   checkValid();
   resolve();
   return Storage.findEntries(
       getGenerations(), path, filePattern, recurse ? BundleWiring.FINDENTRIES_RECURSE : 0);
 }
 @Override
 public Class<?> loadClass(String name) throws ClassNotFoundException {
   try {
     equinoxContainer.checkAdminPermission(this, AdminPermission.CLASS);
   } catch (SecurityException e) {
     throw new ClassNotFoundException(name, e);
   }
   checkValid();
   if (isFragment()) {
     throw new ClassNotFoundException(
         "Can not load a class from a fragment bundle: " + this); // $NON-NLS-1$
   }
   try {
     ModuleClassLoader classLoader = getModuleClassLoader(true);
     if (classLoader != null) {
       if (name.length() > 0 && name.charAt(0) == '[')
         return Class.forName(name, false, classLoader);
       return classLoader.loadClass(name);
     }
   } catch (ClassNotFoundException e) {
     // This is an equinox-ism.  Not sure it is worth it to offer an option to disable ...
     // On failure attempt to activate lazy activating bundles.
     if (State.LAZY_STARTING.equals(module.getState())) {
       try {
         module.start(StartOptions.LAZY_TRIGGER);
       } catch (BundleException e1) {
         equinoxContainer
             .getLogServices()
             .log(EquinoxContainer.NAME, FrameworkLogEntry.WARNING, e.getMessage(), e);
       }
     }
     throw e;
   }
   throw new ClassNotFoundException(
       "No class loader available for the bundle: " + this); // $NON-NLS-1$
 }
  @Override
  public URL getResource(String name) {
    try {
      equinoxContainer.checkAdminPermission(this, AdminPermission.RESOURCE);
    } catch (SecurityException e) {
      return null;
    }
    checkValid();
    if (isFragment()) {
      return null;
    }

    ModuleClassLoader classLoader = getModuleClassLoader(false);
    if (classLoader != null) {
      return classLoader.getResource(name);
    }

    return new ClasspathManager((Generation) module.getCurrentRevision().getRevisionInfo(), null)
        .findLocalResource(name);
  }
 @Override
 public Enumeration<URL> getResources(String name) throws IOException {
   try {
     equinoxContainer.checkAdminPermission(this, AdminPermission.RESOURCE);
   } catch (SecurityException e) {
     return null;
   }
   checkValid();
   if (isFragment()) {
     return null;
   }
   ModuleClassLoader classLoader = getModuleClassLoader(false);
   Enumeration<URL> result = null;
   if (classLoader != null) {
     result = classLoader.getResources(name);
   } else {
     result =
         new ClasspathManager((Generation) module.getCurrentRevision().getRevisionInfo(), null)
             .findLocalResources(name);
   }
   return result != null && result.hasMoreElements() ? result : null;
 }
  @SuppressWarnings("unchecked")
  private <A> A adapt0(Class<A> adapterType) {
    if (AccessControlContext.class.equals(adapterType)) {
      Generation current = (Generation) module.getCurrentRevision().getRevisionInfo();
      ProtectionDomain domain = current.getDomain();
      return (A)
          (domain == null ? null : new AccessControlContext(new ProtectionDomain[] {domain}));
    }

    if (BundleContext.class.equals(adapterType)) {
      try {
        return (A) getBundleContext();
      } catch (SecurityException e) {
        return null;
      }
    }

    if (BundleRevision.class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      return (A) module.getCurrentRevision();
    }

    if (BundleRevisions.class.equals(adapterType)) {
      return (A) module.getRevisions();
    }

    if (BundleStartLevel.class.equals(adapterType)) {
      return (A) module;
    }

    if (BundleWiring.class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      ModuleRevision revision = module.getCurrentRevision();
      if (revision == null) {
        return null;
      }
      return (A) revision.getWiring();
    }

    if (BundleDTO.class.equals(adapterType)) {
      // Unfortunately we need to lock here to make sure the BSN and version
      // are consistent in case of updates
      readLock();
      try {
        return (A) DTOBuilder.newBundleDTO(this);
      } finally {
        readUnlock();
      }
    }

    if (BundleStartLevelDTO.class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      return (A) DTOBuilder.newBundleStartLevelDTO(this, module);
    }

    if (BundleRevisionDTO.class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      return (A) DTOBuilder.newBundleRevisionDTO(module.getCurrentRevision());
    }

    if (BundleRevisionDTO[].class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      // No need to lock the database here since the ModuleRevisions object does the
      // proper locking for us.
      return (A) DTOBuilder.newArrayBundleRevisionDTO(module.getRevisions());
    }

    if (BundleWiringDTO.class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      readLock();
      try {
        return (A) DTOBuilder.newBundleWiringDTO(module.getCurrentRevision());
      } finally {
        readUnlock();
      }
    }

    if (BundleWiringDTO[].class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      readLock();
      try {
        return (A) DTOBuilder.newArrayBundleWiringDTO(module.getRevisions());
      } finally {
        readUnlock();
      }
    }

    if (ServiceReferenceDTO[].class.equals(adapterType)) {
      if (module.getState().equals(State.UNINSTALLED)) {
        return null;
      }
      BundleContextImpl current = getBundleContextImpl();
      ServiceReference<?>[] references =
          (current == null)
              ? null
              : equinoxContainer.getServiceRegistry().getRegisteredServices(current);
      return (A) DTOBuilder.newArrayServiceReferenceDTO(references);
    }

    if (getBundleId() == 0) {
      if (Framework.class.equals(adapterType)) {
        return (A) this;
      }

      if (FrameworkStartLevel.class.equals(adapterType)) {
        return (A) equinoxContainer.getStorage().getModuleContainer().getFrameworkStartLevel();
      }

      if (FrameworkWiring.class.equals(adapterType)) {
        return (A) equinoxContainer.getStorage().getModuleContainer().getFrameworkWiring();
      }

      if (FrameworkDTO.class.equals(adapterType)) {
        BundleContextImpl current = getBundleContextImpl();
        Map<String, String> configuration = equinoxContainer.getConfiguration().getConfiguration();
        readLock();
        try {
          return (A) DTOBuilder.newFrameworkDTO(current, configuration);
        } finally {
          readUnlock();
        }
      }

      if (FrameworkStartLevelDTO.class.equals(adapterType)) {
        return (A)
            DTOBuilder.newFrameworkStartLevelDTO(
                equinoxContainer.getStorage().getModuleContainer().getFrameworkStartLevel());
      }
    }

    // Equinox extras
    if (Module.class.equals(adapterType)) {
      return (A) module;
    }
    if (ProtectionDomain.class.equals(adapterType)) {
      Generation current = (Generation) module.getCurrentRevision().getRevisionInfo();
      return (A) current.getDomain();
    }
    return null;
  }
 private void readUnlock() {
   equinoxContainer.getStorage().getModuleDatabase().readUnlock();
 }
 @Override
 public BundleContext getBundleContext() {
   equinoxContainer.checkAdminPermission(this, AdminPermission.CONTEXT);
   return createBundleContext(true);
 }
 @Override
 public String getLocation() {
   equinoxContainer.checkAdminPermission(getBundle(), AdminPermission.METADATA);
   return module.getLocation();
 }
 @Override
 public Dictionary<String, String> getHeaders(String locale) {
   equinoxContainer.checkAdminPermission(this, AdminPermission.METADATA);
   return privGetHeaders(locale);
 }