-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKinesisConnectorExecutor.java
More file actions
119 lines (101 loc) · 4.73 KB
/
Copy pathKinesisConnectorExecutor.java
File metadata and controls
119 lines (101 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.sumologic.kinesis;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sumologic.client.KinesisConnectorForSumologicConfiguration;
import com.sumologic.kinesis.KinesisConnectorExecutorBase;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
import com.amazonaws.services.kinesis.connectors.KinesisConnectorConfiguration;
/**
* This class defines the execution of a Amazon Kinesis Connector.
*
*/
public abstract class KinesisConnectorExecutor<T, U> extends KinesisConnectorExecutorBase<T, U> {
private static final Log LOG = LogFactory.getLog(KinesisConnectorExecutor.class);
// Create Stream Source constants
private static final String CREATE_STREAM_SOURCE = "createStreamSource";
private static final String LOOP_OVER_STREAM_SOURCE = "loopOverStreamSource";
private static final String INPUT_STREAM_FILE = "inputStreamFile";
private static final boolean DEFAULT_CREATE_STREAM_SOURCE = false;
private static final boolean DEFAULT_LOOP_OVER_STREAM_SOURCE = false;
// Class variables
protected final KinesisConnectorForSumologicConfiguration config;
private final Properties properties;
/**
* Create a new KinesisConnectorExecutor based on the provided configuration (*.propertes) file.
*
* @param configFile
* The name of the configuration file to look for on the classpath
*/
public KinesisConnectorExecutor(String configFile) {
// Load configuration properties
InputStream configStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(configFile);
if (configStream == null) {
String msg = "Could not find resource " + configFile + " in the classpath";
throw new IllegalStateException(msg);
}
properties = new Properties();
try {
properties.load(configStream);
configStream.close();
} catch (IOException e) {
String msg = "Could not load properties file " + configFile + " from classpath";
throw new IllegalStateException(msg, e);
}
this.config = new KinesisConnectorForSumologicConfiguration(properties, getAWSCredentialsProvider());
// Send sample data to AWS Kinesis if specified in the properties file
setupInputStream();
// Initialize executor with configurations
super.initialize((KinesisConnectorConfiguration)config);
}
/**
* Returns an {@link AWSCredentialsProvider} with the permissions necessary to accomplish all specified
* tasks. At the minimum it will require read permissions for Amazon Kinesis. Additional read permissions
* and write permissions may be required based on the Pipeline used.
*
* @return
*/
public AWSCredentialsProvider getAWSCredentialsProvider() {
return new ClasspathPropertiesFileCredentialsProvider("SumologicConnector.properties");
}
/**
* Helper method to spawn the {@link StreamSource} in a separate thread.
*/
private void setupInputStream() {
if (parseBoolean(CREATE_STREAM_SOURCE, DEFAULT_CREATE_STREAM_SOURCE, properties)) {
String inputFile = properties.getProperty(INPUT_STREAM_FILE);
StreamSource streamSource;
if (config.BATCH_RECORDS_IN_PUT_REQUEST) {
streamSource =
new BatchedStreamSource(config, inputFile, parseBoolean(LOOP_OVER_STREAM_SOURCE,
DEFAULT_LOOP_OVER_STREAM_SOURCE,
properties));
} else {
streamSource =
new StreamSource(config, inputFile, parseBoolean(LOOP_OVER_STREAM_SOURCE,
DEFAULT_LOOP_OVER_STREAM_SOURCE,
properties));
}
Thread streamSourceThread = new Thread(streamSource);
LOG.info("Starting stream source.");
streamSourceThread.start();
}
}
/**
* Helper method used to parse boolean properties.
*
* @param property
* The String key for the property
* @param defaultValue
* The default value for the boolean property
* @param properties
* The properties file to get property from
* @return property from property file, or if it is not specified, the default value
*/
private static boolean parseBoolean(String property, boolean defaultValue, Properties properties) {
return Boolean.parseBoolean(properties.getProperty(property, Boolean.toString(defaultValue)));
}
}