View Javadoc

1   package org.kit.furia.fragment;
2   
3   import java.io.File;
4   import java.io.FileWriter;
5   import java.io.IOException;
6   import java.util.Iterator;
7   import java.util.List;
8   
9   import org.apache.log4j.BasicConfigurator;
10  import org.apache.log4j.FileAppender;
11  import org.apache.log4j.Level;
12  import org.apache.log4j.Logger;
13  import org.apache.log4j.PatternLayout;
14  import org.apache.log4j.PropertyConfigurator;
15  import org.kit.furia.FuriaChanConstants;
16  import org.kit.furia.fragment.asm.FragmentExtractorASM;
17  import org.kit.furia.fragment.soot.FragmentExtractorSoot;
18  import org.kit.furia.fragment.soot.NoClassesFound;
19  
20  /*
21   Furia-chan: An Open Source software license violation detector.    
22   Copyright (C) 2007 Kyushu Institute of Technology
23  
24   This program is free software: you can redistribute it and/or modify
25   it under the terms of the GNU General Public License as published by
26   the Free Software Foundation, either version 3 of the License, or
27   (at your option) any later version.
28  
29   This program is distributed in the hope that it will be useful,
30   but WITHOUT ANY WARRANTY; without even the implied warranty of
31   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32   GNU General Public License for more details.
33  
34   You should have received a copy of the GNU General Public License
35   along with this program.  If not, see <http://www.gnu.org/licenses/>.
36   */
37  
38  /**
39   * FragmentBuilderClientAux fragments a directory of class files. It is designed
40   * to be called from the command line. This program is not intended to be called
41   * by humans, only intended to be called by other programs. Receives the
42   * following parameters:
43   * 
44   * <pre>
45   * [1] The directory that is to be processed. 
46   * [2] The output directory in which the fragments will be stored in a &quot;fragments&quot; file.
47   * If the filename {@value org.kit.furia.io.AbstractFuriaInput.fragmentFileName}
48   * is found in the output directory, this directory is silently skipped.
49   * [3] The fragment extractor engine: soot or asm. ASM is faster and works almost in any class files. 
50   *  Soot is slower and sometimes fails to load the class files.
51   * </pre>
52   * 
53   * @author Arnoldo Jose Muller Molina TODO: remove the fragments file if there
54   *         is an error.
55   */
56  
57  public class FragmentBuilderClientAux {
58  
59      private static Logger logger = Logger.getLogger("FragmentBuilderClientAux");
60  
61      public static void main(String[] args) throws Exception {
62  
63          try {
64              File dir = new File(args[0]);
65              File output = new File(args[1]);
66              if (!dir.exists()) {
67                  String msg = "Input directory does not exist: " + dir;
68                  System.err.println(msg);
69                  throw new IOException(msg);
70              }
71              output.mkdirs();
72  
73              if (!output.exists()) {
74                  String msg = "Output directory does not exist: " + output;
75                  System.err.println(msg);
76                  throw new IOException(msg);
77              }
78  
79              Logger root = Logger.getRootLogger();
80              root.addAppender(new FileAppender(new PatternLayout(
81                      PatternLayout.TTCC_CONVERSION_PATTERN), output.toString()
82                      + File.separator + "output.txt"));
83              root.setLevel(Level.DEBUG);
84  
85              File fragmentsFile = new File(output,
86                      org.kit.furia.io.AbstractFuriaInput.fragmentFileName);
87              if (fragmentsFile.exists()) {
88                  // nothing to do here. the file was already processed.
89                  System.exit(0);
90              }
91              FragmentExtractor fEx = null;
92              if (args[2].equals("asm")) {
93                  fEx = new FragmentExtractorASM();
94              } else if (args[2].equals("soot")) {
95                  fEx = new FragmentExtractorSoot();
96              } else {
97                  throw new Exception(
98                          "Must specify fragment extraction engine: soot or asm");
99              }
100 
101             fEx.extractMethodsFromDirectory(dir.toString(),
102                     FuriaChanConstants.MAX_NODES_PER_FRAGMENT,
103                     FuriaChanConstants.MIN_NODES_PER_FRAGMENT, output
104                             .toString(), fragmentsFile.toString());
105             /*
106              * Iterator < FragmentBuilder > it = result.iterator(); FileWriter
107              * outputFile = new FileWriter(fragmentsFile); while (it.hasNext()) {
108              * FragmentBuilder fb = it.next(); StringBuilder tmp = new
109              * StringBuilder(); fb.generateString(tmp);
110              * outputFile.write(tmp.toString()); } outputFile.close();
111              */
112             logger.info("Completed Fragmentation for " + args[0]);
113         } 
114         catch(NoClassesFound e1){
115             System.exit(7);
116         }
117         catch (Exception e) {
118             e.printStackTrace();
119             logger.fatal("Aborting", e);
120             // logger.fatal("Received Env:\n" + System.getenv().toString());
121             System.exit(48);
122         }
123 
124     }
125 }