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.runtime.local.client;
020
021import org.apache.commons.lang.StringUtils;
022import org.apache.reef.runtime.common.files.ClasspathProvider;
023import org.apache.reef.runtime.common.files.REEFFileNames;
024import org.apache.reef.runtime.common.launch.JavaLaunchCommandBuilder;
025import org.apache.reef.runtime.local.process.LoggingRunnableProcessObserver;
026import org.apache.reef.runtime.local.process.RunnableProcess;
027import org.apache.reef.tang.annotations.Parameter;
028
029import javax.inject.Inject;
030import java.io.File;
031import java.util.Collections;
032import java.util.List;
033import java.util.concurrent.ExecutorService;
034import java.util.logging.Level;
035import java.util.logging.Logger;
036import org.apache.reef.runtime.common.launch.parameters.DriverLaunchCommandPrefix;
037
038/**
039 * Launcher for a already prepared driver folder.
040 */
041public class PreparedDriverFolderLauncher {
042
043  /**
044   * The name of the folder for the driver within the Job folder.
045   */
046  public static final String DRIVER_FOLDER_NAME = "driver";
047
048  private final ExecutorService executor;
049  private final REEFFileNames fileNames;
050  private final ClasspathProvider classpath;
051  private final List<String> commandPrefixList;
052
053  /**
054   * The (hard-coded) amount of memory to be used for the driver.
055   */
056  public static final int DRIVER_MEMORY = 512;
057
058  private static final Logger LOG = Logger.getLogger(PreparedDriverFolderLauncher.class.getName());
059
060  @Inject
061  PreparedDriverFolderLauncher(final ExecutorService executor, final REEFFileNames fileNames,
062                               @Parameter(DriverLaunchCommandPrefix.class) final List<String> commandPrefixList,
063                               final ClasspathProvider classpath) {
064    this.executor = executor;
065    this.fileNames = fileNames;
066    this.classpath = classpath;
067    this.commandPrefixList = commandPrefixList;
068  }
069
070
071
072  /**
073   * Launches the driver prepared in driverFolder.
074   *
075   * @param driverFolder
076   */
077  public void launch(final File driverFolder) {
078    launch(driverFolder, this.fileNames.getDriverStdoutFileName(), this.fileNames.getDriverStderrFileName());
079  }
080
081  public void launch(final File driverFolder, final String stdoutFilePath, final String stderrFilePath) {
082    assert driverFolder.isDirectory();
083
084    final List<String> command = makeLaunchCommand();
085
086    final RunnableProcess process = new RunnableProcess(command,
087        "driver",
088        driverFolder,
089        new LoggingRunnableProcessObserver(),
090        stdoutFilePath,
091        stderrFilePath);
092    this.executor.submit(process);
093    this.executor.shutdown();
094  }
095
096  private List<String> makeLaunchCommand() {
097
098    final List<String> command = new JavaLaunchCommandBuilder(commandPrefixList)
099        .setConfigurationFilePaths(Collections.singletonList(this.fileNames.getDriverConfigurationPath()))
100        .setClassPath(this.classpath.getDriverClasspath())
101        .setMemory(DRIVER_MEMORY)
102        .build();
103
104    if (LOG.isLoggable(Level.FINEST)) {
105      LOG.log(Level.FINEST, "REEF app command: {0}", StringUtils.join(command, ' '));
106    }
107    return command;
108  }
109
110}