io.bootique.linkmove.connector.URIConnectorFactoryFactory Maven / Gradle / Ivy
package io.bootique.linkmove.connector;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.inject.Injector;
import com.nhl.link.move.connect.StreamConnector;
import com.nhl.link.move.connect.URIConnector;
import com.nhl.link.move.runtime.connect.IConnectorFactory;
import io.bootique.annotation.BQConfig;
import io.bootique.annotation.BQConfigProperty;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@BQConfig
@JsonTypeName("uri")
public class URIConnectorFactoryFactory implements IConnectorFactoryFactory {
private Map connectors;
public URIConnectorFactoryFactory() {
this.connectors = Collections.emptyMap();
}
@BQConfigProperty
public void setConnectors(Map connectors) {
this.connectors = connectors;
}
@Override
public Class getConnectorType() {
return StreamConnector.class;
}
@Override
public IConnectorFactory getConnectorFactory(Injector injector) {
Map connectorUris = new HashMap<>((int)(connectors.size() / 0.75d) + 1);
connectors.forEach((id, uri) -> {
try {
connectorUris.put(id, new URI(uri));
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid URI for connector with ID: " + id, e);
}
});
return id -> {
if (!connectorUris.containsKey(id)) {
throw new IllegalArgumentException("Unknown connector ID: " + id);
}
return new URIConnector(connectorUris.get(id));
};
}
public Map getConnectors() {
return connectors;
}
}