You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by th...@apache.org on 2011/10/12 12:34:19 UTC

svn commit: r1182304 - in /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk: MicroKernelFactory.java mem/MemoryKernelImpl.java wrapper/ wrapper/LogWrapper.java

Author: thomasm
Date: Wed Oct 12 10:34:19 2011
New Revision: 1182304

URL: http://svn.apache.org/viewvc?rev=1182304&view=rev
Log:
A logging microkernel implementation (URL prefix "log:")

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/wrapper/
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/wrapper/LogWrapper.java
Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelFactory.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelFactory.java?rev=1182304&r1=1182303&r2=1182304&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelFactory.java (original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelFactory.java Wed Oct 12 10:34:19 2011
@@ -25,6 +25,7 @@ import org.apache.jackrabbit.mk.client.C
 import org.apache.jackrabbit.mk.fs.FileUtils;
 import org.apache.jackrabbit.mk.mem.MemoryKernelImpl;
 import org.apache.jackrabbit.mk.util.ExceptionFactory;
+import org.apache.jackrabbit.mk.wrapper.LogWrapper;
 
 /**
  * A factory to create a MicroKernel instance.
@@ -46,6 +47,8 @@ public class MicroKernelFactory {
     public static MicroKernel getInstance(String url) {
         if (url.startsWith("mem:")) {
             return MemoryKernelImpl.get(url);
+        } else if (url.startsWith("log:")) {
+            return LogWrapper.get(url);
         } else if (url.startsWith("fs:")) {
             boolean clean = false;
             if (url.endsWith(";clean")) {

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java?rev=1182304&r1=1182303&r2=1182304&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java (original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryKernelImpl.java Wed Oct 12 10:34:19 2011
@@ -61,7 +61,6 @@ Node structure:
  */
 public class MemoryKernelImpl implements MicroKernel {
 
-    private static final boolean DEBUG = false;
     private static final int MAX_REVISIONS_PER_NODE = 100;
     private static final HashMap<String, MemoryKernelImpl> INSTANCES = new HashMap<String, MemoryKernelImpl>();
 
@@ -77,9 +76,6 @@ public class MemoryKernelImpl implements
     private boolean disposed;
 
     private MemoryKernelImpl(String name) {
-        if (DEBUG) {
-            log("open " + name);
-        }
         this.name = name;
         boolean startServer = false;
         if (name.startsWith("server:")) {
@@ -178,15 +174,9 @@ public class MemoryKernelImpl implements
         // TODO possibly support two phase commit
         // TODO getJournal and getRevision don't have a path,
         // which means we can't implement access rights using path prefixes
-        if (DEBUG) {
-            log("commit " + rootPath + " " + jsonDiff + " " + message);
-        }
         try {
             return doCommit(rootPath, jsonDiff, revisionId, message);
         } catch (Exception e) {
-            if (DEBUG) {
-                log("commit failed with exception: " + e);
-            }
             throw ExceptionFactory.convert(e);
         }
     }
@@ -326,9 +316,6 @@ public class MemoryKernelImpl implements
                 throw ExceptionFactory.get("token: " + (char) t.getTokenType());
             }
         }
-        if (DEBUG) {
-            log(diff.toString());
-        }
         head = head.setChild("data", data, rev);
         Revision revNode = new Revision(rev, clock.time(), diff.toString(), message);
         revisionCache.put(rev, revNode);
@@ -357,9 +344,6 @@ public class MemoryKernelImpl implements
     }
 
     public String getRevisions(long since, int maxEntries) {
-        if (DEBUG) {
-            log("getRevisions " + since);
-        }
         NodeImpl node = getRoot();
         ArrayList<Revision> revisions = new ArrayList<Revision>();
         Revision r = Revision.get(node.getNode("head"));
@@ -385,11 +369,7 @@ public class MemoryKernelImpl implements
                 buff.encodedValue(rev.toString());
             }
         }
