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.output;
27  
28  import com.saptarshidebnath.lib.processrunner.configuration.Configuration;
29  import com.saptarshidebnath.lib.processrunner.constants.OutputSourceType;
30  import com.saptarshidebnath.lib.processrunner.exception.ProcessConfigurationException;
31  import com.saptarshidebnath.lib.processrunner.exception.ProcessException;
32  import com.saptarshidebnath.lib.processrunner.model.OutputRecord;
33  import com.saptarshidebnath.lib.processrunner.process.Runner;
34  import java.io.File;
35  import java.io.IOException;
36  import java.util.List;
37  
38  /**
39   * The Output of a run is returned as a reference of {@link Output} class. The interface for the
40   * Object which is going to be returned after running {@link Runner#run()} or {@link
41   * Runner#runAsync()}.
42   */
43  public interface Output {
44  
45    /**
46     * Prints the {@link OutputRecord#getOutputText()} of type {@link OutputSourceType#SYSOUT} to the
47     * {@link File} supplied.
48     *
49     * @param sysOut A {@link File} object where the log is going to be written.
50     * @return A {@link File} object where the log has been written.
51     * @throws ProcessException In case of any error. This is a generic error. To get the details,
52     *     please use {@link ProcessException#getCause()}.
53     * @throws ProcessException is a generic Exception
54     * @throws ProcessConfigurationException if the {@link Configuration#masterLogFile} is not set. If
55     *     the {@link Configuration#masterLogFile} is not set then the log is not saved at all and
56     *     they are discarded.
57     * @throws IOException is thrown on any disk error.
58     */
59    File saveSysOut(final File sysOut)
60        throws ProcessException, ProcessConfigurationException, IOException;
61  
62    /**
63     * Prints the {@link OutputRecord#getOutputText()} of type {@link OutputSourceType#SYSERR} to the
64     * {@link File} supplied.
65     *
66     * @param sysError A {@link File} object where the log is going to be written.
67     * @return A {@link File} object where the log has been written.
68     * @throws ProcessException In case of any error. This is a generic error. To get the details,
69     *     please use {@link ProcessException#getCause()}.
70     * @throws IOException is thrown on any disk error.
71     * @throws ProcessConfigurationException if the {@link Configuration#masterLogFile} is not set. If
72     *     the {@link Configuration#masterLogFile} is not set then the log is not saved at all and
73     *     they are discarded.
74     */
75    File saveSysError(final File sysError)
76        throws ProcessException, ProcessConfigurationException, IOException;
77  
78    /**
79     * Returns the master log file originally captured while executing the Process. Its an Json Array
80     * of type {@link OutputRecord}.
81     *
82     * @return a {@link File} reference to the json formatted master log .
83     * @throws ProcessConfigurationException if the {@link Configuration#masterLogFile} is not set. If
84     *     the {@link Configuration#masterLogFile} is not set then the log is not saved at all and
85     *     they are discarded.
86     */
87    File getMasterLogAsJson() throws ProcessConfigurationException;
88  
89    /**
90     * Prints the {@link OutputRecord#getOutputText()} of both {@link OutputSourceType#SYSERR} and
91     * {@link OutputSourceType#SYSOUT} to the {@link File} supplied.
92     *
93     * @param log A {@link File} object where the log is going to be written.
94     * @return A {@link File} object where the log is going to be written.
95     * @throws IOException when there is a issue with the IO for reading and writing the file.
96     * @throws ProcessConfigurationException if the {@link Configuration#masterLogFile} is not set. If
97     *     the {@link Configuration#masterLogFile} is not set then the log is not saved at all and
98     *     they are discarded.
99     */
100   File saveLog(final File log) throws IOException, ProcessConfigurationException;
101 
102   /**
103    * Returns the process exit / return code.
104    *
105    * @return return the exit code as an integer value from 0 - 255
106    */
107   int getReturnCode();
108 
109   /**
110    * Search the content of the {@link Configuration#getMasterLogFile()} for a particular regex. The
111    * search is done line by line.
112    *
113    * @param regex a proper Regular Expression that need to be searched for.
114    * @return a {@link Boolean#TRUE} or {@link Boolean#FALSE} depending upon if the search is
115    *     positive or negative.
116    * @throws ProcessConfigurationException if the {@link Configuration#masterLogFile} is not set. If
117    *     the {@link Configuration#masterLogFile} is not set then the log is not saved at all and
118    *     they are discarded.
119    * @throws IOException if there is an error reading the {@link Configuration#masterLogFile}
120    */
121   boolean searchMasterLog(final String regex) throws IOException, ProcessConfigurationException;
122 
123   /**
124    * Search the master log file for the provided regex.
125    *
126    * <p>Searches for a pattern and returns a {@link List} of {@link OutputRecord} which contains
127    * extracts from the output matching the provided regex ins {@link String} format
128    *
129    * @param regex accepts a {@link String} object to search for
130    * @return a {@link List} of {@link String}
131    * @throws ProcessConfigurationException if the {@link Configuration#masterLogFile} is not set. If
132    *     the {@link Configuration#masterLogFile} is not set then the log is not saved at all and
133    *     they are discarded.
134    * @throws IOException if there is an error reading the {@link Configuration#masterLogFile}
135    */
136   List<OutputRecord> grepForRegex(String regex) throws IOException, ProcessConfigurationException;
137 }