You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DhtRepositoryBuilder.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (C) 2011, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.storage.dht;
  44. import java.io.File;
  45. import java.text.MessageFormat;
  46. import java.util.concurrent.TimeoutException;
  47. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  48. import org.eclipse.jgit.lib.BaseRepositoryBuilder;
  49. import org.eclipse.jgit.storage.dht.spi.Database;
  50. /**
  51. * Constructs a {@link DhtRepository}.
  52. *
  53. * @param <B>
  54. * type of builder used by the DHT system.
  55. * @param <R>
  56. * type of repository used by the DHT system.
  57. * @param <D>
  58. * type of database used by the DHT system.
  59. */
  60. public class DhtRepositoryBuilder<B extends DhtRepositoryBuilder, R extends DhtRepository, D extends Database>
  61. extends BaseRepositoryBuilder<B, R> {
  62. private D database;
  63. private DhtReaderOptions readerOptions;
  64. private DhtInserterOptions inserterOptions;
  65. private String name;
  66. private RepositoryKey key;
  67. /** Initializes an empty builder with no values set. */
  68. public DhtRepositoryBuilder() {
  69. setBare();
  70. setMustExist(true);
  71. }
  72. /** @return the database that stores the repositories. */
  73. public D getDatabase() {
  74. return database;
  75. }
  76. /**
  77. * Set the cluster used to store the repositories.
  78. *
  79. * @param database
  80. * the database supplier.
  81. * @return {@code this}
  82. */
  83. public B setDatabase(D database) {
  84. this.database = database;
  85. return self();
  86. }
  87. /** @return options used by readers accessing the repository. */
  88. public DhtReaderOptions getReaderOptions() {
  89. return readerOptions;
  90. }
  91. /**
  92. * Set the reader options.
  93. *
  94. * @param opt
  95. * new reader options object.
  96. * @return {@code this}
  97. */
  98. public B setReaderOptions(DhtReaderOptions opt) {
  99. readerOptions = opt;
  100. return self();
  101. }
  102. /** @return options used by writers accessing the repository. */
  103. public DhtInserterOptions getInserterOptions() {
  104. return inserterOptions;
  105. }
  106. /**
  107. * Set the inserter options.
  108. *
  109. * @param opt
  110. * new inserter options object.
  111. * @return {@code this}
  112. */
  113. public B setInserterOptions(DhtInserterOptions opt) {
  114. inserterOptions = opt;
  115. return self();
  116. }
  117. /** @return name of the repository in the DHT. */
  118. public String getRepositoryName() {
  119. return name;
  120. }
  121. /**
  122. * Set the name of the repository to open.
  123. *
  124. * @param name
  125. * the name.
  126. * @return {@code this}.
  127. */
  128. public B setRepositoryName(String name) {
  129. this.name = name;
  130. return self();
  131. }
  132. /** @return the repository's key. */
  133. public RepositoryKey getRepositoryKey() {
  134. return key;
  135. }
  136. /**
  137. * @param key
  138. * @return {@code this}
  139. */
  140. public B setRepositoryKey(RepositoryKey key) {
  141. this.key = key;
  142. return self();
  143. }
  144. @Override
  145. public B setup() throws IllegalArgumentException, DhtException,
  146. RepositoryNotFoundException {
  147. if (getDatabase() == null)
  148. throw new IllegalArgumentException(DhtText.get().databaseRequired);
  149. if (getReaderOptions() == null)
  150. setReaderOptions(new DhtReaderOptions());
  151. if (getInserterOptions() == null)
  152. setInserterOptions(new DhtInserterOptions());
  153. if (getRepositoryKey() == null) {
  154. if (getRepositoryName() == null)
  155. throw new IllegalArgumentException(DhtText.get().nameRequired);
  156. RepositoryKey r;
  157. try {
  158. r = getDatabase().repositoryIndex().get(
  159. RepositoryName.create(name));
  160. } catch (TimeoutException e) {
  161. throw new DhtTimeoutException(MessageFormat.format(
  162. DhtText.get().timeoutLocatingRepository, name), e);
  163. }
  164. if (isMustExist() && r == null)
  165. throw new RepositoryNotFoundException(getRepositoryName());
  166. if (r != null)
  167. setRepositoryKey(r);
  168. }
  169. return self();
  170. }
  171. @Override
  172. @SuppressWarnings("unchecked")
  173. public R build() throws IllegalArgumentException, DhtException,
  174. RepositoryNotFoundException {
  175. return (R) new DhtRepository(setup());
  176. }
  177. // We don't support local file IO and thus shouldn't permit these to set.
  178. @Override
  179. public B setGitDir(File gitDir) {
  180. if (gitDir != null)
  181. throw new IllegalArgumentException();
  182. return self();
  183. }
  184. @Override
  185. public B setObjectDirectory(File objectDirectory) {
  186. if (objectDirectory != null)
  187. throw new IllegalArgumentException();
  188. return self();
  189. }
  190. @Override
  191. public B addAlternateObjectDirectory(File other) {
  192. throw new UnsupportedOperationException("Alternates not supported");
  193. }
  194. @Override
  195. public B setWorkTree(File workTree) {
  196. if (workTree != null)
  197. throw new IllegalArgumentException();
  198. return self();
  199. }
  200. @Override
  201. public B setIndexFile(File indexFile) {
  202. if (indexFile != null)
  203. throw new IllegalArgumentException();
  204. return self();
  205. }
  206. }