jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1182978 - in /jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src: main/java/org/apache/jackrabbit/spi2microkernel/ test/java/org/apache/jackrabbit/spi2microkernel/
Date Thu, 13 Oct 2011 16:57:10 GMT
Author: mduerig
Date: Thu Oct 13 16:57:10 2011
New Revision: 1182978

URL: http://svn.apache.org/viewvc?rev=1182978&view=rev
Log:
Microkernel based Jackrabbit prototype (WIP)
JSON encode commit messages

Added:
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/CommitInfo.java
      - copied, changed from r1182710, jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/CommitMessage.java
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CommitInfoTest.java
      - copied, changed from r1182710, jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CommitMessageTest.java
Removed:
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/CommitMessage.java
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CommitMessageTest.java
Modified:
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SessionInfoImpl.java
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SubscriptionImpl.java

Copied: jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/CommitInfo.java
(from r1182710, jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/CommitMessage.java)
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/CommitInfo.java?p2=jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/CommitInfo.java&p1=jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/CommitMessage.java&r1=1182710&r2=1182978&rev=1182978&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/CommitMessage.java
(original)
+++ jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/CommitInfo.java
Thu Oct 13 16:57:10 2011
@@ -2,91 +2,102 @@ package org.apache.jackrabbit.spi2microk
 
 
 import org.apache.jackrabbit.spi.commons.util.Unchecked;
