public static void loadDescriptors(
     String pluginsPath,
     List<IdeaPluginDescriptorImpl> result,
     @Nullable StartupProgress progress,
     int pluginsCount) {
   final File pluginsHome = new File(pluginsPath);
   final File[] files = pluginsHome.listFiles();
   if (files != null) {
     int i = result.size();
     for (File file : files) {
       final IdeaPluginDescriptorImpl descriptor = loadDescriptor(file, PLUGIN_XML);
       if (descriptor == null) continue;
       if (progress != null) {
         progress.showProgress(
             descriptor.getName(), PLUGINS_PROGRESS_MAX_VALUE * ((float) ++i / pluginsCount));
       }
       int oldIndex = result.indexOf(descriptor);
       if (oldIndex >= 0) {
         final IdeaPluginDescriptorImpl oldDescriptor = result.get(oldIndex);
         if (StringUtil.compareVersionNumbers(oldDescriptor.getVersion(), descriptor.getVersion())
             < 0) {
           result.set(oldIndex, descriptor);
         }
       } else {
         result.add(descriptor);
       }
     }
   }
 }
 static IdeaPluginDescriptorImpl[] findCorePlugin(IdeaPluginDescriptorImpl[] pluginDescriptors) {
   for (IdeaPluginDescriptorImpl descriptor : pluginDescriptors) {
     if (CORE_PLUGIN_ID.equals(descriptor.getPluginId().getIdString())) {
       return new IdeaPluginDescriptorImpl[] {descriptor};
     }
   }
   return IdeaPluginDescriptorImpl.EMPTY_ARRAY;
 }
 public static void initClassLoader(
     @NotNull ClassLoader parentLoader, @NotNull IdeaPluginDescriptorImpl descriptor) {
   final List<File> classPath = descriptor.getClassPath();
   final ClassLoader loader =
       createPluginClassLoader(
           classPath.toArray(new File[classPath.size()]),
           new ClassLoader[] {parentLoader},
           descriptor);
   descriptor.setLoader(loader, false);
 }
 public static boolean shouldSkipPlugin(final IdeaPluginDescriptor descriptor) {
   if (descriptor instanceof IdeaPluginDescriptorImpl) {
     IdeaPluginDescriptorImpl descriptorImpl = (IdeaPluginDescriptorImpl) descriptor;
     Boolean skipped = descriptorImpl.getSkipped();
     if (skipped != null) {
       return skipped.booleanValue();
     }
     boolean result = shouldSkipPlugin(descriptor, ourPlugins);
     descriptorImpl.setSkipped(result);
     return result;
   }
   return shouldSkipPlugin(descriptor, ourPlugins);
 }
 static void mergeOptionalConfigs(Map<PluginId, IdeaPluginDescriptorImpl> descriptors) {
   final Map<PluginId, IdeaPluginDescriptorImpl> descriptorsWithModules =
       new HashMap<PluginId, IdeaPluginDescriptorImpl>(descriptors);
   addModulesAsDependents(descriptorsWithModules);
   for (IdeaPluginDescriptorImpl descriptor : descriptors.values()) {
     final Map<PluginId, IdeaPluginDescriptorImpl> optionalDescriptors =
         descriptor.getOptionalDescriptors();
     if (optionalDescriptors != null && !optionalDescriptors.isEmpty()) {
       for (Map.Entry<PluginId, IdeaPluginDescriptorImpl> entry : optionalDescriptors.entrySet()) {
         if (descriptorsWithModules.containsKey(entry.getKey())) {
           descriptor.mergeOptionalConfig(entry.getValue());
         }
       }
     }
   }
 }
  @Nullable
  static IdeaPluginDescriptorImpl loadDescriptorFromDir(final File file, @NonNls String fileName) {
    IdeaPluginDescriptorImpl descriptor = null;
    File descriptorFile = new File(file, META_INF + File.separator + fileName);
    if (descriptorFile.exists()) {
      descriptor = new IdeaPluginDescriptorImpl(file);

      try {
        descriptor.readExternal(descriptorFile.toURI().toURL());
      } catch (Exception e) {
        System.err.println("Cannot load: " + descriptorFile.getAbsolutePath());
        e.printStackTrace();
      }
    }
    return descriptor;
  }
  @Nullable
  static IdeaPluginDescriptorImpl loadDescriptorFromJar(File file, @NonNls String fileName) {
    try {
      URI fileURL = file.toURI();
      URL jarURL =
          new URL(
              "jar:"
                  + StringUtil.replace(fileURL.toASCIIString(), "!", "%21")
                  + "!/META-INF/"
                  + fileName);

      IdeaPluginDescriptorImpl descriptor = new IdeaPluginDescriptorImpl(file);
      FileInputStream in = new FileInputStream(file);
      ZipInputStream zipStream = new ZipInputStream(in);
      try {
        ZipEntry entry = zipStream.getNextEntry();
        if (entry.getName().equals(JarMemoryLoader.SIZE_ENTRY)) {
          entry = zipStream.getNextEntry();
          if (entry.getName().equals("META-INF/" + fileName)) {
            byte[] content = FileUtil.loadBytes(zipStream, (int) entry.getSize());
            Document document = JDOMUtil.loadDocument(new ByteArrayInputStream(content));
            descriptor.readExternal(document, jarURL);
            return descriptor;
          }
        }
      } finally {
        zipStream.close();
        in.close();
      }

      descriptor.readExternal(jarURL);
      return descriptor;
    } catch (XmlSerializationException e) {
      getLogger().info("Cannot load " + file, e);
      prepareLoadingPluginsErrorMessage(
          "Plugin file " + file.getName() + " contains invalid plugin descriptor file.");
    } catch (FileNotFoundException e) {
      return null;
    } catch (Exception e) {
      getLogger().info("Cannot load " + file, e);
    } catch (Throwable e) {
      getLogger().info("Cannot load " + file, e);
    }

    return null;
  }
  private static void registerExtensionPointsAndExtensions(
      ExtensionsArea area, List<IdeaPluginDescriptorImpl> loadedPlugins) {
    for (IdeaPluginDescriptorImpl descriptor : loadedPlugins) {
      descriptor.registerExtensionPoints(area);
    }

    Set<String> epNames = ContainerUtil.newHashSet();
    for (ExtensionPoint point : area.getExtensionPoints()) {
      epNames.add(point.getName());
    }

    for (IdeaPluginDescriptorImpl descriptor : loadedPlugins) {
      for (String epName : epNames) {
        descriptor.registerExtensions(area, epName);
      }
    }
  }
  static void loadDescriptorsFromClassPath(
      @NotNull List<IdeaPluginDescriptorImpl> result, @Nullable StartupProgress progress) {
    Collection<URL> urls = getClassLoaderUrls();
    String platformPrefix = System.getProperty(PlatformUtilsCore.PLATFORM_PREFIX_KEY);
    int i = 0;
    for (URL url : urls) {
      i++;
      if ("file".equals(url.getProtocol())) {
        File file = new File(decodeUrl(url.getFile()));

        IdeaPluginDescriptorImpl platformPluginDescriptor = null;
        if (platformPrefix != null) {
          platformPluginDescriptor = loadDescriptor(file, platformPrefix + "Plugin.xml");
          if (platformPluginDescriptor != null && !result.contains(platformPluginDescriptor)) {
            platformPluginDescriptor.setUseCoreClassLoader(true);
            result.add(platformPluginDescriptor);
          }
        }

        IdeaPluginDescriptorImpl pluginDescriptor = loadDescriptor(file, PLUGIN_XML);
        if (platformPrefix != null
            && pluginDescriptor != null
            && pluginDescriptor.getName().equals(SPECIAL_IDEA_PLUGIN)) {
          continue;
        }
        if (pluginDescriptor != null && !result.contains(pluginDescriptor)) {
          if (platformPluginDescriptor != null) {
            // if we found a regular plugin.xml in the same .jar/root as a platform-prefixed
            // descriptor, use the core loader for it too
            pluginDescriptor.setUseCoreClassLoader(true);
          }
          result.add(pluginDescriptor);
          if (progress != null) {
            progress.showProgress(
                "Plugin loaded: " + pluginDescriptor.getName(),
                PLUGINS_PROGRESS_MAX_VALUE * ((float) i / urls.size()));
          }
        }
      }
    }
  }
  @SuppressWarnings({"HardCodedStringLiteral"})
  @Nullable
  public static IdeaPluginDescriptorImpl loadDescriptor(
      final File file, @NonNls final String fileName) {
    IdeaPluginDescriptorImpl descriptor = null;

    if (file.isDirectory()) {
      descriptor = loadDescriptorFromDir(file, fileName);

      if (descriptor == null) {
        File libDir = new File(file, "lib");
        if (!libDir.isDirectory()) {
          return null;
        }
        final File[] files = libDir.listFiles();
        if (files == null || files.length == 0) {
          return null;
        }
        Arrays.sort(
            files,
            new Comparator<File>() {
              @Override
              public int compare(File o1, File o2) {
                if (o2.getName().startsWith(file.getName())) return Integer.MAX_VALUE;
                if (o1.getName().startsWith(file.getName())) return -Integer.MAX_VALUE;
                if (o2.getName().startsWith("resources")) return -Integer.MAX_VALUE;
                if (o1.getName().startsWith("resources")) return Integer.MAX_VALUE;
                return 0;
              }
            });
        for (final File f : files) {
          if (FileUtil.isJarOrZip(f)) {
            descriptor = loadDescriptorFromJar(f, fileName);
            if (descriptor != null) {
              descriptor.setPath(file);
              break;
            }
            //           getLogger().warn("Cannot load descriptor from " + f.getName() + "");
          } else if (f.isDirectory()) {
            IdeaPluginDescriptorImpl descriptor1 = loadDescriptorFromDir(f, fileName);
            if (descriptor1 != null) {
              if (descriptor != null) {
                getLogger()
                    .info("Cannot load " + file + " because two or more plugin.xml's detected");
                return null;
              }
              descriptor = descriptor1;
              descriptor.setPath(file);
            }
          }
        }
      }
    } else if (StringUtil.endsWithIgnoreCase(file.getName(), ".jar") && file.exists()) {
      descriptor = loadDescriptorFromJar(file, fileName);
    }

    if (descriptor != null && !descriptor.getOptionalConfigs().isEmpty()) {
      final Map<PluginId, IdeaPluginDescriptorImpl> descriptors =
          new HashMap<PluginId, IdeaPluginDescriptorImpl>(descriptor.getOptionalConfigs().size());
      for (Map.Entry<PluginId, String> entry : descriptor.getOptionalConfigs().entrySet()) {
        String optionalDescriptorName = entry.getValue();
        assert !Comparing.equal(fileName, optionalDescriptorName)
            : "recursive dependency: " + fileName;

        IdeaPluginDescriptorImpl optionalDescriptor = loadDescriptor(file, optionalDescriptorName);
        if (optionalDescriptor == null && !FileUtil.isJarOrZip(file)) {
          for (URL url : getClassLoaderUrls()) {
            if ("file".equals(url.getProtocol())) {
              optionalDescriptor =
                  loadDescriptor(new File(decodeUrl(url.getFile())), optionalDescriptorName);
              if (optionalDescriptor != null) {
                break;
              }
            }
          }
        }
        if (optionalDescriptor != null) {
          descriptors.put(entry.getKey(), optionalDescriptor);
        } else {
          getLogger().info("Cannot find optional descriptor " + optionalDescriptorName);
        }
      }
      descriptor.setOptionalDescriptors(descriptors);
    }
    return descriptor;
  }
  static void initializePlugins(@Nullable StartupProgress progress) {
    configureExtensions();

    final IdeaPluginDescriptorImpl[] pluginDescriptors = loadDescriptors(progress);

    final Class callerClass = ReflectionUtil.findCallerClass(1);
    assert callerClass != null;
    final ClassLoader parentLoader = callerClass.getClassLoader();

    final List<IdeaPluginDescriptorImpl> result = new ArrayList<IdeaPluginDescriptorImpl>();
    final HashMap<String, String> disabledPluginNames = new HashMap<String, String>();
    for (IdeaPluginDescriptorImpl descriptor : pluginDescriptors) {
      if (descriptor.getPluginId().getIdString().equals(CORE_PLUGIN_ID)) {
        final List<String> modules = descriptor.getModules();
        if (modules != null) {
          ourAvailableModules.addAll(modules);
        }
      }

      if (!shouldSkipPlugin(descriptor, pluginDescriptors)) {
        result.add(descriptor);
      } else {
        descriptor.setEnabled(false);
        disabledPluginNames.put(descriptor.getPluginId().getIdString(), descriptor.getName());
        initClassLoader(parentLoader, descriptor);
      }
    }

    prepareLoadingPluginsErrorMessage(filterBadPlugins(result, disabledPluginNames));

    final Map<PluginId, IdeaPluginDescriptorImpl> idToDescriptorMap =
        new HashMap<PluginId, IdeaPluginDescriptorImpl>();
    for (final IdeaPluginDescriptorImpl descriptor : result) {
      idToDescriptorMap.put(descriptor.getPluginId(), descriptor);
    }

    final IdeaPluginDescriptor corePluginDescriptor =
        idToDescriptorMap.get(PluginId.getId(CORE_PLUGIN_ID));
    assert corePluginDescriptor != null
        : CORE_PLUGIN_ID
            + " not found; platform prefix is "
            + System.getProperty(PlatformUtilsCore.PLATFORM_PREFIX_KEY);
    for (IdeaPluginDescriptorImpl descriptor : result) {
      if (descriptor != corePluginDescriptor) {
        descriptor.insertDependency(corePluginDescriptor);
      }
    }

    mergeOptionalConfigs(idToDescriptorMap);

    // sort descriptors according to plugin dependencies
    Collections.sort(result, getPluginDescriptorComparator(idToDescriptorMap));

    for (int i = 0; i < result.size(); i++) {
      ourId2Index.put(result.get(i).getPluginId(), i);
    }

    int i = 0;
    for (final IdeaPluginDescriptorImpl pluginDescriptor : result) {
      if (pluginDescriptor.getPluginId().getIdString().equals(CORE_PLUGIN_ID)
          || pluginDescriptor.isUseCoreClassLoader()) {
        pluginDescriptor.setLoader(parentLoader, true);
      } else {
        final List<File> classPath = pluginDescriptor.getClassPath();
        final PluginId[] dependentPluginIds = pluginDescriptor.getDependentPluginIds();
        final ClassLoader[] parentLoaders = getParentLoaders(idToDescriptorMap, dependentPluginIds);

        final ClassLoader pluginClassLoader =
            createPluginClassLoader(
                classPath.toArray(new File[classPath.size()]),
                parentLoaders.length > 0 ? parentLoaders : new ClassLoader[] {parentLoader},
                pluginDescriptor);
        pluginDescriptor.setLoader(pluginClassLoader, true);
      }

      pluginDescriptor.registerExtensions();
      if (progress != null) {
        progress.showProgress(
            "", PLUGINS_PROGRESS_MAX_VALUE + (i++ / (float) result.size()) * 0.35f);
      }
    }

    ourPlugins = pluginDescriptors;
  }