You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by be...@apache.org on 2009/03/25 15:36:44 UTC

svn commit: r758269 - in /labs/vysper/src/main/java/org/apache/vysper: storage/jcr/ storage/jcr/roster/ storage/jcr/user/ storage/jcr/vcardtemp/ xmpp/addressing/ xmpp/modules/core/im/handler/ xmpp/modules/roster/ xmpp/modules/roster/handler/ xmpp/writer/

Author: berndf
Date: Wed Mar 25 14:36:40 2009
New Revision: 758269

URL: http://svn.apache.org/viewvc?rev=758269&view=rev
Log:
[vysper] LABS-125 improve and try to fix JCR roster persistence (ongoing)

Modified:
    labs/vysper/src/main/java/org/apache/vysper/storage/jcr/JcrStorage.java
    labs/vysper/src/main/java/org/apache/vysper/storage/jcr/roster/JcrRosterManager.java
    labs/vysper/src/main/java/org/apache/vysper/storage/jcr/user/JcrUserManagement.java
    labs/vysper/src/main/java/org/apache/vysper/storage/jcr/vcardtemp/JcrVcardTempPersistenceManager.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/addressing/EntityImpl.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/core/im/handler/PresenceAvailabilityHandler.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterItem.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/handler/RosterIQHandler.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/writer/DenseStanzaLogRenderer.java

Modified: labs/vysper/src/main/java/org/apache/vysper/storage/jcr/JcrStorage.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/storage/jcr/JcrStorage.java?rev=758269&r1=758268&r2=758269&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/storage/jcr/JcrStorage.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/storage/jcr/JcrStorage.java Wed Mar 25 14:36:40 2009
@@ -106,7 +106,7 @@
                 childNode = parent.addNode(nodeName);
                 parent.save();
                 childNode.save();
-                logger.debug("JCR node created " + childNode);
+                logger.info("JCR node created: " + childNode);
             } catch (RepositoryException e) {
                 throw new JcrStorageException(e);
             }

Modified: labs/vysper/src/main/java/org/apache/vysper/storage/jcr/roster/JcrRosterManager.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/storage/jcr/roster/JcrRosterManager.java?rev=758269&r1=758268&r2=758269&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/storage/jcr/roster/JcrRosterManager.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/storage/jcr/roster/JcrRosterManager.java Wed Mar 25 14:36:40 2009
@@ -19,15 +19,15 @@
 import org.apache.vysper.storage.jcr.JcrStorage;
 import org.apache.vysper.storage.jcr.JcrStorageException;
 import org.apache.vysper.xmpp.addressing.Entity;
-import org.apache.vysper.xmpp.addressing.EntityImpl;
 import org.apache.vysper.xmpp.addressing.EntityFormatException;
+import org.apache.vysper.xmpp.addressing.EntityImpl;
 import org.apache.vysper.xmpp.modules.roster.AskSubscriptionType;
+import org.apache.vysper.xmpp.modules.roster.MutableRoster;
 import org.apache.vysper.xmpp.modules.roster.Roster;
 import org.apache.vysper.xmpp.modules.roster.RosterException;
 import org.apache.vysper.xmpp.modules.roster.RosterGroup;
 import org.apache.vysper.xmpp.modules.roster.RosterItem;
 import org.apache.vysper.xmpp.modules.roster.SubscriptionType;
-import org.apache.vysper.xmpp.modules.roster.MutableRoster;
 import org.apache.vysper.xmpp.modules.roster.persistence.AbstractRosterManager;
 import org.apache.vysper.xmpp.protocol.NamespaceURIs;
 import org.slf4j.Logger;
@@ -37,10 +37,14 @@
 import javax.jcr.NodeIterator;
 import javax.jcr.Property;
 import javax.jcr.RepositoryException;
-import java.util.List;
 import java.util.ArrayList;
