Skip to content

Commit

Permalink
[xmppclient] Workaround for thing actions bug (openhab#8160)
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: Daan Meijer <[email protected]>
  • Loading branch information
lolodomo authored and DaanMeijer committed Sep 1, 2020
1 parent 7c98ea9 commit 5950c06
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,32 @@
/**
* 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.xmppclient.action;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* This is the automation engine action handler service for the publishXMPP action.
* <p>
* <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
* the test <i>actions instanceof XMPPActions</i> fails. This test can fail
* due to an issue in openHAB core v2.5.0 where the {@link IXMPPActions} class
* can be loaded by a different classloader than the <i>actions</i> instance.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface IXMPPActions {

public void publishXMPP(@Nullable String to, @Nullable String text);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
package org.openhab.binding.xmppclient.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 @@ -25,14 +28,18 @@
import org.slf4j.LoggerFactory;

/**
* This is the automation engine action handler service for the
* publishXMPP action.
* This is the automation engine action handler service for the publishXMPP action.
* <p>
* <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
* the test <i>actions instanceof XMPPActions</i> fails. This test can fail
* due to an issue in openHAB core v2.5.0 where the {@link XMPPActions} class
* can be loaded by a different classloader than the <i>actions</i> instance.
*
* @author Pavel Gololobov - Initial contribution
*/
@ThingActionsScope(name = "xmpp")
@NonNullByDefault
public class XMPPActions implements ThingActions {
public class XMPPActions implements ThingActions, IXMPPActions {
private static final Logger logger = LoggerFactory.getLogger(XMPPActions.class);
private @Nullable XMPPClientHandler handler;

Expand All @@ -46,6 +53,7 @@ public void setThingHandler(@Nullable ThingHandler handler) {
return this.handler;
}

@Override
@RuleAction(label = "publishXMPP", description = "Publish to XMPP")
public void publishXMPP(@ActionInput(name = "to", label = "To", description = "Send to") @Nullable String to,
@ActionInput(name = "text", label = "Text", description = "Message text") @Nullable String text) {
Expand All @@ -68,15 +76,25 @@ public void publishXMPP(@ActionInput(name = "to", label = "To", description = "S
}

public static void publishXMPP(@Nullable ThingActions actions, @Nullable String to, @Nullable String text) {
invokeMethodOf(actions).publishXMPP(to, text);
}

private static IXMPPActions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
logger.warn("Sending error, actions is NULL");
throw new IllegalArgumentException("actions is NULL");
throw new IllegalArgumentException("actions cannot be null");
}
if (actions instanceof XMPPActions) {
((XMPPActions) actions).publishXMPP(to, text);
} else {
logger.warn("Sending error, instance of actions is {}", actions.getClass().getName());
throw new IllegalArgumentException("actions is not an XMPPActions class");
if (actions.getClass().getName().equals(XMPPActions.class.getName())) {
if (actions instanceof IXMPPActions) {
return (IXMPPActions) actions;
} else {
return (IXMPPActions) Proxy.newProxyInstance(IXMPPActions.class.getClassLoader(),
new Class[] { IXMPPActions.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 XMPPActions");
}
}

0 comments on commit 5950c06

Please sign in to comment.