final int batchSize, String dataDir) {
final Properties props = new Properties();
if ((agents == null || agents.length == 0) && (properties == null || properties.length == 0)) {
LOGGER.error("No Flume configuration provided");
throw new ConfigurationException("No Flume configuration provided");
}
if ((agents != null && agents.length > 0 && properties != null && properties.length > 0)) {
LOGGER.error("Agents and Flume configuration cannot both be specified");
throw new ConfigurationException("Agents and Flume configuration cannot both be specified");
}
if (agents != null && agents.length > 0) {
props.put(name + ".sources", FlumeEmbeddedManager.SOURCE_NAME);
props.put(name + ".sources." + FlumeEmbeddedManager.SOURCE_NAME + ".type", SOURCE_TYPE);
if (dataDir != null && dataDir.length() > 0) {
if (dataDir.equals(IN_MEMORY)) {
props.put(name + ".channels", "primary");
props.put(name + ".channels.primary.type", "memory");
} else {
props.put(name + ".channels", "primary");
props.put(name + ".channels.primary.type", "file");
if (!dataDir.endsWith(FiLE_SEP)) {
dataDir = dataDir + FiLE_SEP;
}
props.put(name + ".channels.primary.checkpointDir", dataDir + "checkpoint");
props.put(name + ".channels.primary.dataDirs", dataDir + "data");
}
} else {
props.put(name + ".channels", "primary");
props.put(name + ".channels.primary.type", "file");
}
final StringBuilder sb = new StringBuilder();
String leading = "";
int priority = agents.length;
for (int i = 0; i < agents.length; ++i) {
sb.append(leading).append("agent").append(i);
leading = " ";
final String prefix = name + ".sinks.agent" + i;
props.put(prefix + ".channel", "primary");
props.put(prefix + ".type", "avro");
props.put(prefix + ".hostname", agents[i].getHost());
props.put(prefix + ".port", Integer.toString(agents[i].getPort()));
props.put(prefix + ".batch-size", Integer.toString(batchSize));
props.put(name + ".sinkgroups.group1.processor.priority.agent" + i, Integer.toString(priority));
--priority;
}
props.put(name + ".sinks", sb.toString());
props.put(name + ".sinkgroups", "group1");
props.put(name + ".sinkgroups.group1.sinks", sb.toString());
props.put(name + ".sinkgroups.group1.processor.type", "failover");
final String sourceChannels = "primary";
props.put(name + ".channels", sourceChannels);
props.put(name + ".sources." + FlumeEmbeddedManager.SOURCE_NAME + ".channels", sourceChannels);
} else {
String channels = null;
String[] sinks = null;
props.put(name + ".sources", FlumeEmbeddedManager.SOURCE_NAME);
props.put(name + ".sources." + FlumeEmbeddedManager.SOURCE_NAME + ".type", SOURCE_TYPE);
for (final Property property : properties) {
final String key = property.getName();
if (key == null || key.length() == 0) {
final String msg = "A property name must be provided";
LOGGER.error(msg);
throw new ConfigurationException(msg);
}
final String upperKey = key.toUpperCase(Locale.ENGLISH);
if (upperKey.startsWith(name.toUpperCase(Locale.ENGLISH))) {
final String msg =
"Specification of the agent name is not allowed in Flume Appender configuration: " + key;
LOGGER.error(msg);
throw new ConfigurationException(msg);
}
// Prohibit setting the sources as they are set above but allow interceptors to be set
if (upperKey.startsWith("SOURCES.") && !upperKey.startsWith("SOURCES.LOG4J-SOURCE.INTERCEPTORS")) {
final String msg = "Specification of Sources is not allowed in Flume Appender: " + key;
LOGGER.error(msg);
throw new ConfigurationException(msg);
}
final String value = property.getValue();
if (value == null || value.length() == 0) {
final String msg = "A value for property " + key + " must be provided";
LOGGER.error(msg);
throw new ConfigurationException(msg);
}
if (upperKey.equals("CHANNELS")) {
channels = value.trim();
} else if (upperKey.equals("SINKS")) {
sinks = value.trim().split(" ");
}
props.put(name + '.' + key, value);
}
String sourceChannels = channels;
if (channels == null) {
sourceChannels = "primary";
props.put(name + ".channels", sourceChannels);
}
props.put(name + ".sources." + FlumeEmbeddedManager.SOURCE_NAME + ".channels", sourceChannels);
if (sinks == null || sinks.length == 0) {
final String msg = "At least one Sink must be specified";
LOGGER.error(msg);
throw new ConfigurationException(msg);
}
}
return props;
}