+import java.util.List;
 
 /**
+ * roster items are stored for contacts in the following path:
+ * /accountentity/user@vysper.org/jabber_iq_roster/contact@vysper.org
+ * all item properties besides contact jid (which is used as a node name)
+ * are stored as node properties  
  */
 public class JcrRosterManager extends AbstractRosterManager {
 
@@ -69,13 +73,20 @@
         
         NodeIterator nodes = null;
         try {
-            nodes = rosterNode.getNodes("contact");
+            nodes = rosterNode.getNodes();
         } catch (RepositoryException e) {
             return roster; // empty roster object
         }
         while (nodes != null && nodes.hasNext()) {
             Node node = nodes.nextNode();
-            String contactJidString = readAttribute(node, "jid");
+
+            String contactJidString = null;
+            try {
+                contactJidString = node.getName();
+            } catch (RepositoryException e) {
+                logger.warn("when loading roster for user {} cannot read node name for node id = " + node.toString());
+            }
+            logger.warn("try now loading contact " + contactJidString + " from node " + node.toString());
             EntityImpl contactJid = null;
             if (contactJidString != null) {
                 try {
@@ -86,6 +97,7 @@
             }
             if (contactJid == null) {
                 logger.warn("when loading roster for user {}, skipping a contact due to missing or unparsable jid", bareJid);
+                continue;
             }
 
             String name = readAttribute(node, "name");
@@ -94,14 +106,14 @@
             try {
                 subscriptionType = SubscriptionType.valueOf(typeString == null ? "NONE" : typeString.toUpperCase());
             } catch (IllegalArgumentException e) {
-                logger.warn("when loading roster for user {}, contact {} misses a subscription type", bareJid, contactJid);
+                logger.warn("when loading roster for user " + bareJid + ", contact " + contactJid + " misses a subscription type", bareJid, contactJid);
             }
             String askTypeString = readAttribute(node, "askType");
             AskSubscriptionType askSubscriptionType = AskSubscriptionType.NOT_SET;
             try {
                 if (askTypeString != null) askSubscriptionType = AskSubscriptionType.valueOf(askTypeString);
             } catch (IllegalArgumentException e) {
-                logger.warn("when loading roster for user {}, contact {}, the ask subscription type is unparsable. skipping!", bareJid.getFullQualifiedName(), contactJid.getFullQualifiedName());
+                logger.warn("when loading roster for user " + bareJid.getFullQualifiedName() + ", contact " + contactJid.getFullQualifiedName() + ", the ask subscription type is unparsable. skipping!");
                 continue; // don't return it, don't set a default!
             }
 
@@ -109,6 +121,7 @@
             // TODO read groups
 
             RosterItem item = new RosterItem(contactJid, name, subscriptionType, askSubscriptionType, groups);
+            logger.info("item loaded for " + bareJid.getFullQualifiedName() + ": " + item.toString());
             roster.addItem(item);
         }
         return roster;
@@ -132,24 +145,55 @@
     @Override
     public void addContact(Entity jid, RosterItem rosterItem) throws RosterException {
         if (jid == null) throw new RosterException("jid not provided");
+        if (rosterItem.getJid() == null) throw new RosterException("contact jid not provided"); 
+
+        // TODO think about concurrent updates
+        
+        Entity contactJid = rosterItem.getJid().getBareJID();
+        Node contactNode = getOrCreateContactNode(jid, contactJid);
+        try {
+            setOrRemoveAttribute(contactNode, "name", rosterItem.getName());
+            String subscriptionTypeValue = rosterItem.getSubscriptionType() == null ? null : rosterItem.getSubscriptionType().value();
+            setOrRemoveAttribute(contactNode, "type", subscriptionTypeValue);
+            String askSubscriptionTypeValue = null;
+            if (rosterItem.getAskSubscriptionType() != null && 
+                rosterItem.getAskSubscriptionType() != AskSubscriptionType.NOT_SET) {
+                askSubscriptionTypeValue = rosterItem.getAskSubscriptionType().value();
+            }
+            setOrRemoveAttribute(contactNode, "askType", askSubscriptionTypeValue);
+            contactNode.save();
+            logger.info("JCR node created/updated: " + contactNode);
+        } catch (RepositoryException e) {
+            throw new RosterException("failed to add contact node to roster for user = " + jid.getFullQualifiedName() + " and contact jid = " + rosterItem.getJid().getFullQualifiedName(), e);
+        }
+    }
+
+    private void setOrRemoveAttribute(Node contactNode, String attributeName, String attributeValue) throws RepositoryException {
+        if (attributeValue != null) contactNode.setProperty(attributeName, attributeValue);
+        else if (contactNode.hasProperty(attributeName)) contactNode.setProperty(attributeName, (String)null);
+    }
+
+    private Node getOrCreateContactNode(Entity jid, Entity contactJid) throws RosterException {
         Node entityNode = null;
         try {
             entityNode = jcrStorage.getEntityNode(jid, NamespaceURIs.JABBER_IQ_ROSTER, true);
         } catch (JcrStorageException e) {
             throw new RosterException("failed to create roster store for " + jid.getFullQualifiedName(), e);
         }
+        Node contactNode = null;
         try {
-            Node contactNode = entityNode.addNode("contact");
-            entityNode.save();
-            contactNode.setProperty("jid", rosterItem.getJid().getBareJID().getFullQualifiedName());
-            if (rosterItem.getName() != null) contactNode.setProperty("name", rosterItem.getName());
-            if (rosterItem.getSubscriptionType() != null) contactNode.setProperty("type", rosterItem.getSubscriptionType().value());
-            if (rosterItem.getAskSubscriptionType() != AskSubscriptionType.NOT_SET) contactNode.setProperty("askType", rosterItem.getAskSubscriptionType().value());
-            contactNode.save();
-            logger.debug("JCR node created " + contactNode);
+            contactNode = entityNode.getNode(contactJid.getFullQualifiedName());
         } catch (RepositoryException e) {
-            throw new RosterException("failed to add contact node to roster for user = " + jid.getFullQualifiedName() + " and contact jid = " + rosterItem.getJid().getFullQualifiedName(), e);
+            // not exists, create
+            try {
+                contactNode = entityNode.addNode(contactJid.getFullQualifiedName());
+                entityNode.save();
+            } catch (RepositoryException addNodeEx) {
+                throw new RosterException("failed to add contact node to roster for user = " + jid.getFullQualifiedName() + " and contact jid = " + contactJid.getFullQualifiedName(), addNodeEx);
+            }
+            
         }
+        return contactNode;
     }
 
     @Override
@@ -183,6 +227,6 @@
                 }
             }
         }
