io.tracee.contextlogger.ConnectorFactory Maven / Gradle / Ivy
package io.tracee.contextlogger;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.tracee.contextlogger.connector.Connector;
import io.tracee.contextlogger.connector.ConnectorOutputProvider;
import io.tracee.contextlogger.connector.LogConnector;
import io.tracee.contextlogger.connector.WellKnownConnectorClassNames;
/**
* Class to pipe messages to all configured connectors.
* Created by Tobias Gindler, holisticon AG on 26.03.14.
*/
class ConnectorFactory {
// Connector settings
private static final Pattern KEY_MATCHER_PATTERN = Pattern.compile(TraceeContextLoggerConstants.SYSTEM_PROPERTY_CONTEXT_LOGGER_CONNECTOR_KEY_PATTERN);
private static final String CONNECTOR_PROPERTY_GRABBER_PATTERN = TraceeContextLoggerConstants.SYSTEM_PROPERTY_CONNECTOR_PREFIX.replaceAll("\\.", "\\.")
+ "%s\\.(.*)";
private static final Logger LOGGER = LoggerFactory.getLogger(TraceeContextLogger.class);
private static final Map WELL_KNOW_CONNECTOR_MAPPINGS = new HashMap();
static {
WELL_KNOW_CONNECTOR_MAPPINGS.put("HttpConnector", WellKnownConnectorClassNames.HTTP_CONNECTOR);
WELL_KNOW_CONNECTOR_MAPPINGS.put(LogConnector.class.getName(), LogConnector.class.getCanonicalName());
}
// Connector
private final Map connectorMap = new HashMap();
ConnectorFactory() {
initConnectors();
}
/**
* Initializes all available connectors.
*/
private void initConnectors() {
// first get all connector configuration Names
Set connectorConfigurationNames = this.getConnectorConfigurationNames();
for (String connectorConfigurationName : connectorConfigurationNames) {
Connector connector = this.createConnector(connectorConfigurationName);
if (connector != null) {
this.connectorMap.put(connectorConfigurationName, connector);
}
}
// Add mandatory logger
if (!isConnectorConfigured(LogConnector.class)) {
Connector logConnector = new LogConnector();
this.connectorMap.put("LOGGER", logConnector);
}
}
/**
* Send error report to all initialized connector instances.
*
* @param connectorOutputProvider the context data provider used for output
*/
final void sendErrorReportToConnectors(ConnectorOutputProvider connectorOutputProvider) {
for (Connector connector : this.connectorMap.values()) {
connector.sendErrorReport(connectorOutputProvider);
}
}
/**
* Extracts all names for connector configurations from System properties.
*
* @return a Set containing all connector configuration names
*/
final Set getConnectorConfigurationNames() {
Set connectorNames = new HashSet();
Enumeration