Abstracted snapshot work into interface.

This commit is contained in:
Winston Li
2014-11-03 17:19:17 +00:00
parent 8879cb46db
commit 21ec6dd28d
7 changed files with 186 additions and 207 deletions
@@ -22,7 +22,7 @@ public class WLGitBridgeApplication {
/**
* Constructs an instance of the WriteLatex-Git Bridge application.
* @param args args from main, which should be in the format "port root_git_directory_path"
* @param args args from main, which should be in the format [port, root_git_directory_path]
*/
public WLGitBridgeApplication(String[] args) {
try {
@@ -12,6 +12,8 @@ import org.eclipse.jgit.transport.resolver.RepositoryResolver;
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
import uk.ac.ic.wlgitbridge.writelatex.RepositorySource;
import uk.ac.ic.wlgitbridge.writelatex.SnapshotRepositorySource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
@@ -23,25 +25,16 @@ import java.io.IOException;
public class WLRepositoryResolver implements RepositoryResolver<HttpServletRequest> {
private File rootGitDirectory;
private RepositorySource repositorySource;
public WLRepositoryResolver(String rootGitDirectoryPath) throws InvalidRootDirectoryPathException {
initRootGitDirectory(rootGitDirectoryPath);
repositorySource = new SnapshotRepositorySource();
}
@Override
public Repository open(HttpServletRequest httpServletRequest, String name) throws RepositoryNotFoundException, ServiceNotAuthorizedException, ServiceNotEnabledException, ServiceMayNotContinueException {
File repositoryRoot = new File(rootGitDirectory.getAbsolutePath(), name);
Repository repository = null;
try {
repository = new FileRepositoryBuilder().setWorkTree(repositoryRoot).build();
} catch (IOException e) {
throw new RepositoryNotFoundException(name);
}
if (!repository.getObjectDatabase().exists()) {
throw new RepositoryNotFoundException(name);
}
return repository;
return repositorySource.getRepositoryWithNameAtRootDirectory(name, rootGitDirectory);
}
private void initRootGitDirectory(String rootGitDirectoryPath) throws InvalidRootDirectoryPathException {
@@ -0,0 +1,15 @@
package uk.ac.ic.wlgitbridge.writelatex;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
import java.io.File;
/**
* Created by Winston on 03/11/14.
*/
public interface RepositorySource {
public Repository getRepositoryWithNameAtRootDirectory(String name, File rootDirectory) throws RepositoryNotFoundException;
}
@@ -0,0 +1,31 @@
package uk.ac.ic.wlgitbridge.writelatex;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import java.io.File;
import java.io.IOException;
/**
* Created by Winston on 03/11/14.
*/
public class SnapshotRepositorySource implements RepositorySource {
@Override
public Repository getRepositoryWithNameAtRootDirectory(String name, File rootDirectory) throws RepositoryNotFoundException {
File repositoryRoot = new File(rootDirectory.getAbsolutePath(), name);
Repository repository = null;
try {
repository = new FileRepositoryBuilder().setWorkTree(repositoryRoot).build();
} catch (IOException e) {
throw new RepositoryNotFoundException(name);
}
if (!repository.getObjectDatabase().exists()) {
throw new RepositoryNotFoundException(name);
}
return repository;
}
}