@Override protected IRemoteRepo _call() { Preconditions.checkNotNull(remote, "remote to connect to not provided"); Optional<IRemoteRepo> opRemote = RemoteResolver.newRemote(remote, remoteHints); checkArgument(opRemote.isPresent(), "Unknown remote type: " + remote.getFetchURL()); IRemoteRepo remoteRepo = opRemote.get(); try { remoteRepo.open(); } catch (RepositoryConnectionException e) { throw new IllegalStateException("Failed to connect to remote: " + e.getMessage(), e); } return remoteRepo; }
/** * Constructs an interface to allow access to a remote repository. * * @param remote the remote to connect to * @param remoteHints hints for the remote repo, like read-only, etc. * @return an {@link Optional} of the interface to the remote repository, or * {@link Optional#absent()} if a connection to the remote could not be established. */ public static Optional<IRemoteRepo> newRemote(Remote remote, @Nullable Hints remoteHints) { if (remoteHints == null) { remoteHints = new Hints(); } Iterator<RemoteResolver> resolvers = ServiceLoader.load(RemoteResolver.class).iterator(); while (resolvers.hasNext()) { RemoteResolver resolver = resolvers.next(); Optional<IRemoteRepo> resolved = resolver.resolve(remote, remoteHints); if (resolved.isPresent()) { return resolved; } } return Optional.absent(); } }
@Override protected IRemoteRepo _call() { Preconditions.checkNotNull(remote, "remote to connect to not provided"); Optional<IRemoteRepo> opRemote = RemoteResolver.newRemote(remote, remoteHints); checkArgument(opRemote.isPresent(), "Unknown remote type: " + remote.getFetchURL()); IRemoteRepo remoteRepo = opRemote.get(); try { remoteRepo.open(); } catch (RepositoryConnectionException e) { throw new IllegalStateException("Failed to connect to remote: " + e.getMessage(), e); } return remoteRepo; }
/** * Constructs an interface to allow access to a remote repository. * * @param remote the remote to connect to * @param remoteHints hints for the remote repo, like read-only, etc. * @return an {@link Optional} of the interface to the remote repository, or * {@link Optional#absent()} if a connection to the remote could not be established. */ public static Optional<IRemoteRepo> newRemote(Remote remote, @Nullable Hints remoteHints) { if (remoteHints == null) { remoteHints = new Hints(); } Iterator<RemoteResolver> resolvers = ServiceLoader .load(RemoteResolver.class, RemoteResolver.class.getClassLoader()).iterator(); while (resolvers.hasNext()) { RemoteResolver resolver = resolvers.next(); Optional<IRemoteRepo> resolved = resolver.resolve(remote, remoteHints); if (resolved.isPresent()) { return resolved; } } return Optional.absent(); } }
private static Ref pingRemote(Remote remote) throws Exception { Optional<IRemoteRepo> remoteRepo; try { Hints hints = Hints.readOnly(); Repository localRepo = GlobalContextBuilder.builder().build(hints).repository(); remoteRepo = RemoteResolver.newRemote(remote, null); if (!remoteRepo.isPresent()) { throw new IllegalArgumentException("Repository not found or not reachable"); } else { IRemoteRepo repo = remoteRepo.get(); try { repo.open(); Optional<Ref> head = repo.headRef(); return head.orNull(); } finally { repo.close(); } } } catch (Exception e) { throw new IllegalArgumentException("Unable to connect: " + e.getMessage(), e); } }