Package scripts

Source Code of scripts.MainScript

package scripts;

import java.io.File;
import java.sql.ResultSet;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.BasicAuthenticationManager;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;

import database.MySQLConnection;

/**
*
* The main script, which first executes all scanning tools via the Bash
* scripts, then launches the different parsers on the generated reports
*
*/
public class MainScript {

  private final String REPORTS_PATH = "/tmp/pdpscanner/reports";
  private final String CHECKOUT_DESTINATION_PATH = "/tmp/pdpscanner/checkouts";
  private final String XML_FILE_PATH = "/tmp/pdpscanner/reports";
  private final String JNCSS_XML_FILE = "javancss.xml";
  private final String PMD_XML_FILE = "pmd.xml";
  private final String CLOC_XML_FILE = "cloc.xml";
  private final String COBERTURA_XML_FILE = "coverage.xml";
  private final String FINDBUGS_XML_FILE = "findbugs.xml";
  private String COBERTURA_SCRIPT_PATH = "/home/user/Documents/trunk/trunk/Scripts/cobertura.sh";
  private String FINDBUGS_SCRIPT_PATH = "/home/user/Documents/trunk/trunk/Scripts/findbugs.sh";
  private String JNCSS_SCRIPT_PATH = "/home/user/Documents/trunk/trunk/Scripts/javancss.sh";
  private String CLOC_SCRIPT_PATH = "/home/user/Documents/trunk/trunk/Scripts/cloc.sh";
  private String PMD_SCRIPT_PATH = "/home/user/Documents/trunk/trunk/Scripts/pmd.sh";
  private final String CLOC_LANGUAGE = "Java";

  private static SVNClientManager clientManager = SVNClientManager
      .newInstance();

  public MainScript(String path) {

    COBERTURA_SCRIPT_PATH = path + "/cobertura.sh";
    FINDBUGS_SCRIPT_PATH = path + "/findbugs.sh";
    JNCSS_SCRIPT_PATH = path + "/javancss.sh";
    CLOC_SCRIPT_PATH = path + "/cloc.sh";
    PMD_SCRIPT_PATH = path + "/pmd.sh";
  }

  public void analyzeAll() throws Exception {

    // Database connection
    System.out.println("connecting to server");
    MySQLConnection conn = new MySQLConnection();
    conn.connect();

    // Get the project repository details
    ResultSet rs = conn.SQLSelect("SELECT * FROM projects");

    while (rs.next()) {

      String url = rs.getString("repos_url");
      String user = rs.getString("repos_user");
      if (rs.wasNull())
        user = "";

      String password = rs.getString("repos_password");
      if (rs.wasNull())
        password = "";

      String name = rs.getString("name");

      int idProject = rs.getInt("id");
      int lastVersion = 1;
      ResultSet rs2 = conn
          .SQLSelect("select max(version)+1 as last_version from versions where project = "
              + idProject);

      while (rs2.next()) {
        lastVersion = Math.max(lastVersion, rs2.getInt("last_version"));
      }

      int idVersion = 1;
      conn.SQLUpdate("insert into versions (project , version , creation_date) values ("
          + idProject + "," + lastVersion + " , sysdate())");

      ResultSet rs3 = conn
          .SQLSelect("select id from versions where version = "
              + lastVersion + " and project = " + idProject);

      while (rs3.next()) {
        idVersion = rs3.getInt("id");
      }

      System.out.println("Checking out project : " + name + " ...");

      checkoutProject(url, user, password, CHECKOUT_DESTINATION_PATH
          + "/" + name);

      deleteDirectory(new File(REPORTS_PATH));

      File reportsDir = new File(REPORTS_PATH);

      if (reportsDir.mkdir()) {
        System.out.println("Reports directory created");
      }

      ExecuteScript javaNcssThread = new ExecuteScript(JNCSS_SCRIPT_PATH,
          CHECKOUT_DESTINATION_PATH + "/" + name + "/src");

      ExecuteScript pmdThread = new ExecuteScript(PMD_SCRIPT_PATH,
          CHECKOUT_DESTINATION_PATH + "/" + name + "/src");

      ExecuteScript clocThread = new ExecuteScript(CLOC_SCRIPT_PATH,
          CHECKOUT_DESTINATION_PATH + "/" + name + "/src");

      ExecuteScript findBugsThread = new ExecuteScript(
          FINDBUGS_SCRIPT_PATH, CHECKOUT_DESTINATION_PATH + "/"
              + name + "/bin");

      ExecuteScript coberturaThread = new ExecuteScript(
          COBERTURA_SCRIPT_PATH, CHECKOUT_DESTINATION_PATH + "/"
              + name + "/bin");

      System.out.println("Analyzing ...");

      System.out.println("Starting JavaNCSS ...");
      javaNcssThread.start();
      System.out.println("Starting PMD /CPD ...");
      pmdThread.start();
      System.out.println("Starting CLOC ...");
      clocThread.start();
      System.out.println("Starting FindBugs ...");
      findBugsThread.start();

      javaNcssThread.join();
      pmdThread.join();
      clocThread.join();
      findBugsThread.join();
      System.out.println("JavaNCSS ended");
      System.out.println("PMD /CPD ended");
      System.out.println("CLOC ended");
      System.out.println("FindBugs ended");

      System.out.println("Starting Cobertura ...");
      coberturaThread.start();
      coberturaThread.join();
      System.out.println("Cobertura ended");

      System.out.println("Analyzing done");

      GeneralParserThread generalParser = new GeneralParserThread(
          XML_FILE_PATH + "/" + JNCSS_XML_FILE, XML_FILE_PATH + "/"
              + PMD_XML_FILE,
          XML_FILE_PATH + "/" + CLOC_XML_FILE, CLOC_LANGUAGE,
          idVersion);

      CoberturaParserThread coberturaParserThread = new CoberturaParserThread(
          XML_FILE_PATH + "/" + this.COBERTURA_XML_FILE, idVersion);

      FindbugsParserThread findBugsParserThread = new FindbugsParserThread(
          XML_FILE_PATH + "/" + this.FINDBUGS_XML_FILE, idVersion);

      System.out.println("Parsing reports ...");

      generalParser.start();
      coberturaParserThread.start();
      findBugsParserThread.start();

      generalParser.join();
      coberturaParserThread.join();
      findBugsParserThread.join();

      System.out.println("Parsing of the reports done");

      System.out.println("Deleting directories ...");

      File path = new File(CHECKOUT_DESTINATION_PATH);
      deleteDirectory(path);
      path = new File(XML_FILE_PATH);
      deleteDirectory(path);

      System.out.println("Directories deleted");
    }

    conn.disconnect();
  }

  long checkoutProject(String url, String user, String password,
      String destPath) throws SVNException {

    // create an SVNURL
    SVNURL svnUrl = SVNURL.parseURIEncoded(url);

    // create a destination directory
    File destinationDir = new File(destPath);

    if (!user.isEmpty() && !password.isEmpty()) {

      ISVNAuthenticationManager authManager = new BasicAuthenticationManager(
          user, password);
      clientManager.setAuthenticationManager(authManager);
    }

    SVNUpdateClient updateClient = clientManager.getUpdateClient();

    updateClient.setIgnoreExternals(false);

    return updateClient.doCheckout(svnUrl, destinationDir,
        SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, false);
  }

  static public boolean deleteDirectory(File path) {
   
    if (path.exists()) {
      File[] files = path.listFiles();
      for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
          deleteDirectory(files[i]);
        } else {
          files[i].delete();
        }
      }
    }
    return (path.delete());
  }

}
TOP

Related Classes of scripts.MainScript

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.