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:
Wouter Born
2020-10-14 19:01:12 +02:00
committed by GitHub
parent 4adc214c69
commit bef1046258
86 changed files with 708 additions and 2096 deletions

View File

@@ -1,32 +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.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);
}

View File

@@ -16,7 +16,12 @@ import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.ReconnectionManager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.chat2.Chat;
import org.jivesoftware.smack.chat2.ChatManager;
import org.jivesoftware.smack.chat2.IncomingChatMessageListener;
@@ -28,7 +33,7 @@ import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import org.openhab.binding.xmppclient.handler.XMPPClientMessageSubscriber;
import org.openhab.binding.xmppclient.internal.handler.XMPPClientMessageSubscriber;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.xmppclient;
package org.openhab.binding.xmppclient.internal;
import org.openhab.core.thing.ThingTypeUID;

View File

@@ -15,8 +15,7 @@ package org.openhab.binding.xmppclient.internal;
import java.util.Collections;
import java.util.Set;
import org.openhab.binding.xmppclient.XMPPClientBindingConstants;
import org.openhab.binding.xmppclient.handler.XMPPClientHandler;
import org.openhab.binding.xmppclient.internal.handler.XMPPClientHandler;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;

View File

@@ -10,15 +10,12 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.xmppclient.action;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
package org.openhab.binding.xmppclient.internal.action;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.xmppclient.handler.XMPPClientHandler;
import org.openhab.binding.xmppclient.internal.XMPPClient;
import org.openhab.binding.xmppclient.internal.handler.XMPPClientHandler;
import org.openhab.core.automation.annotation.ActionInput;
import org.openhab.core.automation.annotation.RuleAction;
import org.openhab.core.thing.binding.ThingActions;
@@ -29,17 +26,12 @@ import org.slf4j.LoggerFactory;
/**
* 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, IXMPPActions {
public class XMPPActions implements ThingActions {
private static final Logger logger = LoggerFactory.getLogger(XMPPActions.class);
private @Nullable XMPPClientHandler handler;
@@ -50,11 +42,10 @@ public class XMPPActions implements ThingActions, IXMPPActions {
@Override
public @Nullable ThingHandler getThingHandler() {
return this.handler;
return handler;
}
@Override
@RuleAction(label = "publishXMPP", description = "Publish to XMPP")
@RuleAction(label = "publish a message", description = "Publish a message using 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) {
XMPPClientHandler clientHandler = handler;
@@ -76,25 +67,10 @@ public class XMPPActions implements ThingActions, IXMPPActions {
}
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) {
throw new IllegalArgumentException("actions cannot be null");
if (actions instanceof XMPPActions) {
((XMPPActions) actions).publishXMPP(to, text);
} else {
throw new IllegalArgumentException("Actions is not an instance of XMPPActions");
}
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");
}
}

View File

@@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.xmppclient.handler;
package org.openhab.binding.xmppclient.internal.handler;
import org.openhab.binding.xmppclient.internal.XMPPClient;
import org.openhab.core.thing.ChannelUID;

View File

@@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.xmppclient.handler;
package org.openhab.binding.xmppclient.internal.handler;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

View File

@@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.xmppclient.handler;
package org.openhab.binding.xmppclient.internal.handler;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

View File

@@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.xmppclient.handler;
package org.openhab.binding.xmppclient.internal.handler;
import java.io.IOException;
import java.util.Collection;
@@ -20,8 +20,8 @@ import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.jivesoftware.smack.*;
import org.openhab.binding.xmppclient.action.XMPPActions;
import org.openhab.binding.xmppclient.internal.XMPPClient;
import org.openhab.binding.xmppclient.internal.action.XMPPActions;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;

View File

@@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.xmppclient.handler;
package org.openhab.binding.xmppclient.internal.handler;
/**
* Subscriber interface