PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/atlassian/jira/plugins/bitbucket/spi/CachingCommunicator.java

https://bitbucket.org/atlassian/jira-bitbucket-connector/
Java | 170 lines | 100 code | 21 blank | 49 comment | 6 complexity | bf4af3b6c6452f21321314c3228da7a3 MD5 | raw file
  1. package com.atlassian.jira.plugins.bitbucket.spi;
  2. import com.atlassian.jira.plugins.bitbucket.api.Changeset;
  3. import com.atlassian.jira.plugins.bitbucket.api.SourceControlException;
  4. import com.atlassian.jira.plugins.bitbucket.api.SourceControlRepository;
  5. import com.atlassian.jira.plugins.bitbucket.api.SourceControlUser;
  6. import com.google.common.base.Function;
  7. import com.google.common.collect.ComputationException;
  8. import com.google.common.collect.MapMaker;
  9. import org.apache.commons.lang.builder.EqualsBuilder;
  10. import org.apache.commons.lang.builder.HashCodeBuilder;
  11. import java.util.Map;
  12. import java.util.concurrent.TimeUnit;
  13. /**
  14. * A {@link com.atlassian.jira.plugins.bitbucket.spi.Communicator} implementation that caches results for quicker subsequent lookup times
  15. */
  16. public class CachingCommunicator implements Communicator
  17. {
  18. private final Communicator delegate;
  19. // private class ChangesetKey
  20. // {
  21. // final String id;
  22. // private final SourceControlRepository repository;
  23. //
  24. // public ChangesetKey(SourceControlRepository repository, String id)
  25. // {
  26. // this.repository = repository;
  27. // this.id = id;
  28. // }
  29. //
  30. // @Override
  31. // public boolean equals(Object o)
  32. // {
  33. // if (this == o) return true;
  34. // if (o == null || getClass() != o.getClass()) return false;
  35. // ChangesetKey that = (ChangesetKey) o;
  36. // if (!repository.equals(that.repository)) return false;
  37. // if (!id.equals(that.id)) return false;
  38. // return true;
  39. // }
  40. //
  41. // @Override
  42. // public int hashCode()
  43. // {
  44. // int result = repository.hashCode();
  45. // result = 31 * result + id.hashCode();
  46. // return result;
  47. // }
  48. // }
  49. private class UserKey
  50. {
  51. SourceControlRepository repository;
  52. String username;
  53. public UserKey(SourceControlRepository repository, String username)
  54. {
  55. this.repository = repository;
  56. this.username = username;
  57. }
  58. @Override
  59. public boolean equals(Object obj)
  60. {
  61. if (obj == null) return false;
  62. if (this == obj) return true;
  63. if (this.getClass() != obj.getClass()) return false;
  64. UserKey that = (UserKey) obj;
  65. return new EqualsBuilder().append(repository, that.repository).append(username, that.username).isEquals();
  66. }
  67. @Override
  68. public int hashCode()
  69. {
  70. return new HashCodeBuilder(17, 37).append(repository).append(username).toHashCode();
  71. }
  72. }
  73. private final Map<UserKey, SourceControlUser> userMap = new MapMaker().expiration(30, TimeUnit.MINUTES).makeComputingMap(
  74. new Function<UserKey, SourceControlUser>()
  75. {
  76. @Override
  77. public SourceControlUser apply(UserKey key)
  78. {
  79. return delegate.getUser(key.repository, key.username);
  80. }
  81. });
  82. // private final Map<ChangesetKey, Changeset> changesetMap = new MapMaker().expiration(30, TimeUnit.MINUTES).makeComputingMap(
  83. // new Function<ChangesetKey, Changeset>()
  84. // {
  85. // @Override
  86. // public Changeset apply(ChangesetKey key)
  87. // {
  88. // return delegate.getChangeset(key.repository, key.id);
  89. // }
  90. // });
  91. public CachingCommunicator(Communicator delegate)
  92. {
  93. this.delegate = delegate;
  94. }
  95. @Override
  96. public SourceControlUser getUser(SourceControlRepository repository, String username)
  97. {
  98. try
  99. {
  100. return userMap.get(new UserKey(repository, username));
  101. } catch (ComputationException e)
  102. {
  103. throw unrollException(e);
  104. }
  105. }
  106. @Override
  107. public Changeset getChangeset(SourceControlRepository repository, String id)
  108. {
  109. return delegate.getChangeset(repository, id);
  110. // try
  111. // {
  112. // return changesetMap.get(new ChangesetKey(repository, id));
  113. // } catch (ComputationException e)
  114. // {
  115. // throw unrollException(e);
  116. // }
  117. }
  118. private SourceControlException unrollException(ComputationException e)
  119. {
  120. return e.getCause() instanceof SourceControlException ? (SourceControlException) e.getCause() : new SourceControlException(e
  121. .getCause());
  122. }
  123. @Override
  124. public void setupPostcommitHook(SourceControlRepository repo, String postCommitUrl)
  125. {
  126. delegate.setupPostcommitHook(repo, postCommitUrl);
  127. }
  128. @Override
  129. public void removePostcommitHook(SourceControlRepository repo, String postCommitUrl)
  130. {
  131. delegate.removePostcommitHook(repo, postCommitUrl);
  132. }
  133. @Override
  134. public Iterable<Changeset> getChangesets(SourceControlRepository repository)
  135. {
  136. return delegate.getChangesets(repository);
  137. }
  138. @Override
  139. public UrlInfo getUrlInfo(RepositoryUri repositoryUri)
  140. {
  141. return delegate.getUrlInfo(repositoryUri);
  142. }
  143. @Override
  144. public void validateRepositoryAccess(String repositoryType, String projectKey, RepositoryUri repositoryUri, String username,
  145. String password, String adminUsername, String adminPassword, String accessToken) throws SourceControlException
  146. {
  147. delegate.validateRepositoryAccess(repositoryType, projectKey, repositoryUri, username, password, adminUsername, adminPassword, accessToken);
  148. }
  149. }