diff options
Diffstat (limited to 'config-application-package/src/main/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistry.java')
-rw-r--r-- | config-application-package/src/main/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistry.java | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistry.java b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistry.java new file mode 100644 index 00000000000..67a24e0159b --- /dev/null +++ b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistry.java @@ -0,0 +1,98 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.model.application.provider; + +import com.yahoo.config.FileReference; +import com.yahoo.config.application.api.FileRegistry; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.Reader; +import java.util.*; +import java.util.regex.Pattern; + +/** + * Registry of files added earlier (i.e. during deployment) + * + * @author tonytv + */ +public class PreGeneratedFileRegistry implements FileRegistry { + + private final String fileSourceHost; + private final Map<String, String> path2Hash = new LinkedHashMap<>(); + + private static String entryDelimiter = "\t"; + private static Pattern entryDelimiterPattern = Pattern.compile(entryDelimiter, Pattern.LITERAL); + + private PreGeneratedFileRegistry(Reader readerArg) { + BufferedReader reader = new BufferedReader(readerArg); + try { + fileSourceHost = reader.readLine(); + if (fileSourceHost == null) + throw new RuntimeException("Error while reading pre generated file registry"); + + String line; + while ((line = reader.readLine()) != null) { + addFromLine(line); + } + } catch(IOException e) { + throw new RuntimeException("Error while reading pre generated file registry", e); + } finally { + try { + reader.close(); + } catch(IOException e) {} + } + } + + private void addFromLine(String line) { + String[] parts = entryDelimiterPattern.split(line); + addEntry(parts[0], parts[1]); + } + + private void addEntry(String relativePath, String hash) { + path2Hash.put(relativePath, hash); + } + + public static String exportRegistry(FileRegistry registry) { + List<FileRegistry.Entry> entries = registry.export(); + StringBuilder builder = new StringBuilder(); + + builder.append(registry.fileSourceHost()).append('\n'); + for (FileRegistry.Entry entry : entries) { + builder.append(entry.relativePath).append(entryDelimiter).append(entry.reference.value()). + append('\n'); + } + + return builder.toString(); + } + + public static PreGeneratedFileRegistry importRegistry(Reader reader) { + return new PreGeneratedFileRegistry(reader); + } + + public FileReference addFile(String relativePath) { + return FileReferenceCreator.create(path2Hash.get(relativePath)); + } + + @Override + public String fileSourceHost() { + return fileSourceHost; + } + + public Set<String> getPaths() { + return path2Hash.keySet(); + } + + @Override + public Set<String> allRelativePaths() { + return path2Hash.keySet(); + } + + @Override + public List<Entry> export() { + List<Entry> entries = new ArrayList<>(); + for (Map.Entry<String, String> entry : path2Hash.entrySet()) { + entries.add(new Entry(entry.getKey(), FileReferenceCreator.create(entry.getValue()))); + } + return entries; + } +} |