diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/defaultscope/ScriptThingActions.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/defaultscope/ScriptThingActions.java index c9fa0d303..28d4b8be8 100644 --- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/defaultscope/ScriptThingActions.java +++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/defaultscope/ScriptThingActions.java @@ -21,9 +21,6 @@ import org.openhab.core.thing.ThingUID; import org.openhab.core.thing.binding.ThingActions; import org.openhab.core.thing.binding.ThingActionsScope; import org.openhab.core.thing.binding.ThingHandler; -import org.osgi.service.component.annotations.Reference; -import org.osgi.service.component.annotations.ReferenceCardinality; -import org.osgi.service.component.annotations.ReferencePolicy; /** * The methods of this class are made available as functions in the scripts. @@ -35,13 +32,12 @@ import org.osgi.service.component.annotations.ReferencePolicy; public class ScriptThingActions { private static final Map THING_ACTIONS_MAP = new HashMap<>(); + private ThingRegistry thingRegistry; ScriptThingActions(ThingRegistry thingRegistry) { this.thingRegistry = thingRegistry; } - private ThingRegistry thingRegistry; - public void dispose() { this.thingRegistry = null; } @@ -67,7 +63,6 @@ public class ScriptThingActions { return null; } - @Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MULTIPLE) void addThingActions(ThingActions thingActions) { String key = getKey(thingActions); THING_ACTIONS_MAP.put(key, thingActions); diff --git a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/ScriptServiceUtil.java b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/ScriptServiceUtil.java index 7f1a998a0..164a6b4b8 100644 --- a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/ScriptServiceUtil.java +++ b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/ScriptServiceUtil.java @@ -18,11 +18,11 @@ import java.util.concurrent.atomic.AtomicReference; import org.openhab.core.events.EventPublisher; import org.openhab.core.items.ItemRegistry; -import org.openhab.core.thing.ThingRegistry; -import org.openhab.core.thing.binding.ThingActions; import org.openhab.core.model.core.ModelRepository; import org.openhab.core.model.script.engine.ScriptEngine; import org.openhab.core.model.script.engine.action.ActionService; +import org.openhab.core.thing.ThingRegistry; +import org.openhab.core.thing.binding.ThingActions; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; diff --git a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/internal/engine/action/ThingActionService.java b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/internal/engine/action/ThingActionService.java index f4f62f2e7..1fba6016c 100644 --- a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/internal/engine/action/ThingActionService.java +++ b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/internal/engine/action/ThingActionService.java @@ -15,6 +15,8 @@ package org.openhab.core.model.script.internal.engine.action; import java.util.HashMap; import java.util.Map; +import org.openhab.core.model.script.actions.Things; +import org.openhab.core.model.script.engine.action.ActionService; import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingRegistry; import org.openhab.core.thing.ThingStatusInfo; @@ -22,8 +24,7 @@ import org.openhab.core.thing.ThingUID; import org.openhab.core.thing.binding.ThingActions; import org.openhab.core.thing.binding.ThingActionsScope; import org.openhab.core.thing.binding.ThingHandler; -import org.openhab.core.model.script.actions.Things; -import org.openhab.core.model.script.engine.action.ActionService; +import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.ReferenceCardinality; @@ -38,23 +39,19 @@ import org.osgi.service.component.annotations.ReferencePolicy; @Component(immediate = true) public class ThingActionService implements ActionService { + private static final Map THING_ACTIONS_MAP = new HashMap<>(); private static ThingRegistry thingRegistry; - private static final Map thingActionsMap = new HashMap<>(); + + @Activate + public ThingActionService(final @Reference ThingRegistry thingRegistry) { + ThingActionService.thingRegistry = thingRegistry; + } @Override public Class getActionClass() { return Things.class; } - @Reference - public void setThingRegistry(ThingRegistry thingRegistry) { - ThingActionService.thingRegistry = thingRegistry; - } - - public void unsetThingRegistry(ThingRegistry thingRegistry) { - ThingActionService.thingRegistry = null; - } - public static ThingStatusInfo getThingStatusInfo(String thingUid) { ThingUID uid = new ThingUID(thingUid); Thing thing = thingRegistry.get(uid); @@ -66,13 +63,21 @@ public class ThingActionService implements ActionService { } } + /** + * Gets an actions instance of a certain scope for a given thing UID + * + * @param scope the action scope + * @param thingUid the UID of the thing + * + * @return actions the actions instance or null, if not available + */ public static ThingActions getActions(String scope, String thingUid) { ThingUID uid = new ThingUID(thingUid); Thing thing = thingRegistry.get(uid); if (thing != null) { ThingHandler handler = thing.getHandler(); if (handler != null) { - ThingActions thingActions = thingActionsMap.get(getKey(scope, thingUid)); + ThingActions thingActions = THING_ACTIONS_MAP.get(getKey(scope, thingUid)); return thingActions; } } @@ -82,12 +87,12 @@ public class ThingActionService implements ActionService { @Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MULTIPLE) public void addThingActions(ThingActions thingActions) { String key = getKey(thingActions); - thingActionsMap.put(key, thingActions); + THING_ACTIONS_MAP.put(key, thingActions); } public void removeThingActions(ThingActions thingActions) { String key = getKey(thingActions); - thingActionsMap.remove(key); + THING_ACTIONS_MAP.remove(key); } private static String getKey(ThingActions thingActions) { @@ -108,4 +113,5 @@ public class ThingActionService implements ActionService { ThingActionsScope scopeAnnotation = actions.getClass().getAnnotation(ThingActionsScope.class); return scopeAnnotation.name(); } + } diff --git a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/scoping/ActionClassLoader.java b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/scoping/ActionClassLoader.java index 41d277a67..b2c39f860 100644 --- a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/scoping/ActionClassLoader.java +++ b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/scoping/ActionClassLoader.java @@ -12,9 +12,9 @@ */ package org.openhab.core.model.script.scoping; -import org.openhab.core.thing.binding.ThingActions; import org.openhab.core.model.script.ScriptServiceUtil; import org.openhab.core.model.script.engine.action.ActionService; +import org.openhab.core.thing.binding.ThingActions; /** * This is a special class loader that tries to resolve classes from available {@link ActionService}s, diff --git a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/scoping/ScriptImplicitlyImportedTypes.java b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/scoping/ScriptImplicitlyImportedTypes.java index fc28c38b4..c77150fb5 100644 --- a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/scoping/ScriptImplicitlyImportedTypes.java +++ b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/scoping/ScriptImplicitlyImportedTypes.java @@ -19,12 +19,13 @@ import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.StringUtils; +import org.eclipse.xtext.xbase.scoping.batch.ImplicitlyImportedFeatures; +import org.joda.time.DateTime; import org.openhab.core.library.unit.BinaryPrefix; import org.openhab.core.library.unit.ImperialUnits; import org.openhab.core.library.unit.MetricPrefix; import org.openhab.core.library.unit.SIUnits; import org.openhab.core.library.unit.SmartHomeUnits; -import org.openhab.core.thing.binding.ThingActions; import org.openhab.core.model.persistence.extensions.PersistenceExtensions; import org.openhab.core.model.script.actions.Audio; import org.openhab.core.model.script.actions.BusEvent; @@ -39,8 +40,7 @@ import org.openhab.core.model.script.engine.IActionServiceProvider; import org.openhab.core.model.script.engine.IThingActionsProvider; import org.openhab.core.model.script.engine.action.ActionService; import org.openhab.core.model.script.lib.NumberExtensions; -import org.eclipse.xtext.xbase.scoping.batch.ImplicitlyImportedFeatures; -import org.joda.time.DateTime; +import org.openhab.core.thing.binding.ThingActions; import com.google.inject.Inject; import com.google.inject.Singleton; @@ -53,7 +53,6 @@ import com.google.inject.Singleton; * @author Kai Kreuzer - Initial contribution * @author Oliver Libutzki - Xtext 2.5.0 migration */ -@SuppressWarnings("restriction") @Singleton public class ScriptImplicitlyImportedTypes extends ImplicitlyImportedFeatures { diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/ThingActions.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/ThingActions.java index 670b53aa3..5dc7d2c5e 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/ThingActions.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/ThingActions.java @@ -12,11 +12,14 @@ */ package org.openhab.core.thing.binding; +import org.eclipse.jdt.annotation.NonNullByDefault; + /** - * Marker interface for Automation Actions with access to a ThingHandler + * Marker interface for Automation Actions with access to a {@link ThingHandler}. * * @author Stefan Triller - Initial contribution */ +@NonNullByDefault public interface ThingActions extends ThingHandlerService { } diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/ThingHandlerService.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/ThingHandlerService.java index e2de19486..e1c1f79a2 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/ThingHandlerService.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/ThingHandlerService.java @@ -12,12 +12,16 @@ */ package org.openhab.core.thing.binding; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; + /** * - * Interface for a service that provides access to a {@link ThingHandler} + * Interface for a service that provides access to a {@link ThingHandler}. * * @author Stefan Triller - Initial contribution */ +@NonNullByDefault public interface ThingHandlerService { /** @@ -32,6 +36,7 @@ public interface ThingHandlerService { * * @return the {@link ThingHandler} */ + @Nullable ThingHandler getThingHandler(); /**