/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.gobblin.azkaban;

import java.io.IOException;
import java.util.Properties;

import azkaban.jobExecutor.AbstractJob;

import com.google.common.base.Optional;

import org.apache.log4j.Logger;

import org.apache.gobblin.compaction.Compactor;
import org.apache.gobblin.compaction.CompactorFactory;
import org.apache.gobblin.compaction.CompactorCreationException;
import org.apache.gobblin.compaction.listeners.CompactorListener;
import org.apache.gobblin.compaction.listeners.CompactorListenerCreationException;
import org.apache.gobblin.compaction.listeners.CompactorListenerFactory;
import org.apache.gobblin.compaction.ReflectionCompactorFactory;
import org.apache.gobblin.compaction.listeners.ReflectionCompactorListenerFactory;
import org.apache.gobblin.metrics.Tag;


/**
 * A class for launching a Gobblin MR job for compaction through Azkaban.
 */
public class AzkabanCompactionJobLauncher extends AbstractJob {

  private static final Logger LOG = Logger.getLogger(AzkabanCompactionJobLauncher.class);

  private final Properties properties;
  private final Compactor compactor;

  public AzkabanCompactionJobLauncher(String jobId, Properties props) {
    super(jobId, LOG);
    this.properties = new Properties();
    this.properties.putAll(props);
    this.compactor = getCompactor(getCompactorFactory(), getCompactorListener(getCompactorListenerFactory()));
  }

  private Compactor getCompactor(CompactorFactory compactorFactory, Optional<CompactorListener> compactorListener) {
    try {
      return compactorFactory
          .createCompactor(this.properties, Tag.fromMap(AzkabanTags.getAzkabanTags()), compactorListener);
    } catch (CompactorCreationException e) {
      throw new RuntimeException("Unable to create compactor", e);
    }
  }

  protected CompactorFactory getCompactorFactory() {
    return new ReflectionCompactorFactory();
  }

  private Optional<CompactorListener> getCompactorListener(CompactorListenerFactory compactorListenerFactory) {
    try {
      return compactorListenerFactory.createCompactorListener(this.properties);
    } catch (CompactorListenerCreationException e) {
      throw new RuntimeException("Unable to create compactor listener", e);
    }
  }

  protected CompactorListenerFactory getCompactorListenerFactory() {
    return new ReflectionCompactorListenerFactory();
  }

  @Override
  public void run() throws Exception {
    this.compactor.compact();
  }

  @Override
  public void cancel() throws IOException {
    this.compactor.cancel();
  }
}

@@ -856,24 +856,46 @@
 OException;%0A
+import java.util.Map;%0A
 import java.
@@ -992,16 +992,91 @@
 ptional;
+%0Aimport com.typesafe.config.Config;%0Aimport com.typesafe.config.ConfigValue;
 %0A%0Aimport
@@ -1670,104 +1670,401 @@
 lin.
-metrics.Tag;%0A%0A%0A/**%0A * A class for launching a Gobblin MR job for compaction through Azkaban.%0A */
+configuration.DynamicConfigGenerator;%0Aimport org.apache.gobblin.metrics.Tag;%0Aimport org.apache.gobblin.runtime.DynamicConfigGeneratorFactory;%0Aimport org.apache.gobblin.util.ConfigUtils;%0A%0A%0A/**%0A * A class for launching a Gobblin MR job for compaction through Azkaban.%0A * @deprecated use %7B@link AzkabanJobLauncher%7D and %7B@link org.apache.gobblin.compaction.source.CompactionSource%7D%0A */%0A@Deprecated
 %0Apub
@@ -2459,24 +2459,599 @@
 All(props);%0A
+%0A    // load dynamic configuration and add them to the job properties%0A    Config propsAsConfig = ConfigUtils.propertiesToConfig(props);%0A    DynamicConfigGenerator dynamicConfigGenerator =%0A        DynamicConfigGeneratorFactory.createDynamicConfigGenerator(propsAsConfig);%0A    Config dynamicConfig = dynamicConfigGenerator.generateDynamicConfig(propsAsConfig);%0A%0A    // add the dynamic config to the job config%0A    for (Map.Entry%3CString, ConfigValue%3E entry : dynamicConfig.entrySet()) %7B%0A      this.properties.put(entry.getKey(), entry.getValue().unwrapped().toString());%0A    %7D%0A%0A
     this.com

Support DynamicConfig in AzkabanCompactionJobLauncher