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

[INLONG-1890] Inlong-Sort-Standalone add sort-standalone-common module. #1923

Merged
merged 12 commits into from
Dec 8, 2021
30 changes: 30 additions & 0 deletions inlong-sort-standalone/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<name>Apache InLong - Sort - Standalone</name>

<modules>
<module>sort-standalone-common</module>
<module>sort-standalone-source</module>
<module>sort-standalone-dist</module>
</modules>
Expand All @@ -42,6 +43,12 @@
<flume.version>1.9.0</flume.version>
<plugin.assembly.version>3.2.0</plugin.assembly.version>
<pulsar.version>2.7.2</pulsar.version>
<junit.version>4.13</junit.version>
<guava.version>19.0</guava.version>
<skipTests>false</skipTests>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compiler.source>1.8</compiler.source>
<compiler.target>1.8</compiler.target>
</properties>

<dependencies>
Expand Down Expand Up @@ -70,6 +77,29 @@
<artifactId>inlong-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
33 changes: 33 additions & 0 deletions inlong-sort-standalone/sort-standalone-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for additional
information regarding copyright ownership. The ASF licenses this file to
you 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. -->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.inlong</groupId>
<artifactId>inlong-sort-standalone</artifactId>
<version>0.12.0-incubating-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>Apache InLong - SortStandalone Common</name>
<artifactId>sort-standalone-common</artifactId>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compiler.source>1.8</compiler.source>
<compiler.target>1.8</compiler.target>
</properties>

<dependencies>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.inlong.sort.standalone.config.holder;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.flume.Context;
import org.apache.inlong.sort.standalone.config.loader.ClassResourceCommonPropertiesLoader;
import org.apache.inlong.sort.standalone.config.loader.CommonPropertiesLoader;
import org.slf4j.Logger;
import org.apache.inlong.sort.standalone.utils.InlongLoggerFactory;

/**
*
* CommonPropertiesHolder
*/
public class CommonPropertiesHolder {

public static final Logger LOG = InlongLoggerFactory.getLogger(CommonPropertiesHolder.class);
public static final String KEY_COMMON_PROPERTIES = "common-properties-loader";
public static final String DEFAULT_LOADER = ClassResourceCommonPropertiesLoader.class.getName();
public static final String KEY_CLUSTER_ID = "clusterId";

private static Map<String, String> props;
private static Context context;

/**
* init
*/
private static void init() {
synchronized (KEY_COMMON_PROPERTIES) {
if (props == null) {
props = new ConcurrentHashMap<>();
String loaderClassName = System.getenv(KEY_COMMON_PROPERTIES);
loaderClassName = (loaderClassName == null) ? DEFAULT_LOADER : loaderClassName;
try {
Class<?> loaderClass = ClassUtils.getClass(loaderClassName);
Object loaderObject = loaderClass.getDeclaredConstructor().newInstance();
if (loaderObject instanceof CommonPropertiesLoader) {
CommonPropertiesLoader loader = (CommonPropertiesLoader) loaderObject;
props.putAll(loader.load());
LOG.info("loaderClass:{},properties:{}", loaderClassName, props);
}
} catch (Throwable t) {
LOG.error("Fail to init CommonPropertiesLoader,loaderClass:{},error:{}",
loaderClassName, t.getMessage());
LOG.error(t.getMessage(), t);
}
context = new Context(props);
}
}
}

/**
* get props
*
* @return the props
*/
public static Map<String, String> get() {
if (props != null) {
return props;
}
init();
return props;
}

/**
* get context
*
* @return the context
*/
public static Context getContext() {
if (context != null) {
return context;
}
init();
return context;
}

/**
* Gets value mapped to key, returning defaultValue if unmapped.
*
* @param key to be found
* @param defaultValue returned if key is unmapped
* @return value associated with key
*/
public static String getString(String key, String defaultValue) {
return get().getOrDefault(key, defaultValue);
}

/**
* Gets value mapped to key, returning null if unmapped.
*
* @param key to be found
* @return value associated with key or null if unmapped
*/
public static String getString(String key) {
return get().get(key);
}

/**
* Gets value mapped to key, returning defaultValue if unmapped.
*
* @param key to be found
* @param defaultValue returned if key is unmapped
* @return value associated with key
*/
public static Long getLong(String key, Long defaultValue) {
return NumberUtils.toLong(get().get(key), defaultValue);
}

/**
* Gets value mapped to key, returning null if unmapped.
*
* @param key to be found
* @return value associated with key or null if unmapped
*/
public static Long getLong(String key) {
String strValue = get().get(key);
Long value = (strValue == null) ? null : NumberUtils.toLong(get().get(key));
return value;
}

/**
* getStringFromContext
*
* @param context
* @param key
* @param defaultValue
* @return
*/
public static String getStringFromContext(Context context, String key, String defaultValue) {
String value = context.getString(key);
value = (value != null) ? value : props.getOrDefault(key, defaultValue);
return value;
}

/**
* getClusterId
*
* @return
*/
public static String getClusterId() {
return getString(KEY_CLUSTER_ID);
}
}
Loading