View Javadoc
1   package net.logAnalyzer.config;
2   
3   import java.io.File;
4   import java.io.FileNotFoundException;
5   
6   import net.logAnalyzer.LogAnalyzerBatch;
7   import net.logAnalyzer.utils.FilesSetReader;
8   
9   import org.apache.log4j.Logger;
10  
11  /***
12   * This class is a simple container for command lines options.
13   * 
14   * @author KKI
15   * @see net.logAnalyzer.LogAnalyzerBatch
16   */
17  public class StartUpOptions {
18      /***
19       * Class logger.
20       */
21      private static Logger logger = Logger.getLogger(LogAnalyzerBatch.class
22              .getName());
23  
24      /***
25       * Commands file defining commands to execute during analysis.
26       */
27      private File commandsFile = null;
28  
29      /***
30       * File name of the commands file to execute during analysis.
31       */
32      private String commandsFilename = null;
33  
34      /***
35       * Log file(s) to analyse.
36       */
37      private FilesSetReader logFiles = null;
38  
39      /***
40       * If <tt>true</tt>, the option "-GUI" shows the GUI at the end of the
41       * analysis.
42       */
43      private boolean optionShowGUI = false;
44  
45      /***
46       * Directory in which to save the generated reports.
47       */
48      private File reportsDirectory = null;
49  
50      /***
51       * Reports prefix used as prefix for reports files names.
52       */
53      private String reportsPrefix = "";
54  
55      /***
56       * Default construtor.
57       */
58      public StartUpOptions() {
59          super();
60      }
61  
62      /***
63       * Commands file defining commands to execute during analysis.
64       * 
65       * @return Returns the commandsFile.
66       */
67      public final File getCommandsFile() {
68          return commandsFile;
69      }
70  
71      /***
72       * File name of the commands file to execute during analysis.
73       * 
74       * @return Returns the commandsFilename.
75       */
76      public final String getCommandsFilename() {
77          return commandsFilename;
78      }
79  
80      /***
81       * Log files to analyse.
82       * 
83       * @return Returns the logFiles.
84       */
85      public final FilesSetReader getLogFiles() {
86          return logFiles;
87      }
88  
89      /***
90       * If <tt>true</tt>, the option "-GUI" shows the GUI at the end of the
91       * analysis.
92       * 
93       * @return Returns the optionShowGUI.
94       */
95      public final boolean isOptionShowGUI() {
96          return optionShowGUI;
97      }
98  
99      /***
100      * Directory in which to save the generated reports.
101      * 
102      * @return Returns the reportsDirectory.
103      */
104     public final File getReportsDirectory() {
105         return reportsDirectory;
106     }
107 
108     /***
109      * Reports prefix used as prefix for reports files names.
110      * 
111      * @return Returns the reportsPrefix.
112      */
113     public final String getReportsPrefix() {
114         return reportsPrefix;
115     }
116 
117     /***
118      * Extracts options from the command line args array.
119      * 
120      * @param args
121      *            Command line args array.
122      */
123     public final void setArgs(String[] args) {
124         if (args == null || args.length < 2) {
125             throw new IllegalArgumentException();
126         }
127         int argsOffset = 0;
128         // Extracts optional args
129         while (args[argsOffset] != null && args[argsOffset].length() > 0
130                 && args[argsOffset].charAt(0) == '-'
131                 && "-gui -reportsprefix".indexOf(args[argsOffset]) >= 0) {
132             // Option -gui
133             if ("-gui".equalsIgnoreCase(args[argsOffset])) {
134                 optionShowGUI = true;
135                 argsOffset++;
136             }
137             // Option -reportsprefix
138             if ("-reportsprefix".equalsIgnoreCase(args[argsOffset])) {
139                 argsOffset++;
140                 reportsPrefix = args[argsOffset];
141                 argsOffset++;
142             }
143         }
144         // Extracts commands file name
145         logger.debug("Control if the commands file exists");
146         commandsFilename = args[argsOffset];
147         commandsFile = new File(commandsFilename);
148         if (!commandsFile.exists() || !commandsFile.canRead()) {
149             System.err.println("Can't find or read the commands file ["
150                     + commandsFilename + "]");
151             logger.error("Can't find or read the commands file ["
152                     + commandsFilename + "]");
153             System.exit(1);
154         }
155         logger.debug("...OK [" + commandsFilename + "]");
156         argsOffset++;
157         // Extracts logfiles names
158         if (args.length - argsOffset <= 0) {
159             System.err.println("You must provide one or more files to analyze");
160             logger.error("You must provide one or more files to analyze");
161             System.exit(-1);
162         }
163         logger.debug("Control if the logfile(s) exist");
164         File[] files = new File[args.length - argsOffset];
165         for (int i = 0; i < files.length; i++) {
166             files[i] = new File(args[argsOffset++]);
167         }
168         logFiles = new FilesSetReader();
169         try {
170             logFiles.setFiles(files);
171         } catch (FileNotFoundException fnfe) {
172             System.err.println("Can't find or read the log file(s)");
173             logger.error("Can't find or read the log file(s)", fnfe);
174             System.exit(1);
175         }
176         if (logger.isDebugEnabled()) {
177             String msg = "...OK \n";
178             for (int i = 0; i < files.length; i++) {
179                 msg += "\t[" + files[i] + "]";
180             }
181             logger.debug(msg);
182         }
183         argsOffset += files.length;
184         // Create the reports directory
185         reportsDirectory = new File(files[files.length - 1].getAbsolutePath()
186                 + ".reports");
187         if (!reportsDirectory.exists()) {
188             logger.debug("Create the reports directory ["
189                     + reportsDirectory.getAbsolutePath() + "]");
190             reportsDirectory.mkdirs();
191         } else {
192             logger.debug("The reports directory ["
193                     + reportsDirectory.getAbsolutePath() + "] exists");
194         }
195     }
196 }