1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-23 04:57:58 +01:00

AMP should use provider/packet packages and JUL

- Move AMPExtensionProvider to amp.provider
- Move AMPExtension to amp.packet
- Move java.util.logging for logging

SMACK-544
This commit is contained in:
Florian Schmaus 2014-03-10 17:00:36 +01:00
parent 6110872062
commit 0058631ed6
8 changed files with 48 additions and 28 deletions

View file

@ -17,6 +17,8 @@
package org.jivesoftware.smackx.amp;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.amp.packet.AMPExtension;
public class AMPDeliverCondition implements AMPExtension.Condition {
@ -26,8 +28,9 @@ public class AMPDeliverCondition implements AMPExtension.Condition {
* Check if server supports deliver condition
* @param connection Smack connection instance
* @return true if deliver condition is supported.
* @throws XMPPException
*/
public static boolean isSupported(XMPPConnection connection) {
public static boolean isSupported(XMPPConnection connection) throws XMPPException {
return AMPManager.isConditionSupported(connection, NAME);
}

View file

@ -17,7 +17,9 @@
package org.jivesoftware.smackx.amp;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.XmppDateTime;
import org.jivesoftware.smackx.amp.packet.AMPExtension;
import java.util.Date;
@ -30,8 +32,9 @@ public class AMPExpireAtCondition implements AMPExtension.Condition {
* Check if server supports expire-at condition
* @param connection Smack connection instance
* @return true if expire-at condition is supported.
* @throws XMPPException
*/
public static boolean isSupported(XMPPConnection connection) {
public static boolean isSupported(XMPPConnection connection) throws XMPPException {
return AMPManager.isConditionSupported(connection, NAME);
}

View file

@ -19,6 +19,7 @@ package org.jivesoftware.smackx.amp;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.amp.packet.AMPExtension;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
@ -82,8 +83,9 @@ public class AMPManager {
* @param connection active xmpp connection
* @param action action to check
* @return true if this action is supported.
* @throws XMPPException
*/
public static boolean isActionSupported(XMPPConnection connection, AMPExtension.Action action) {
public static boolean isActionSupported(XMPPConnection connection, AMPExtension.Action action) throws XMPPException {
String featureName = AMPExtension.NAMESPACE + "?action=" + action.toString();
return isFeatureSupportedByServer(connection, featureName, AMPExtension.NAMESPACE);
}
@ -93,28 +95,25 @@ public class AMPManager {
* @param connection active xmpp connection
* @param conditionName name of condition to check
* @return true if this condition is supported.
* @throws XMPPException
* @see AMPDeliverCondition
* @see AMPExpireAtCondition
* @see AMPMatchResourceCondition
*/
public static boolean isConditionSupported(XMPPConnection connection, String conditionName) {
public static boolean isConditionSupported(XMPPConnection connection, String conditionName) throws XMPPException {
String featureName = AMPExtension.NAMESPACE + "?condition=" + conditionName;
return isFeatureSupportedByServer(connection, featureName, AMPExtension.NAMESPACE);
}
private static boolean isFeatureSupportedByServer(XMPPConnection connection, String featureName, String node) {
try {
ServiceDiscoveryManager discoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
DiscoverInfo info = discoveryManager.discoverInfo(connection.getServiceName(), node);
Iterator<DiscoverInfo.Feature> it = info.getFeatures();
while (it.hasNext()) {
DiscoverInfo.Feature feature = it.next();
if (featureName.equals(feature.getVar())) {
return true;
}
private static boolean isFeatureSupportedByServer(XMPPConnection connection, String featureName, String node) throws XMPPException {
ServiceDiscoveryManager discoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
DiscoverInfo info = discoveryManager.discoverInfo(connection.getServiceName(), node);
Iterator<DiscoverInfo.Feature> it = info.getFeatures();
while (it.hasNext()) {
DiscoverInfo.Feature feature = it.next();
if (featureName.equals(feature.getVar())) {
return true;
}
} catch (XMPPException e) {
e.printStackTrace();
}
return false;
}

View file

@ -17,6 +17,8 @@
package org.jivesoftware.smackx.amp;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.amp.packet.AMPExtension;
public class AMPMatchResourceCondition implements AMPExtension.Condition {
@ -26,8 +28,9 @@ public class AMPMatchResourceCondition implements AMPExtension.Condition {
* Check if server supports match-resource condition
* @param connection Smack connection instance
* @return true if match-resource condition is supported.
* @throws XMPPException
*/
public static boolean isSupported(XMPPConnection connection) {
public static boolean isSupported(XMPPConnection connection) throws XMPPException {
return AMPManager.isConditionSupported(connection, NAME);
}

View file

@ -14,9 +14,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.amp;
package org.jivesoftware.smackx.amp.packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.amp.AMPDeliverCondition;
import org.jivesoftware.smackx.amp.AMPExpireAtCondition;
import org.jivesoftware.smackx.amp.AMPMatchResourceCondition;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
@ -259,7 +262,7 @@ public class AMPExtension implements PacketExtension {
*/
notify;
static final String ATTRIBUTE_NAME="action";
public static final String ATTRIBUTE_NAME="action";
}
/**

View file

@ -14,14 +14,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.amp;
package org.jivesoftware.smackx.amp.provider;
import java.util.logging.Logger;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smackx.amp.AMPDeliverCondition;
import org.jivesoftware.smackx.amp.AMPExpireAtCondition;
import org.jivesoftware.smackx.amp.AMPMatchResourceCondition;
import org.jivesoftware.smackx.amp.packet.AMPExtension;
import org.xmlpull.v1.XmlPullParser;
public class AMPExtensionProvider implements PacketExtensionProvider {
private static final Logger LOGGER = Logger.getLogger(AMPExtensionProvider.class.getName());
/**
* Creates a new AMPExtensionProvider.
@ -46,7 +53,7 @@ public class AMPExtensionProvider implements PacketExtensionProvider {
try {
status = AMPExtension.Status.valueOf(statusString);
} catch (IllegalArgumentException ex) {
System.err.println("Found invalid amp status " + statusString);
LOGGER.severe("Found invalid amp status " + statusString);
}
}
@ -73,12 +80,12 @@ public class AMPExtensionProvider implements PacketExtensionProvider {
try {
action = AMPExtension.Action.valueOf(actionString);
} catch (IllegalArgumentException ex) {
System.err.println("Found invalid rule action value " + actionString);
LOGGER.severe("Found invalid rule action value " + actionString);
}
}
if (action == null || condition == null) {
System.err.println("Rule is skipped because either it's action or it's condition is invalid");
LOGGER.severe("Rule is skipped because either it's action or it's condition is invalid");
} else {
AMPExtension.Rule rule = new AMPExtension.Rule(action, condition);
ampExtension.addRule(rule);
@ -96,7 +103,7 @@ public class AMPExtensionProvider implements PacketExtensionProvider {
private AMPExtension.Condition createCondition(String name, String value) {
if (name == null || value == null) {
System.err.println("Can't create rule condition from null name and/or value");
LOGGER.severe("Can't create rule condition from null name and/or value");
return null;
}
@ -105,7 +112,7 @@ public class AMPExtensionProvider implements PacketExtensionProvider {
try {
return new AMPDeliverCondition(AMPDeliverCondition.Value.valueOf(value));
} catch (IllegalArgumentException ex) {
System.err.println("Found invalid rule delivery condition value " + value);
LOGGER.severe("Found invalid rule delivery condition value " + value);
return null;
}
} else if (AMPExpireAtCondition.NAME.equals(name)) {
@ -114,11 +121,11 @@ public class AMPExtensionProvider implements PacketExtensionProvider {
try {
return new AMPMatchResourceCondition(AMPMatchResourceCondition.Value.valueOf(value));
} catch (IllegalArgumentException ex) {
System.err.println("Found invalid rule match-resource condition value " + value);
LOGGER.severe("Found invalid rule match-resource condition value " + value);
return null;
}
} else {
System.err.println("Found unknown rule condition name " + name);
LOGGER.severe("Found unknown rule condition name " + name);
return null;
}
}

View file

@ -453,7 +453,7 @@
<extensionProvider>
<elementName>amp</elementName>
<namespace>http://jabber.org/protocol/amp</namespace>
<className>org.jivesoftware.smackx.amp.AMPExtensionProvider</className>
<className>org.jivesoftware.smackx.amp.provider.AMPExtensionProvider</className>
</extensionProvider>
</smackProviders>

View file

@ -17,6 +17,8 @@
package org.jivesoftware.smackx.amp;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.amp.packet.AMPExtension;
import org.jivesoftware.smackx.amp.provider.AMPExtensionProvider;
import org.junit.Before;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;