nstream.adapter.common.provision.ProvisionLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nstream-adapter-common Show documentation
Show all versions of nstream-adapter-common Show documentation
General server-side data flow templates with Swim
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://redis.com/legal/rsalv2-agreement/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nstream.adapter.common.provision;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Properties;
import nstream.adapter.common.AdapterUtils;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
import swim.util.Log;
public final class ProvisionLoader {
private static final Map> REGISTRY = new HashMap<>();
private ProvisionLoader() {
}
@SuppressWarnings("unchecked")
public static AbstractProvision getProvision(String name) {
final AbstractProvision> result = REGISTRY.get(name);
if (result == null) {
throw new NoSuchElementException("No ProvisionLoader entry under " + name);
}
return (AbstractProvision) result;
}
public static int loadProvisions(Log log, Value config) {
int errors = 0;
for (Item provisionVal: config.get("provisions")) {
try {
loadFromItem(log, provisionVal);
} catch (RuntimeException e) {
errors++;
e.printStackTrace();
}
}
return errors;
}
@SuppressWarnings("unchecked")
static void loadFromItem(Log log, Item conf) {
if (!(conf instanceof Record)) {
log.warn("Ignored invalid Provision configuration " + conf);
return;
}
final String name = conf.getAttr("provision").stringValue(null);
if (name == null || name.isEmpty()) {
log.warn("Ignored nameless Provision configuration " + conf);
return;
}
final Class> provisionClass;
try {
provisionClass = (Class>) Class.forName(conf.get("class").stringValue());
} catch (Exception e) {
log.warn("Ignored Provision configuration " + conf + " with invalid class");
return;
}
final Properties props = new Properties();
AdapterUtils.loadPropsFromUse(log, "provision " + name, props, conf.get("use"));
AdapterUtils.loadPropsFromDef(log, "provision " + name, props, conf.get("def"));
final AbstractProvision> collision = REGISTRY.get(name);
loadFromProperties(log, name, provisionClass, props);
if (collision != null) {
log.warn("Existing " + collision.getClass().getName() + " under " + name
+ " will be unloaded to accommodate incoming same-named provision");
collision.unload(log);
}
}
private static void loadFromProperties(Log log, String name,
Class> provisionClass, Properties p) {
try {
loadProvision(log, name, provisionClass, p);
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Provision " + name + " incurred issue during reflection", e);
}
}
private static void loadProvision(Log log, String name,
Class> clazz, Properties properties)
throws ReflectiveOperationException {
final AbstractProvision result = clazz.getDeclaredConstructor().newInstance();
result.load(log, properties);
REGISTRY.put(name, result);
}
public static void debugProvisionNames(Log log) {
log.info("Loaded provisions named: " + REGISTRY.keySet());
}
public static void logProvisionNames(Log log) {
log.info("Loaded provisions named: " + REGISTRY.keySet());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy