Initial EGit contribution to eclipse.org
[egit.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / ListRemoteOperation.java
blob6963d750d2840869d84b6a5dc1a7b22b8f144cb0
1 /*******************************************************************************
2 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
9 *******************************************************************************/
10 package org.eclipse.egit.core.op;
12 import java.lang.reflect.InvocationTargetException;
13 import java.util.Collection;
14 import java.util.Map;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.egit.core.CoreText;
18 import org.eclipse.jface.operation.IRunnableWithProgress;
19 import org.eclipse.jgit.errors.NotSupportedException;
20 import org.eclipse.jgit.errors.TransportException;
21 import org.eclipse.jgit.lib.Ref;
22 import org.eclipse.jgit.lib.Repository;
23 import org.eclipse.jgit.transport.Connection;
24 import org.eclipse.jgit.transport.Transport;
25 import org.eclipse.jgit.transport.URIish;
27 /**
28 * Operation of listing remote repository advertised refs.
30 public class ListRemoteOperation implements IRunnableWithProgress {
31 private final Repository localDb;
33 private final URIish uri;
35 private Map<String, Ref> remoteRefsMap;
37 /**
38 * Create listing operation for specified local repository (needed by
39 * transport) and remote repository URI.
41 * @param localDb
42 * local repository (needed for transport) where fetch would
43 * occur.
44 * @param uri
45 * URI of remote repository to list.
47 public ListRemoteOperation(final Repository localDb, final URIish uri) {
48 this.localDb = localDb;
49 this.uri = uri;
52 /**
53 * @return collection of refs advertised by remote side.
54 * @throws IllegalStateException
55 * if error occurred during earlier remote refs listing.
57 public Collection<Ref> getRemoteRefs() {
58 checkState();
59 return remoteRefsMap.values();
62 /**
63 * @param refName
64 * remote ref name to search for.
65 * @return ref with specified refName or null if not found.
66 * @throws IllegalStateException
67 * if error occurred during earlier remote refs listing.
69 public Ref getRemoteRef(final String refName) {
70 checkState();
71 return remoteRefsMap.get(refName);
74 public void run(IProgressMonitor pm) throws InvocationTargetException,
75 InterruptedException {
76 Transport transport = null;
77 Connection connection = null;
78 try {
79 transport = Transport.open(localDb, uri);
81 if (pm != null)
82 pm.beginTask(CoreText.ListRemoteOperation_title,
83 IProgressMonitor.UNKNOWN);
84 connection = transport.openFetch();
85 remoteRefsMap = connection.getRefsMap();
86 } catch (NotSupportedException e) {
87 throw new InvocationTargetException(e);
88 } catch (TransportException e) {
89 throw new InvocationTargetException(e);
90 } finally {
91 if (connection != null)
92 connection.close();
93 if (transport != null)
94 transport.close();
95 if (pm != null)
96 pm.done();
100 private void checkState() {
101 if (remoteRefsMap == null)
102 throw new IllegalStateException(
103 "Error occurred during remote repo listing, no refs available");