diff --git a/src/java/main/br/ufpe/cin/groundhog/codehistory/GitCodeHistory.java b/src/java/main/br/ufpe/cin/groundhog/codehistory/GitCodeHistory.java index 08e7028..1e766f1 100644 --- a/src/java/main/br/ufpe/cin/groundhog/codehistory/GitCodeHistory.java +++ b/src/java/main/br/ufpe/cin/groundhog/codehistory/GitCodeHistory.java @@ -3,11 +3,21 @@ import java.io.File; import java.util.Date; +import com.google.inject.Inject; + import br.ufpe.cin.groundhog.scmclient.EmptyProjectAtDateException; import br.ufpe.cin.groundhog.scmclient.GitClient; import br.ufpe.cin.groundhog.util.FileUtil; public class GitCodeHistory implements CodeHistory { + + private final GitClient gitClient; + + @Inject + public GitCodeHistory(GitClient gitClient){ + this.gitClient = gitClient; + } + @Override public File checkoutToDate(String project, String url, Date date) { throw new NoSuchMethodError("Not implemented"); @@ -22,7 +32,7 @@ public File checkoutToDate(String project, File repositoryFolder, .createTempDir(), project); FileUtil.getInstance().copyDirectory(repositoryFolder, projectFolder); - GitClient.getInstance().checkout(projectFolder, date); + this.gitClient.checkout(projectFolder, date); return projectFolder; } catch (Exception e) { throw new CheckoutException(e); diff --git a/src/java/main/br/ufpe/cin/groundhog/codehistory/SvnCodeHistory.java b/src/java/main/br/ufpe/cin/groundhog/codehistory/SvnCodeHistory.java index 3e15b66..77e4c0b 100644 --- a/src/java/main/br/ufpe/cin/groundhog/codehistory/SvnCodeHistory.java +++ b/src/java/main/br/ufpe/cin/groundhog/codehistory/SvnCodeHistory.java @@ -8,15 +8,29 @@ import org.tmatesoft.svn.core.wc.SVNRevision; import br.ufpe.cin.groundhog.scmclient.SVNClient; +import br.ufpe.cin.groundhog.scmclient.ScmModule; import br.ufpe.cin.groundhog.util.FileUtil; +import com.google.inject.Guice; +import com.google.inject.Inject; +import com.google.inject.Injector; + public class SvnCodeHistory implements CodeHistory { - + + private final SVNClient svnClient; + + @Inject + public SvnCodeHistory(SVNClient svnClient) { + this.svnClient = svnClient; + } + @Override - public File checkoutToDate(String project, String url, Date date) throws CheckoutException { + public File checkoutToDate(String project, String url, Date date) + throws CheckoutException { try { - File projectFolder = new File(FileUtil.getInstance().createTempDir(), project); - SVNClient.getInstance().checkout(url, projectFolder, SVNRevision.create(date)); + File projectFolder = new File(FileUtil.getInstance() + .createTempDir(), project); + svnClient.checkout(url, projectFolder, SVNRevision.create(date)); return projectFolder; } catch (SVNException e) { throw new CheckoutException(e); @@ -27,12 +41,14 @@ public File checkoutToDate(String project, String url, Date date) throws Checkou public File checkoutToDate(String project, File repositoryFolder, Date date) { throw new NoSuchMethodError("Not implemented"); } - + public static void main(String[] args) throws Exception { Date d = new GregorianCalendar(2009, 2, 10, 18, 32, 17).getTime(); System.out.println(d); - new SvnCodeHistory().checkoutToDate("geom-java", - "http://wkhtmltopdf.googlecode.com/svn/", - d); + Injector injector = Guice.createInjector(new ScmModule()); + SVNClient svnClient = injector.getInstance(SVNClient.class); + + new SvnCodeHistory(svnClient).checkoutToDate("geom-java", + "http://wkhtmltopdf.googlecode.com/svn/", d); } } \ No newline at end of file diff --git a/src/java/main/br/ufpe/cin/groundhog/crawler/CrawlGitHub.java b/src/java/main/br/ufpe/cin/groundhog/crawler/CrawlGitHub.java index b0b5b9c..eb6bafd 100644 --- a/src/java/main/br/ufpe/cin/groundhog/crawler/CrawlGitHub.java +++ b/src/java/main/br/ufpe/cin/groundhog/crawler/CrawlGitHub.java @@ -13,22 +13,30 @@ import com.google.inject.Inject; +/** + * A concrete class to crawl GitHub. + * + * @author fjsj + */ public class CrawlGitHub extends ForgeCrawler { + private final GitClient gitClient; + @Inject - public CrawlGitHub(File destinationFolder) { + public CrawlGitHub(GitClient gitClient, File destinationFolder) { super(destinationFolder); + this.gitClient = gitClient; } - + @Override - protected File downloadProject(Project project) - throws JSONException, IOException, - InvalidRemoteException, TransportException, GitAPIException { + protected File downloadProject(Project project) throws JSONException, + IOException, InvalidRemoteException, TransportException, + GitAPIException { String projectName = project.getName(); String cloneUrl = project.getScmURL(); File projectFolder = new File(destinationFolder, projectName); - - GitClient.getInstance().clone(cloneUrl, projectFolder); + + gitClient.clone(cloneUrl, projectFolder); return projectFolder; } } \ No newline at end of file diff --git a/src/java/main/br/ufpe/cin/groundhog/crawler/CrawlGoogleCode.java b/src/java/main/br/ufpe/cin/groundhog/crawler/CrawlGoogleCode.java index 4d10c82..6091a70 100644 --- a/src/java/main/br/ufpe/cin/groundhog/crawler/CrawlGoogleCode.java +++ b/src/java/main/br/ufpe/cin/groundhog/crawler/CrawlGoogleCode.java @@ -16,12 +16,24 @@ import br.ufpe.cin.groundhog.Project; import br.ufpe.cin.groundhog.SCM; import br.ufpe.cin.groundhog.scmclient.GitClient; +import br.ufpe.cin.groundhog.scmclient.ScmModule; +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * A concrete class to crawl GitHub. + * + * @author fjsj + */ public class CrawlGoogleCode extends ForgeCrawler { private static Logger logger = LoggerFactory.getLogger(CrawlGoogleCode.class); - public CrawlGoogleCode(File destinationFolder) { + private final GitClient gitClient; + + public CrawlGoogleCode(GitClient gitClient, File destinationFolder) { super(destinationFolder); + this.gitClient = gitClient; } @Override @@ -39,7 +51,7 @@ protected File downloadProject(Project project) break; case GIT: String url = project.getScmURL(); - GitClient.getInstance().clone(url, projectFolder); + gitClient.clone(url, projectFolder); break; case NONE: logger.warn(String.format("Project %s has no SCM.", projectName)); @@ -65,7 +77,11 @@ public static void main(String[] args) throws Exception { List projects = Arrays.asList( new Project("epubcheck", "")); File dest = new File("C:\\Users\\fjsj\\Downloads\\EponaProjects\\"); - CrawlGoogleCode crawl = new CrawlGoogleCode(dest); + + Injector injector = Guice.createInjector(new ScmModule()); + GitClient gitClient = injector.getInstance(GitClient.class); + + CrawlGoogleCode crawl = new CrawlGoogleCode(gitClient, dest); List> fs = crawl.downloadProjects(projects); crawl.shutdown(); for (Future f : fs) f.get(); diff --git a/src/java/main/br/ufpe/cin/groundhog/main/CmdMain.java b/src/java/main/br/ufpe/cin/groundhog/main/CmdMain.java index 8188cde..f0cf119 100644 --- a/src/java/main/br/ufpe/cin/groundhog/main/CmdMain.java +++ b/src/java/main/br/ufpe/cin/groundhog/main/CmdMain.java @@ -40,7 +40,8 @@ import br.ufpe.cin.groundhog.http.Requests; import br.ufpe.cin.groundhog.parser.JavaParser; import br.ufpe.cin.groundhog.scmclient.EmptyProjectAtDateException; -import br.ufpe.cin.groundhog.scmclient.SVNClient; +import br.ufpe.cin.groundhog.scmclient.GitClient; +import br.ufpe.cin.groundhog.scmclient.ScmModule; import br.ufpe.cin.groundhog.search.ForgeSearch; import br.ufpe.cin.groundhog.search.SearchException; import br.ufpe.cin.groundhog.search.SearchGitHub; @@ -75,17 +76,19 @@ public static ForgeSearch defineForgeSearch(SupportedForge f) { public static ForgeCrawler defineForgeCrawler(SupportedForge f, File destinationFolder) { ForgeCrawler crawler = null; + Injector injector = Guice.createInjector(new HttpModule(), new ScmModule()); switch (f) { case GITHUB: - crawler = new CrawlGitHub(destinationFolder); + GitClient client = injector.getInstance(GitClient.class); + crawler = new CrawlGitHub(client, destinationFolder); break; case SOURCEFORGE: - Injector injector = Guice.createInjector(new HttpModule()); Requests requests = injector.getInstance(Requests.class); crawler = new CrawlSourceForge(requests, destinationFolder); break; case GOOGLECODE: - crawler = new CrawlGoogleCode(destinationFolder); + GitClient gitClient = injector.getInstance(GitClient.class); + crawler = new CrawlGoogleCode(gitClient, destinationFolder); break; } return crawler; @@ -168,7 +171,6 @@ public static void analyzeProject(Project project, File projectFolder, Date date public static void freeResources(ForgeCrawler crawler, OutputStream errorStream) { crawler.shutdown(); - SVNClient.getInstance().close(); try { FileUtil.getInstance().deleteTempDirs(); } catch (IOException e) { diff --git a/src/java/main/br/ufpe/cin/groundhog/main/TestMain.java b/src/java/main/br/ufpe/cin/groundhog/main/TestMain.java index 810c745..4dbd940 100644 --- a/src/java/main/br/ufpe/cin/groundhog/main/TestMain.java +++ b/src/java/main/br/ufpe/cin/groundhog/main/TestMain.java @@ -26,6 +26,8 @@ import br.ufpe.cin.groundhog.http.Requests; import br.ufpe.cin.groundhog.parser.JavaParser; import br.ufpe.cin.groundhog.parser.MutableInt; +import br.ufpe.cin.groundhog.scmclient.GitClient; +import br.ufpe.cin.groundhog.scmclient.ScmModule; import br.ufpe.cin.groundhog.search.SearchGitHub; import br.ufpe.cin.groundhog.search.SearchGoogleCode; import br.ufpe.cin.groundhog.search.SearchModule; @@ -42,7 +44,7 @@ public static void gitHubExample(String term) throws Exception { File downloadFolder = FileUtil.getInstance().createTempDir(); logger.info("1 - Search for projects according to term..."); - Injector injector = Guice.createInjector(new SearchModule(), new CodeHistoryModule(), new CodeHistoryModule()); + Injector injector = Guice.createInjector(new SearchModule(), new CodeHistoryModule(), new CodeHistoryModule(), new ScmModule()); SearchGitHub search = injector.getInstance(SearchGitHub.class); List projects = search.getProjects(term, 1); @@ -50,7 +52,7 @@ public static void gitHubExample(String term) throws Exception { projects = Arrays.asList(project); // analyze only the first project logger.info("2 - Download 1st result..."); - ForgeCrawler crawler = new CrawlGitHub(downloadFolder); + ForgeCrawler crawler = new CrawlGitHub(injector.getInstance(GitClient.class), downloadFolder); List> futures = crawler.downloadProjects(projects); crawler.shutdown(); File repositoryFolder = null; @@ -122,7 +124,7 @@ public static void googleCodeExample(String term) throws Exception { File downloadFolder = FileUtil.getInstance().createTempDir(); logger.info("1 - Search for projects according to term..."); - Injector injector = Guice.createInjector(new SearchModule(), new CodeHistoryModule()); + Injector injector = Guice.createInjector(new SearchModule(), new CodeHistoryModule(), new ScmModule()); SearchGoogleCode search = injector.getInstance(SearchGoogleCode.class); List projects = search.getProjects(term, 1); @@ -130,7 +132,7 @@ public static void googleCodeExample(String term) throws Exception { projects = Arrays.asList(project); // analyze only the first project logger.info("2 - Download 1st result..."); - ForgeCrawler crawler = new CrawlGoogleCode(downloadFolder); + ForgeCrawler crawler = new CrawlGoogleCode(injector.getInstance(GitClient.class), downloadFolder); List> futures = crawler.downloadProjects(projects); crawler.shutdown(); File repositoryFolder = null; @@ -163,8 +165,8 @@ public static void googleCodeExample(String term) throws Exception { } public static void main(String[] args) throws Exception { -// gitHubExample("jsoup"); - sourceForgeExample(); + gitHubExample("jsoup"); +// sourceForgeExample(); // googleCodeExample("facebook-java-api"); // Google Code SVN // googleCodeExample("guava-libraries"); // Google Code Git } diff --git a/src/java/main/br/ufpe/cin/groundhog/scmclient/GitClient.java b/src/java/main/br/ufpe/cin/groundhog/scmclient/GitClient.java index b078a30..4cf460e 100644 --- a/src/java/main/br/ufpe/cin/groundhog/scmclient/GitClient.java +++ b/src/java/main/br/ufpe/cin/groundhog/scmclient/GitClient.java @@ -27,19 +27,7 @@ import org.gitective.core.filter.commit.CommitterDateFilter; public class GitClient { - private static GitClient instance; - - public static GitClient getInstance() { - if (instance == null) { - instance = new GitClient(); - } - return instance; - } - - private GitClient() { - - } - + public void clone(String url, File destination) throws InvalidRemoteException, TransportException, GitAPIException { Repository rep = Git.cloneRepository(). diff --git a/src/java/main/br/ufpe/cin/groundhog/scmclient/SVNClient.java b/src/java/main/br/ufpe/cin/groundhog/scmclient/SVNClient.java index 9f7fd3f..c7b7c75 100644 --- a/src/java/main/br/ufpe/cin/groundhog/scmclient/SVNClient.java +++ b/src/java/main/br/ufpe/cin/groundhog/scmclient/SVNClient.java @@ -10,19 +10,10 @@ import org.tmatesoft.svn.core.wc.SVNUpdateClient; public class SVNClient { - private static SVNClient instance; - - public static SVNClient getInstance() { - if (instance == null) { - instance = new SVNClient(); - } - return instance; - } - private SVNClientManager svn; - private SVNClient() { - svn = SVNClientManager.newInstance(); + public SVNClient() { + this.svn = SVNClientManager.newInstance(); } public void checkout(String url, File destination) throws SVNException { @@ -47,6 +38,5 @@ public void checkout(String url, File destination, SVNRevision revision) throws public void close() { svn.dispose(); - instance = null; } } \ No newline at end of file diff --git a/src/java/main/br/ufpe/cin/groundhog/scmclient/ScmModule.java b/src/java/main/br/ufpe/cin/groundhog/scmclient/ScmModule.java new file mode 100644 index 0000000..e0b7b78 --- /dev/null +++ b/src/java/main/br/ufpe/cin/groundhog/scmclient/ScmModule.java @@ -0,0 +1,13 @@ +package br.ufpe.cin.groundhog.scmclient; + +import com.google.inject.AbstractModule; +import com.google.inject.Singleton; + +public class ScmModule extends AbstractModule { + + @Override + protected void configure() { + bind(GitClient.class).in(Singleton.class); + bind(SVNClient.class).in(Singleton.class); + } +} \ No newline at end of file diff --git a/src/java/test/br/ufpe/cin/groundhog/crawler/CrawlGitHubTest.java b/src/java/test/br/ufpe/cin/groundhog/crawler/CrawlGitHubTest.java index 60d31ad..5a77342 100644 --- a/src/java/test/br/ufpe/cin/groundhog/crawler/CrawlGitHubTest.java +++ b/src/java/test/br/ufpe/cin/groundhog/crawler/CrawlGitHubTest.java @@ -10,6 +10,8 @@ import org.junit.Test; import br.ufpe.cin.groundhog.Project; +import br.ufpe.cin.groundhog.scmclient.GitClient; +import br.ufpe.cin.groundhog.scmclient.ScmModule; import br.ufpe.cin.groundhog.search.SearchGitHub; import br.ufpe.cin.groundhog.search.SearchModule; @@ -20,11 +22,13 @@ public class CrawlGitHubTest { private SearchGitHub searchGitHub; + private GitClient gitClient; @Before public void setup() { - Injector injector = Guice.createInjector(new SearchModule()); + Injector injector = Guice.createInjector(new SearchModule(), new ScmModule()); searchGitHub = injector.getInstance(SearchGitHub.class); + gitClient = injector.getInstance(GitClient.class); } @Test @@ -35,7 +39,7 @@ public void testCrawlGithub() { Project playframework = searchGitHub.getProjects("playframework", 1).get(0); List projects = Arrays.asList(playframework); - CrawlGitHub crawl = new CrawlGitHub(Files.createTempDir()); + CrawlGitHub crawl = new CrawlGitHub(gitClient, Files.createTempDir()); List> fs = crawl.downloadProjects(projects); crawl.shutdown(); for (Future f : fs) {