jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1166064 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/blobs/ main/java/org/apache/jackrabbit/mk/util/ test/java/org/apache/jackrabbit/mk/blobs/
Date Wed, 07 Sep 2011 08:51:34 GMT
Author: thomasm
Date: Wed Sep  7 08:51:33 2011
New Revision: 1166064

URL: http://svn.apache.org/viewvc?rev=1166064&view=rev
Log:
Use MicroKernelException where possible.

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/blobs/AbstractBlobStore.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/ExceptionFactory.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/blobs/DataStoreTest.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/blobs/DbBlobStoreTest.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/blobs/AbstractBlobStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/blobs/AbstractBlobStore.java?rev=1166064&r1=1166063&r2=1166064&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/blobs/AbstractBlobStore.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/blobs/AbstractBlobStore.java
Wed Sep  7 08:51:33 2011
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.mk.blobs;
 
+import org.apache.jackrabbit.mk.util.ExceptionFactory;
 import org.apache.jackrabbit.mk.util.IOUtils;
 import org.apache.jackrabbit.mk.util.StringUtils;
 import org.h2.util.SmallLRUCache;
@@ -82,12 +83,16 @@ public abstract class AbstractBlobStore 
         this.blockSize = x;
     }
 
-    public String writeBlob(InputStream in) throws Exception {
-        ByteArrayOutputStream idStream = new ByteArrayOutputStream();
-        convertBlobToId(in, idStream, 0, 0);
-        byte[] id = idStream.toByteArray();
-        // System.out.println("    write blob " +  StringUtils.convertBytesToHex(id));
-        return StringUtils.convertBytesToHex(id);
+    public String writeBlob(InputStream in) {
+        try {
+            ByteArrayOutputStream idStream = new ByteArrayOutputStream();
+            convertBlobToId(in, idStream, 0, 0);
+            byte[] id = idStream.toByteArray();
+            // System.out.println("    write blob " +  StringUtils.convertBytesToHex(id));
+            return StringUtils.convertBytesToHex(id);
+        } catch (Exception e) {
+            throw ExceptionFactory.convert(e);
+        }
     }
 
     private void convertBlobToId(InputStream in, ByteArrayOutputStream idStream, int level,
long totalLength) throws Exception {
@@ -143,50 +148,54 @@ public abstract class AbstractBlobStore 
 
     protected abstract void storeBlock(byte[] blockId, int level, byte[] data) throws Exception;
 
-    public int readBlob(String blobId, long pos, byte[] buff, int off, int length) throws
Exception {
-        byte[] id = StringUtils.convertHexToBytes(blobId);
-        ByteArrayInputStream idStream = new ByteArrayInputStream(id);
-        while (true) {
-            int type = idStream.read();
-            if (type == -1) {
-                return -1;
-            } else if (type == TYPE_DATA) {
-                int len = IOUtils.readVarInt(idStream);
-                if (pos < len) {
-                    IOUtils.skipFully(idStream, (int) pos);
-                    len -= pos;
-                    if (length < len) {
-                        len = length;
+    public int readBlob(String blobId, long pos, byte[] buff, int off, int length) {
+        try {
+            byte[] id = StringUtils.convertHexToBytes(blobId);
+            ByteArrayInputStream idStream = new ByteArrayInputStream(id);
+            while (true) {
+                int type = idStream.read();
+                if (type == -1) {
+                    return -1;
+                } else if (type == TYPE_DATA) {
+                    int len = IOUtils.readVarInt(idStream);
+                    if (pos < len) {
+                        IOUtils.skipFully(idStream, (int) pos);
+                        len -= pos;
+                        if (length < len) {
+                            len = length;
+                        }
+                        IOUtils.readFully(idStream, buff, off, len);
+                        return len;
                     }
-                    IOUtils.readFully(idStream, buff, off, len);
-                    return len;
-                }
-                IOUtils.skipFully(idStream, len);
-                pos -= len;
-            } else if (type == TYPE_HASH) {
-                int level = IOUtils.readVarInt(idStream);
-                long totalLength = IOUtils.readVarLong(idStream);
-                if (level > 0) {
-                    // block length (ignored)
-                    IOUtils.readVarLong(idStream);
-                }
-                byte[] digest = new byte[IOUtils.readVarInt(idStream)];
-                IOUtils.readFully(idStream, digest, 0, digest.length);
-                if (pos >= totalLength) {
-                    pos -= totalLength;
-                } else {
-                    byte[] block = readBlock(digest);
+                    IOUtils.skipFully(idStream, len);
+                    pos -= len;
+                } else if (type == TYPE_HASH) {
+                    int level = IOUtils.readVarInt(idStream);
+                    long totalLength = IOUtils.readVarLong(idStream);
                     if (level > 0) {
-                        idStream = new ByteArrayInputStream(block);
+                        // block length (ignored)
+                        IOUtils.readVarLong(idStream);
+                    }
+                    byte[] digest = new byte[IOUtils.readVarInt(idStream)];
+                    IOUtils.readFully(idStream, digest, 0, digest.length);
+                    if (pos >= totalLength) {
+                        pos -= totalLength;
                     } else {
-                        ByteArrayInputStream in = new ByteArrayInputStream(block);
-                        IOUtils.skipFully(in, (int) pos);
-                        return IOUtils.readFully(in, buff, off, length);
+                        byte[] block = readBlock(digest);
+                        if (level > 0) {
+                            idStream = new ByteArrayInputStream(block);
+                        } else {
+                            ByteArrayInputStream in = new ByteArrayInputStream(block);
+                            IOUtils.skipFully(in, (int) pos);
+                            return IOUtils.readFully(in, buff, off, length);
+                        }
                     }
+                } else {
+                    throw new IOException("Unknown blobs id type " + type + " for blob "
+ blobId);
                 }
-            } else {
-                throw new IOException("Unknown blobs id type " + type + " for blob " + blobId);
             }
+        } catch (Exception e) {
+            throw ExceptionFactory.convert(e);
         }
     }
 
@@ -202,33 +211,37 @@ public abstract class AbstractBlobStore 
 
     protected abstract byte[] readBlockFromBackend(byte[] blockId) throws Exception;
 
-    public long getBlobLength(String blobId) throws IOException {
-        byte[] id = StringUtils.convertHexToBytes(blobId);
-        ByteArrayInputStream idStream = new ByteArrayInputStream(id);
-        long totalLength = 0;
-        while (true) {
-            int type = idStream.read();
-            if (type == -1) {
-                break;
-            }
-            if (type == TYPE_DATA) {
-                int len = IOUtils.readVarInt(idStream);
-                IOUtils.skipFully(idStream, len);
-                totalLength += len;
-            } else if (type == TYPE_HASH) {
-                int level = IOUtils.readVarInt(idStream);
-                totalLength += IOUtils.readVarLong(idStream);
-                if (level > 0) {
-                    // block length (ignored)
-                    IOUtils.readVarLong(idStream);
+    public long getBlobLength(String blobId) {
+        try {
+            byte[] id = StringUtils.convertHexToBytes(blobId);
+            ByteArrayInputStream idStream = new ByteArrayInputStream(id);
+            long totalLength = 0;
+            while (true) {
+                int type = idStream.read();
+                if (type == -1) {
+                    break;
+                }
+                if (type == TYPE_DATA) {
+                    int len = IOUtils.readVarInt(idStream);
+                    IOUtils.skipFully(idStream, len);
+                    totalLength += len;
+                } else if (type == TYPE_HASH) {
+                    int level = IOUtils.readVarInt(idStream);
+                    totalLength += IOUtils.readVarLong(idStream);
+                    if (level > 0) {
+                        // block length (ignored)
+                        IOUtils.readVarLong(idStream);
+                    }
+                    int digestLength = IOUtils.readVarInt(idStream);
+                    IOUtils.skipFully(idStream, digestLength);
+                } else {
+                    throw new IOException("Datastore id type " + type + " for blob " + blobId);
                 }
-                int digestLength = IOUtils.readVarInt(idStream);
-                IOUtils.skipFully(idStream, digestLength);
-            } else {
-                throw new IOException("Datastore id type " + type + " for blob " + blobId);
             }
+            return totalLength;
+        } catch (IOException e) {
+            throw ExceptionFactory.convert(e);
         }
-        return totalLength;
     }
 
     public abstract void clear();

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/ExceptionFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/ExceptionFactory.java?rev=1166064&r1=1166063&r2=1166064&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/ExceptionFactory.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/ExceptionFactory.java
Wed Sep  7 08:51:33 2011
@@ -17,6 +17,7 @@
 package org.apache.jackrabbit.mk.util;
 
 import org.apache.jackrabbit.mk.Constants;
+import org.apache.jackrabbit.mk.api.MicroKernelException;
 
 import java.text.MessageFormat;
 import java.util.ConcurrentModificationException;
@@ -34,6 +35,16 @@ public class ExceptionFactory {
 
     static final HashMap<Integer, String> MESSAGE_MAP = new HashMap<Integer, String>();
 
+    public static MicroKernelException convert(Exception e) {
+        if (e instanceof MicroKernelException) {
+            return (MicroKernelException) e;
+        }
+        return new MicroKernelException(e.getMessage(), e);
+    }
+
+    public static MicroKernelException get(String s) {
+        return new MicroKernelException(s);
+    }
 
     static {
         MESSAGE_MAP.put(CLOSED_0, "The repository is closed");

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/blobs/DataStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/blobs/DataStoreTest.java?rev=1166064&r1=1166063&r2=1166064&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/blobs/DataStoreTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/blobs/DataStoreTest.java
Wed Sep  7 08:51:33 2011
@@ -33,7 +33,7 @@ import java.util.Random;
 public class DataStoreTest extends TestCase {
 
     // private static final String URL = "fs:{homeDir}/target;clean";
-    private static final String URL = "mem:fs:target/temp";
+    private static final String URL = "mem:fs:target/temp;clean";
     // private static final String URL = "mem:";
 
     private MicroKernel mk;

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/blobs/DbBlobStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/blobs/DbBlobStoreTest.java?rev=1166064&r1=1166063&r2=1166064&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/blobs/DbBlobStoreTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/blobs/DbBlobStoreTest.java
Wed Sep  7 08:51:33 2011
@@ -17,6 +17,7 @@
 package org.apache.jackrabbit.mk.blobs;
 
 import junit.framework.TestCase;
+import org.apache.jackrabbit.mk.api.MicroKernelException;
 import org.apache.jackrabbit.mk.json.JsopBuilder;
 import org.apache.jackrabbit.mk.json.JsopTokenizer;
 import org.apache.jackrabbit.mk.util.IOUtilsTest;
@@ -91,13 +92,13 @@ public class DbBlobStoreTest extends Tes
         try {
             store.readBlob("ff", 0, data, 0, 1);
             fail();
-        } catch (IOException e) {
+        } catch (MicroKernelException e) {
             // expected
         }
         try {
             store.getBlobLength("ff");
             fail();
-        } catch (IOException e) {
+        } catch (MicroKernelException e) {
             // expected
         }
     }



Mime
View raw message