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

TH2-4267 split common #232

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# th2 common library (Java) (3.41.0)
# th2 common library (Java) (4.0.0)

## Usage

Expand Down Expand Up @@ -309,6 +309,12 @@ dependencies {

## Release notes

### 4.0.0

+ Dynamic structure of common
+ Extracted common/cassandra to separate module
+ Implemented api for dynamic loading of modules and configurations

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
+ Implemented api for dynamic loading of modules and configurations
+ Implemented the API for dynamic loading of modules and configurations

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we should describe module API and how it works in a real project


### 3.41.0

+ Work was done to eliminate vulnerabilities in _common_ and _bom_ dependencies.
Expand Down
20 changes: 13 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ ext {

repositories {
mavenCentral()
mavenLocal()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be the last repo in the list

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't agree. I think it will be first or won't be because if you build project via clean build pTML gradle publishes artifact as release.
If we want to write mavenLocal() in the build.gradle script it should be first otherwise we shouldn't be specified

maven {
name 'Sonatype_snapshots'
url 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
Expand All @@ -46,7 +47,6 @@ repositories {
name 'Sonatype_releases'
url 'https://s01.oss.sonatype.org/content/repositories/releases/'
}
mavenLocal()

configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
Expand Down Expand Up @@ -169,16 +169,22 @@ tasks.register('integrationTest', Test) {

dependencies {
api platform("com.exactpro.th2:bom:4.0.1")
api ("com.exactpro.th2:cradle-core:${cradleVersion}") {
exclude group: 'org.slf4j', module: 'slf4j-log4j12' // because of the vulnerability
}
api 'com.exactpro.th2:grpc-common:3.11.1'

implementation 'com.google.protobuf:protobuf-java-util'
implementation 'com.exactpro.th2:grpc-service-generator:3.2.2'
implementation ("com.exactpro.th2:cradle-cassandra:${cradleVersion}") {
exclude group: 'org.slf4j', module: 'slf4j-log4j12' // because of the vulnerability
}

// todo remove
// implementation 'com.exactpro.th2:full-cradle-manager:2.22.0-local'
implementation 'com.exactpro.th2:basic-cradle-manager:2.22.0-local'
implementation 'com.exactpro.th2:cradle-manager-api:2.22.0-local'
implementation "com.exactpro.th2:common-api-j:0.0.1-th2-1436-init-3574652642-d3de09f-SNAPSHOT"

annotationProcessor 'com.google.auto.service:auto-service:1.0.1'
compileOnly 'com.google.auto.service:auto-service:1.0.1'

implementation 'com.beust:jcommander:1.82'
implementation "commons-cli:commons-cli"
Xanclry marked this conversation as resolved.
Show resolved Hide resolved

//FIXME: Add these dependencies as api to grpc-... artifacts
implementation "io.grpc:grpc-protobuf"
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.41.0
release_version=4.0.0

description = 'th2 common library (Java)'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.exactpro.th2.common.module;

import com.exactpro.th2.common.ConfigurationProviderFactory;
import com.exactpro.th2.common.module.provider.FileConfigurationProviderFactory;
import com.google.auto.service.AutoService;
import kotlin.Deprecated;
import kotlin.DeprecationLevel;

@AutoService(ConfigurationProviderFactory.class)
@Deprecated(
message = "Don't use this class, it is only meant to be captured by ServiceLoader",
level = DeprecationLevel.HIDDEN)
public final class FileConfigurationProviderFactoryShim extends FileConfigurationProviderFactory {
Xanclry marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
package com.exactpro.th2.common.schema.factory;

import com.exactpro.cradle.CradleManager;
import com.exactpro.cradle.cassandra.CassandraCradleManager;
import com.exactpro.cradle.cassandra.connection.CassandraConnection;
import com.exactpro.cradle.cassandra.connection.CassandraConnectionSettings;
import com.exactpro.cradle.utils.CradleStorageException;
import com.exactpro.th2.common.event.Event;
import com.exactpro.th2.common.grpc.EventBatch;
import com.exactpro.th2.common.grpc.MessageBatch;
Expand All @@ -30,9 +26,6 @@
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.cradle.CradleConfidentialConfiguration;
import com.exactpro.th2.common.schema.cradle.CradleConfiguration;
import com.exactpro.th2.common.schema.cradle.CradleNonConfidentialConfiguration;
import com.exactpro.th2.common.schema.dictionary.DictionaryType;
import com.exactpro.th2.common.schema.event.EventBatchRouter;
import com.exactpro.th2.common.schema.exception.CommonFactoryException;
Expand Down Expand Up @@ -65,7 +58,6 @@
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.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -95,8 +87,6 @@
import java.util.jar.Manifest;
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 java.util.Collections.emptyMap;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
Expand All @@ -109,7 +99,6 @@
*/
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} */
Expand Down Expand Up @@ -384,23 +373,13 @@ public <T> MessageRouter<T> getCustomMessageRouter(Class<T> messageClass) {
return (MessageRouter<T>)router;
}

/**
* @return Configuration by specified path
* @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);
}

/**
* Load configuration, save and return. If already loaded return saved configuration.
* @param configClass configuration class
* @param optional creates an instance of a configuration class via the default constructor if this option is true and the config file doesn't exist or empty
* @return configuration object
*/
protected <T> T getConfigurationOrLoad(Class<T> configClass, boolean optional) {
return getConfigurationManager().getConfigurationOrLoad(MAPPER, stringSubstitutor, configClass, optional);
}
// /**
// * @return Configuration by specified path
// * @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 RabbitMQConfiguration getRabbitMqConfiguration() {
return getConfigurationOrLoad(RabbitMQConfiguration.class, false);
Expand All @@ -426,76 +405,22 @@ public BoxConfiguration getBoxConfiguration() {
return getConfigurationOrLoad(BoxConfiguration.class, true);
}

protected CradleConfidentialConfiguration getCradleConfidentialConfiguration() {
return getConfigurationOrLoad(CradleConfidentialConfiguration.class, false);
}

protected CradleNonConfidentialConfiguration getCradleNonConfidentialConfiguration() {
return getConfigurationOrLoad(CradleNonConfidentialConfiguration.class, true);
}

/**
* @return Schema cradle configuration
* @throws IllegalStateException if cannot read configuration
* @deprecated please use {@link #getCradleManager()}
* @return Configuration by specified path
* @throws IllegalStateException if can not read configuration
*/
@Deprecated
public CradleConfiguration getCradleConfiguration() {
return new CradleConfiguration(getCradleConfidentialConfiguration(), getCradleNonConfidentialConfiguration());
public <T> T getConfiguration(Path configPath, Class<T> configClass, ObjectMapper customObjectMapper) {
return getConfigurationManager().loadConfiguration(customObjectMapper, stringSubstitutor, configClass, configPath, false);
Nikita-Smirnov-Exactpro marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @return Cradle manager
* @throws IllegalStateException if cannot read configuration or initialization failure
* Load configuration, save and return. If already loaded return saved configuration.
* @param configClass configuration class
* @param optional creates an instance of a configuration class via the default constructor if this option is true and the config file doesn't exist or empty
* @return configuration object
*/
public CradleManager getCradleManager() {
return cradleManager.updateAndGet(manager -> {
if (manager == null) {
try {
CradleConfidentialConfiguration confidentialConfiguration = getCradleConfidentialConfiguration();
CradleNonConfidentialConfiguration nonConfidentialConfiguration = getCradleNonConfidentialConfiguration();

CassandraConnectionSettings cassandraConnectionSettings = new CassandraConnectionSettings(
confidentialConfiguration.getDataCenter(),
confidentialConfiguration.getHost(),
confidentialConfiguration.getPort(),
confidentialConfiguration.getKeyspace());

if (StringUtils.isNotEmpty(confidentialConfiguration.getUsername())) {
cassandraConnectionSettings.setUsername(confidentialConfiguration.getUsername());
}

if (StringUtils.isNotEmpty(confidentialConfiguration.getPassword())) {
cassandraConnectionSettings.setPassword(confidentialConfiguration.getPassword());
}

if (nonConfidentialConfiguration.getTimeout() > 0) {
cassandraConnectionSettings.setTimeout(nonConfidentialConfiguration.getTimeout());
}

if (nonConfidentialConfiguration.getPageSize() > 0) {
cassandraConnectionSettings.setResultPageSize(nonConfidentialConfiguration.getPageSize());
}

manager = new CassandraCradleManager(new CassandraConnection(cassandraConnectionSettings));
manager.init(
defaultIfBlank(confidentialConfiguration.getCradleInstanceName(), DEFAULT_CRADLE_INSTANCE_NAME),
nonConfidentialConfiguration.getPrepareStorage(),
nonConfidentialConfiguration.getCradleMaxMessageBatchSize() > 0
? nonConfidentialConfiguration.getCradleMaxMessageBatchSize()
: DEFAULT_MAX_MESSAGE_BATCH_SIZE,
nonConfidentialConfiguration.getCradleMaxEventBatchSize() > 0
? nonConfidentialConfiguration.getCradleMaxEventBatchSize()
: DEFAULT_MAX_EVENT_BATCH_SIZE
);
} catch (CradleStorageException | RuntimeException e) {
throw new CommonFactoryException("Cannot create Cradle manager", e);
}
}

return manager;
});

public <T> T getConfigurationOrLoad(Class<T> configClass, boolean optional) {
Nikita-Smirnov-Exactpro marked this conversation as resolved.
Show resolved Hide resolved
return getConfigurationManager().getConfigurationOrLoad(MAPPER, stringSubstitutor, configClass, optional);
}

/**
Expand Down
Loading