org.zalando.baigan.service.github.GitCacheLoader Maven / Gradle / Ivy
package org.zalando.baigan.service.github;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import org.eclipse.egit.github.core.RepositoryContents;
import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.ContentsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zalando.baigan.model.Configuration;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.cache.CacheLoader;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import javax.annotation.Nonnull;
import static com.google.common.util.concurrent.MoreExecutors.listeningDecorator;
import static org.apache.commons.codec.binary.Base64.*;
/**
* This class implements the {@link CacheLoader} offering Configuration loading
* from a remote Git repository.
*
* @author mchand
*
*/
public class GitCacheLoader
extends CacheLoader> {
private static final Logger LOG = LoggerFactory
.getLogger(GitCacheLoader.class);
private String latestSha;
private GitConfig config;
private final ListeningExecutorService executorService = listeningDecorator(Executors.newFixedThreadPool(1));
private ObjectMapper objectMapper = new ObjectMapper().registerModule(new GuavaModule());
private final ContentsService contentsService;
private static ContentsService buildContentsService(@Nonnull final GitConfig gitConfig) {
Objects.requireNonNull(gitConfig, "gitConfig is required");
final GitHubClient client = new GitHubClient(gitConfig.getGitHost());
client.setOAuth2Token(gitConfig.getOauthToken());
return new ContentsService(client);
}
public GitCacheLoader(@Nonnull final GitConfig gitConfig) {
this(gitConfig, buildContentsService(gitConfig));
}
@VisibleForTesting
GitCacheLoader(GitConfig gitConfig, ContentsService contentsService) {
this.config = gitConfig;
this.contentsService = contentsService;
}
@Override
public Map load(String key) throws Exception {
final RepositoryContents contents = getContentsForFile(key);
if (contents != null) {
return updateContent(contents);
}
LOG.warn("Failed to load the repository contents for {}", key);
return ImmutableMap.of();
}
private Map updateContent(
@Nonnull final RepositoryContents contents) {
final String contentsSha = contents.getSha();
LOG.info("Loading the new repository contents [ SHA:{} ; NAME:{} ] ",
contentsSha, contents.getPath());
final List configurations = getConfigurations(getTextContent(contents));
latestSha = contentsSha;
final ImmutableMap.Builder builder = ImmutableMap.builder();
for (final Configuration each : configurations) {
builder.put(each.getAlias(), each);
}
return builder.build();
}
public ListenableFuture © 2015 - 2025 Weber Informatics LLC | Privacy Policy