Yi EungJun 2015-03-06
Make yobi.home configurable
Data and configuration is read from and stores into the home directory,
which is indicated by yobi.home system property.

The default home direcory equals to user.dir.
@a88c1c72f400468f069e1fa1262c7d24440ef029
app/Global.java
--- app/Global.java
+++ app/Global.java
@@ -378,11 +378,12 @@
          * @return the path to the directory to store configuration files
          */
         static Path getDirectoryPath() {
-            return Paths.get(CONFIG_DIRNAME);
+            return Paths.get(Config.getYobiHome(""), CONFIG_DIRNAME);
         }
 
         boolean isExternal() throws IOException, URISyntaxException {
-            return !FileUtil.isSubpathOf(getPath(), getDirectoryPath());
+            return !FileUtil.isSubpathOf(getPath(), getDirectoryPath()) &&
+                   !FileUtil.isSubpathOf(getPath(), Paths.get(Config.getYobiHome()));
         }
     }
 }
app/controllers/ImportApp.java
--- app/controllers/ImportApp.java
+++ app/controllers/ImportApp.java
@@ -109,7 +109,7 @@
 
         if (!filledNewProjectForm.errors().isEmpty()) {
             List<OrganizationUser> orgUserList = OrganizationUser.findByAdmin(UserApp.currentUser().id);
-            FileUtil.rm_rf(new File(GitRepository.getGitDirectory(project)));
+            FileUtil.rm_rf(GitRepository.getGitDirectory(project));
             return badRequest(importing.render("title.newProject", filledNewProjectForm, orgUserList));
         } else {
             return redirect(routes.ProjectApp.project(project.owner, project.name));
app/models/Attachment.java
--- app/models/Attachment.java
+++ app/models/Attachment.java
@@ -33,6 +33,7 @@
 import play.libs.Akka;
 import scala.concurrent.duration.Duration;
 import utils.AttachmentCache;
+import utils.Config;
 import utils.FileUtil;
 import utils.JodaDateUtil;
 
@@ -277,7 +278,11 @@
      * @return the file
      */
     public File getFile() {
-        return new File(uploadDirectory, this.hash);
+        return new File(getUploadDirectory(), this.hash);
+    }
+
+    public static File getUploadDirectory() {
+        return new File(utils.Config.getYobiHome(), uploadDirectory);
     }
 
     /**
@@ -300,7 +305,7 @@
      * @return true if the file exists
      */
     public static boolean fileExists(String hash) {
-        return new File(uploadDirectory, hash).isFile();
+        return new File(getUploadDirectory(), hash).isFile();
     }
 
     /**
@@ -577,7 +582,7 @@
 
     // Create the upload directory if it doesn't exist.
     private static File createUploadDirectory() throws NotDirectoryException {
-        File uploads = new File(uploadDirectory);
+        File uploads = getUploadDirectory();
         uploads.mkdirs();
         if (!uploads.isDirectory()) {
             throw new NotDirectoryException(
app/playRepository/GitRepository.java
--- app/playRepository/GitRepository.java
+++ app/playRepository/GitRepository.java
@@ -133,11 +133,11 @@
                                                 boolean alternatesMergeRepo) {
         try {
             RepositoryBuilder repo = new RepositoryBuilder()
-                    .setGitDir(new File(getGitDirectory(ownerName, projectName)));
+                    .setGitDir(getGitDirectory(ownerName, projectName));
 
             if (alternatesMergeRepo) {
-                repo.addAlternateObjectDirectory(new File(getDirectoryForMergingObjects(ownerName,
-                        projectName)));
+                repo.addAlternateObjectDirectory(getDirectoryForMergingObjects(ownerName,
+                        projectName));
             }
 
             return repo.build();
@@ -922,7 +922,7 @@
      * @return
      * @see #getGitDirectory(String, String)
      */
-    public static String getGitDirectory(Project project) {
+    public static File getGitDirectory(Project project) {
         return getGitDirectory(project.owner, project.name);
     }
 
@@ -937,8 +937,7 @@
      * @throws IOException
      */
     public static String getGitDirectoryURL(Project project) throws IOException {
-        String currentDirectory = new java.io.File( "." ).getCanonicalPath();
-        return currentDirectory + "/" + getGitDirectory(project);
+        return getGitDirectory(project).getCanonicalPath();
     }
 
     /**
@@ -951,8 +950,16 @@
      * @param projectName
      * @return
      */
-    public static String getGitDirectory(String ownerName, String projectName) {
-        return getRepoPrefix() + ownerName + "/" + projectName + ".git";
+    public static File getGitDirectory(String ownerName, String projectName) {
+        return new File(getRootDirectory(), ownerName + "/" + projectName + ".git");
+    }
+
+    private static File getRootDirectory() {
+        return new File(utils.Config.getYobiHome(), getRepoPrefix());
+    }
+
+    private static File getRootDirectoryForMerging() {
+        return new File(utils.Config.getYobiHome(), getRepoForMergingPrefix());
     }
 
     /**
@@ -968,20 +975,18 @@
      * * @see <a href="https://www.kernel.org/pub/software/scm/git/docs/gitglossary.html#def_bare_repository">bare repository</a>
      */
     public static void cloneRepository(String gitUrl, Project forkingProject) throws GitAPIException {
-        String directory = getGitDirectory(forkingProject);
         Git.cloneRepository()
                 .setURI(gitUrl)
-                .setDirectory(new File(directory))
+                .setDirectory(getGitDirectory(forkingProject))
                 .setCloneAllBranches(true)
                 .setBare(true)
                 .call();
     }
 
     public static void cloneRepository(String gitUrl, Project forkingProject, String authId, String authPw) throws GitAPIException {
-        String directory = getGitDirectory(forkingProject);
         Git.cloneRepository()
                 .setURI(gitUrl)
-                .setDirectory(new File(directory))
+                .setDirectory(getGitDirectory(forkingProject))
                 .setCloneAllBranches(true)
                 .setBare(true)
                 .setCredentialsProvider(new UsernamePasswordCredentialsProvider(authId, authPw))
@@ -1011,12 +1016,12 @@
      * @param projectName
      * @return
      */
-    public static String getDirectoryForMerging(String owner, String projectName) {
-        return getRepoForMergingPrefix() + owner + "/" + projectName + ".git";
+    public static File getDirectoryForMerging(String owner, String projectName) {
+        return new File(getRootDirectoryForMerging(), owner + "/" + projectName + ".git");
     }
 
-    public static String getDirectoryForMergingObjects(String owner, String projectName) {
-        return getDirectoryForMerging(owner, projectName) + "/.git/objects";
+    public static File getDirectoryForMergingObjects(String owner, String projectName) {
+        return new File(getDirectoryForMerging(owner, projectName), ".git/objects");
     }
 
     @SuppressWarnings("unchecked")
@@ -1228,12 +1233,12 @@
     }
 
     public static Repository buildMergingRepository(Project project) {
-        String workingTree = GitRepository.getDirectoryForMerging(project.owner, project.name);
+        File workingDirectory = GitRepository.getDirectoryForMerging(project.owner, project.name);
 
         try {
-            File gitDir = new File(workingTree + "/.git");
+            File gitDir = new File(workingDirectory + "/.git");
             if(!gitDir.exists()) {
-                return cloneRepository(project, workingTree).getRepository();
+                return cloneRepository(project, workingDirectory).getRepository();
             } else {
                 return new RepositoryBuilder().setGitDir(gitDir).build();
             }
@@ -1242,10 +1247,10 @@
         }
     }
 
-    private static Git cloneRepository(Project project, String workingTreePath) throws GitAPIException, IOException {
+    private static Git cloneRepository(Project project, File workingDirectory) throws GitAPIException, IOException {
         return Git.cloneRepository()
                 .setURI(GitRepository.getGitDirectoryURL(project))
-                .setDirectory(new File(workingTreePath))
+                .setDirectory(workingDirectory)
                 .call();
     }
 
@@ -1859,10 +1864,10 @@
         WindowCacheConfig config = new WindowCacheConfig();
         config.install();
 
-        File srcGitDirectory = new File(getGitDirectory(srcProjectOwner, srcProjectName));
-        File destGitDirectory = new File(getGitDirectory(desrProjectOwner, destProjectName));
-        File srcGitDirectoryForMerging = new File(getDirectoryForMerging(srcProjectOwner, srcProjectName));
-        File destGitDirectoryForMerging = new File(getDirectoryForMerging(desrProjectOwner, destProjectName));
+        File srcGitDirectory = getGitDirectory(srcProjectOwner, srcProjectName);
+        File destGitDirectory = getGitDirectory(desrProjectOwner, destProjectName);
+        File srcGitDirectoryForMerging = getDirectoryForMerging(srcProjectOwner, srcProjectName);
+        File destGitDirectoryForMerging = getDirectoryForMerging(desrProjectOwner, destProjectName);
         srcGitDirectory.setWritable(true);
         srcGitDirectoryForMerging.setWritable(true);
 
app/playRepository/RepositoryService.java
--- app/playRepository/RepositoryService.java
+++ app/playRepository/RepositoryService.java
@@ -36,6 +36,7 @@
 import play.mvc.Http.Request;
 import play.mvc.Http.Response;
 import playRepository.hooks.*;
+import utils.Config;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
@@ -148,7 +149,7 @@
             @Override
             public String getInitParameter(String name) {
                 if (name.equals("SVNParentPath")) {
-                    return new File(SVNRepository.getRepoPrefix() + userName + "/").getAbsolutePath();
+                    return new File(SVNRepository.getRootDirectory(), userName + "/").getAbsolutePath();
                 } else {
                     return play.Configuration.root().getString("application." + name);
                 }
app/playRepository/SVNRepository.java
--- app/playRepository/SVNRepository.java
+++ app/playRepository/SVNRepository.java
@@ -201,14 +201,12 @@
 
     @Override
     public void create() throws SVNException {
-        String svnPath = new File(SVNRepository.getRepoPrefix() + ownerName + "/" + projectName)
-                .getAbsolutePath();
-        SVNRepositoryFactory.createLocalRepository(new File(svnPath), true, false);
+        SVNRepositoryFactory.createLocalRepository(getDirectory(), true, false);
     }
 
     @Override
     public void delete() throws Exception {
-        FileUtil.rm_rf(new File(getRepoPrefix() + ownerName + "/" + projectName));
+        FileUtil.rm_rf(getDirectory());
     }
 
     @Override
@@ -224,7 +222,7 @@
 
     private String getPatch(long revA, long revB) throws SVNException, UnsupportedEncodingException {
         // Prepare required arguments.
-        SVNURL svnURL = SVNURL.fromFile(new File(getRepoPrefix() + ownerName + "/" + projectName));
+        SVNURL svnURL = SVNURL.fromFile(getDirectory());
 
         // Get diffClient.
         SVNClientManager clientManager = SVNClientManager.newInstance();
@@ -253,7 +251,7 @@
     public List<Commit> getHistory(int page, int limit, String until, String path) throws
             IOException, GitAPIException, SVNException {
         // Get the repository
-        SVNURL svnURL = SVNURL.fromFile(new File(repoPrefix + ownerName + "/" + projectName));
+        SVNURL svnURL = SVNURL.fromFile(getDirectory());
         org.tmatesoft.svn.core.io.SVNRepository repository = SVNRepositoryFactory.create(svnURL);
 
         // path to get log
@@ -287,7 +285,7 @@
     public Commit getCommit(String revNumber) throws IOException, SVNException {
         long rev = Integer.parseInt(revNumber);
         String[] paths = {"/"};
-        SVNURL svnURL = SVNURL.fromFile(new File(getRepoPrefix() + ownerName + "/" + projectName));
+        SVNURL svnURL = SVNURL.fromFile(getDirectory());
         org.tmatesoft.svn.core.io.SVNRepository repository = SVNRepositoryFactory.create(svnURL);
 
         for(Object entry : repository.log(paths, null, rev, rev, false, false)) {
@@ -326,8 +324,7 @@
     }
 
     private org.tmatesoft.svn.core.io.SVNRepository getSVNRepository() throws SVNException {
-        SVNURL svnURL = SVNURL.fromFile(new File(getRepoPrefix() + ownerName + "/" +
-                projectName));
+        SVNURL svnURL = SVNURL.fromFile(getDirectory());
 
         return SVNRepositoryFactory.create(svnURL);
     }
@@ -376,7 +373,7 @@
         SVNURL svnURL;
         org.tmatesoft.svn.core.io.SVNRepository repository = null;
         try {
-            svnURL = SVNURL.fromFile(new File(repoPrefix + ownerName + "/" + projectName));
+            svnURL = SVNURL.fromFile(getDirectory());
             repository = SVNRepositoryFactory.create(svnURL);
             return repository.getLatestRevision() == 0;
         } catch (SVNException e) {
@@ -389,8 +386,8 @@
     }
 
     public boolean move(String srcProjectOwner, String srcProjectName, String desrProjectOwner, String destProjectName) {
-        File src = new File(getRepoPrefix() + srcProjectOwner + "/" + srcProjectName);
-        File dest = new File(getRepoPrefix() + desrProjectOwner + "/" + destProjectName);
+        File src = new File(getRootDirectory(), srcProjectOwner + "/" + srcProjectName);
+        File dest = new File(getRootDirectory(), desrProjectOwner + "/" + destProjectName);
         src.setWritable(true);
 
         try {
@@ -406,6 +403,10 @@
 
     @Override
     public File getDirectory() {
-        return new File(getRepoPrefix() + ownerName + "/" + projectName);
+        return new File(getRootDirectory(), ownerName + "/" + projectName);
+    }
+
+    public static File getRootDirectory() {
+        return new File(Config.getYobiHome(), getRepoPrefix());
     }
 }
app/utils/Config.java
--- app/utils/Config.java
+++ app/utils/Config.java
@@ -20,13 +20,20 @@
  */
 package utils;
 
+import com.typesafe.config.ConfigFactory;
 import models.SiteAdmin;
 import org.apache.commons.lang3.ObjectUtils;
 import play.Configuration;
 import play.mvc.Http;
 
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
 import java.net.*;
 import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.Enumeration;
 
 public class Config {
@@ -303,4 +310,12 @@
     public static <T> T get(Key<T> key) {
         return (T) Configuration.root().getObject(key.getName(), key.getDefault());
     }
+
+    public static String getYobiHome() {
+        return System.getProperty("yobi.home");
+    }
+
+    public static String getYobiHome(String defaultValue) {
+        return System.getProperty("yobi.home", defaultValue);
+    }
 }
build.sbt
--- build.sbt
+++ build.sbt
@@ -75,6 +75,7 @@
 
 NativePackagerKeys.bashScriptExtraDefines += """# Added by build.sbt
     |YOBI_HOME=$(cd "$(realpath "$(dirname "$(realpath "$0")")")/.."; pwd -P)
+    |addJava "-Dyobi.home=$YOBI_HOME"
     |
     |yobi_config_file="$YOBI_HOME"/conf/application.conf
     |yobi_log_config_file="$YOBI_HOME"/conf/application-logger.xml
test/playRepository/GitRepositoryTest.java
--- test/playRepository/GitRepositoryTest.java
+++ test/playRepository/GitRepositoryTest.java
@@ -204,8 +204,8 @@
         Project original = createProject(userName, projectName);
         Project fork = createProject("keesun", projectName);
 
-        support.Files.rm_rf(new File(GitRepository.getGitDirectory(original)));
-        support.Files.rm_rf(new File(GitRepository.getGitDirectory(fork)));
+        support.Files.rm_rf(GitRepository.getGitDirectory(original));
+        support.Files.rm_rf(GitRepository.getGitDirectory(fork));
 
         GitRepository fromRepo = new GitRepository(userName, projectName);
         fromRepo.create();
@@ -215,7 +215,7 @@
         GitRepository.cloneRepository(gitUrl, fork);
 
         // Then
-        File file = new File(GitRepository.getGitDirectory(fork));
+        File file = GitRepository.getGitDirectory(fork);
         assertThat(file.exists()).isTrue();
     }
 
Add a comment
List