Author: jbonofre
Date: Sun Dec 19 05:33:26 2010
New Revision: 1050762

URL: http://svn.apache.org/viewvc?rev=1050762&view=rev
Log:
[SM-1974] Store should provide a method to load an object without remove (peek).

Modified:
    servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/Store.java
    
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/jdbc/JdbcStore.java
    
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/memory/MemoryStore.java
    
servicemix/utils/trunk/src/test/java/org/apache/servicemix/store/jdbc/JdbcStoreTransactionalTest.java

Modified: 
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/Store.java
URL: 
http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/Store.java?rev=1050762&r1=1050761&r2=1050762&view=diff
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/Store.java 
(original)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/Store.java 
Sun Dec 19 05:33:26 2010
@@ -73,5 +73,15 @@ public interface Store {
      * @throws IOException if an error occurs
      */
     Object load(String id) throws IOException;
+
+    /**
+     * Loads an object that has been previously stored under the specified key.
+     * The object is not removed from the store.
+     * 
+     * @param id the id of the object
+     * @return the object, or <code>null</code> if the object could not be 
found
+     * @throws IOException if an error occurs
+     */
+    Object peek(String id) throws IOException;
     
 }

Modified: 
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/jdbc/JdbcStore.java
URL: 
http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/jdbc/JdbcStore.java?rev=1050762&r1=1050761&r2=1050762&view=diff
==============================================================================
--- 
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/jdbc/JdbcStore.java
 (original)
+++ 
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/jdbc/JdbcStore.java
 Sun Dec 19 05:33:26 2010
@@ -82,12 +82,31 @@ public class JdbcStore implements Store 
             }
             return result;
         } catch (Exception e) {
-            throw (IOException) new IOException("Error storing 
object").initCause(e);
+            throw (IOException) new IOException("Error loading 
object").initCause(e);
         } finally {
             close(connection);
         }
     }
 
+    public Object peek(String id) throws IOException {
+        LOG.debug("Peeking object with id: " + id);
+        Connection connection = null;
+        try {
+            connection = factory.getDataSource().getConnection();
+            byte[] data = factory.getAdapter().doLoadData(connection, name + 
":" + id);
+            Object result = null;
+            if (data != null) {
+                ObjectInputStream ois = new ObjectInputStream(new 
ByteArrayInputStream(data));
+                result = ois.readObject();
+            }
+            return result;
+        } catch (Exception e) {
+            throw (IOException) new IOException("Error loading 
object").initCause(e);
+        } finally {
+            close(connection);   
+        }
+    }
+
     protected void close(Connection connection) throws IOException {
         if (connection != null) {
             try {

Modified: 
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/memory/MemoryStore.java
URL: 
http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/memory/MemoryStore.java?rev=1050762&r1=1050761&r2=1050762&view=diff
==============================================================================
--- 
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/memory/MemoryStore.java
 (original)
+++ 
servicemix/utils/trunk/src/main/java/org/apache/servicemix/store/memory/MemoryStore.java
 Sun Dec 19 05:33:26 2010
@@ -59,8 +59,13 @@ public class MemoryStore implements Stor
     }
 
     public Object load(String id) throws IOException {
-        LOG.debug("Loading object with id: " + id);
+        LOG.debug("Loading/Removing object with id: " + id);
         return datas.remove(id);
     }
 
+    public Object peek(String id) throws IOException {
+        LOG.debug("Peeking object with id: " + id);
+        return datas.get(id);
+    }
+
 }

Modified: 
servicemix/utils/trunk/src/test/java/org/apache/servicemix/store/jdbc/JdbcStoreTransactionalTest.java
URL: 
http://svn.apache.org/viewvc/servicemix/utils/trunk/src/test/java/org/apache/servicemix/store/jdbc/JdbcStoreTransactionalTest.java?rev=1050762&r1=1050761&r2=1050762&view=diff
==============================================================================
--- 
servicemix/utils/trunk/src/test/java/org/apache/servicemix/store/jdbc/JdbcStoreTransactionalTest.java
 (original)
+++ 
servicemix/utils/trunk/src/test/java/org/apache/servicemix/store/jdbc/JdbcStoreTransactionalTest.java
 Sun Dec 19 05:33:26 2010
@@ -72,6 +72,15 @@ public class JdbcStoreTransactionalTest 
         assertNull(store.load("a"));
     }
 
+    public void testStoreAndPeek() throws Exception {
+        Store store = factory.open("store");
+        String id = store.store(new Integer(10));
+        Integer i = (Integer) store.peek(id);
+        assertEquals(10, i.intValue());
+        assertNotNull(store.peek(id));
+        assertNull(store.load("a"));
+    }
+
     public void testStoreAndLoadInOneTx() throws Exception {
         Store store = factory.open("store");
         tm.begin();


Reply via email to