-import org.apache.jackrabbit.util.Text;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.json.simple.parser.ParseException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class CommitMessage {
-    private static final Logger log = LoggerFactory.getLogger(CommitMessage.class);
+import java.util.Map;
 
-    private final String userId;
-    private final String userData;
+public class CommitInfo {
+    private static final Logger log = LoggerFactory.getLogger(CommitInfo.class);
+    
+    private final JSONObject jsonObject;
 
-    private static final char ESCAPE_CHAR = '\\';
-    private static final char SEPARATOR_CHAR = ':';
-
-    public static CommitMessage parse(String s) {
-        if (s == null) {
+    public static CommitInfo parse(String json) {
+        if (json == null) {
             IllegalArgumentException e = new IllegalArgumentException("null");
             log.error(e.getMessage(), e);
             throw e;
         }
 
-        int k = s.indexOf(SEPARATOR_CHAR);
-        while (k > 0 && s.charAt(k - 1) == ESCAPE_CHAR) {
-            k = s.indexOf(SEPARATOR_CHAR, k + 1);
+        try {
+            Object o = new JSONParser().parse(json);
+            if (o instanceof JSONObject) {
+                return new CommitInfo((JSONObject) o);
+            }
+            else {
+                IllegalArgumentException e = new IllegalArgumentException("Not a JSON object:
" + json);
+                log.error(e.getMessage(), e);
+                throw e;
+            }
+        }
+        catch (ParseException pe) {
+            log.error(pe.getMessage(), pe);
+            throw new IllegalArgumentException("Syntax error in JSON: " + json, pe);
         }
-
-        return k == -1
-            ? null
-            : new CommitMessage(unescape(s.substring(0, k)), unescape(s.substring(k + 1,
s.length())));
     }
 
-    public CommitMessage(String userId, String userData) {
-        if (userId == null) {
-            IllegalArgumentException e = new IllegalArgumentException("UserId is null");
-            log.error(e.getMessage(), e);
-            throw e;
-        }
-        if (userData == null) {
-            IllegalArgumentException e = new IllegalArgumentException("UserData is null");
-            log.error(e.getMessage(), e);
-            throw e;
-        }
+    private CommitInfo(JSONObject jsonObject) {
+        this.jsonObject = jsonObject;
+    }
 
-        this.userId = userId;
-        this.userData = userData;
+    public CommitInfo() {
+        jsonObject = new JSONObject();
     }
 
     public String getUserId() {
-        return userId;
+        return getValue("UserId");
+    }
+
+    public CommitInfo setUserId(String userId) {
+        return setValue("UserId", userId);
     }
 
     public String getUserData() {
-        return userData;
+        return getValue("UserData");
     }
 
-    public String encode() {
-        return escape(userId) + SEPARATOR_CHAR + escape(userData);
+    public CommitInfo setUserData(String userData) {
+        return setValue("UserData", userData);
     }
 
-    @Override
-    public int hashCode() {
-        return 37 * (37 * 17 + userId.hashCode()) + userData.hashCode();
+    public String getSessionId() {
+        return getValue("SessionId");
     }
 
-    @Override
-    public boolean equals(Object other) {
-        return other == this || other instanceof CommitMessage && equals(Unchecked.<CommitMessage>cast(other));
+    public CommitInfo setSessionId(String sessionId) {
+        return setValue("SessionId", sessionId);
     }
 
-    @Override
-    public String toString() {
-        return encode();
+    public String getValue(String key) {
+        Object value = jsonObject.get(key);
+        return value == null ? null : value.toString();
     }
 
-    //------------------------------------------< private >---
+    public CommitInfo setValue(String key, String value) {
+        Unchecked.<Map<String, String>>cast(jsonObject).put(key, value);
+        return this;
+    }
 
-    protected boolean equals(CommitMessage other) {
-        return other.userId.equals(userId) && other.userData.equals(userData);
+    public String toJson() {
+        return jsonObject.toJSONString();
     }
 
-    private static String escape(String s) {
-        return Text.escapeWith(s, SEPARATOR_CHAR, ESCAPE_CHAR);
+    @Override
+    public String toString() {
+        return toJson();
     }
 
-    private static String unescape(String s) {
-        return Text.unescapeWith(s, ESCAPE_CHAR);
+    @Override
+    public boolean equals(Object that) {
+        return that instanceof CommitInfo && ((CommitInfo) that).jsonObject.equals(jsonObject);
+    }
+
+    @Override
+    public int hashCode() {
+        return jsonObject.hashCode();
     }
-        
+
 }

Modified: jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java?rev=1182978&r1=1182977&r2=1182978&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java
(original)
+++ jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java
Thu Oct 13 16:57:10 2011
@@ -1071,14 +1071,22 @@ public class RepositoryServiceImpl exten
                 @Override
                 public String apply(MicroKernel microKernel) throws RepositoryException {
                     String rev = getRevision(sessionInfo);
-                    String userId = sessionInfo.getUserID();
-                    String userData = sessionInfo.getUserData();
-                    CommitMessage commitMessage = new CommitMessage(userId, userData == null
? "" : userData);
-                    return microKernel.commit("/", jsop.toString(), rev, commitMessage.encode());
+                    CommitInfo commitInfo = commitInfo(sessionInfo);
+                    return microKernel.commit("/", jsop.toString(), rev, commitInfo.toJson());
                 }
             });
         }
 
+        private CommitInfo commitInfo(SessionInfoImpl sessionInfo) {
+            CommitInfo commitInfo = new CommitInfo();
+            commitInfo.setUserId(sessionInfo.getUserID());
+            commitInfo.setSessionId(sessionInfo.getId());
+            if (sessionInfo.getUserData() != null) {
+                commitInfo.setUserData(sessionInfo.getUserData());
+            }
+            return commitInfo;
+        }
+
         private String target(NodeId nodeId) throws RepositoryException {
             String wspName = sessionInfo.getWorkspaceName();
             Path path = getPath(nodeId, wspName, getRevision(sessionInfo));

Modified: jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SessionInfoImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SessionInfoImpl.java?rev=1182978&r1=1182977&r2=1182978&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SessionInfoImpl.java
(original)
+++ jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SessionInfoImpl.java
Thu Oct 13 16:57:10 2011
@@ -27,10 +27,12 @@ import org.slf4j.LoggerFactory;
 import javax.jcr.RepositoryException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.UUID;
 
 public class SessionInfoImpl extends org.apache.jackrabbit.spi.commons.SessionInfoImpl {
     private static final Logger log = LoggerFactory.getLogger(SessionInfoImpl.class);
 
+    private final String id = UUID.randomUUID().toString();
     private final MicroKernel microKernel;
     private final List<String> commitLog = new ArrayList<String>();
 
@@ -44,6 +46,10 @@ public class SessionInfoImpl extends org
         commitLog.add(revision);
     }
 
+    public String getId() {
+        return id;
+    }
+
     public String getRevision() {
         return revision;
     }
@@ -58,7 +64,7 @@ public class SessionInfoImpl extends org
         }
         catch (Exception e) {
             log.error(e.getMessage(), e);
-            throw new RepositoryException(e.getMessage(), e); 
+            throw new RepositoryException(e.getMessage(), e);
         }
     }
 

Modified: jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SubscriptionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SubscriptionImpl.java?rev=1182978&r1=1182977&r2=1182978&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SubscriptionImpl.java
(original)
+++ jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SubscriptionImpl.java
Thu Oct 13 16:57:10 2011
@@ -302,8 +302,8 @@ public class SubscriptionImpl implements
         private EventBundle createEventBundle(JSONObject jsonObject) throws RepositoryException
{
             String revisionId = getString(jsonObject, "id", false);
             String msg = getString(jsonObject, "msg", true);
-            CommitMessage commitMessage = parseCommitMessage(msg);
-            if (commitMessage == null) {
+            CommitInfo commitInfo = parseCommitMessage(msg);
+            if (commitInfo == null) {
                 // Skip alien commits
                 return null;
             }
@@ -314,8 +314,8 @@ public class SubscriptionImpl implements
 
             EventTemplate eventTemplate = new EventTemplate()
                     .setTimestamp(timeStamp)
-                    .setUserId(commitMessage.getUserId())
-                    .setUserData(commitMessage.getUserData());
+                    .setUserId(commitInfo.getUserId())
+                    .setUserData(commitInfo.getUserData());
 
             Collection<Event> events = getEvents(changes, eventTemplate);
             boolean isLocal = sessionInfo.hasRevision(revisionId);
@@ -338,8 +338,8 @@ public class SubscriptionImpl implements
             return new EventBundleImpl(events, isLocal);
         }
 
-        private CommitMessage parseCommitMessage(String msg) {
-            return msg == null ? null : CommitMessage.parse(msg);
+        private CommitInfo parseCommitMessage(String msg) {
+            return msg == null ? null : CommitInfo.parse(msg);
         }
 
         private Collection<Event> getEvents(String changes, EventTemplate eventTemplate)
throws RepositoryException {

Copied: jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CommitInfoTest.java
(from r1182710, jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CommitMessageTest.java)
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CommitInfoTest.java?p2=jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CommitInfoTest.java&p1=jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CommitMessageTest.java&r1=1182710&r2=1182978&rev=1182978&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CommitMessageTest.java
(original)
+++ jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/CommitInfoTest.java
Thu Oct 13 16:57:10 2011
@@ -4,45 +4,32 @@ import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
 
-public class CommitMessageTest {
+public class CommitInfoTest {
 
     @Test
     public void testEncode() {
-        CommitMessage msg = new CommitMessage("user", "data");
+        CommitInfo msg = new CommitInfo().setUserId("user").setUserData("data").setSessionId("session");
         assertEquals("user", msg.getUserId());
         assertEquals("data", msg.getUserData());
-        assertEquals("user:data", msg.encode());
-        assertEquals(msg, CommitMessage.parse(msg.encode()));
+        assertEquals("session", msg.getSessionId());
+        assertEquals(msg, CommitInfo.parse(msg.toJson()));
 
-        msg = new CommitMessage("us:er", "data");
-        assertEquals("us:er", msg.getUserId());
-        assertEquals("data", msg.getUserData());
-        assertEquals("us\\:er:data", msg.encode());
-        assertEquals(msg, CommitMessage.parse(msg.encode()));
-
-        msg = new CommitMessage("", "data");
+        msg = new CommitInfo().setUserId("").setUserData("data").setSessionId("session");
         assertEquals("", msg.getUserId());
         assertEquals("data", msg.getUserData());
-        assertEquals(":data", msg.encode());
-        assertEquals(msg, CommitMessage.parse(msg.encode()));
+        assertEquals("session", msg.getSessionId());
+        assertEquals(msg, CommitInfo.parse(msg.toJson()));
 
-        msg = new CommitMessage("user", "");
+        msg = new CommitInfo().setUserId("user").setUserData("").setSessionId("session");
         assertEquals("user", msg.getUserId());
         assertEquals("", msg.getUserData());
-        assertEquals("user:", msg.encode());
-        assertEquals(msg, CommitMessage.parse(msg.encode()));
+        assertEquals("session", msg.getSessionId());
+        assertEquals(msg, CommitInfo.parse(msg.toJson()));
 
-        msg = new CommitMessage("", "");
+        msg = new CommitInfo().setUserId("").setUserData("").setSessionId("session");
         assertEquals("", msg.getUserId());
         assertEquals("", msg.getUserData());
-        assertEquals(":", msg.encode());
-        assertEquals(msg, CommitMessage.parse(msg.encode()));
-
-        msg = new CommitMessage(":", ":");
-        assertEquals(":", msg.getUserId());
-        assertEquals(":", msg.getUserData());
-        assertEquals("\\::\\:", msg.encode());
-        assertEquals(msg, CommitMessage.parse(msg.encode()));
-
+        assertEquals("session", msg.getSessionId());
+        assertEquals(msg, CommitInfo.parse(msg.toJson()));
     }
 }



Mime
View raw message