Skip to content

Commit 4eb0595

Browse files
committed
Merge pull request #99 from surevine/issue-98
Remote node configuration data is never pulled from cache.... or indeed cached locally!
2 parents 37a2da7 + 742b992 commit 4eb0595

8 files changed

Lines changed: 203 additions & 1 deletion

File tree

src/main/java/org/buddycloud/channelserver/channel/ChannelManagerImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ public Map<String, String> getNodeConf(String nodeId)
8383
throws NodeStoreException {
8484
return nodeStore.getNodeConf(nodeId);
8585
}
86+
87+
88+
@Override
89+
public boolean isCachedNodeConfig(String nodeId) throws NodeStoreException {
90+
return nodeStore.isCachedNodeConfig(nodeId);
91+
}
8692

8793
@Override
8894
public boolean nodeExists(String nodeId) throws NodeStoreException {

src/main/java/org/buddycloud/channelserver/db/NodeStore.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,14 @@ void deleteNodeItemById(String nodeId, String nodeItemId)
536536
*/
537537
boolean isCachedJID(JID jid) throws NodeStoreException;
538538

539+
/**
540+
* Return whether node config is cached locally
541+
*
542+
* @param nodeId
543+
* @return
544+
* @throws NodeStoreException
545+
*/
546+
boolean isCachedNodeConfig(String nodeId) throws NodeStoreException;
539547

540548
/**
541549
* Allows the server to determine if the requested (subscriptions) node

src/main/java/org/buddycloud/channelserver/db/jdbc/JDBCNodeStore.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,11 @@ public ArrayList<String> getNodeList() throws NodeStoreException {
16431643
public boolean isCachedNode(String nodeId) throws NodeStoreException {
16441644
return (this.countNodeItems(nodeId) > 0);
16451645
}
1646+
1647+
@Override
1648+
public boolean isCachedNodeConfig(String nodeId) throws NodeStoreException {
1649+
return (this.getNodeConf(nodeId).size() > 0);
1650+
}
16461651

16471652
@Override
16481653
public boolean isCachedJID(JID jid) throws NodeStoreException {

src/main/java/org/buddycloud/channelserver/packetprocessor/iq/namespace/pubsub/PubSubResult.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.buddycloud.channelserver.channel.ChannelManager;
88
import org.buddycloud.channelserver.packetprocessor.PacketProcessor;
99
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.result.AffiliationsResult;
10+
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.result.Configuration;
1011
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.result.ItemsResult;
1112
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.result.SubscriptionsResult;
1213
import org.buddycloud.channelserver.queue.FederatedQueueManager;
@@ -42,6 +43,7 @@ private void initElementProcessors() {
4243
elementProcessors.add(new SubscriptionsResult(channelManager));
4344
elementProcessors.add(new AffiliationsResult(channelManager));
4445
elementProcessors.add(new ItemsResult(channelManager));
46+
elementProcessors.add(new Configuration(channelManager));
4547
}
4648

4749
@Override

src/main/java/org/buddycloud/channelserver/packetprocessor/iq/namespace/pubsub/get/NodeConfigureGet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm)
5151
return;
5252
}
5353

54-
if (!channelManager.isLocalNode(node)) {
54+
if (!channelManager.isLocalNode(node) && !channelManager.isCachedNodeConfig(node)) {
5555
makeRemoteRequest();
5656
return;
5757
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.result;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
6+
import org.apache.log4j.Logger;
7+
import org.buddycloud.channelserver.channel.ChannelManager;
8+
import org.buddycloud.channelserver.channel.node.configuration.NodeConfigurationException;
9+
import org.buddycloud.channelserver.channel.node.configuration.field.Owner;
10+
import org.buddycloud.channelserver.db.exception.NodeStoreException;
11+
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.PubSubElementProcessorAbstract;
12+
import org.buddycloud.channelserver.pubsub.model.NodeSubscription;
13+
import org.buddycloud.channelserver.pubsub.model.impl.NodeSubscriptionImpl;
14+
import org.buddycloud.channelserver.pubsub.subscription.Subscriptions;
15+
import org.dom4j.Element;
16+
import org.xmpp.packet.IQ;
17+
import org.xmpp.packet.JID;
18+
import org.xmpp.packet.PacketError;
19+
20+
public class Configuration extends PubSubElementProcessorAbstract {
21+
22+
private IQ request;
23+
Element configure;
24+
25+
private static final Logger logger = Logger
26+
.getLogger(Configuration.class);
27+
28+
public Configuration(ChannelManager channelManager) {
29+
this.channelManager = channelManager;
30+
}
31+
32+
@Override
33+
public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm)
34+
throws Exception {
35+
this.request = reqIQ;
36+
37+
if (-1 != request.getFrom().toString().indexOf("@")) {
38+
logger.debug("Ignoring result packet, only interested in stanzas "
39+
+ "from other buddycloud servers");
40+
return;
41+
}
42+
43+
configure = request.getChildElement().element("configure");
44+
node = configure.attributeValue("node");
45+
if (0 == node.length()) return;
46+
47+
if (false == channelManager.nodeExists(node)) {
48+
channelManager.addRemoteNode(node);
49+
}
50+
setNodeConfiguration();
51+
}
52+
53+
private void setNodeConfiguration() throws Exception {
54+
try {
55+
getNodeConfigurationHelper().parse(request);
56+
updateNodeConfiguration(getNodeConfigurationHelper()
57+
.getValues());
58+
} catch (NodeConfigurationException e) {
59+
logger.error("Node configuration exception", e);
60+
} catch (NodeStoreException e) {
61+
logger.error("Data Store Exception", e);
62+
}
63+
}
64+
65+
private void updateNodeConfiguration(HashMap<String, String> configuration)
66+
throws Exception {
67+
channelManager.setNodeConf(node, configuration);
68+
}
69+
70+
@Override
71+
public boolean accept(Element elm) {
72+
return elm.getName().equals("configure");
73+
}
74+
}

src/test/java/org/buddycloud/channelserver/db/jdbc/JDBCNodeStoreTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,17 @@ public void testGetNodeConf() throws Exception {
489489
entry.getValue(), result.get(entry.getKey()));
490490
}
491491
}
492+
493+
@Test
494+
public void testNodeWithConfigSaysConfigIsCached() throws Exception {
495+
dbTester.loadData("node_1");
496+
Assert.assertTrue(store.isCachedNodeConfig(TEST_SERVER1_NODE1_ID));
497+
}
498+
499+
@Test
500+
public void testNodeWithoutConfigSaysConfigNotCached() throws Exception {
501+
Assert.assertFalse(store.isCachedNodeConfig(TEST_SERVER1_NODE1_ID));
502+
}
492503

493504
@Test
494505
public void testAddUserSubscriptionNewSubscription() throws Exception {
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.result;
2+
3+
import junit.framework.Assert;
4+
5+
import org.buddycloud.channelserver.channel.ChannelManager;
6+
import org.buddycloud.channelserver.packetHandler.iq.IQTestHandler;
7+
import org.buddycloud.channelserver.pubsub.affiliation.Affiliations;
8+
import org.buddycloud.channelserver.pubsub.model.NodeSubscription;
9+
import org.dom4j.Element;
10+
import org.dom4j.tree.BaseElement;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
import org.mockito.Mockito;
14+
import org.xmpp.packet.IQ;
15+
import org.xmpp.packet.JID;
16+
17+
public class ConfigurationTest extends IQTestHandler {
18+
19+
private IQ resultWithNode;
20+
private IQ resultNoNode;
21+
private Configuration confResult;
22+
private Element element;
23+
24+
private String node = "/user/pamela@denmark.lit/posts";
25+
private JID jid = new JID("juliet@shakespeare.lit");
26+
private ChannelManager channelManager;
27+
28+
@Before
29+
public void setUp() throws Exception {
30+
31+
channelManager = Mockito.mock(ChannelManager.class);
32+
33+
confResult = new Configuration(channelManager);
34+
resultWithNode = readStanzaAsIq("/iq/pubsub/subscriptions/reply-with-node.stanza");
35+
resultNoNode = readStanzaAsIq("/iq/pubsub/subscriptions/reply-no-node.stanza");
36+
37+
element = new BaseElement("configure");
38+
element.addAttribute("node", node);
39+
40+
confResult.setChannelManager(channelManager);
41+
}
42+
43+
@Test
44+
public void testPassingConfigureAsElementNameReturnsTrue() {
45+
Assert.assertTrue(confResult.accept(element));
46+
}
47+
48+
@Test
49+
public void testPassingNotConfigureAsElementNameReturnsFalse() {
50+
Element element = new BaseElement("not-configure");
51+
Assert.assertFalse(confResult.accept(element));
52+
}
53+
54+
@Test(expected = NullPointerException.class)
55+
public void testInvalidStanzaThrowsException() throws Exception {
56+
57+
IQ result = toIq("<iq type=\"result\" id=\"subscriptions1\" "
58+
+ "from=\"channels.shakespeare.lit\" "
59+
+ "to=\"channels.denmark.lit\">"
60+
+ "<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\" />"
61+
+ "</iq>");
62+
63+
confResult.process(element, jid, result, null);
64+
}
65+
66+
@Test
67+
public void testEmptyNodeValueCausesNoAction() throws Exception {
68+
IQ result = toIq("<iq type=\"result\" id=\"subscriptions1\" "
69+
+ "from=\"channels.shakespeare.lit\" "
70+
+ "to=\"channels.denmark.lit\">"
71+
+ "<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\">"
72+
+ "<configure node=\"\" />" + "</pubsub>" + "</iq>");
73+
74+
confResult.process(element, jid, result, null);
75+
76+
Mockito.verify(channelManager, Mockito.times(0)).nodeExists(
77+
Mockito.anyString());
78+
}
79+
80+
@Test
81+
public void testIfNodeIsUnknownThenItIsAddedToDatabase() throws Exception {
82+
IQ result = toIq("<iq type=\"result\" id=\"subscriptions1\" "
83+
+ "from=\"channels.shakespeare.lit\" "
84+
+ "to=\"channels.denmark.lit\">"
85+
+ "<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\">"
86+
+ "<configure node=\"some-node\" />" + "</pubsub>" + "</iq>");
87+
88+
confResult.process(element, jid, result, null);
89+
90+
Mockito.when(channelManager.nodeExists(Mockito.anyString())).thenReturn(false);
91+
Mockito.verify(channelManager, Mockito.times(1)).nodeExists(
92+
Mockito.anyString());
93+
Mockito.verify(channelManager, Mockito.times(1)).addRemoteNode(
94+
Mockito.anyString());
95+
}
96+
}

0 commit comments

Comments
 (0)