-        if (!foundOne) logger.warn("failed to remove from roster for user {} the contact jid {}", jidUser, jidContact);
+        if (!foundOne) logger.warn("failed to remove from roster for user " + jidUser + " the contact jid " + jidContact);
     }
 }

Modified: labs/vysper/src/main/java/org/apache/vysper/storage/jcr/user/JcrUserManagement.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/storage/jcr/user/JcrUserManagement.java?rev=758269&r1=758268&r2=758269&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/storage/jcr/user/JcrUserManagement.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/storage/jcr/user/JcrUserManagement.java Wed Mar 25 14:36:40 2009
@@ -94,7 +94,7 @@
             final Node credentialsNode = jcrStorage.getEntityNode(entity, CREDENTIALS_NAMESPACE, true);
             credentialsNode.setProperty("password", password);
             credentialsNode.save();
-            logger.debug("JCR node created " + credentialsNode);
+            logger.info("JCR node created: " + credentialsNode);
         } catch (Exception e) {
             // TODO remove account?
             throw new AccountCreationException("failed to create the account set credentials", e);

Modified: labs/vysper/src/main/java/org/apache/vysper/storage/jcr/vcardtemp/JcrVcardTempPersistenceManager.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/storage/jcr/vcardtemp/JcrVcardTempPersistenceManager.java?rev=758269&r1=758268&r2=758269&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/storage/jcr/vcardtemp/JcrVcardTempPersistenceManager.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/storage/jcr/vcardtemp/JcrVcardTempPersistenceManager.java Wed Mar 25 14:36:40 2009
@@ -76,7 +76,7 @@
         try {
             entityNode.setProperty("content", xml);
             entityNode.save();
-            logger.debug("JCR node created " + entityNode);
+            logger.info("JCR node created: " + entityNode);
             return true;
         } catch (RepositoryException e) {
             return false;

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/addressing/EntityImpl.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/addressing/EntityImpl.java?rev=758269&r1=758268&r2=758269&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/addressing/EntityImpl.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/addressing/EntityImpl.java Wed Mar 25 14:36:40 2009
@@ -32,6 +32,8 @@
 	private String node;
 	private String domain;
 	private String resource;
+    
+    protected String fullyQualifiedCached = null;
 
     public static EntityImpl parse(String entity) throws EntityFormatException {
 		String node = null;
@@ -84,7 +86,8 @@
 	}
 
 	public String getFullQualifiedName() {
-        return buildEntityString(node, domain, resource);
+        if (fullyQualifiedCached == null) fullyQualifiedCached = buildEntityString(node, domain, resource);
+        return fullyQualifiedCached;
 	}
 
     private String buildEntityString(String node, String domain, String resource) {

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/core/im/handler/PresenceAvailabilityHandler.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/core/im/handler/PresenceAvailabilityHandler.java?rev=758269&r1=758268&r2=758269&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/core/im/handler/PresenceAvailabilityHandler.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/core/im/handler/PresenceAvailabilityHandler.java Wed Mar 25 14:36:40 2009
@@ -55,6 +55,7 @@
 
     final Logger logger = LoggerFactory.getLogger(PresenceAvailabilityHandler.class);
     
+    @Override
     /*package*/ Stanza executeCorePresence(ServerRuntimeContext serverRuntimeContext, boolean isOutboundStanza, SessionContext sessionContext, PresenceStanza presenceStanza, RosterManager rosterManager) {
 
         if (isSubscriptionType(presenceStanza.getPresenceType())) {

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterItem.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterItem.java?rev=758269&r1=758268&r2=758269&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterItem.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterItem.java Wed Mar 25 14:36:40 2009
@@ -109,4 +109,15 @@
     public boolean isBoth() {
         return subscriptionType == BOTH; 
     }
+
+    @Override
+    public String toString() {
+        return "RosterItem{" +
+                "jid=" + (jid == null ? "NULL" : jid.getFullQualifiedName()) +
+                ", name='" + name + '\'' +
+                ", groups=" + groups +
+                ", subscriptionType=" + subscriptionType +
+                ", askSubscriptionType=" + askSubscriptionType +
+                '}';
+    }
 }

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/handler/RosterIQHandler.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/handler/RosterIQHandler.java?rev=758269&r1=758268&r2=758269&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/handler/RosterIQHandler.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/handler/RosterIQHandler.java Wed Mar 25 14:36:40 2009
@@ -113,9 +113,11 @@
             return ServerErrorResponses.getInstance().getStanzaError(StanzaErrorCondition.NOT_ACCEPTABLE, stanza, StanzaErrorType.MODIFY, e.getMessage(), null, null);
         }
 
+        Entity contactJid = setRosterItem.getJid().getBareJID();
+
         RosterItem existingItem;
         try {
-            existingItem = rosterManager.getContact(user.getBareJID(), setRosterItem.getJid().getBareJID());
+            existingItem = rosterManager.getContact(user.getBareJID(), contactJid);
         } catch (RosterException e) {
             existingItem = null;
         }
@@ -125,14 +127,15 @@
         } /* else: all other subscription types are ignored in a roster set and have been filtered out by RosterUtils.parseRosterItem() */
 
         if (existingItem == null) {
-            existingItem = new RosterItem(user.getBareJID(), SubscriptionType.NONE);
+            existingItem = new RosterItem(contactJid, SubscriptionType.NONE);
             try {
                 rosterManager.addContact(user.getBareJID(), existingItem);
             } catch (RosterException e) {
-                return ServerErrorResponses.getInstance().getStanzaError(StanzaErrorCondition.BAD_REQUEST, stanza, StanzaErrorType.CANCEL, "roster item contact not (yet) in roster: " + setRosterItem.getJid().getBareJID(), null, null);
+                return ServerErrorResponses.getInstance().getStanzaError(StanzaErrorCondition.BAD_REQUEST, stanza, StanzaErrorType.CANCEL, "roster item contact not (yet) in roster: " + contactJid, null, null);
             }
         }
 
+        // TODO this does not work beyond memory-based rosters, right?
         synchronized (existingItem) {
             if (setRosterItem.getName() != null) {
                 existingItem.setName(setRosterItem.getName());

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/writer/DenseStanzaLogRenderer.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/writer/DenseStanzaLogRenderer.java?rev=758269&r1=758268&r2=758269&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/writer/DenseStanzaLogRenderer.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/writer/DenseStanzaLogRenderer.java Wed Mar 25 14:36:40 2009
@@ -40,6 +40,7 @@
     public static String render(XMLElement stanza) {
         try {
             StringBuilder stringBuilder = new StringBuilder();
+            if (stanza == null) return stringBuilder.append("NULL_STANZA").toString();
             String outerName = stanza.getName();
             stringBuilder.append(outerName);
             XMLElement firstInnerElement = stanza.getFirstInnerElement();



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org