-        String result = buff.endArray().toString();
-        if (DEBUG) {
-            log("getRevisions returned " + getHead(result));
-        }
-        return result;
+        return buff.endArray().toString();
     }
 
     public String waitForCommit(String oldHeadRevision, long maxWaitMillis) throws InterruptedException {
@@ -397,9 +377,6 @@ public class MemoryKernelImpl implements
     }
 
     public String getJournal(String fromRevisionId, String toRevisionId) {
-        if (DEBUG) {
-            log("getJournal " + fromRevisionId + " " + toRevisionId);
-        }
     	String revRange = fromRevisionId + "-" + toRevisionId;
     	synchronized (this) {
     	    if (revRange.equals(lastJournalRevRange)) {
@@ -435,16 +412,9 @@ public class MemoryKernelImpl implements
             lastJournalRevRange = revRange;
             lastJournal = buff.endArray().toString();
         }
-        if (DEBUG) {
-            log("getJournal returned " + getHead(lastJournal));
-        }
         return lastJournal;
     }
 
-    private String getHead(String s) {
-        return s.length() < 100 ? s : (s.substring(0, 100) + "...");
-    }
-
     /**
      * Get the nodes. The following prefixes are supported:
      * <ul><li>:pretty - beautify (format) the result
@@ -457,28 +427,10 @@ public class MemoryKernelImpl implements
      * @return the json string
      */
     public String getNodes(String path, String revisionId) {
-        if (DEBUG) {
-            log("getNodes " + path + " revision:" + revisionId);
-        }
-        String result = doGetNodes(path, revisionId, 1, 0, -1);
-        if (DEBUG) {
-            log("getNodes returned " + getHead(result));
-        }
-        return result;
+        return getNodes(path, revisionId, 1, 0, -1);
     }
 
     public String getNodes(String path, String revisionId, int depth, long offset, int count) {
-        if (DEBUG) {
-            log("getNodes " + path + " revision:" + revisionId + " depth:" + depth + " offset:" + offset + " count:" + count);
-        }
-        String result = doGetNodes(path, revisionId, depth, offset, count);
-        if (DEBUG) {
-            log("getNodes returned " + getHead(result));
-        }
-        return result;
-    }
-
-    private String doGetNodes(String path, String revisionId, int depth, long offset, int count) {
         if (count < 0) {
             count = nodeMap.getMaxMemoryChildren();
         }
@@ -489,7 +441,7 @@ public class MemoryKernelImpl implements
         if (path.startsWith("/:")) {
             if (path.startsWith("/:pretty")) {
                 return JsopBuilder.prettyPrint(
-                        doGetNodes("/" + PathUtils.relativize("/:pretty", path),
+                        getNodes("/" + PathUtils.relativize("/:pretty", path),
                                 revisionId, depth, offset, count));
             } else if (path.startsWith("/:root")) {
                 n = getRoot().getNode(PathUtils.relativize("/:root", path));
@@ -518,9 +470,6 @@ public class MemoryKernelImpl implements
         if (revisionId.equals(headRev)) {
             node = head;
         } else {
-            if (DEBUG) {
-                log("getRevision " + revisionId);
-            }
             while (true) {
                 if (!node.exists("old")) {
                     throw ExceptionFactory.get("Revision not found: " + revisionId);
@@ -536,45 +485,26 @@ public class MemoryKernelImpl implements
     }
 
     public boolean nodeExists(String path, String revisionId) {
-        if (DEBUG) {
-            log("nodeExists " + path + " revision:" + revisionId);
-        }
         if (!PathUtils.isAbsolute(path)) {
             throw ExceptionFactory.get("Not an absolute path: " + path);
         }
         // TODO possibly use a cache / a bloom filter
-        boolean result = getRevision(revisionId).exists(path.substring(1));
-        if (DEBUG) {
-            log("nodeExists returned " + result);
-        }
-        return result;
+        return getRevision(revisionId).exists(path.substring(1));
     }
 
     public long getLength(String blobId) {
-        if (DEBUG) {
-            log("getLength " + blobId);
-        }
         return ds.getBlobLength(blobId);
     }
 
     public int read(String blobId, long pos, byte[] buff, int off, int length) {
-        if (DEBUG) {
-            log("read " + blobId);
-        }
         return ds.readBlob(blobId, pos, buff, off, length);
     }
 
     public String write(InputStream in) {
-        if (DEBUG) {
-            log("write " + in);
-        }
         return ds.writeBlob(in);
     }
 
     public synchronized void dispose() {
-        if (DEBUG) {
-            log("dispose");
-        }
         if (!disposed) {
             disposed = true;
             gate.commit("end");
@@ -587,10 +517,4 @@ public class MemoryKernelImpl implements
         }
     }
 
-    private static void log(String s) {
-        if (DEBUG) {
-            System.out.println(s);
-        }
-    }
-
 }

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/wrapper/LogWrapper.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/wrapper/LogWrapper.java?rev=1182304&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/wrapper/LogWrapper.java (added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/wrapper/LogWrapper.java Wed Oct 12 10:34:19 2011
@@ -0,0 +1,243 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.mk.wrapper;
+
+import java.io.InputStream;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.jackrabbit.mk.MicroKernelFactory;
+import org.apache.jackrabbit.mk.api.MicroKernel;
+import org.apache.jackrabbit.mk.json.JsopBuilder;
+import org.apache.jackrabbit.mk.util.ExceptionFactory;
+
+/**
+ * A logging microkernel implementation.
+ */
+public class LogWrapper implements MicroKernel {
+
+    private static final boolean DEBUG = Boolean.parseBoolean(System.getProperty("mk.debug", "true"));
+    private static final AtomicInteger NEXT_ID = new AtomicInteger();
+
+    private final MicroKernel mk;
+    private final int id = NEXT_ID.getAndIncrement();
+
+    public LogWrapper(MicroKernel mk) {
+        this.mk = mk;
+    }
+
+    public static synchronized LogWrapper get(String url) {
+        String u = url.substring("log:".length());
+        LogWrapper w = new LogWrapper(MicroKernelFactory.getInstance(u));
+        w.log("MicroKernel mk" + w.id + " = MicroKernelFactory.getInstance("
+                + JsopBuilder.encode(u) + ");");
+        return w;
+    }
+
+    public String commit(String path, String jsonDiff, String revisionId, String message) {
+        try {
+            logMethod("commit", path, jsonDiff, revisionId, message);
+            String result = mk.commit(path, jsonDiff, revisionId, message);
+            logResult(result);
+            return result;
+        } catch (Exception e) {
+            logException(e);
+            throw convert(e);
+        }
+    }
+
+    public void dispose() {
+        try {
+            logMethod("dispose");
+            mk.dispose();
+            logResult();
+        } catch (Exception e) {
+            logException(e);
+            throw convert(e);
+        }
+    }
+
+    public String getHeadRevision() {
+        try {
+            logMethod("getHeadRevision");
+            String result = mk.getHeadRevision();
+            logResult(result);
+            return result;
+        } catch (Exception e) {
+            logException(e);
+            throw convert(e);
+        }
+    }
+
+    public String getJournal(String fromRevisionId, String toRevisionId) {
+        try {
+            logMethod("getJournal", fromRevisionId, toRevisionId);
+            String result = mk.getJournal(fromRevisionId, toRevisionId);
+            logResult(result);
+            return result;
+        } catch (Exception e) {
+            logException(e);
+            throw convert(e);
+        }
+    }
+
+    public long getLength(String blobId) {
+        try {
+            logMethod("getLength", blobId);
+            long result = mk.getLength(blobId);
+            logResult(Long.toString(result));
+            return result;
+        } catch (Exception e) {
+            logException(e);
+            throw convert(e);
+        }
+    }
+
+    public String getNodes(String path, String revisionId) {
+        try {
+            logMethod("getNodes", path, revisionId);
+            String result = mk.getNodes(path, revisionId);
+            logResult(result);
+            return result;
+        } catch (Exception e) {
+            logException(e);
+            throw convert(e);
+        }
+    }
+
+    public String getNodes(String path, String revisionId, int depth, long offset, int count) {
+        try {
+            logMethod("getNodes", path, revisionId, depth, offset, count);
+            String result = mk.getNodes(path, revisionId, depth, offset, count);
+            logResult(result);
+            return result;
+        } catch (Exception e) {
+            logException(e);
+            throw convert(e);
+        }
+    }
+
+    public String getRevisions(long since, int maxEntries) {
+        try {
+            logMethod("getRevisions", since, maxEntries);
+            String result = mk.getRevisions(since, maxEntries);
+            logResult(result);
+            return result;
+        } catch (Exception e) {
+            logException(e);
+            throw convert(e);
+        }
+    }
+
+    public boolean nodeExists(String path, String revisionId) {
+        try {
+            logMethod("nodeExists", path, revisionId);
+            boolean result = mk.nodeExists(path, revisionId);
+            logResult(Boolean.toString(result));
+            return result;
+        } catch (Exception e) {
+            logException(e);
+            throw convert(e);
+        }
+    }
+
+    public int read(String blobId, long pos, byte[] buff, int off, int length) {
+        try {
+            logMethod("read", blobId, pos, buff, off, length);
+            int result = mk.read(blobId, pos, buff, off, length);
+            logResult(Integer.toString(result));
+            return result;
+        } catch (Exception e) {
+            logException(e);
+            throw convert(e);
+        }
+    }
+
+    public String waitForCommit(String oldHeadRevision, long maxWaitMillis) throws InterruptedException {
+        try {
+            logMethod("waitForCommit", oldHeadRevision, maxWaitMillis);
+            String result = mk.waitForCommit(oldHeadRevision, maxWaitMillis);
+            logResult(result);
+            return result;
+        } catch (InterruptedException e) {
+            logException(e);
+            throw e;
+        } catch (Exception e) {
+            logException(e);
+            throw convert(e);
+        }
+    }
+
+    public String write(InputStream in) {
+        try {
+            logMethod("write", in.toString());
+            String result = mk.write(in);
+            logResult(result);
+            return result;
+        } catch (Exception e) {
+            logException(e);
+            throw convert(e);
+        }
+    }
+
+    private void logMethod(String methodName, Object... args) {
+        StringBuilder buff = new StringBuilder("mk");
+        buff.append(id).append('.').append(methodName).append('(');
+        for (int i = 0; i < args.length; i++) {
+            if (i > 0) {
+                buff.append(", ");
+            }
+            buff.append(quote(args[i]));
+        }
+        buff.append(");");
+        log(buff.toString());
+    }
+
+    public static String quote(Object o) {
+        if (o == null) {
+            return "null";
+        } else if (o instanceof String) {
+            return JsopBuilder.encode((String) o);
+        }
+        return o.toString();
+    }
+
+    private RuntimeException convert(Exception e) {
+        if (e instanceof RuntimeException) {
+            return (RuntimeException) e;
+        }
+        log("// unexpected exception type: " + e.getClass().getName());
+        return ExceptionFactory.convert(e);
+    }
+
+    private void logException(Exception e) {
+        log("// exception: " + e.toString());
+    }
+
+    private void logResult(Object result) {
+        log("// " + quote(result));
+    }
+
+    private void logResult() {
+        // ignored
+    }
+
+    private void log(String message) {
+        if (DEBUG) {
+            System.out.println(message);
+        }
+    }
+
+}