/*
 * Copyright (c) 2013 DataTorrent, Inc. ALL Rights Reserved.
 *
 * Licensed 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 com.datatorrent.api;

import java.util.Iterator;
import java.util.ServiceLoader;

import org.apache.hadoop.conf.Configuration;

/**
 * Local mode execution for single application
 *
 * @since 0.3.2
 */
public abstract class LocalMode {

  /**
   * <p>getDAG.</p>
   */
  abstract public DAG getDAG();
  /**
   * <p>cloneDAG.</p>
   */
  abstract public DAG cloneDAG() throws Exception;

  /**
   * Build the logical plan through the given streaming application instance and/or from configuration.
   * <p>
   * The plan will be constructed through {@link StreamingApplication#populateDAG}. If configuration properties are
   * specified, they function as override, as would be the case when launching an application through CLI.
   * <p>
   * This method can also be used to construct the plan declaratively from configuration only, by passing null for the
   * application. In this case the configuration contains all operators and streams.
   * <p>
   *
   * @param app
   * @param conf
   * @return
   * @throws Exception
   * @since 0.3.5
   */
  abstract public DAG prepareDAG(StreamingApplication app, Configuration conf) throws Exception;

  /**
   * <p>getController.</p>
   */
  abstract public Controller getController();

  public interface Controller {
    public void run();
    public void run(long runMillis);
    public void runAsync();
    public void shutdown();
    public void setHeartbeatMonitoringEnabled(boolean enabled);
  }

  /**
   * <p>newInstance.</p>
   */
  public static LocalMode newInstance() {
    ServiceLoader<LocalMode> loader = ServiceLoader.load(LocalMode.class);
    Iterator<LocalMode> impl = loader.iterator();
    if (!impl.hasNext()) {
      throw new RuntimeException("No implementation for " + LocalMode.class);
    }
    return impl.next();
  }

  /**
   * Shortcut to run an application. Used for testing.
   *
   * @param app
   * @param runMillis
   */
  public static void runApp(StreamingApplication app, int runMillis) {
    LocalMode lma = newInstance();
    app.populateDAG(lma.getDAG(), new Configuration(false));
    LocalMode.Controller lc = lma.getController();
    lc.run(runMillis);
  }

}

@@ -858,17 +858,17 @@
 ocalMode
- 
+%0A
 %7B%0A%0A  /**
@@ -876,16 +876,22 @@
    * %3Cp%3E
+%0A   * 
 getDAG.%3C
@@ -886,32 +886,50 @@
   * getDAG.%3C/p%3E%0A
+   *%0A   * @return%0A
    */%0A  abstract
@@ -950,16 +950,17 @@
 tDAG();%0A
+%0A
   /**%0A  
@@ -965,16 +965,22 @@
    * %3Cp%3E
+%0A   * 
 cloneDAG
@@ -981,24 +981,75 @@
 oneDAG.%3C/p%3E%0A
+   *%0A   * @return%0A   * @throws java.lang.Exception%0A
    */%0A  abst
@@ -1868,16 +1868,22 @@
    * %3Cp%3E
+%0A   * 
 getContr
@@ -1889,24 +1889,42 @@
 roller.%3C/p%3E%0A
+   *%0A   * @return%0A
    */%0A  abst
@@ -1993,16 +1993,18 @@
 ntroller
+%0A 
  %7B%0A    p
@@ -2017,24 +2017,25 @@
 void run();%0A
+%0A
     public v
@@ -2055,24 +2055,25 @@
 runMillis);%0A
+%0A
     public v
@@ -2084,24 +2084,25 @@
 runAsync();%0A
+%0A
     public v
@@ -2113,24 +2113,25 @@
 shutdown();%0A
+%0A
     public v
@@ -2182,16 +2182,17 @@
 abled);%0A
+%0A
   %7D%0A%0A  /
@@ -2202,16 +2202,22 @@
    * %3Cp%3E
+%0A   * 
 newInsta
@@ -2221,24 +2221,42 @@
 stance.%3C/p%3E%0A
+   *%0A   * @return%0A
    */%0A  publ
@@ -2288,16 +2288,18 @@
 stance()
+%0A 
  %7B%0A    S
@@ -2736,16 +2736,448 @@
 nMillis)
+%0A  %7B%0A    runApp(app, null, runMillis);%0A  %7D%0A%0A  /**%0A   * Shortcut to run an application with the modified configuration.%0A   *%0A   * @param app           - Application to be run%0A   * @param configuration - Configuration%0A   * @param runMillis     - The time after which the application will be shutdown; pass 0 to run indefinitely.%0A   */%0A  public static void runApp(StreamingApplication app, Configuration configuration, int runMillis)%0A 
  %7B%0A    L
@@ -3239,16 +3239,40 @@
 etDAG(),
+ configuration == null ?
  new Con
@@ -3288,16 +3288,32 @@
 n(false)
+ : configuration
 );%0A    L

Allow to tweak the configuration while testing.