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 int getState() {
   switch (module.getState()) {
     case INSTALLED:
       return Bundle.INSTALLED;
     case RESOLVED:
       return Bundle.RESOLVED;
     case STARTING:
     case LAZY_STARTING:
       return Bundle.STARTING;
     case ACTIVE:
       return Bundle.ACTIVE;
     case STOPPING:
       return Bundle.STOPPING;
     case UNINSTALLED:
       return Bundle.UNINSTALLED;
     default:
       throw new IllegalStateException(
           "No valid bundle state for module state: " + module.getState()); // $NON-NLS-1$
   }
 }
 BundleContextImpl createBundleContext(boolean checkPermission) {
   if (isFragment()) {
     // fragments cannot have contexts
     return null;
   }
   synchronized (this.monitor) {
     if (context == null) {
       // only create the context if we are starting, active or stopping
       // this is so that SCR can get the context for lazy-start bundles
       if (Module.ACTIVE_SET.contains(module.getState())) {
         context = new BundleContextImpl(this, equinoxContainer);
       }
     }
     return context;
   }
 }
 @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$
 }
 private final void checkValid() {
   if (module.getState().equals(State.UNINSTALLED))
     throw new IllegalStateException("Module has been uninstalled."); // $NON-NLS-1$
 }
  @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;
  }
 ResolutionReport resolve() {
   if (!Module.RESOLVED_SET.contains(module.getState())) {
     return module.getContainer().resolve(Arrays.asList(module), true);
   }
   return null;
 }