View Javadoc
1   /*
2    *
3    * MIT License
4    *
5    * Copyright (c) [2016] [Saptarshi Debnath]
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in all
15   * copies or substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23   * SOFTWARE.
24   */
25  
26  package com.saptarshidebnath.lib.processrunner.process;
27  
28  import com.saptarshidebnath.lib.processrunner.configuration.Configuration;
29  import com.saptarshidebnath.lib.processrunner.configuration.Configuration.ConfigBuilder;
30  import com.saptarshidebnath.lib.processrunner.output.Output;
31  import java.io.IOException;
32  import java.util.concurrent.ExecutionException;
33  import java.util.concurrent.Future;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  /**
38   * Factory method to to run command or to get an instance of {@link Runner}.
39   *
40   * <p>This is the only way to create a reference of {@link Runner}. Please note that if you opt for
41   * Start process, then you either received an {@link Future} or an {@link Output}.
42   */
43  public class RunnerFactory {
44  
45    private static final Logger logger = LoggerFactory.getLogger(RunnerFactory.class);
46  
47    /** Hidden constructor so that no body can create object of the class. */
48    private RunnerFactory() {}
49  
50    /**
51     * Create a instance of {@link Runner} by consuming a reference of the {@link Configuration}. The
52     * process is then <strong>triggered in synchronously</strong>.
53     *
54     * @param configuration Takes a valid {@link Configuration} object.
55     * @return a reference to {@link Output}
56     * @throws IOException denoting there is an IO problem during writing the log files.
57     * @throws InterruptedException there is a problem when writing the logs via thread enabled log
58     *     handlers.
59     * @throws ExecutionException this when the threads crash.
60     */
61    public static Output startProcess(final Configuration configuration)
62        throws IOException, InterruptedException, ExecutionException {
63      logger.debug("Starting process with config : {}", configuration);
64      return new RunnerImpl(configuration).run();
65    }
66  
67    /**
68     * Create a instance of {@link Runner} by consuming a reference of the {@link Configuration}. The
69     * process is then <strong>triggered asynchronously</strong>.
70     *
71     * @param configuration Takes a valid {@link Configuration} object.
72     * @return a reference of {@link Future} of type {@link Output} from where you can retrieve the
73     *     {@link Output} of the process.
74     */
75    public static Future<Output> startAsyncProcess(final Configuration configuration) {
76      logger.debug("Starting asynchronous process with configuration : {}", configuration);
77      return new RunnerImpl(configuration).runAsync();
78    }
79  
80    /**
81     * Create a instance of {@link Runner}. This method doesn't start the process and is upon the
82     * developer to actually trigger the process.
83     *
84     * @param configuration a reference of {@link Configuration}. The {@link Configuration} can be
85     *     build quite easily via {@link ConfigBuilder}
86     * @return a reference of {@link Runner}
87     */
88    public static Runner getRunner(Configuration configuration) {
89      logger.debug("Creating Process with the configuration : ", configuration);
90      return new RunnerImpl(configuration);
91    }
92  }