public static ISpannerMigrationTransformer getApplyTransformationImpl()

in spanner-data-validator-java/src/main/java/com/google/migration/transform/CustomTransformationImplFetcher.java [26:75]


  public static ISpannerMigrationTransformer getApplyTransformationImpl(
      CustomTransformation customTransformation) {
    if (customTransformation == null
        || customTransformation.jarPath() == null
        || customTransformation.classPath() == null) {
      return null;
    }
    if (!customTransformation.jarPath().isEmpty() && !customTransformation.classPath().isEmpty()) {
      LOG.info(
          "Getting spanner migration transformer : "
              + customTransformation.jarPath()
              + " with class: "
              + customTransformation.classPath());
      try {
        // Get the start time of loading the custom class
        Instant startTime = Instant.now();

        // Getting the jar URL which contains target class
        URL[] classLoaderUrls = JarFileReader.saveFilesLocally(customTransformation.jarPath());

        // Create a new URLClassLoader
        URLClassLoader urlClassLoader = new URLClassLoader(classLoaderUrls);

        // Load the target class
        Class<?> customTransformationClass =
            urlClassLoader.loadClass(customTransformation.classPath());

        // Create a new instance from the loaded class
        Constructor<?> constructor = customTransformationClass.getConstructor();
        ISpannerMigrationTransformer spannerMigrationTransformation =
            (ISpannerMigrationTransformer) constructor.newInstance();
        // Get the end time of loading the custom class
        Instant endTime = Instant.now();
        LOG.info(
            "Custom jar "
                + customTransformation.jarPath()
                + ": Took "
                + (new Duration(startTime, endTime)).toString()
                + " to load");
        LOG.info(
            "Invoking init of the custom class with input as {}",
            customTransformation.customParameters());
        spannerMigrationTransformation.init(customTransformation.customParameters());
        return spannerMigrationTransformation;
      } catch (Exception e) {
        throw new RuntimeException("Error loading custom class : " + e.getMessage());
      }
    }
    return null;
  }