Java tutorial
/** * Copyright 2012 Alex Jones * * 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 uk.co.unclealex.process.builder; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.io.Reader; import java.nio.charset.Charset; import java.util.Arrays; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import org.apache.commons.io.IOUtils; import uk.co.unclealex.process.BuildableProcessRequest; import uk.co.unclealex.process.EchoingProcessCallback; import uk.co.unclealex.process.ProcessCallback; import uk.co.unclealex.process.ProcessFailedException; import uk.co.unclealex.process.ProcessResult; import uk.co.unclealex.process.stream.StandardInputSupplier; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; /** * An implementation of {@link BuildableProcessRequest}. * * @author alex * */ abstract class BuildingProcessRequest extends BasicProcessRequest implements BuildableProcessRequest { /** * The arguments to the command. */ private final List<String> arguments = Lists.newArrayList(); /** * The class to use to supply information to standard input. */ private StandardInputSupplier standardInputSupplier; /** * The {@link ProcessCallback}s to use to monitor command execution. */ private final List<ProcessCallback> processCallbacks = Lists.newArrayList(); /** * Instantiates a new building process request. * * @param executor * the executor */ public BuildingProcessRequest(final ExecutorService executor) { super(executor); } /** * {@inheritDoc} */ @Override public BuildableProcessRequest withArguments(final String... arguments) { getArguments().addAll(Arrays.asList(arguments)); return this; } /** * {@inheritDoc} */ @Override public BuildableProcessRequest withArguments(final Iterable<String> arguments) { Iterables.addAll(getArguments(), arguments); return this; } /** * {@inheritDoc} */ @Override public BuildableProcessRequest withCallbacks(final ProcessCallback... processCallbacks) { getProcessCallbacks().addAll(Arrays.asList(processCallbacks)); return this; } /** * {@inheritDoc} */ @Override public BuildableProcessRequest withCallbacks(final Iterable<ProcessCallback> processCallbacks) { Iterables.addAll(getProcessCallbacks(), processCallbacks); return this; } /** * {@inheritDoc} */ @Override public BuildableProcessRequest withStandardInput(final InputStream standardInput) { final StandardInputSupplier copier = new StandardInputCopier() { @Override public void copyToStandardInput(final OutputStream standardInputOutputStream) throws IOException { IOUtils.copy(standardInput, standardInputOutputStream); } }; return withStandardInputSupplier(copier); } /** * {@inheritDoc} */ @Override public BuildableProcessRequest withStandardInput(final Reader reader, final String charset) { final StandardInputSupplier copier = new StandardInputCopier() { @Override public void copyToStandardInput(final OutputStream standardInputOutputStream) throws IOException { IOUtils.copy(reader, standardInputOutputStream, charset); } }; return withStandardInputSupplier(copier); } /** * {@inheritDoc} */ @Override public BuildableProcessRequest withStandardInput(final Reader reader) { final StandardInputSupplier copier = new StandardInputCopier() { @Override public void copyToStandardInput(final OutputStream standardInputOutputStream) throws IOException { IOUtils.copy(reader, standardInputOutputStream, Charset.defaultCharset().name()); } }; return withStandardInputSupplier(copier); } /** * {@inheritDoc} */ @Override public BuildableProcessRequest withStandardInputSupplier(final StandardInputSupplier standardInputSupplier) { setStandardInputSupplier(standardInputSupplier); if (standardInputSupplier instanceof ProcessCallback) { return withCallbacks((ProcessCallback) standardInputSupplier); } else { return this; } } /** * {@inheritDoc} */ @Override public BuildableProcessRequest withStandardOutput(final PrintStream stdout) { return withCallbacks(new EchoingProcessCallback(stdout, null)); } /** * {@inheritDoc} */ @Override public BuildableProcessRequest withStandardError(final PrintStream stderr) { return withCallbacks(new EchoingProcessCallback(null, stderr)); } /** * Create the command file required to run this command, if neccessary. * * @return The string required to call this command or file. * @throws IOException * Signals that an I/O exception has occurred. */ protected abstract String createCommand() throws IOException; /** * {@inheritDoc} */ @Override public ProcessResult executeAndWait() throws IOException { return executeAndWait(true); } @Override public ProcessResult executeAndWait(final boolean throwOnNonZeroReturn) throws IOException { try { final ProcessResult processResult = createExecutable().call(); if (processResult.getReturnValue() != 0) { throw new ProcessFailedException("A process returned code " + processResult.getReturnValue(), processResult); } return processResult; } catch (final Exception e) { if (e instanceof IOException) { throw (IOException) e; } else { throw new IOException(e); } } } /** * {@inheritDoc} * * @throws IOException */ @Override public Future<ProcessResult> execute() throws IOException { return getExecutor().submit(createExecutable()); } /** * Create a {@link Callable} that creates the command and executes it. * * @return A {@link Callable} that creates the command and executes it. * @throws IOException */ protected Callable<ProcessResult> createExecutable() throws IOException { final String command = createCommand(); final Callable<ProcessResult> task = execute(command, getProcessCallbacks(), getArguments(), getStandardInputSupplier()); final Callable<ProcessResult> callable = new Callable<ProcessResult>() { @Override public ProcessResult call() throws Exception { try { return task.call(); } finally { destroyCommand(); } } }; return callable; } /** * Destroy any remnants created by the {@link #createCommand()} methods. */ protected abstract void destroyCommand(); /** * Gets the arguments to the command. * * @return the arguments */ protected List<String> getArguments() { return arguments; } /** * Gets the {@link ProcessCallback}s to use to monitor command execution. * * @return the processCallbacks */ protected List<ProcessCallback> getProcessCallbacks() { return processCallbacks; } protected StandardInputSupplier getStandardInputSupplier() { return standardInputSupplier; } protected void setStandardInputSupplier(final StandardInputSupplier standardInputSupplier) { this.standardInputSupplier = standardInputSupplier; } }