Rework ThingActions (#8732)

* Remove proxy workarounds
* Move ThingActions and a few other classes into the internal package
* Use more consistent action labels/descriptions

Related to:

* openhab/openhab-core#1714
* openhab/openhab-core#1639

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born
2020-10-14 19:01:12 +02:00
committed by GitHub
parent 4adc214c69
commit bef1046258
86 changed files with 708 additions and 2096 deletions

View File

@@ -1,27 +0,0 @@
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.mqtt.action;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
/**
* The {@link IMQTTActions} defines the interface for all thing actions supported by the binding.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface IMQTTActions {
public void publishMQTT(@Nullable String topic, @Nullable String value, @Nullable Boolean retain);
}

View File

@@ -21,9 +21,9 @@ import java.util.concurrent.TimeoutException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.mqtt.action.MQTTActions;
import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryParticipant;
import org.openhab.binding.mqtt.discovery.TopicSubscribe;
import org.openhab.binding.mqtt.internal.action.MQTTActions;
import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
import org.openhab.core.io.transport.mqtt.MqttConnectionObserver;
import org.openhab.core.io.transport.mqtt.MqttConnectionState;

View File

@@ -10,10 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.mqtt.action;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
package org.openhab.binding.mqtt.internal.action;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@@ -29,17 +26,12 @@ import org.slf4j.LoggerFactory;
/**
* This is the automation engine action handler service for the publishMQTT action.
* <p>
* <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
* the test <i>actions instanceof MQTTActions</i> fails. This test can fail
* due to an issue in openHAB core v2.5.0 where the {@link MQTTActions} class
* can be loaded by a different classloader than the <i>actions</i> instance.
*
* @author David Graeff - Initial contribution
*/
@ThingActionsScope(name = "mqtt")
@NonNullByDefault
public class MQTTActions implements ThingActions, IMQTTActions {
public class MQTTActions implements ThingActions {
private final Logger logger = LoggerFactory.getLogger(MQTTActions.class);
private @Nullable AbstractBrokerHandler handler;
@@ -50,7 +42,7 @@ public class MQTTActions implements ThingActions, IMQTTActions {
@Override
public @Nullable ThingHandler getThingHandler() {
return this.handler;
return handler;
}
@RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
@@ -60,7 +52,6 @@ public class MQTTActions implements ThingActions, IMQTTActions {
publishMQTT(topic, value, null);
}
@Override
@RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
public void publishMQTT(
@ActionInput(name = "topic", label = "@text/actionInputTopicLabel", description = "@text/actionInputTopicDesc") @Nullable final String topic,
@@ -100,25 +91,10 @@ public class MQTTActions implements ThingActions, IMQTTActions {
public static void publishMQTT(@Nullable ThingActions actions, @Nullable String topic, @Nullable String value,
@Nullable Boolean retain) {
invokeMethodOf(actions).publishMQTT(topic, value, retain);
}
private static IMQTTActions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
throw new IllegalArgumentException("actions cannot be null");
if (actions instanceof MQTTActions) {
((MQTTActions) actions).publishMQTT(topic, value, retain);
} else {
throw new IllegalArgumentException("Actions is not an instance of MQTTActions");
}
if (actions.getClass().getName().equals(MQTTActions.class.getName())) {
if (actions instanceof IMQTTActions) {
return (IMQTTActions) actions;
} else {
return (IMQTTActions) Proxy.newProxyInstance(IMQTTActions.class.getClassLoader(),
new Class[] { IMQTTActions.class }, (Object proxy, Method method, Object[] args) -> {
Method m = actions.getClass().getDeclaredMethod(method.getName(),
method.getParameterTypes());
return m.invoke(actions, args);
});
}
}
throw new IllegalArgumentException("Actions is not an instance of MQTTActions");
}
}