Пример #1
0
 private Map<String, Plugin> loadPluginsFromClasspath(Settings settings) {
   Map<String, Plugin> plugins = newHashMap();
   Enumeration<URL> pluginUrls = null;
   try {
     pluginUrls = settings.getClassLoader().getResources("es-plugin.properties");
   } catch (IOException e) {
     logger.warn("Failed to find plugins from classpath", e);
   }
   while (pluginUrls.hasMoreElements()) {
     URL pluginUrl = pluginUrls.nextElement();
     Properties pluginProps = new Properties();
     InputStream is = null;
     try {
       is = pluginUrl.openStream();
       pluginProps.load(is);
       String sPluginClass = pluginProps.getProperty("plugin");
       Class<?> pluginClass = settings.getClassLoader().loadClass(sPluginClass);
       Plugin plugin = (Plugin) pluginClass.newInstance();
       plugins.put(plugin.name(), plugin);
     } catch (Exception e) {
       logger.warn("Failed to load plugin from [" + pluginUrl + "]", e);
     } finally {
       if (is != null) {
         try {
           is.close();
         } catch (IOException e) {
           // ignore
         }
       }
     }
   }
   return plugins;
 }
Пример #2
0
  private void loadPluginsIntoClassLoader() {
    File pluginsFile = environment.pluginsFile();
    if (!pluginsFile.exists()) {
      return;
    }
    if (!pluginsFile.isDirectory()) {
      return;
    }

    ClassLoader classLoader = settings.getClassLoader();
    Class classLoaderClass = classLoader.getClass();
    Method addURL = null;
    while (!classLoaderClass.equals(Object.class)) {
      try {
        addURL = classLoaderClass.getDeclaredMethod("addURL", URL.class);
        addURL.setAccessible(true);
        break;
      } catch (NoSuchMethodException e) {
        // no method, try the parent
        classLoaderClass = classLoaderClass.getSuperclass();
      }
    }
    if (addURL == null) {
      logger.debug(
          "Failed to find addURL method on classLoader [" + classLoader + "] to add methods");
      return;
    }

    File[] pluginsFiles = pluginsFile.listFiles();
    for (File pluginFile : pluginsFiles) {
      if (!pluginFile.getName().endsWith(".zip")) {
        continue;
      }
      if (logger.isTraceEnabled()) {
        logger.trace("Processing [{}]", pluginFile);
      }

      String pluginNameNoExtension =
          pluginFile.getName().substring(0, pluginFile.getName().lastIndexOf('.'));
      File extractedPluginDir =
          new File(new File(environment.workFile(), "plugins"), pluginNameNoExtension);
      extractedPluginDir.mkdirs();

      File stampsDir = new File(new File(environment.workFile(), "plugins"), "_stamps");
      stampsDir.mkdirs();

      boolean extractPlugin = true;
      File stampFile = new File(stampsDir, pluginNameNoExtension + ".stamp");
      if (stampFile.exists()) {
        // read it, and check if its the same size as the pluginFile
        RandomAccessFile raf = null;
        try {
          raf = new RandomAccessFile(stampFile, "r");
          long size = raf.readLong();
          if (size == pluginFile.length()) {
            extractPlugin = false;
            if (logger.isTraceEnabled()) {
              logger.trace("--- No need to extract plugin, same size [" + size + "]");
            }
          }
        } catch (Exception e) {
          // ignore and extract the plugin
        } finally {
          if (raf != null) {
            try {
              raf.close();
            } catch (IOException e) {
              // ignore
            }
          }
        }
      }

      if (extractPlugin) {
        if (logger.isTraceEnabled()) {
          logger.trace("--- Extracting plugin to [" + extractedPluginDir + "]");
        }
        deleteRecursively(extractedPluginDir, false);

        ZipFile zipFile = null;
        try {
          zipFile = new ZipFile(pluginFile);
          Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
          while (zipEntries.hasMoreElements()) {
            ZipEntry zipEntry = zipEntries.nextElement();
            if (!(zipEntry.getName().endsWith(".jar") || zipEntry.getName().endsWith(".zip"))) {
              continue;
            }
            String name = zipEntry.getName().replace('\\', '/');
            File target = new File(extractedPluginDir, name);
            Streams.copy(zipFile.getInputStream(zipEntry), new FileOutputStream(target));
          }
        } catch (Exception e) {
          logger.warn("Failed to extract plugin [" + pluginFile + "], ignoring...", e);
          continue;
        } finally {
          if (zipFile != null) {
            try {
              zipFile.close();
            } catch (IOException e) {
              // ignore
            }
          }
        }

        try {
          RandomAccessFile raf = new RandomAccessFile(stampFile, "rw");
          raf.writeLong(pluginFile.length());
          raf.close();
        } catch (Exception e) {
          // ignore
        }
      }

      try {
        for (File jarToAdd : extractedPluginDir.listFiles()) {
          if (!(jarToAdd.getName().endsWith(".jar") || jarToAdd.getName().endsWith(".zip"))) {
            continue;
          }
          addURL.invoke(classLoader, jarToAdd.toURI().toURL());
        }
      } catch (Exception e) {
        logger.warn("Failed to add plugin [" + pluginFile + "]", e);
      }
    }
  }