public synchronized Response createPlugin()

in iotdb-collector/collector-core/src/main/java/org/apache/iotdb/collector/runtime/plugin/PluginRuntime.java [95:179]


  public synchronized Response createPlugin(
      final String pluginName,
      final String className,
      final String jarName,
      final String jarMD5FromDB,
      final boolean isRestRequest) {
    try {
      // validate whether the plugin jar file exists
      if (isRestRequest && !PluginFileUtils.isPluginJarFileExist(jarName)) {
        final String errorMessage =
            String.format(
                "Failed to register Plugin %s, because the plugin jar file %s is not found",
                pluginName, jarName);
        LOGGER.warn(errorMessage);
        return Response.serverError().entity(errorMessage).build();
      }

      // validate whether the plugin has been loaded
      final PluginMeta information = metaKeeper.getPipePluginMeta(pluginName);
      if (Objects.nonNull(information)) {
        // validate whether the plugin is builtin plugin
        if (information.isBuiltin()) {
          final String errorMessage =
              String.format(
                  "Failed to register Plugin %s, because the given Plugin name is the same as a built-in Plugin name.",
                  pluginName);
          LOGGER.warn(errorMessage);
          return Response.serverError().entity(errorMessage).build();
        }

        // otherwise the plugin has been registered
        final String errorMessage =
            String.format(
                "Failed to register Plugin %s, because the Plugin has been registered.",
                pluginName);
        LOGGER.warn(errorMessage);
        return Response.serverError().entity(errorMessage).build();
      }

      // get the plugin jar md5
      final String jarMD5 =
          jarMD5FromDB == null
              ? DigestUtils.md5Hex(
                  Files.newInputStream(Paths.get(PluginFileUtils.getPluginJarFilePath(jarName))))
              : jarMD5FromDB;

      // If the {pluginName} directory already exists, delete the directory and the files under it,
      // recreate the directory, and move the files to the new directory. If an exception occurs in
      // the middle, delete the created directory.
      final String pluginJarFileNameWithMD5 =
          PluginFileUtils.getPluginJarFileNameWithMD5(jarName, jarMD5);
      PluginFileUtils.savePluginToInstallDir(pluginName, jarName, pluginJarFileNameWithMD5);

      // create and save plugin class loader
      final PluginClassLoader classLoader =
          classLoaderManager.createPluginClassLoader(
              PluginFileUtils.getPluginInstallDirPath(pluginName));

      final Class<?> pluginClass = Class.forName(className, true, classLoader);
      @SuppressWarnings("unused") // ensure that it is a PipePlugin class
      final PipePlugin ignored = (PipePlugin) pluginClass.getDeclaredConstructor().newInstance();

      classLoaderManager.addPluginClassLoader(pluginName, classLoader);
      metaKeeper.addPipePluginMeta(
          pluginName, new PluginMeta(pluginName, className, false, jarName, jarMD5));
      metaKeeper.addJarNameAndMd5(jarName, jarMD5);

      // storage registered plugin info
      if (isRestRequest) {
        PersistenceService.plugin()
            .ifPresent(
                pluginPersistence ->
                    pluginPersistence.tryPersistencePlugin(pluginName, className, jarName, jarMD5));
      }

      final String successMessage = String.format("Successfully register Plugin %s", pluginName);
      LOGGER.info(successMessage);
      return Response.ok().entity(successMessage).build();
    } catch (final Exception e) {
      final String errorMessage =
          String.format("Failed to register Plugin %s, because %s", pluginName, e);
      LOGGER.warn(errorMessage);
      return Response.serverError().entity(errorMessage).build();
    }
  }