Skip to content

Commit

Permalink
[TH2-2422] Multiple dictionaries access methods (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
Topru333 authored Feb 4, 2022
1 parent e9bbcef commit 6f8699b
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 59 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# th2 common library (Java) (3.32.1)
# th2 common library (Java) (3.33.0)

## Usage

Expand Down Expand Up @@ -288,6 +288,11 @@ dependencies {

## Release notes

### 3.33.0

+ Added ability to read dictionaries by aliases and as group of all available aliases
+ New methods for api: loadDictionary(String), getDictionaryAliases(), loadSingleDictionary()

### 3.32.1

+ Fixed: gRPC router didn't shut down underlying Netty's EventLoopGroup and ExecutorService
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
#

release_version=3.32.1
release_version=3.33.0

description = 'th2 common library (Java)'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 Exactpro (Exactpro Systems Limited)
* Copyright 2020-2022 Exactpro (Exactpro Systems Limited)
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 Exactpro (Exactpro Systems Limited)
* Copyright 2020-2022 Exactpro (Exactpro Systems Limited)
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down Expand Up @@ -72,7 +72,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -95,17 +94,16 @@
import java.util.jar.Attributes.Name;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static com.exactpro.cradle.cassandra.CassandraStorageSettings.DEFAULT_MAX_EVENT_BATCH_SIZE;
import static com.exactpro.cradle.cassandra.CassandraStorageSettings.DEFAULT_MAX_MESSAGE_BATCH_SIZE;
import static com.exactpro.th2.common.schema.util.ArchiveUtils.getGzipBase64StringDecoder;
import static java.util.Collections.emptyMap;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;

/**
*
* Class for load <b>JSON</b> schema configuration and create {@link GrpcRouter} and {@link MessageRouter}
*
* @see CommonFactory
Expand All @@ -114,6 +112,7 @@ public abstract class AbstractCommonFactory implements AutoCloseable {

protected static final String DEFAULT_CRADLE_INSTANCE_NAME = "infra";
protected static final String EXACTPRO_IMPLEMENTATION_VENDOR = "Exactpro Systems LLC";

/** @deprecated please use {@link #LOG4J_PROPERTIES_DEFAULT_PATH} */
@Deprecated
protected static final String LOG4J_PROPERTIES_DEFAULT_PATH_OLD = "/home/etc";
Expand Down Expand Up @@ -524,48 +523,41 @@ public <T> T getCustomConfiguration(Class<T> confClass) {
}

/**
* Read first and only one dictionary
* @return Dictionary as {@link InputStream}
* @throws IllegalStateException if can not read dictionary or found more than one target
*/
public abstract InputStream loadSingleDictionary();

/**
* @return list of available dictionary aliases or an empty list
* @throws IllegalStateException if can not read dictionary
*/
public InputStream readDictionary() {
return readDictionary(DictionaryType.MAIN);
}
public abstract Set<String> getDictionaryAliases();

/**
* @param dictionaryType desired type of dictionary
* @param alias name of dictionary
* @return Dictionary as {@link InputStream}
* @throws IllegalStateException if can not read dictionary
*/
public InputStream readDictionary(DictionaryType dictionaryType) {
try {
List<Path> dictionaries = null;
Path dictionaryTypeDictionary = dictionaryType.getDictionary(getPathToDictionariesDir());
if (Files.exists(dictionaryTypeDictionary) && Files.isDirectory(dictionaryTypeDictionary)) {
dictionaries = Files.list(dictionaryType.getDictionary(getPathToDictionariesDir()))
.filter(Files::isRegularFile)
.collect(Collectors.toList());
}

// Find with old format
if (dictionaries == null || dictionaries.isEmpty()) {
dictionaries = Files.list(getOldPathToDictionariesDir())
.filter(path -> Files.isRegularFile(path) && path.getFileName().toString().contains(dictionaryType.name()))
.collect(Collectors.toList());
}
public abstract InputStream loadDictionary(String alias);

if (dictionaries.isEmpty()) {
throw new IllegalStateException("No dictionary found with type '" + dictionaryType + "'");
} else if (dictionaries.size() > 1) {
throw new IllegalStateException("Found several dictionaries satisfying the '" + dictionaryType + "' type");
}

var targetDictionary = dictionaries.get(0);
/**
* Read dictionary of {@link DictionaryType#MAIN} type
* @return Dictionary as {@link InputStream}
* @throws IllegalStateException if can not read dictionary
*/
@Deprecated(since = "3.33.0", forRemoval = true)
public abstract InputStream readDictionary();

return new ByteArrayInputStream(getGzipBase64StringDecoder().decode(Files.readString(targetDictionary)));
} catch (IOException e) {
throw new IllegalStateException("Can not read dictionary", e);
}
}
/**
* @deprecated Dictionary types will be removed in future releases of infra, use alias instead
* @param dictionaryType desired type of dictionary
* @return Dictionary as {@link InputStream}
* @throws IllegalStateException if can not read dictionary
*/
@Deprecated(since = "3.33.0", forRemoval = true)
public abstract InputStream readDictionary(DictionaryType dictionaryType);

/**
* If root event does not exist, it creates root event with its name = box name and timestamp
Expand Down Expand Up @@ -610,10 +602,17 @@ public String getRootEventId() {
protected abstract Path getPathToCustomConfiguration();

/**
* @return Path to dictionary
* @return Path to dictionaries with type dir
*/
@Deprecated(since = "3.33.0", forRemoval = true)
protected abstract Path getPathToDictionaryTypesDir();

/**
* @return Path to dictionaries with alias dir
*/
protected abstract Path getPathToDictionariesDir();
protected abstract Path getPathToDictionaryAliasesDir();

@Deprecated(since = "3.33.0", forRemoval = true)
protected abstract Path getOldPathToDictionariesDir();

/**
Expand Down
Loading

0 comments on commit 6f8699b

Please sign in to comment.