/**
   * Execute {@code HelloWorld} example on the grid.
   *
   * @param args Command line arguments, none required but if provided first one should point to the
   *     Spring XML configuration file. See {@code "examples/config/"} for configuration file
   *     examples.
   * @throws GridException If example execution failed.
   */
  public static void main(String[] args) throws GridException {
    if (args.length == 0) {
      G.start();
    } else {
      G.start(args[0]);
    }

    try {
      // Broadcast this message to all nodes using GridClosure.
      broadcastWordsClosure("Broadcasting This Message To All Nodes!");

      // Print individual words from this phrase on different nodes
      // using GridClosure
      spreadWordsClosure("Print Worlds Functional Style!");

      // Print this message using anonymous runnable object.
      unicastWordsRunnable("Printing This Message From Runnable!");

      // Split the message into words and pass them as arguments
      // for remote execution of Callable objects.
      countLettersCallable("Letter Count With Callable!");

      // Split the message into words and pass them as arguments
      // for remote execution of GridClosure objects.
      countLettersClosure("Letter Count With Closure!");

      // Split the message into words and pass them as arguments
      // for remote execution of GridClosure objects and
      // then aggregate results using GridReducer.
      countLettersReducer("Letter Count With Reducer!");
    } finally {
      G.stop(true);
    }
  }
  /** @throws Exception If failed. */
  @SuppressWarnings("unchecked")
  public void testCancel() throws Exception {
    Grid grid = G.grid(getTestGridName());

    grid.compute()
        .localDeployTask(GridCancelTestTask.class, U.detectClassLoader(GridCancelTestTask.class));

    GridComputeTaskFuture<?> fut = grid.compute().execute(GridCancelTestTask.class.getName(), null);

    // Wait until jobs begin execution.
    boolean await = startSignal.await(WAIT_TIME, TimeUnit.MILLISECONDS);

    assert await : "Jobs did not start.";

    info("Test task result: " + fut);

    assert fut != null;

    // Only first job should successfully complete.
    Object res = fut.get();
    assert (Integer) res == 1;

    // Wait for all jobs to finish.
    await = stopSignal.await(WAIT_TIME, TimeUnit.MILLISECONDS);
    assert await : "Jobs did not stop.";

    // One is definitely processed. But there might be some more processed or cancelled or processed
    // and cancelled.
    // Thus total number should be at least SPLIT_COUNT and at most (SPLIT_COUNT - 1) *2 +1
    assert (cancelCnt + processedCnt) >= SPLIT_COUNT
            && (cancelCnt + processedCnt) <= (SPLIT_COUNT - 1) * 2 + 1
        : "Invalid cancel count value: " + cancelCnt;
  }
  /**
   * Prints a phrase on the grid nodes running anonymous closure objects and calculating total
   * number of letters.
   *
   * @param phrase Phrase to print on of the grid nodes.
   * @throws GridException If failed.
   */
  private static void countLettersClosure(String phrase) throws GridException {
    X.println(">>> Starting countLettersClosure() example...");

    // Explicitly execute the collection of callable objects and receive a result.
    Collection<Integer> results =
        G.grid()
            .call(
                SPREAD,
                new GridClosure<String, Integer>() { // Create executable logic.
                  @Override
                  public Integer apply(String word) {
                    // Print out a given word, just so we can
                    // see which node is doing what.
                    X.println(">>> Executing word: " + word);

                    // Return the length of a given word, i.e. number of letters.
                    return word.length();
                  }
                },
                Arrays.asList(phrase.split(" "))); // Collection of arguments for closures.

    // Add up all results using convenience 'sum()' method.
    int letterCnt = F.sum(results);

    X.println(">>>");
    X.println(">>> Finished execution of counting letters with closure based on GridGain 3.0 API.");
    X.println(">>> You should see the phrase '" + phrase + "' printed out on the nodes.");
    X.println(">>> Total number of letters in the phrase is '" + letterCnt + "'.");
    X.println(">>> Check all nodes for output (this node is also part of the grid).");
    X.println(">>>");
  }
  /**
   * Prints a phrase on the grid nodes running anonymous callable objects and calculating total
   * number of letters.
   *
   * @param phrase Phrase to print on of the grid nodes.
   * @throws GridException If failed.
   */
  private static void countLettersCallable(String phrase) throws GridException {
    X.println(">>> Starting countLettersCallable() example...");

    Collection<Callable<Integer>> calls = new HashSet<Callable<Integer>>();

    for (final String word : phrase.split(" "))
      calls.add(
          new GridCallable<Integer>() { // Create executable logic.
            @Override
            public Integer call() throws Exception {
              // Print out a given word, just so we can
              // see which node is doing what.
              X.println(">>> Executing word: " + word);

              // Return the length of a given word, i.e. number of letters.
              return word.length();
            }
          });

    // Explicitly execute the collection of callable objects and receive a result.
    Collection<Integer> results = G.grid().call(SPREAD, calls);

    // Add up all results using convenience 'sum()' method on GridFunc class.
    int letterCnt = F.sum(results);

    X.println(">>>");
    X.println(
        ">>> Finished execution of counting letters with callables based on GridGain 3.0 API.");
    X.println(">>> You should see the phrase '" + phrase + "' printed out on the nodes.");
    X.println(">>> Total number of letters in the phrase is '" + letterCnt + "'.");
    X.println(">>> Check all nodes for output (this node is also part of the grid).");
    X.println(">>>");
  }
  /**
   * Prints a phrase on one of the grid nodes running anonymous runnable.
   *
   * @param phrase Phrase to print on one of the grid nodes.
   * @throws GridException If failed.
   */
  private static void unicastWordsRunnable(final String phrase) throws GridException {
    X.println(">>> Starting unicastWordsRunnable() example...");

    G.grid()
        .run(
            UNICAST,
            new GridRunnable() {
              @Override
              public void run() {
                X.println(">>> Printing phrase: " + phrase);
              }
            });

    // NOTE:
    //
    // Alternatively, you can use existing closure 'F.println()' to
    // print any text like so:
    //
    // G.grid().run(UNICAST, F.println(">>> Printing phrase: " + phrase));
    //

    X.println(">>>");
    X.println(">>> Finished execution of runnable object based on GridGain 3.0 API.");
    X.println(">>> You should see the phrase '" + phrase + "' printed out on one of the nodes.");
    X.println(">>> Check all nodes for output (this node is also part of the grid).");
    X.println(">>>");
  }
  /**
   * Prints every word a phrase on different nodes.
   *
   * @param phrase Phrase from which to print words on different nodes.
   * @throws GridException If failed.
   */
  private static void spreadWordsClosure(String phrase) throws GridException {
    X.println(">>> Starting spreadWordsClosure() example...");

    // Splits the passed in phrase into words and prints every word
    // on a individual grid node. If there are more words than nodes -
    // some nodes will print more than one word.
    G.grid()
        .run(
            SPREAD,
            F.yield(
                phrase.split(" "),
                new GridInClosure<String>() {
                  @Override
                  public void apply(String word) {
                    X.println(word);
                  }
                }));

    // NOTE:
    //
    // Alternatively, you can use existing closure 'F.println()' to
    // print any yield result in 'F.yield()' like so:
    //
    // G.grid().run(SPREAD, F.yield(phrase.split(" "), F.println()));
    //

    X.println(">>>");
    X.println(
        ">>> Finished printing individual words on different nodes based on GridGain 3.0 API.");
    X.println(">>> Check all nodes for output (this node is also part of the grid).");
    X.println(">>>");
  }
  /**
   * Broadcasts a give phrase to all nodes.
   *
   * @param phrase Phrase to broadcast.
   * @throws GridException If failed.
   */
  private static void broadcastWordsClosure(final String phrase) throws GridException {
    X.println(">>> Starting broadcastWordsClosure() example...");

    G.grid()
        .run(
            BROADCAST,
            new GridAbsClosure() {
              @Override
              public void apply() {
                X.println(">>> Printing phrase: " + phrase);
              }
            });

    // NOTE:
    //
    // Alternatively, you can use existing closure 'F.println()' to
    // print any text like so:
    //
    // G.grid().run(BROADCAST, F.println(">>> Printing phrase: " + phrase));
    //

    X.println(">>>");
    X.println(">>> Finished broadcasting a phrase to all grid nodes based on GridGain 3.0 API.");
    X.println(">>> Check all nodes for output (this node is also part of the grid).");
    X.println(">>>");
  }
  /**
   * Calculates length of a given phrase on the grid.
   *
   * @param phrase Phrase to count the number of letters in.
   * @throws GridException If failed.
   */
  private static void countLettersReducer(String phrase) throws GridException {
    X.println(">>> Starting countLettersReducer() example...");

    Grid grid = G.grid();

    // Logger to use in your closure. Note that even though we assign it
    // to a local variable, GridGain still allows to use it from remotely
    // executed code.
    final GridLogger log = grid.log();

    // Execute Hello World task.
    int letterCnt =
        grid.reduce(
            BALANCE,
            new GridClosure<String, Integer>() { // Create executable logic.
              @Override
              public Integer apply(String word) {
                // Print out a given word, just so we can
                // see which node is doing what.
                log.info(">>> Calculating for word: " + word);

                // Return the length of a given word, i.e. number of letters.
                return word.length();
              }
            },
            Arrays.asList(phrase.split(" ")), // Collection of words.
            // Create custom reducer.
            // NOTE: Alternatively, you can use existing reducer: F.sumIntReducer()
            new GridReducer<Integer, Integer>() {
              private int sum;

              @Override
              public boolean collect(Integer res) {
                sum += res;

                return true; // True means continue collecting until last result.
              }

              @Override
              public Integer apply() {
                return sum;
              }
            });

    X.println(">>>");
    X.println(">>> Finished execution of counting letters with reducer based on GridGain 3.0 API.");
    X.println(">>> Total number of letters in the phrase is '" + letterCnt + "'.");
    X.println(">>> You should see individual words printed out on different nodes.");
    X.println(">>> Check all nodes for output (this node is also part of the grid).");
    X.println(">>>");
  }
  /**
   * See <a href="http://e-docs.bea.com/wls/docs100/javadocs/weblogic/common/T3StartupDef.html">
   * http://e-docs.bea.com/wls/docs100/javadocs/weblogic/common/T3StartupDef.html</a> for more
   * information.
   *
   * @param str Virtual name by which the class is registered as a {@code startupClass} in the
   *     {@code config.xml} file
   * @param params A hashtable that is made up of the name-value pairs supplied from the {@code
   *     startupArgs} property
   * @return Result string (log message).
   * @throws Exception Thrown if error occurred.
   */
  @SuppressWarnings({"unchecked", "CatchGenericClass"})
  @Override
  public String startup(String str, Hashtable params) throws Exception {
    GridLogger log = new GridJavaLogger(LoggingHelper.getServerLogger());

    cfgFile = (String) params.get(cfgFilePathParam);

    if (cfgFile == null) {
      throw new IllegalArgumentException("Failed to read property: " + cfgFilePathParam);
    }

    String workMgrName = (String) params.get(workMgrParam);

    URL cfgUrl = U.resolveGridGainUrl(cfgFile);

    if (cfgUrl == null)
      throw new ServerLifecycleException(
          "Failed to find Spring configuration file (path provided should be "
              + "either absolute, relative to GRIDGAIN_HOME, or relative to META-INF folder): "
              + cfgFile);

    GenericApplicationContext springCtx;

    try {
      springCtx = new GenericApplicationContext();

      XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(springCtx);

      xmlReader.loadBeanDefinitions(new UrlResource(cfgUrl));

      springCtx.refresh();
    } catch (BeansException e) {
      throw new ServerLifecycleException(
          "Failed to instantiate Spring XML application context: " + e.getMessage(), e);
    }

    Map cfgMap;

    try {
      // Note: Spring is not generics-friendly.
      cfgMap = springCtx.getBeansOfType(GridConfiguration.class);
    } catch (BeansException e) {
      throw new ServerLifecycleException(
          "Failed to instantiate bean [type="
              + GridConfiguration.class
              + ", err="
              + e.getMessage()
              + ']',
          e);
    }

    if (cfgMap == null)
      throw new ServerLifecycleException(
          "Failed to find a single grid factory configuration in: " + cfgUrl);

    if (cfgMap.isEmpty())
      throw new ServerLifecycleException("Can't find grid factory configuration in: " + cfgUrl);

    try {
      ExecutorService execSvc = null;

      MBeanServer mbeanSrv = null;

      for (GridConfiguration cfg : (Collection<GridConfiguration>) cfgMap.values()) {
        assert cfg != null;

        GridConfigurationAdapter adapter = new GridConfigurationAdapter(cfg);

        // Set logger.
        if (cfg.getGridLogger() == null) adapter.setGridLogger(log);

        if (cfg.getExecutorService() == null) {
          if (execSvc == null)
            execSvc =
                workMgrName != null
                    ? new GridThreadWorkManagerExecutor(workMgrName)
                    : new GridThreadWorkManagerExecutor(J2EEWorkManager.getDefault());

          adapter.setExecutorService(execSvc);
        }

        if (cfg.getMBeanServer() == null) {
          if (mbeanSrv == null) {
            InitialContext ctx = null;

            try {
              ctx = new InitialContext();

              mbeanSrv = (MBeanServer) ctx.lookup("java:comp/jmx/runtime");
            } catch (Exception e) {
              throw new IllegalArgumentException(
                  "MBean server was not provided and failed to obtain " + "Weblogic MBean server.",
                  e);
            } finally {
              if (ctx != null) ctx.close();
            }
          }

          adapter.setMBeanServer(mbeanSrv);
        }

        Grid grid = G.start(adapter, springCtx);

        // Test if grid is not null - started properly.
        if (grid != null) gridNames.add(grid.name());
      }

      return getClass().getSimpleName() + " started successfully.";
    } catch (GridException e) {
      // Stop started grids only.
      for (String name : gridNames) G.stop(name, true);

      throw new ServerLifecycleException("Failed to start GridGain.", e);
    }
  }