This project has retired. For details please refer to its Attic page.
Source code
001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.examples.library;
020
021import org.apache.reef.tang.annotations.Parameter;
022import org.apache.reef.task.Task;
023import org.apache.reef.util.CommandUtils;
024import org.apache.reef.wake.remote.impl.ObjectSerializableCodec;
025
026import javax.inject.Inject;
027import java.util.logging.Level;
028import java.util.logging.Logger;
029
030/**
031 * Execute command, capture its stdout, and return that string to the job driver.
032 */
033public final class ShellTask implements Task {
034
035  /**
036   * Standard java logger.
037   */
038  private static final Logger LOG = Logger.getLogger(ShellTask.class.getName());
039
040  /**
041   * A command to execute.
042   */
043  private final String command;
044
045  /**
046   * Object Serializable Codec.
047   */
048  private static final ObjectSerializableCodec<String> CODEC = new ObjectSerializableCodec<>();
049
050  /**
051   * Task constructor. Parameters are injected automatically by TANG.
052   *
053   * @param command a command to execute.
054   */
055  @Inject
056  private ShellTask(@Parameter(Command.class) final String command) {
057    this.command = command;
058  }
059
060  /**
061   * Execute the shell command and return the result, which is sent back to
062   * the JobDriver and surfaced in the CompletedTask object.
063   *
064   * @param memento ignored.
065   * @return byte string containing the stdout from executing the shell command.
066   */
067  @Override
068  public byte[] call(final byte[] memento) {
069    LOG.log(Level.INFO, "RUN: command: {0}", this.command);
070    final String result = CommandUtils.runCommand(this.command);
071    LOG.log(Level.INFO, "RUN: result: {0}", result);
072    return CODEC.encode(result);
073  }
074}