James Moger
2011-10-21 4d44cf806ddfa8d051f2d6b1289fa3b67b0daf2e
src/com/gitblit/GitBlit.java
@@ -59,13 +59,17 @@
import com.gitblit.Constants.FederationToken;
import com.gitblit.models.FederationModel;
import com.gitblit.models.FederationProposal;
import com.gitblit.models.FederationSet;
import com.gitblit.models.Metric;
import com.gitblit.models.ObjectCache;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.UserModel;
import com.gitblit.utils.ByteFormat;
import com.gitblit.utils.FederationUtils;
import com.gitblit.utils.JGitUtils;
import com.gitblit.utils.JsonUtils;
import com.gitblit.utils.MetricUtils;
import com.gitblit.utils.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
 * GitBlit is the servlet context listener singleton that acts as the core for
@@ -95,6 +99,10 @@
         .synchronizedList(new ArrayList<FederationModel>());
   private final Map<String, FederationModel> federationPullResults = new ConcurrentHashMap<String, FederationModel>();
   private final ObjectCache<Long> repositorySizeCache = new ObjectCache<Long>();
   private final ObjectCache<List<Metric>> repositoryMetricsCache = new ObjectCache<List<Metric>>();
   private RepositoryResolver<Void> repositoryResolver;
@@ -418,6 +426,16 @@
   }
   /**
    * Clears all the cached data for the specified repository.
    *
    * @param repositoryName
    */
   public void clearRepositoryCache(String repositoryName) {
      repositorySizeCache.remove(repositoryName);
      repositoryMetricsCache.remove(repositoryName);
   }
   /**
    * Returns the list of all repositories available to Gitblit. This method
    * does not consider user access permissions.
    * 
@@ -468,6 +486,20 @@
         if (model != null) {
            repositories.add(model);
         }
      }
      if (getBoolean(Keys.web.showRepositorySizes, true)) {
         int repoCount = 0;
         long startTime = System.currentTimeMillis();
         ByteFormat byteFormat = new ByteFormat();
         for (RepositoryModel model : repositories) {
            if (!model.skipSizeCalculation) {
               repoCount++;
               model.size = byteFormat.format(calculateSize(model));
            }
         }
         long duration = System.currentTimeMillis() - startTime;
         logger.info(MessageFormat.format("{0} repository sizes calculated in {1} msecs",
               repoCount, duration));
      }
      return repositories;
   }
@@ -522,6 +554,7 @@
         model.showRemoteBranches = getConfig(config, "showRemoteBranches", false);
         model.isFrozen = getConfig(config, "isFrozen", false);
         model.showReadme = getConfig(config, "showReadme", false);
         model.skipSizeCalculation = getConfig(config, "skipSizeCalculation", false);
         model.federationStrategy = FederationStrategy.fromName(getConfig(config,
               "federationStrategy", null));
         model.federationSets = new ArrayList<String>(Arrays.asList(config.getStringList(
@@ -534,14 +567,22 @@
   }
   /**
    * Returns the size in bytes of the repository.
    * Returns the size in bytes of the repository. Gitblit caches the
    * repository sizes to reduce the performance penalty of recursive
    * calculation. The cache is updated if the repository has been changed
    * since the last calculation.
    * 
    * @param model
    * @return size in bytes
    */
   public long calculateSize(RepositoryModel model) {
      if (repositorySizeCache.hasCurrent(model.name, model.lastChange)) {
         return repositorySizeCache.getObject(model.name);
      }
      File gitDir = FileKey.resolve(new File(repositoriesFolder, model.name), FS.DETECTED);
      return com.gitblit.utils.FileUtils.folderSize(gitDir);
      long size = com.gitblit.utils.FileUtils.folderSize(gitDir);
      repositorySizeCache.updateObject(model.name, model.lastChange, size);
      return size;
   }
   /**
@@ -580,7 +621,26 @@
   }
   /**
    * Returns the gitblit string vlaue for the specified key. If key is not
    * Returns the metrics for the default branch of the specified repository.
    * This method builds a metrics cache. The cache is updated if the
    * repository is updated. A new copy of the metrics list is returned on each
    * call so that modifications to the list are non-destructive.
    *
    * @param model
    * @param repository
    * @return a new array list of metrics
    */
   public List<Metric> getRepositoryDefaultMetrics(RepositoryModel model, Repository repository) {
      if (repositoryMetricsCache.hasCurrent(model.name, model.lastChange)) {
         return new ArrayList<Metric>(repositoryMetricsCache.getObject(model.name));
      }
      List<Metric> metrics = MetricUtils.getDateMetrics(repository, null, true, null);
      repositoryMetricsCache.updateObject(model.name, model.lastChange, metrics);
      return new ArrayList<Metric>(metrics);
   }
   /**
    * Returns the gitblit string value for the specified key. If key is not
    * set, returns defaultValue.
    * 
    * @param config
@@ -662,6 +722,9 @@
                     "Failed to rename repository permissions ''{0}'' to ''{1}''.",
                     repositoryName, repository.name));
            }
            // clear the cache
            clearRepositoryCache(repositoryName);
         }
         // load repository
@@ -702,6 +765,7 @@
      config.setBoolean("gitblit", null, "showRemoteBranches", repository.showRemoteBranches);
      config.setBoolean("gitblit", null, "isFrozen", repository.isFrozen);
      config.setBoolean("gitblit", null, "showReadme", repository.showReadme);
      config.setBoolean("gitblit", null, "skipSizeCalculation", repository.skipSizeCalculation);
      config.setStringList("gitblit", null, "federationSets", repository.federationSets);
      config.setString("gitblit", null, "federationStrategy",
            repository.federationStrategy.name());
@@ -741,6 +805,9 @@
               return true;
            }
         }
         // clear the repository cache
         clearRepositoryCache(repositoryName);
      } catch (Throwable t) {
         logger.error(MessageFormat.format("Failed to delete repository {0}", repositoryName), t);
      }
@@ -875,6 +942,29 @@
   }
   /**
    * Returns the list of federation sets.
    *
    * @return list of federation sets
    */
   public List<FederationSet> getFederationSets(String gitblitUrl) {
      List<FederationSet> list = new ArrayList<FederationSet>();
      // generate standard tokens
      for (FederationToken type : FederationToken.values()) {
         FederationSet fset = new FederationSet(type.toString(), type, getFederationToken(type));
         fset.repositories = getRepositories(gitblitUrl, fset.token);
         list.add(fset);
      }
      // generate tokens for federation sets
      for (String set : settings.getStrings(Keys.federation.sets)) {
         FederationSet fset = new FederationSet(set, FederationToken.REPOSITORIES,
               getFederationToken(set));
         fset.repositories = getRepositories(gitblitUrl, fset.token);
         list.add(fset);
      }
      return list;
   }
   /**
    * Returns the list of possible federation tokens for this Gitblit instance.
    * 
    * @return list of federation tokens
@@ -978,8 +1068,7 @@
    */
   public boolean submitFederationProposal(FederationProposal proposal, String gitblitUrl) {
      // convert proposal to json
      Gson gson = new GsonBuilder().setPrettyPrinting().create();
      String json = gson.toJson(proposal);
      String json = JsonUtils.toJsonString(proposal);
      try {
         // make the proposals folder
@@ -1025,10 +1114,10 @@
                     && file.getName().toLowerCase().endsWith(Constants.PROPOSAL_EXT);
            }
         });
         Gson gson = new Gson();
         for (File file : files) {
            String json = com.gitblit.utils.FileUtils.readContent(file, null);
            FederationProposal proposal = gson.fromJson(json, FederationProposal.class);
            FederationProposal proposal = JsonUtils.fromJsonString(json,
                  FederationProposal.class);
            list.add(proposal);
         }
      }