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.

BaseConnection.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  6. *
  7. * This program and the accompanying materials are made available under the
  8. * terms of the Eclipse Distribution License v. 1.0 which is available at
  9. * https://www.eclipse.org/org/documents/edl-v10.php.
  10. *
  11. * SPDX-License-Identifier: BSD-3-Clause
  12. */
  13. package org.eclipse.jgit.transport;
  14. import java.io.StringWriter;
  15. import java.io.Writer;
  16. import java.util.Collection;
  17. import java.util.Collections;
  18. import java.util.Map;
  19. import org.eclipse.jgit.errors.TransportException;
  20. import org.eclipse.jgit.internal.JGitText;
  21. import org.eclipse.jgit.lib.Ref;
  22. /**
  23. * Base helper class for implementing operations connections.
  24. *
  25. * @see BasePackConnection
  26. * @see BaseFetchConnection
  27. */
  28. public abstract class BaseConnection implements Connection {
  29. private Map<String, Ref> advertisedRefs = Collections.emptyMap();
  30. private String peerUserAgent;
  31. private boolean startedOperation;
  32. private Writer messageWriter;
  33. /** {@inheritDoc} */
  34. @Override
  35. public Map<String, Ref> getRefsMap() {
  36. return advertisedRefs;
  37. }
  38. /** {@inheritDoc} */
  39. @Override
  40. public final Collection<Ref> getRefs() {
  41. return advertisedRefs.values();
  42. }
  43. /** {@inheritDoc} */
  44. @Override
  45. public final Ref getRef(String name) {
  46. return advertisedRefs.get(name);
  47. }
  48. /** {@inheritDoc} */
  49. @Override
  50. public String getMessages() {
  51. return messageWriter != null ? messageWriter.toString() : ""; //$NON-NLS-1$
  52. }
  53. /**
  54. * {@inheritDoc}
  55. *
  56. * User agent advertised by the remote server.
  57. * @since 4.0
  58. */
  59. @Override
  60. public String getPeerUserAgent() {
  61. return peerUserAgent;
  62. }
  63. /**
  64. * Remember the remote peer's agent.
  65. *
  66. * @param agent
  67. * remote peer agent string.
  68. * @since 4.0
  69. */
  70. protected void setPeerUserAgent(String agent) {
  71. peerUserAgent = agent;
  72. }
  73. /** {@inheritDoc} */
  74. @Override
  75. public abstract void close();
  76. /**
  77. * Denote the list of refs available on the remote repository.
  78. * <p>
  79. * Implementors should invoke this method once they have obtained the refs
  80. * that are available from the remote repository.
  81. *
  82. * @param all
  83. * the complete list of refs the remote has to offer. This map
  84. * will be wrapped in an unmodifiable way to protect it, but it
  85. * does not get copied.
  86. */
  87. protected void available(Map<String, Ref> all) {
  88. advertisedRefs = Collections.unmodifiableMap(all);
  89. }
  90. /**
  91. * Helper method for ensuring one-operation per connection. Check whether
  92. * operation was already marked as started, and mark it as started.
  93. *
  94. * @throws org.eclipse.jgit.errors.TransportException
  95. * if operation was already marked as started.
  96. */
  97. protected void markStartedOperation() throws TransportException {
  98. if (startedOperation)
  99. throw new TransportException(
  100. JGitText.get().onlyOneOperationCallPerConnectionIsSupported);
  101. startedOperation = true;
  102. }
  103. /**
  104. * Get the writer that buffers messages from the remote side.
  105. *
  106. * @return writer to store messages from the remote.
  107. */
  108. protected Writer getMessageWriter() {
  109. if (messageWriter == null)
  110. setMessageWriter(new StringWriter());
  111. return messageWriter;
  112. }
  113. /**
  114. * Set the writer that buffers messages from the remote side.
  115. *
  116. * @param writer
  117. * the writer that messages will be delivered to. The writer's
  118. * {@code toString()} method should be overridden to return the
  119. * complete contents.
  120. */
  121. protected void setMessageWriter(Writer writer) {
  122. if (messageWriter != null)
  123. throw new IllegalStateException(JGitText.get().writerAlreadyInitialized);
  124. messageWriter = writer;
  125. }
  126. }