comparison src/org/tmatesoft/hg/repo/HgRemoteRepository.java @ 698:822f3a83ff57

in, out and clone tests pass for ssh repositories. Infrastructure to decouple HgRemoteRepository from specific Connector implementation
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 06 Aug 2013 21:18:33 +0200
parents 24f4efedc9d5
children a483b2b68a2e
comparison
equal deleted inserted replaced
697:24f4efedc9d5 698:822f3a83ff57
19 import static org.tmatesoft.hg.internal.remote.Connector.*; 19 import static org.tmatesoft.hg.internal.remote.Connector.*;
20 import static org.tmatesoft.hg.util.Outcome.Kind.Failure; 20 import static org.tmatesoft.hg.util.Outcome.Kind.Failure;
21 import static org.tmatesoft.hg.util.Outcome.Kind.Success; 21 import static org.tmatesoft.hg.util.Outcome.Kind.Success;
22 22
23 import java.io.BufferedReader; 23 import java.io.BufferedReader;
24 import java.io.ByteArrayOutputStream;
25 import java.io.File; 24 import java.io.File;
26 import java.io.IOException; 25 import java.io.IOException;
27 import java.io.InputStream; 26 import java.io.InputStream;
28 import java.io.InputStreamReader; 27 import java.io.InputStreamReader;
29 import java.io.OutputStream; 28 import java.io.OutputStream;
30 import java.io.StreamTokenizer; 29 import java.io.StreamTokenizer;
31 import java.net.ContentHandler;
32 import java.net.ContentHandlerFactory;
33 import java.net.URL;
34 import java.net.URLConnection;
35 import java.util.ArrayList; 30 import java.util.ArrayList;
36 import java.util.Arrays; 31 import java.util.Arrays;
37 import java.util.Collection; 32 import java.util.Collection;
38 import java.util.Collections; 33 import java.util.Collections;
39 import java.util.HashSet; 34 import java.util.HashSet;
56 import org.tmatesoft.hg.internal.EncodingHelper; 51 import org.tmatesoft.hg.internal.EncodingHelper;
57 import org.tmatesoft.hg.internal.FileUtils; 52 import org.tmatesoft.hg.internal.FileUtils;
58 import org.tmatesoft.hg.internal.Internals; 53 import org.tmatesoft.hg.internal.Internals;
59 import org.tmatesoft.hg.internal.PropertyMarshal; 54 import org.tmatesoft.hg.internal.PropertyMarshal;
60 import org.tmatesoft.hg.internal.remote.Connector; 55 import org.tmatesoft.hg.internal.remote.Connector;
61 import org.tmatesoft.hg.internal.remote.HttpConnector; 56 import org.tmatesoft.hg.internal.remote.RemoteConnectorDescriptor;
62 import org.tmatesoft.hg.internal.remote.SshConnector; 57 import org.tmatesoft.hg.repo.HgLookup.RemoteDescriptor;
63 import org.tmatesoft.hg.util.LogFacility.Severity; 58 import org.tmatesoft.hg.util.LogFacility.Severity;
64 import org.tmatesoft.hg.util.Outcome; 59 import org.tmatesoft.hg.util.Outcome;
65 import org.tmatesoft.hg.util.Pair; 60 import org.tmatesoft.hg.util.Pair;
66 61
67 /** 62 /**
79 private HgLookup lookupHelper; 74 private HgLookup lookupHelper;
80 private final SessionContext sessionContext; 75 private final SessionContext sessionContext;
81 private Set<String> remoteCapabilities; 76 private Set<String> remoteCapabilities;
82 private Connector remote; 77 private Connector remote;
83 78
84 static { 79 HgRemoteRepository(SessionContext ctx, RemoteDescriptor rd) throws HgBadArgumentException {
85 URLConnection.setContentHandlerFactory(new ContentHandlerFactory() { 80 if (false == rd instanceof RemoteConnectorDescriptor) {
86 81 throw new IllegalArgumentException(String.format("Present implementation supports remote connections via %s only", Connector.class.getName()));
87 public ContentHandler createContentHandler(String mimetype) {
88 if ("application/mercurial-0.1".equals(mimetype)) {
89 return new ContentHandler() {
90
91 @Override
92 public Object getContent(URLConnection urlc) throws IOException {
93 if (urlc.getContentLength() > 0) {
94 ByteArrayOutputStream bos = new ByteArrayOutputStream();
95 InputStream is = urlc.getInputStream();
96 int r;
97 while ((r = is.read()) != -1) {
98 bos.write(r);
99 }
100 return new String(bos.toByteArray());
101 }
102 return "<empty>";
103 }
104 };
105 }
106 return null;
107 }
108 });
109 }
110
111 HgRemoteRepository(SessionContext ctx, URL url) throws HgBadArgumentException {
112 if (url == null || ctx == null) {
113 throw new IllegalArgumentException();
114 } 82 }
115 sessionContext = ctx; 83 sessionContext = ctx;
116 debug = new PropertyMarshal(ctx).getBoolean("hg4j.remote.debug", false); 84 debug = new PropertyMarshal(ctx).getBoolean("hg4j.remote.debug", false);
117 remote = "ssh".equals(url.getProtocol()) ? new SshConnector() : new HttpConnector(); 85 remote = ((RemoteConnectorDescriptor) rd).createConnector();
118 remote.init(url, ctx, null); 86 remote.init(rd.getURI(), ctx, null);
119 } 87 }
120 88
121 public boolean isInvalid() throws HgRemoteConnectionException { 89 public boolean isInvalid() throws HgRemoteConnectionException {
122 initCapabilities(); 90 initCapabilities();
123 return remoteCapabilities.isEmpty(); 91 return remoteCapabilities.isEmpty();
516 } 484 }
517 RemoteBranch o = (RemoteBranch) obj; 485 RemoteBranch o = (RemoteBranch) obj;
518 // in fact, p1 and p2 are not supposed to be null, ever (at least for RemoteBranch created from server output) 486 // in fact, p1 and p2 are not supposed to be null, ever (at least for RemoteBranch created from server output)
519 return head.equals(o.head) && root.equals(o.root) && (p1 == null && o.p1 == null || p1.equals(o.p1)) && (p2 == null && o.p2 == null || p2.equals(o.p2)); 487 return head.equals(o.head) && root.equals(o.root) && (p1 == null && o.p1 == null || p1.equals(o.p1)) && (p2 == null && o.p2 == null || p2.equals(o.p2));
520 } 488 }
489
490 @Override
491 public int hashCode() {
492 return head.hashCode() ^ root.hashCode();
493 }
494
495 @Override
496 public String toString() {
497 String none = String.valueOf(-1);
498 String s1 = p1 == null || p1.isNull() ? none : p1.shortNotation();
499 String s2 = p2 == null || p2.isNull() ? none : p2.shortNotation();
500 return String.format("RemoteBranch[root: %s, head:%s, p1:%s, p2:%s]", root.shortNotation(), head.shortNotation(), s1, s2);
501 }
521 } 502 }
522 503
523 public static final class Bookmarks implements Iterable<Pair<String, Nodeid>> { 504 public static final class Bookmarks implements Iterable<Pair<String, Nodeid>> {
524 private final List<Pair<String, Nodeid>> bm; 505 private final List<Pair<String, Nodeid>> bm;
525 506