/*
 * 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.reef.examples.hello;

import org.apache.reef.client.DriverConfiguration;
import org.apache.reef.client.DriverLauncher;
import org.apache.reef.client.LauncherStatus;
import org.apache.reef.runtime.yarn.client.YarnClientConfiguration;
import org.apache.reef.tang.Configuration;
import org.apache.reef.tang.exceptions.BindException;
import org.apache.reef.tang.exceptions.InjectionException;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The Client for running HelloREEF on YARN.
 */
public final class HelloREEFYarn {

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

  /**
   * Number of milliseconds to wait for the job to complete.
   * Setting to 100 sec because running on RM HA clusters take around
   * 50 seconds to set the job to running.
   */
  private static final int JOB_TIMEOUT = 100000; // 100 sec.

  /**
   * @return the configuration of the runtime
   */
  private static Configuration getRuntimeConfiguration() {
    return YarnClientConfiguration.CONF.build();
  }

  /**
   * @return the configuration of the HelloREEF driver.
   */
  private static Configuration getDriverConfiguration() {
    return DriverConfiguration.CONF
        .set(DriverConfiguration.GLOBAL_LIBRARIES,
            HelloREEFYarn.class.getProtectionDomain().getCodeSource().getLocation().getFile())
        .set(DriverConfiguration.DRIVER_IDENTIFIER, "HelloREEF")
        .set(DriverConfiguration.ON_DRIVER_STARTED, HelloDriver.StartHandler.class)
        .set(DriverConfiguration.ON_EVALUATOR_ALLOCATED, HelloDriver.EvaluatorAllocatedHandler.class)
        .build();
  }

  /**
   * Start Hello REEF job.
   *
   * @param args command line parameters.
   * @throws BindException      configuration error.
   * @throws InjectionException configuration error.
   */
  public static void main(final String[] args) throws BindException, InjectionException {
    final Configuration runtimeConf = getRuntimeConfiguration();
    final Configuration driverConf = getDriverConfiguration();

    final LauncherStatus status = DriverLauncher
        .getLauncher(runtimeConf)
        .run(driverConf, JOB_TIMEOUT);
    LOG.log(Level.INFO, "REEF job completed: {0}", status);
  }

  /**
   * Empty private constructor to prohibit instantiation of utility class.
   */
  private HelloREEFYarn() {
  }
}

@@ -1135,20 +1135,25 @@
 eptions.
-Bind
+Injection
 Exceptio
@@ -1182,42 +1182,29 @@
 eef.
-tang.exceptions.InjectionException
+util.EnvironmentUtils
 ;%0A%0Ai
@@ -1692,39 +1692,26 @@
  sec.%0A%0A  /**
-%0A   * @return t
+ T
 he configura
@@ -1729,19 +1729,17 @@
  runtime
-%0A  
+.
  */%0A  pr
@@ -1755,60 +1755,42 @@
 tic 
-Configuration getRuntimeConfiguration() %7B%0A    return
+final Configuration RUNTIME_CONF =
  Yar
@@ -1827,34 +1827,17 @@
 d();
-%0A  %7D
 %0A%0A  /**
-%0A   * @return t
+ T
 he c
@@ -1873,19 +1873,16 @@
  driver.
-%0A  
  */%0A  pr
@@ -1894,16 +1894,22 @@
  static 
+final 
 Configur
@@ -1914,19 +1914,30 @@
 uration 
-get
+DRIVER_CONF = 
 DriverCo
@@ -1951,24 +1951,25 @@
 tion
-() %7B%0A    return 
+.CONF%0A      .set(
 Driv
@@ -1980,31 +1980,56 @@
 nfiguration.
-CONF%0A  
+DRIVER_IDENTIFIER, %22HelloREEF%22)%0A
       .set(D
@@ -2068,171 +2068,65 @@
 IES,
-%0A            HelloREEFYarn.class.getProtectionDomain().getCodeSource().getLocation().getFile())%0A        .set(DriverConfiguration.DRIVER_IDENTIFIER, %22HelloREEF%22)%0A  
+ EnvironmentUtils.getClassLocation(HelloREEFYarn.class))%0A
     
@@ -2199,26 +2199,24 @@
 dler.class)%0A
-  
       .set(D
@@ -2309,18 +2309,16 @@
 )%0A      
-  
 .build()
@@ -2318,20 +2318,16 @@
 build();
-%0A  %7D
 %0A%0A  /**%0A
@@ -2357,13 +2357,8 @@
 ob.%0A
-   *%0A
    *
@@ -2398,60 +2398,17 @@
 ers.
-%0A   * @throws BindException      configuration error
+ Not used
 .%0A  
@@ -2521,23 +2521,8 @@
 rows
- BindException,
  Inj
@@ -2543,137 +2543,8 @@
 n %7B%0A
-    final Configuration runtimeConf = getRuntimeConfiguration();%0A    final Configuration driverConf = getDriverConfiguration();%0A%0A
     
@@ -2587,25 +2587,16 @@
 Launcher
-%0A        
 .getLaun
@@ -2604,44 +2604,37 @@
 her(
-runtimeConf)%0A        .run(driverConf
+RUNTIME_CONF).run(DRIVER_CONF
 , JO

Refactor HelloREEF example on YARN