com.chutneytesting.action.domain.DefaultActionTemplateRegistry Maven / Gradle / Ivy
The newest version!
/*
* SPDX-FileCopyrightText: 2017-2024 Enedis
*
* SPDX-License-Identifier: Apache-2.0
*
*/
package com.chutneytesting.action.domain;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DefaultActionTemplateRegistry implements ActionTemplateRegistry {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultActionTemplateRegistry.class);
private final ActionTemplateLoaders loaders;
private final Map actionTemplatesByType = new ConcurrentHashMap<>();
public DefaultActionTemplateRegistry(ActionTemplateLoaders actionTemplateLoader) {
this.loaders = actionTemplateLoader;
refresh();
}
@Override
public synchronized void refresh() {
actionTemplatesByType.clear();
loaders
.orderedTemplates()
.forEach(actionTemplate -> {
ActionTemplate alreadyStoredActionTemplate = actionTemplatesByType.putIfAbsent(actionTemplate.identifier(), actionTemplate);
if (alreadyStoredActionTemplate != null) {
LOGGER.warn("Unable to register ActionTemplate[" + actionTemplate.identifier() + " (" + actionTemplate.implementationClass().getName() + ")]: already defined by " + alreadyStoredActionTemplate.implementationClass().getName());
} else {
LOGGER.debug("Action registered: " + actionTemplate.identifier() + " (" + actionTemplate.implementationClass().getName() + ")");
}
});
}
@Override
public Optional getByIdentifier(String identifier) {
return Optional.ofNullable(actionTemplatesByType.get(identifier));
}
@Override
public Collection getAll() {
return actionTemplatesByType.values();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy