Skip to content

Commit

Permalink
updating scmclient package to use guice. related to #8, #20
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Pinto committed Apr 28, 2013
1 parent a8a99b0 commit dfe6ee3
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
22 changes: 15 additions & 7 deletions src/java/main/br/ufpe/cin/groundhog/crawler/CrawlGitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
22 changes: 19 additions & 3 deletions src/java/main/br/ufpe/cin/groundhog/crawler/CrawlGoogleCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
Expand All @@ -65,7 +77,11 @@ public static void main(String[] args) throws Exception {
List<Project> 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<Future<File>> fs = crawl.downloadProjects(projects);
crawl.shutdown();
for (Future<File> f : fs) f.get();
Expand Down
12 changes: 7 additions & 5 deletions src/java/main/br/ufpe/cin/groundhog/main/CmdMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 8 additions & 6 deletions src/java/main/br/ufpe/cin/groundhog/main/TestMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,15 +44,15 @@ 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<Project> projects = search.getProjects(term, 1);
Project project = projects.get(0);
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<Future<File>> futures = crawler.downloadProjects(projects);
crawler.shutdown();
File repositoryFolder = null;
Expand Down Expand Up @@ -122,15 +124,15 @@ 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<Project> projects = search.getProjects(term, 1);
Project project = projects.get(0);
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<Future<File>> futures = crawler.downloadProjects(projects);
crawler.shutdown();
File repositoryFolder = null;
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 1 addition & 13 deletions src/java/main/br/ufpe/cin/groundhog/scmclient/GitClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down
14 changes: 2 additions & 12 deletions src/java/main/br/ufpe/cin/groundhog/scmclient/SVNClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -47,6 +38,5 @@ public void checkout(String url, File destination, SVNRevision revision) throws

public void close() {
svn.dispose();
instance = null;
}
}
13 changes: 13 additions & 0 deletions src/java/main/br/ufpe/cin/groundhog/scmclient/ScmModule.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -35,7 +39,7 @@ public void testCrawlGithub() {

Project playframework = searchGitHub.getProjects("playframework", 1).get(0);
List<Project> projects = Arrays.asList(playframework);
CrawlGitHub crawl = new CrawlGitHub(Files.createTempDir());
CrawlGitHub crawl = new CrawlGitHub(gitClient, Files.createTempDir());
List<Future<File>> fs = crawl.downloadProjects(projects);
crawl.shutdown();
for (Future<File> f : fs) {
Expand Down

1 comment on commit dfe6ee3

@rodrigoalvesvieira
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow. Guice is one of the neatest ideas ever!

Please sign in to comment.