Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added microservice entry-point #278

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
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) (5.4.2)
# th2 common library (Java) (5.6.0)

## Usage

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

## Release notes

### 5.6.0-dev
#### Feature:
+ Added common microservice entry point
+ Added configuration provider to common factory

### 5.5.0-dev

#### Changed:
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 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
release_version=5.5.0
release_version=5.6.0
description='th2 common library (Java)'
vcs_url=https://github.com/th2-net/th2-common-j
kapt.include.compile.classpath=false
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.exactpro.th2.common.metrics.PrometheusConfiguration;
import com.exactpro.th2.common.schema.box.configuration.BoxConfiguration;
import com.exactpro.th2.common.schema.configuration.ConfigurationManager;
import com.exactpro.th2.common.schema.configuration.impl.JsonConfigurationProvider;
import com.exactpro.th2.common.schema.cradle.CradleConfidentialConfiguration;
import com.exactpro.th2.common.schema.cradle.CradleNonConfidentialConfiguration;
import com.exactpro.th2.common.schema.dictionary.DictionaryType;
Expand All @@ -56,21 +57,15 @@
import com.exactpro.th2.common.schema.message.impl.rabbitmq.custom.RabbitCustomRouter;
import com.exactpro.th2.common.schema.message.impl.rabbitmq.transport.GroupBatch;
import com.exactpro.th2.common.schema.message.impl.rabbitmq.transport.TransportGroupBatchRouter;
import com.exactpro.th2.common.schema.strategy.route.json.RoutingStrategyModule;
import com.exactpro.th2.common.schema.util.Log4jConfigUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.kotlin.KotlinFeature;
import com.fasterxml.jackson.module.kotlin.KotlinModule;
import io.prometheus.client.exporter.HTTPServer;
import io.prometheus.client.hotspot.DefaultExports;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -91,7 +86,6 @@
import static com.exactpro.cradle.cassandra.CassandraStorageSettings.DEFAULT_MAX_UNCOMPRESSED_TEST_EVENT_SIZE;
import static com.exactpro.cradle.cassandra.CassandraStorageSettings.DEFAULT_RESULT_PAGE_SIZE;
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}
Expand All @@ -108,25 +102,9 @@ public abstract class AbstractCommonFactory implements AutoCloseable {
protected static final Path LOG4J_PROPERTIES_DEFAULT_PATH = Path.of("/var/th2/config");
protected static final String LOG4J2_PROPERTIES_NAME = "log4j2.properties";

public static final ObjectMapper MAPPER = new ObjectMapper();

static {
MAPPER.registerModules(
new KotlinModule.Builder()
.withReflectionCacheSize(512)
.configure(KotlinFeature.NullToEmptyCollection, false)
.configure(KotlinFeature.NullToEmptyMap, false)
.configure(KotlinFeature.NullIsSameAsDefault, false)
.configure(KotlinFeature.SingletonSupport, false)
.configure(KotlinFeature.StrictNullChecks, false)
.build(),
new RoutingStrategyModule(MAPPER),
new JavaTimeModule()
);
}

static final String CUSTOM_CFG_ALIAS = "custom";
public static final ObjectMapper MAPPER = JsonConfigurationProvider.MAPPER;
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractCommonFactory.class);
private final StringSubstitutor stringSubstitutor;

private final Class<? extends MessageRouter<MessageBatch>> messageRouterParsedBatchClass;
private final Class<? extends MessageRouter<RawMessageBatch>> messageRouterRawBatchClass;
Expand Down Expand Up @@ -165,7 +143,6 @@ public AbstractCommonFactory(FactorySettings settings) {
eventBatchRouterClass = settings.getEventBatchRouterClass();
grpcRouterClass = settings.getGrpcRouterClass();
notificationEventBatchRouterClass = settings.getNotificationEventBatchRouterClass();
stringSubstitutor = new StringSubstitutor(key -> defaultIfBlank(settings.getVariables().get(key), System.getenv(key)));
}

public void start() {
Expand Down Expand Up @@ -397,11 +374,11 @@ public <T> MessageRouter<T> getCustomMessageRouter(Class<T> messageClass) {
}

/**
* @return Configuration by specified path
* @return Configuration by specified alias
* @throws IllegalStateException if can not read configuration
*/
public <T> T getConfiguration(Path configPath, Class<T> configClass, ObjectMapper customObjectMapper) {
return getConfigurationManager().loadConfiguration(customObjectMapper, stringSubstitutor, configClass, configPath, false);
public <T> T getConfiguration(String configAlias, Class<T> configClass, ObjectMapper customObjectMapper) {
OptimumCode marked this conversation as resolved.
Show resolved Hide resolved
return getConfigurationManager().loadConfiguration(configClass, configAlias, false);
}

/**
Expand All @@ -412,7 +389,7 @@ public <T> T getConfiguration(Path configPath, Class<T> configClass, ObjectMappe
* @return configuration object
*/
protected <T> T getConfigurationOrLoad(Class<T> configClass, boolean optional) {
return getConfigurationManager().getConfigurationOrLoad(MAPPER, stringSubstitutor, configClass, optional);
return getConfigurationManager().getConfigurationOrLoad(configClass, optional);
}

public RabbitMQConfiguration getRabbitMqConfiguration() {
Expand All @@ -428,7 +405,7 @@ public MessageRouterConfiguration getMessageRouterConfiguration() {
}

public GrpcConfiguration getGrpcConfiguration() {
return getConfigurationManager().getConfigurationOrLoad(MAPPER, stringSubstitutor, GrpcConfiguration.class, false);
return getConfigurationManager().getConfigurationOrLoad(GrpcConfiguration.class, false);
}

public GrpcRouterConfiguration getGrpcRouterConfiguration() {
Expand Down Expand Up @@ -518,17 +495,7 @@ public CradleManager getCradleManager() {
* @throws IllegalStateException if can not read configuration
*/
public <T> T getCustomConfiguration(Class<T> confClass, ObjectMapper customObjectMapper) {
File configFile = getPathToCustomConfiguration().toFile();
if (!configFile.exists()) {
try {
return confClass.getConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
NoSuchMethodException e) {
return null;
}
}

return getConfiguration(getPathToCustomConfiguration(), confClass, customObjectMapper);
return getConfiguration(CUSTOM_CFG_ALIAS, confClass, customObjectMapper);
}

/**
Expand Down Expand Up @@ -618,11 +585,6 @@ public EventID getRootEventId() {

protected abstract ConfigurationManager getConfigurationManager();

/**
* @return Path to custom configuration
*/
protected abstract Path getPathToCustomConfiguration();
OptimumCode marked this conversation as resolved.
Show resolved Hide resolved

/**
* @return Path to dictionaries with type dir
*/
Expand Down
Loading
Loading