Skip to content

Commit

Permalink
[pushbullet] Workaround for thing actions bug (openhab#8143)
Browse files Browse the repository at this point in the history
Related to openhab#8116

Signed-off-by: Laurent Garnier <[email protected]>
Signed-off-by: MPH80 <[email protected]>
  • Loading branch information
lolodomo authored and MPH80 committed Aug 3, 2020
1 parent 6c90b55 commit 7edd28e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
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.eclipse.smarthome.core.thing.binding.ThingActions;
Expand All @@ -26,12 +29,17 @@

/**
* 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 {
public class PushbulletActions implements ThingActions, IPushbulletActions {

private final Logger logger = LoggerFactory.getLogger(PushbulletActions.class);

Expand All @@ -47,6 +55,7 @@ public void setThingHandler(@Nullable ThingHandler handler) {
return this.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,
Expand All @@ -67,13 +76,10 @@ public void setThingHandler(@Nullable ThingHandler handler) {

public static boolean sendPushbulletNote(@Nullable ThingActions actions, @Nullable String recipient,
@Nullable String title, @Nullable String message) {
if (actions instanceof PushbulletActions) {
return ((PushbulletActions) actions).sendPushbulletNote(recipient, title, message);
} else {
throw new IllegalArgumentException("Instance is not a PushbulletActions class ( " + actions + " )");
}
return invokeMethodOf(actions).sendPushbulletNote(recipient, title, message);
}

@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,
Expand All @@ -93,10 +99,25 @@ public static boolean sendPushbulletNote(@Nullable ThingActions actions, @Nullab

public static boolean sendPushbulletNote(@Nullable ThingActions actions, @Nullable String recipient,
@Nullable String message) {
if (actions instanceof PushbulletActions) {
return ((PushbulletActions) actions).sendPushbulletNote(recipient, message);
} else {
throw new IllegalArgumentException("Instance is not a PushbulletActions class ( " + actions + " )");
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.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");
}
}

0 comments on commit 7edd28e

Please sign in to comment.