View Javadoc

1   package org.kit.furia;
2   
3   import java.io.File;
4   import java.util.Arrays;
5   
6   import org.ajmm.obsearch.asserts.OBAsserts;
7   import org.ajmm.obsearch.example.HelpException;
8   import org.apache.commons.cli.CommandLine;
9   import org.apache.commons.cli.CommandLineParser;
10  import org.apache.commons.cli.GnuParser;
11  import org.apache.commons.cli.HelpFormatter;
12  import org.apache.commons.cli.Option;
13  import org.apache.commons.cli.OptionBuilder;
14  import org.apache.commons.cli.Options;
15  import org.apache.commons.cli.ParseException;
16  import org.apache.log4j.LogManager;
17  import org.apache.log4j.Logger;
18  import org.apache.log4j.PropertyConfigurator;
19  import org.kit.furia.fragment.FragmentBuilderClient;
20  
21  /*
22   Furia-chan: An Open Source software license violation detector.    
23   Copyright (C) 2008 Kyushu Institute of Technology
24  
25   This program is free software: you can redistribute it and/or modify
26   it under the terms of the GNU General Public License as published by
27   the Free Software Foundation, either version 3 of the License, or
28   (at your option) any later version.
29  
30   This program is distributed in the hope that it will be useful,
31   but WITHOUT ANY WARRANTY; without even the implied warranty of
32   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33   GNU General Public License for more details.
34  
35   You should have received a copy of the GNU General Public License
36   along with this program.  If not, see <http://www.gnu.org/licenses/>.
37   */
38  
39  /**
40   * BytecodeFrag is a command line utility that fragments Java byte-code.
41   * The program will create n threads according to the number of processors you have
42   * and will match everything in parallel.
43   * If no -timeout flag is given, a default of 30 minutes timeout per application is set.
44   * <div>
45   * Regarding the output directory:
46   * 1) If it does not exists it will be created.
47   * 2) If it exists, it will be unmodified.
48   * 
49   * If the output directory exists:
50   * if given "dm" (directory of directories mode):
51   * 1) All the directories in the output folder that contain a "fragments" file (that is, if the fragments have been modified) will be ignored.
52   * if not given "dm":
53   * 1) The output directory will be ignored if it contains a "fragments" file.
54   * </div>
55   * @author Arnoldo Jose Muller Molina
56   */
57  
58  public class BytecodeFrag  extends AbstractFuriaChanCommandLine{
59      
60      private static final Logger logger = Logger.getLogger("BytecodeFrag");
61      
62      public static void main(String args[]){
63          int returnValue = 0;        
64          try {
65  
66              initLogger();
67              
68              final CommandLine cline = getCommandLine(initCommandLine(),
69                      BytecodeFrag.class, args);
70              
71              boolean directoryOfDirectoriesMode = false;
72              if(cline.hasOption("dm")){
73                  logger.info("Directory of directories mode");
74                  directoryOfDirectoriesMode = true;                
75              }
76              
77              long timeout = 30 *  60  * 1000; // default value.
78              if(cline.hasOption("timeout")){
79                  timeout = Integer.parseInt( cline.getOptionValue("timeout"));
80              }
81              File input = new File(cline.getOptionValue("input"));
82              OBAsserts.chkFileExists(input);
83              File output = new File(cline.getOptionValue("output"));
84              String engine = cline.getOptionValue("engine");
85              FragmentBuilderClient c = new FragmentBuilderClient(directoryOfDirectoriesMode,
86                      input, Runtime.getRuntime().availableProcessors(), output, false, timeout, engine);
87              
88          }catch (final ParseException exp) {
89              logger.fatal("Argument parsing failed args: "
90                      + Arrays.toString(args), exp);
91              returnValue = 84;
92          } catch (final HelpException exp) {
93              // no problem, we just display the help and quit
94              logger.debug("Should have shown the help msg");
95          } catch (final Exception e) {
96              logger.fatal("Exception caught", e);
97              returnValue = 83;
98          }
99  
100         LogManager.shutdown();
101         System.exit(returnValue);
102         
103     }
104                 
105     /**
106      * Initializes the command line definition. Here we define all the command
107      * line options to be received by the program.
108      * @return The options of the program.
109      */
110     public static Options initCommandLine() {
111         
112         final Option dm= new Option("dm", "Directory mode: If given this option, then the program will process a directory of directories of class files");
113         dm.setRequired(false);
114 
115         final Option in = OptionBuilder.withArgName("dir").hasArg().isRequired(
116                 true).withDescription("Input directory (directory of directories if dm mode is given)").create("input");
117         
118         final Option output = OptionBuilder.withArgName("dir").hasArg().isRequired(
119                 true).withDescription("Output directory where the fragments will be stored.").create("output");
120         
121         final Option timeout = OptionBuilder.withArgName("seconds").hasArg().isRequired(
122                 false).withDescription("Timeout in seconds to give to the processing of each application").create("timeout");
123         
124         final Option engine = OptionBuilder.withArgName("seconds").hasArg().isRequired(
125                 true).withDescription("Fragment engine to employ: soot or asm").create("engine");
126         
127         Options options = new Options();
128         options.addOption(in);
129         options.addOption(dm);
130         options.addOption(output);
131         options.addOption(timeout);
132         options.addOption(engine);
133         return options;
134     }
135 
136     
137 }