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:
@@ -1,29 +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.pushbullet.internal.action;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* The {@link IPushbulletActions} interface defines rule actions for sending notifications
|
||||
*
|
||||
* @author Laurent Garnier - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public interface IPushbulletActions {
|
||||
|
||||
public Boolean sendPushbulletNote(@Nullable String recipient, @Nullable String title, @Nullable String message);
|
||||
|
||||
public Boolean sendPushbulletNote(@Nullable String recipient, @Nullable String message);
|
||||
}
|
||||
@@ -12,9 +12,6 @@
|
||||
*/
|
||||
package org.openhab.binding.pushbullet.internal.action;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.binding.pushbullet.internal.handler.PushbulletHandler;
|
||||
@@ -29,17 +26,12 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* The {@link PushbulletActions} class defines rule actions for sending notifications
|
||||
* <p>
|
||||
* <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
|
||||
* the test <i>actions instanceof PushbulletActions</i> fails. This test can fail
|
||||
* due to an issue in openHAB core v2.5.0 where the {@link PushbulletActions} class
|
||||
* can be loaded by a different classloader than the <i>actions</i> instance.
|
||||
*
|
||||
* @author Hakan Tandogan - Initial contribution
|
||||
*/
|
||||
@ThingActionsScope(name = "pushbullet")
|
||||
@NonNullByDefault
|
||||
public class PushbulletActions implements ThingActions, IPushbulletActions {
|
||||
public class PushbulletActions implements ThingActions {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(PushbulletActions.class);
|
||||
|
||||
@@ -52,10 +44,9 @@ public class PushbulletActions implements ThingActions, IPushbulletActions {
|
||||
|
||||
@Override
|
||||
public @Nullable ThingHandler getThingHandler() {
|
||||
return this.handler;
|
||||
return handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
@RuleAction(label = "@text/actionSendPushbulletNoteLabel", description = "@text/actionSendPushbulletNoteDesc")
|
||||
public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendPushbulletNote(
|
||||
@ActionInput(name = "recipient", label = "@text/actionSendPushbulletNoteInputRecipientLabel", description = "@text/actionSendPushbulletNoteInputRecipientDesc") @Nullable String recipient,
|
||||
@@ -63,9 +54,7 @@ public class PushbulletActions implements ThingActions, IPushbulletActions {
|
||||
@ActionInput(name = "message", label = "@text/actionSendPushbulletNoteInputMessageLabel", description = "@text/actionSendPushbulletNoteInputMessageDesc") @Nullable String message) {
|
||||
logger.trace("sendPushbulletNote '{}', '{}', '{}'", recipient, title, message);
|
||||
|
||||
// Use local variable so the SAT check can do proper flow analysis
|
||||
PushbulletHandler localHandler = handler;
|
||||
|
||||
if (localHandler == null) {
|
||||
logger.warn("Pushbullet service Handler is null!");
|
||||
return false;
|
||||
@@ -76,19 +65,20 @@ public class PushbulletActions implements ThingActions, IPushbulletActions {
|
||||
|
||||
public static boolean sendPushbulletNote(@Nullable ThingActions actions, @Nullable String recipient,
|
||||
@Nullable String title, @Nullable String message) {
|
||||
return invokeMethodOf(actions).sendPushbulletNote(recipient, title, message);
|
||||
if (actions instanceof PushbulletActions) {
|
||||
return ((PushbulletActions) actions).sendPushbulletNote(recipient, title, message);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Actions is not an instance of PushbulletActions");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@RuleAction(label = "@text/actionSendPushbulletNoteLabel", description = "@text/actionSendPushbulletNoteDesc")
|
||||
public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendPushbulletNote(
|
||||
@ActionInput(name = "recipient", label = "@text/actionSendPushbulletNoteInputRecipientLabel", description = "@text/actionSendPushbulletNoteInputRecipientDesc") @Nullable String recipient,
|
||||
@ActionInput(name = "message", label = "@text/actionSendPushbulletNoteInputMessageLabel", description = "@text/actionSendPushbulletNoteInputMessageDesc") @Nullable String message) {
|
||||
logger.trace("sendPushbulletNote '{}', '{}'", recipient, message);
|
||||
|
||||
// Use local variable so the SAT check can do proper flow analysis
|
||||
PushbulletHandler localHandler = handler;
|
||||
|
||||
if (localHandler == null) {
|
||||
logger.warn("Pushbullet service Handler is null!");
|
||||
return false;
|
||||
@@ -99,25 +89,10 @@ public class PushbulletActions implements ThingActions, IPushbulletActions {
|
||||
|
||||
public static boolean sendPushbulletNote(@Nullable ThingActions actions, @Nullable String recipient,
|
||||
@Nullable String message) {
|
||||
return invokeMethodOf(actions).sendPushbulletNote(recipient, message);
|
||||
}
|
||||
|
||||
private static IPushbulletActions invokeMethodOf(@Nullable ThingActions actions) {
|
||||
if (actions == null) {
|
||||
throw new IllegalArgumentException("actions cannot be null");
|
||||
if (actions instanceof PushbulletActions) {
|
||||
return ((PushbulletActions) actions).sendPushbulletNote(recipient, message);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Actions is not an instance of PushbulletActions");
|
||||
}
|
||||
if (actions.getClass().getName().equals(PushbulletActions.class.getName())) {
|
||||
if (actions instanceof IPushbulletActions) {
|
||||
return (IPushbulletActions) actions;
|
||||
} else {
|
||||
return (IPushbulletActions) Proxy.newProxyInstance(IPushbulletActions.class.getClassLoader(),
|
||||
new Class[] { IPushbulletActions.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 PushbulletActions");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user