2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.xmppclient.action;
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.xmppclient.handler.XMPPClientHandler;
21 import org.openhab.binding.xmppclient.internal.XMPPClient;
22 import org.openhab.core.automation.annotation.ActionInput;
23 import org.openhab.core.automation.annotation.RuleAction;
24 import org.openhab.core.thing.binding.ThingActions;
25 import org.openhab.core.thing.binding.ThingActionsScope;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * This is the automation engine action handler service for the publishXMPP action.
33 * <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
34 * the test <i>actions instanceof XMPPActions</i> fails. This test can fail
35 * due to an issue in openHAB core v2.5.0 where the {@link XMPPActions} class
36 * can be loaded by a different classloader than the <i>actions</i> instance.
38 * @author Pavel Gololobov - Initial contribution
40 @ThingActionsScope(name = "xmpp")
42 public class XMPPActions implements ThingActions, IXMPPActions {
43 private static final Logger logger = LoggerFactory.getLogger(XMPPActions.class);
44 private @Nullable XMPPClientHandler handler;
47 public void setThingHandler(@Nullable ThingHandler handler) {
48 this.handler = (XMPPClientHandler) handler;
52 public @Nullable ThingHandler getThingHandler() {
57 @RuleAction(label = "publishXMPP", description = "Publish to XMPP")
58 public void publishXMPP(@ActionInput(name = "to", label = "To", description = "Send to") @Nullable String to,
59 @ActionInput(name = "text", label = "Text", description = "Message text") @Nullable String text) {
60 XMPPClientHandler clientHandler = handler;
61 if (clientHandler == null) {
62 logger.warn("XMPP ThingHandler is null");
66 XMPPClient connection = clientHandler.getXMPPClient();
67 if (connection == null) {
68 logger.warn("XMPP ThingHandler connection is null");
71 if ((to == null) || (text == null)) {
72 logger.info("Skipping XMPP messaging to {} value {}", to, text);
75 connection.sendMessage(to, text);
78 public static void publishXMPP(@Nullable ThingActions actions, @Nullable String to, @Nullable String text) {
79 invokeMethodOf(actions).publishXMPP(to, text);
82 private static IXMPPActions invokeMethodOf(@Nullable ThingActions actions) {
83 if (actions == null) {
84 throw new IllegalArgumentException("actions cannot be null");
86 if (actions.getClass().getName().equals(XMPPActions.class.getName())) {
87 if (actions instanceof IXMPPActions) {
88 return (IXMPPActions) actions;
90 return (IXMPPActions) Proxy.newProxyInstance(IXMPPActions.class.getClassLoader(),
91 new Class[] { IXMPPActions.class }, (Object proxy, Method method, Object[] args) -> {
92 Method m = actions.getClass().getDeclaredMethod(method.getName(),
93 method.getParameterTypes());
94 return m.invoke(actions, args);
98 throw new IllegalArgumentException("Actions is not an instance of XMPPActions");