Skip to content

Commit

Permalink
Revert JDK 1.8 dependency. Allow Java 9+ to run.
Browse files Browse the repository at this point in the history
  • Loading branch information
chaochenq committed Jan 28, 2020
1 parent 75c3dd7 commit 6d0e32b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
5 changes: 5 additions & 0 deletions bin/start-aws-kinesis-agent
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ function check_java_version() {
local javacmd=$1
local verbose=$2
local javaversion=$($javacmd -version 2>&1 | awk -F '"' '/version/ {print $2}')

if [[ "$javaversion" == "10" || "$javaversion" > "10" ]]; then
return 0
fi

if [[ "$javaversion" < "$MIN_JAVA_VERSION" ]]; then
if [[ -n "$verbose" ]]; then
_error_msg "JVM version >= $MIN_JAVA_VERSION is required. Found following JVM:" \
Expand Down
4 changes: 2 additions & 2 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@

<target name="compile" depends="check-java-version, init">
<mkdir dir="${build.private}" />
<property name="build.compiler" value="javac1.7" />
<javac source="1.8" target="1.8" includeantruntime="false" classpathref="classpath" srcdir="${src.dir}" destdir="${build.private}" debug="true" />
<property name="build.compiler" value="javac1.7" />
<javac source="1.7" target="1.7" includeantruntime="false" classpathref="classpath" srcdir="${src.dir}" destdir="${build.private}" debug="true" />
<property name="src.agent.path" value="com/amazon/kinesis/streaming/agent" />
<copy todir="${build.private}/${src.agent.path}" failonerror="true">
<fileset dir="${src.dir}/${src.agent.path}" excludes="**/*.java" />
Expand Down
2 changes: 2 additions & 0 deletions configuration/example/agent.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"kinesis.endpoint": "https://kinesis.us-west-2.amazonaws.com",
"awsAccessKeyId": "ACCESSKEY",
"awsSecretAccessKey": "SECRETKEY",
"userDefinedCredentialsProvider.className":"com.amazonaws.samples.NewCustomCredentialsProvider",
"userDefinedCredentialsProvider.location":"/home/ec2-user/NewCustomCredentialsProvider.jar",
"flows": [
{
"filePattern": "/tmp/aws-kinesis-agent-test1.log*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class UserDefinedCredentialsProvider implements AWSCredentialsProvider{
private static final String CREDENTIALS_PROVIDER_LOCATION = "userDefinedCredentialsProvider.location";
Expand All @@ -28,7 +27,7 @@ public class UserDefinedCredentialsProvider implements AWSCredentialsProvider{


private final AgentConfiguration configuration;
private Optional<AWSCredentialsProvider> awsCredentialsProvider = Optional.empty();
private AWSCredentialsProvider awsCredentialsProvider = null;

public UserDefinedCredentialsProvider(AgentConfiguration configuration) {
this.configuration = configuration;
Expand All @@ -42,8 +41,8 @@ public UserDefinedCredentialsProvider(AgentConfiguration configuration) {
@Override
public AWSCredentials getCredentials() {
try {
if (awsCredentialsProvider.isPresent()) {
return awsCredentialsProvider.get().getCredentials();
if (awsCredentialsProvider != null) {
return awsCredentialsProvider.getCredentials();
}
} catch (Exception e) {
LOGGER.error("There is a problem with the User defined credentials provider. " + e);
Expand All @@ -54,8 +53,8 @@ public AWSCredentials getCredentials() {
@Override
public void refresh() {
try {
if (awsCredentialsProvider.isPresent()) {
awsCredentialsProvider.get().refresh();
if (awsCredentialsProvider != null) {
awsCredentialsProvider.refresh();
return;
}
} catch (Exception e) {
Expand All @@ -69,9 +68,9 @@ public String toString() {
}

private void instantiateCredentialsProvider () {
Optional<Class<AWSCredentialsProvider>> awsCredentialsProviderClass = loadUserDefinedCredentialsProvider();
if (awsCredentialsProviderClass.isPresent()) {
Class<AWSCredentialsProvider> credentialsProviderClass = awsCredentialsProviderClass.get();
Class<AWSCredentialsProvider> awsCredentialsProviderClass = loadUserDefinedCredentialsProvider();
if (awsCredentialsProviderClass != null) {
Class<AWSCredentialsProvider> credentialsProviderClass = awsCredentialsProviderClass;

if (!AWSCredentialsProvider.class.isAssignableFrom(credentialsProviderClass)) {
LOGGER.error("The loaded credential provider " + credentialsProviderClass.getName() +
Expand All @@ -88,7 +87,7 @@ private void instantiateCredentialsProvider () {
}

try {
awsCredentialsProvider = Optional.of(constructor.newInstance());
awsCredentialsProvider = constructor.newInstance();
LOGGER.info("Instantiated the user defined credentials provider.");
} catch (Exception e) {
LOGGER.error("Exception while instantiating user defined " +
Expand All @@ -97,34 +96,34 @@ private void instantiateCredentialsProvider () {
}
}

private Optional<Class<AWSCredentialsProvider>> loadUserDefinedCredentialsProvider() {
Optional<Pair<String, Optional<String>>> customCredentialsProvider = getUserDefinedCredentialsProviderFromConfig();
if (customCredentialsProvider.isPresent()) {
private Class<AWSCredentialsProvider> loadUserDefinedCredentialsProvider() {
Pair<String, String> customCredentialsProvider = getUserDefinedCredentialsProviderFromConfig();
if (customCredentialsProvider != null) {
try {
List<URL> classPathList = new ArrayList<>();

File libDirPath = new File(AGENT_LIB_DIRECTORY);
classPathList.add(libDirPath.toURI().toURL());

if (customCredentialsProvider.get().getRight().isPresent()) {
if (customCredentialsProvider.getRight() != null && customCredentialsProvider.getRight().length() > 0) {
addCustomCredentialsJarToClassPath(customCredentialsProvider, classPathList);
}
URL[] urlArray = new URL[classPathList.size()];
URLClassLoader urlClassLoader = new URLClassLoader(classPathList.toArray(urlArray),
ClassLoader.getSystemClassLoader());
Class classToLoad = Class.forName(customCredentialsProvider.get().getLeft(), true,
Class classToLoad = Class.forName(customCredentialsProvider.getLeft(), true,
urlClassLoader);
return Optional.of((Class<AWSCredentialsProvider>) classToLoad);
return (Class<AWSCredentialsProvider>) classToLoad;
} catch (Exception e) {
LOGGER.error("Error loading user defined credentials provider. " + e);
}
}
return Optional.empty();
return null;
}

private void addCustomCredentialsJarToClassPath(Optional<Pair<String, Optional<String>>> customCredentialsProvider,
private void addCustomCredentialsJarToClassPath(Pair<String, String> customCredentialsProvider,
List<URL> classPathList) throws IOException {
File customCredentialProviderJar = new File(customCredentialsProvider.get().getRight().get());
File customCredentialProviderJar = new File(customCredentialsProvider.getRight());
classPathList.add(customCredentialProviderJar.toURI().toURL());
checkIfOwnerIsKinesisAgentUser(customCredentialProviderJar);
}
Expand All @@ -143,18 +142,18 @@ private void checkIfOwnerIsKinesisAgentUser(File fileOrDirectory) {
}
}

private Optional<Pair<String, Optional<String>>> getUserDefinedCredentialsProviderFromConfig() {
private Pair<String, String> getUserDefinedCredentialsProviderFromConfig() {
String credentialsProviderClass = "";
Optional<String> credentialsProviderLocation = Optional.empty();
String credentialsProviderLocation = "";
try {
credentialsProviderClass = configuration.readString(CREDENTIALS_PROVIDER_CLASS);
credentialsProviderLocation = Optional.of(configuration.readString(CREDENTIALS_PROVIDER_LOCATION));
credentialsProviderLocation = configuration.readString(CREDENTIALS_PROVIDER_LOCATION);
} catch (ConfigurationException e) {
if (credentialsProviderClass.length() == 0) {
LOGGER.info("No custom implementation of credentials provider present in the config file");
return Optional.empty();
return null;
}
}
return Optional.of(Pair.of(credentialsProviderClass, credentialsProviderLocation));
return Pair.of(credentialsProviderClass, credentialsProviderLocation);
}
}

0 comments on commit 6d0e32b

Please sign in to comment.