]> git.basschouten.com Git - openhab-addons.git/blob
1d617fd7db6683645eaecf49ed29abc1fb87ad97
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.xmppclient.action;
14
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
17
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;
29
30 /**
31  * This is the automation engine action handler service for the publishXMPP action.
32  * <p>
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.
37  *
38  * @author Pavel Gololobov - Initial contribution
39  */
40 @ThingActionsScope(name = "xmpp")
41 @NonNullByDefault
42 public class XMPPActions implements ThingActions, IXMPPActions {
43     private static final Logger logger = LoggerFactory.getLogger(XMPPActions.class);
44     private @Nullable XMPPClientHandler handler;
45
46     @Override
47     public void setThingHandler(@Nullable ThingHandler handler) {
48         this.handler = (XMPPClientHandler) handler;
49     }
50
51     @Override
52     public @Nullable ThingHandler getThingHandler() {
53         return this.handler;
54     }
55
56     @Override
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");
63             return;
64         }
65
66         XMPPClient connection = clientHandler.getXMPPClient();
67         if (connection == null) {
68             logger.warn("XMPP ThingHandler connection is null");
69             return;
70         }
71         if ((to == null) || (text == null)) {
72             logger.info("Skipping XMPP messaging to {} value {}", to, text);
73             return;
74         }
75         connection.sendMessage(to, text);
76     }
77
78     public static void publishXMPP(@Nullable ThingActions actions, @Nullable String to, @Nullable String text) {
79         invokeMethodOf(actions).publishXMPP(to, text);
80     }
81
82     private static IXMPPActions invokeMethodOf(@Nullable ThingActions actions) {
83         if (actions == null) {
84             throw new IllegalArgumentException("actions cannot be null");
85         }
86         if (actions.getClass().getName().equals(XMPPActions.class.getName())) {
87             if (actions instanceof IXMPPActions) {
88                 return (IXMPPActions) actions;
89             } else {
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);
95                         });
96             }
97         }
98         throw new IllegalArgumentException("Actions is not an instance of XMPPActions");
99     }
100 }