rule engine should not depend on specific registry implementation (#819)

The current rule registry implementation provides a configuration for
the "rule reinitialization delay". That configuration is not used by the
rule registry itself but used by the rule engine implementation.
This required the rule engine implementation to contain a special code
path that checks if the injected rule registry is an instance of that
special implementation to access that configuration parameter.
As the configuration is not used by the registry itself, it does not
make sense that it is a configuration of that component.
If the rule engine (at least this special implementation) would like to
use a configurable parameter, it should be part of that specific
component implementation.

Signed-off-by: Markus Rathgeb <maggu2810@gmail.com>
This commit is contained in:
Markus Rathgeb
2019-05-15 23:03:05 +02:00
committed by Christoph Weitkamp
parent ccd66f1491
commit 457df1e853
2 changed files with 14 additions and 50 deletions
@@ -74,6 +74,7 @@ import org.openhab.core.automation.type.TriggerType;
import org.openhab.core.automation.util.ReferenceResolver;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ComponentPropertyType;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
@@ -103,6 +104,14 @@ import org.slf4j.LoggerFactory;
@NonNullByDefault
public class RuleEngineImpl implements RuleManager, RegistryChangeListener<ModuleType> {
@ComponentPropertyType
public @interface Config {
/**
* Delay between rule's re-initialization tries.
*/
long rule_reinitialization_delay() default 500;
}
/**
* Constant defining separator between module id and output name.
*/
@@ -240,7 +249,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
* Constructor of {@link RuleEngineImpl}.
*/
@Activate
public RuleEngineImpl(final @Reference ModuleTypeRegistry moduleTypeRegistry,
public RuleEngineImpl(final Config config, final @Reference ModuleTypeRegistry moduleTypeRegistry,
final @Reference RuleRegistry ruleRegistry, final @Reference StorageService storageService) {
this.contextMap = new HashMap<String, Map<String, Object>>();
this.moduleHandlerFactories = new HashMap<String, ModuleHandlerFactory>(20);
@@ -255,11 +264,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
this.ruleRegistry = ruleRegistry;
if (ruleRegistry instanceof RuleRegistryImpl) {
scheduleReinitializationDelay = ((RuleRegistryImpl) this.ruleRegistry).getScheduleReinitializationDelay();
} else {
scheduleReinitializationDelay = RuleRegistryImpl.DEFAULT_REINITIALIZATION_DELAY;
}
this.scheduleReinitializationDelay = config.rule_reinitialization_delay();
listener = new RegistryChangeListener<Rule>() {
@Override
@@ -49,7 +49,6 @@ import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
@@ -100,28 +99,14 @@ import org.slf4j.LoggerFactory;
* @author Benedikt Niehues - added events for rules
* @author Victor Toni - return only copies of {@link Rule}s
*/
@Component(service = RuleRegistry.class, immediate = true, property = { "rule.reinitialization.delay:Long=500" })
@Component(service = RuleRegistry.class, immediate = true)
public class RuleRegistryImpl extends AbstractRegistry<Rule, String, RuleProvider>
implements RuleRegistry, RegistryChangeListener<RuleTemplate> {
/**
* Default value of delay between rule's re-initialization tries.
*/
static final long DEFAULT_REINITIALIZATION_DELAY = 500;
/**
* Delay between rule's re-initialization tries.
*/
private static final String CONFIG_PROPERTY_REINITIALIZATION_DELAY = "rule.reinitialization.delay";
private static final String SOURCE = RuleRegistryImpl.class.getSimpleName();
private final Logger logger = LoggerFactory.getLogger(RuleRegistryImpl.class.getName());
/**
* Delay between rule's re-initialization tries.
*/
private long scheduleReinitializationDelay;
private ModuleTypeRegistry moduleTypeRegistry;
private RuleTemplateRegistry templateRegistry;
@@ -143,27 +128,12 @@ public class RuleRegistryImpl extends AbstractRegistry<Rule, String, RuleProvide
*
* @param componentContext this component context.
*/
@Override
@Activate
protected void activate(BundleContext bundleContext, Map<String, Object> properties) throws Exception {
modified(properties);
protected void activate(BundleContext bundleContext) {
super.activate(bundleContext);
}
/**
* This method is responsible for updating the value of delay between rule's re-initialization tries.
*
* @param config a {@link Map} containing the new value of delay.
*/
@Modified
protected void modified(Map<String, Object> config) {
Object value = config == null ? null : config.get(CONFIG_PROPERTY_REINITIALIZATION_DELAY);
this.scheduleReinitializationDelay = (value != null && value instanceof Number) ? (((Number) value).longValue())
: DEFAULT_REINITIALIZATION_DELAY;
if (value != null && !(value instanceof Number)) {
logger.warn("Invalid configuration value: {}. It MUST be Number.", value);
}
}
@Override
@Deactivate
protected void deactivate() {
@@ -678,15 +648,4 @@ public class RuleRegistryImpl extends AbstractRegistry<Rule, String, RuleProvide
// Do nothing - resolved rules are independent from templates
}
/**
* Getter for {@link #scheduleReinitializationDelay} used by {@link RuleEngineImpl} to schedule rule's
* re-initialization
* tries.
*
* @return the {@link #scheduleReinitializationDelay}.
*/
long getScheduleReinitializationDelay() {
return scheduleReinitializationDelay;
}
}