geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgenen...@apache.org
Subject svn commit: r450195 - in /geronimo/sandbox/gcache/server/src: main/java/org/apache/geronimo/gcache/command/ main/java/org/apache/geronimo/gcache/server/listeners/ main/java/org/apache/geronimo/gcache/util/ test/java/org/apache/geronimo/gcache/command/
Date Tue, 26 Sep 2006 20:43:53 GMT
Author: jgenender
Date: Tue Sep 26 13:43:52 2006
New Revision: 450195

URL: http://svn.apache.org/viewvc?view=rev&rev=450195
Log:
Add new commands and cache tests

Added:
    geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BulkSendCommand.java
  (with props)
    geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/GetCacheCommand.java
  (with props)
    geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/PutSessionCommand.java
  (with props)
    geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/RemoveSessionCommand.java
  (with props)
    geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/BulkSendCommandTest.java
  (with props)
    geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/PutSessionCommandTest.java
  (with props)
    geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/RemoveSessionCommandTest.java
  (with props)
Modified:
    geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BaseCommand.java
    geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/ClearCacheCommand.java
    geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/Command.java
    geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/CommandTypes.java
    geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/MessageAckCommand.java
    geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/PutEntryCommand.java
    geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/RemoveEntryCommand.java
    geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/server/listeners/CacheNotifier.java
    geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/server/listeners/DefaultCacheNotifier.java
    geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArray.java

Modified: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BaseCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BaseCommand.java?view=diff&rev=450195&r1=450194&r2=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BaseCommand.java
(original)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BaseCommand.java
Tue Sep 26 13:43:52 2006
@@ -50,7 +50,7 @@
     }
 
 
-    public void execute(CacheInfoHolder info) {
+    public void execute(CacheInfoHolder info) throws IOException{
         // nothing to do in the base
     }
 
@@ -88,6 +88,31 @@
         } catch (ClassNotFoundException e) {
             throw new IOException(e.getMessage());
         }
+    }
+
+    protected int readInt(ReadableByteChannel channel) throws IOException {
+        ByteBuffer buffer = ByteBuffer.allocateDirect(4);
+        int read = channel.read(buffer);
+        int intValue = 0;
+        if (read == 4) {
+            buffer.flip();
+            intValue = buffer.getInt();
+        } else {
+            throw new IOException("Could not read identifier length from the channel");
+        }
+
+        return intValue;
+    }
+
+    protected void writeInt(WritableByteChannel channel, int intValue) throws IOException
{
+
+        // first the length of the string
+        ByteBuffer buffer = ByteBuffer.allocateDirect(4);
+        buffer.putInt(intValue);
+
+        buffer.flip();
+        channel.write(buffer);
+
     }
 
     protected long readLong(ReadableByteChannel channel) throws IOException {

Added: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BulkSendCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BulkSendCommand.java?view=auto&rev=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BulkSendCommand.java
(added)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BulkSendCommand.java
Tue Sep 26 13:43:52 2006
@@ -0,0 +1,55 @@
+/*
+ * 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.geronimo.gcache.command;
+
+import org.apache.geronimo.gcache.CacheInfoHolder;
+
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
+import java.io.IOException;
+
+public class BulkSendCommand extends BaseCommand{
+
+    private int numberOfCommands;
+
+    public int getNumberOfCommands() {
+        return numberOfCommands;
+    }
+
+    public void setNumberOfCommands(int numberOfCommands) {
+        this.numberOfCommands = numberOfCommands;
+    }
+
+
+    public void readExternal(ReadableByteChannel channel) throws IOException {
+        super.readExternal(channel);
+
+        numberOfCommands = readInt(channel);
+    }
+
+    public void writeExternal(WritableByteChannel channel) throws IOException {
+        super.writeExternal(channel);
+
+        writeInt(channel, numberOfCommands);
+    }
+
+    public void execute(CacheInfoHolder info) throws IOException{
+       //Nothing to execute, this is just an outbound header
+    }
+}

Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BulkSendCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BulkSendCommand.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/BulkSendCommand.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/ClearCacheCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/ClearCacheCommand.java?view=diff&rev=450195&r1=450194&r2=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/ClearCacheCommand.java
(original)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/ClearCacheCommand.java
Tue Sep 26 13:43:52 2006
@@ -21,8 +21,11 @@
 import net.sf.ehcache.Cache;
 import org.apache.geronimo.gcache.CacheInfoHolder;
 
-public class ClearCacheCommand extends CacheBaseCommand{
-        public void execute(CacheInfoHolder info) {
+import java.io.IOException;
+
+public class ClearCacheCommand extends CacheBaseCommand {
+
+    public void execute(CacheInfoHolder info) throws IOException {
         Cache cache = info.getCache(getCacheName(), true);
         cache.removeAll();
 

Modified: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/Command.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/Command.java?view=diff&rev=450195&r1=450194&r2=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/Command.java
(original)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/Command.java
Tue Sep 26 13:43:52 2006
@@ -21,6 +21,8 @@
 import org.apache.geronimo.gcache.marshal.MarshalAware;
 import org.apache.geronimo.gcache.CacheInfoHolder;
 
+import java.io.IOException;
+
 /**
  * Commands are pushed around the cluster to the various nodes and they are
  * invoked upon reciept.
@@ -28,5 +30,5 @@
 public interface Command extends MarshalAware {
     long getCommandId();
 
-    void execute(CacheInfoHolder info);
+    void execute(CacheInfoHolder info) throws IOException;
 }

Modified: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/CommandTypes.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/CommandTypes.java?view=diff&rev=450195&r1=450194&r2=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/CommandTypes.java
(original)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/CommandTypes.java
Tue Sep 26 13:43:52 2006
@@ -5,6 +5,9 @@
     public static final int REMOVE_ENTRY_COMMAND = 2;
     public static final int CLEARCACHE_COMMAND = 3;
     public static final int MESSAGE_ACK_COMMAND = 4;
+    public static final int BULK_SEND_COMMAND = 5;
+    public static final int REMOVE_SESSION_COMMAND = 6;
+    public static final int PUT_SESSION_COMMAND = 7;
 
     public static Command createCommand(int identifier) {
         Command command = null;
@@ -20,6 +23,15 @@
                 break;
             case(MESSAGE_ACK_COMMAND):
                 command = new MessageAckCommand();
+                break;
+            case(BULK_SEND_COMMAND):
+                command = new BulkSendCommand();
+                break;
+            case(REMOVE_SESSION_COMMAND):
+                command = new RemoveSessionCommand();
+                break;
+            case(PUT_SESSION_COMMAND):
+                command = new PutSessionCommand();
                 break;
         }
         return command;

Added: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/GetCacheCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/GetCacheCommand.java?view=auto&rev=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/GetCacheCommand.java
(added)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/GetCacheCommand.java
Tue Sep 26 13:43:52 2006
@@ -0,0 +1,39 @@
+/*
+ * 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.geronimo.gcache.command;
+
+import org.apache.geronimo.gcache.CacheInfoHolder;
+
+import java.io.IOException;
+import java.util.List;
+
+import net.sf.ehcache.Cache;
+
+public class GetCacheCommand extends CacheBaseCommand{
+
+    public void execute(CacheInfoHolder info) throws IOException {
+        //TODO - Do we lock the Cache???
+        Cache cache = info.getCache(getCacheName(), true);
+
+        List list = cache.getKeys();
+        
+        int size = list.size();
+
+    }
+}

Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/GetCacheCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/GetCacheCommand.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/GetCacheCommand.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/MessageAckCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/MessageAckCommand.java?view=diff&rev=450195&r1=450194&r2=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/MessageAckCommand.java
(original)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/MessageAckCommand.java
Tue Sep 26 13:43:52 2006
@@ -18,6 +18,8 @@
  */
 package org.apache.geronimo.gcache.command;
 
+import org.apache.geronimo.gcache.CacheInfoHolder;
+
 import java.io.IOException;
 import java.nio.channels.ReadableByteChannel;
 import java.nio.channels.WritableByteChannel;
@@ -49,4 +51,7 @@
         writeLong(channel, messageId);
     }
 
+    public void execute(CacheInfoHolder info) throws IOException{
+        //Nothing to send, this is just an ack
+    }
 }

Modified: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/PutEntryCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/PutEntryCommand.java?view=diff&rev=450195&r1=450194&r2=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/PutEntryCommand.java
(original)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/PutEntryCommand.java
Tue Sep 26 13:43:52 2006
@@ -26,10 +26,11 @@
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Collections;
+import java.io.IOException;
 
 public class PutEntryCommand extends CachePayloadBaseCommand {
 
-    public void execute(CacheInfoHolder info) {
+    public void execute(CacheInfoHolder info) throws IOException {
         Cache cache = info.getCache(getCacheName(), true);
 
         //Check if we are using sessions
@@ -46,7 +47,6 @@
             }
 
             sessionMap.put(getHashableKey(), getRawPayload());
-            
             //Put the session away
             cache.put(new Element(getSessionId(), sessionMap));
 

Added: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/PutSessionCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/PutSessionCommand.java?view=auto&rev=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/PutSessionCommand.java
(added)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/PutSessionCommand.java
Tue Sep 26 13:43:52 2006
@@ -0,0 +1,269 @@
+/*
+ * 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.geronimo.gcache.command;
+
+import org.apache.geronimo.gcache.util.ByteArray;
+import org.apache.geronimo.gcache.util.ByteArrayInputStream;
+import org.apache.geronimo.gcache.util.ByteArrayOutputStream;
+import org.apache.geronimo.gcache.CacheInfoHolder;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.Element;
+
+public class PutSessionCommand extends CachePayloadBaseCommand {
+
+    private int entryCount;
+
+    public void readExternal(ReadableByteChannel channel) throws IOException {
+        super.readExternal(channel);
+        entryCount = readInt(channel);
+    }
+
+    public void writeExternal(WritableByteChannel channel) throws IOException {
+        super.writeExternal(channel);
+        writeInt(channel, entryCount);
+    }
+
+    /**
+     * Converts a session map made up of objects into a byte array payload.
+     * This should be used primarily by the client as the server will only
+     * be dealing with raw data.
+     * @param session
+     * @throws IOException
+     */
+    public void setPayloadFromSession(Map session) throws IOException {
+        Set set = session.keySet();
+        entryCount = set.size();
+
+        //Set up the output stream
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ByteBuffer buffer = ByteBuffer.allocateDirect(4);
+        WritableByteChannel channel = Channels.newChannel(baos);
+
+        for (Object aSet : set) {
+
+            buffer.rewind();
+
+            byte rawKey[] = convertObjectToBytes(aSet);
+
+            //Keysize
+            buffer.putInt(rawKey.length);
+            buffer.flip();
+            channel.write(buffer);
+
+            baos.write(rawKey);
+
+            //Data
+            buffer.rewind();
+            Object objData = session.get(aSet);
+            byte data[] = convertObjectToBytes(objData);
+
+            //Data Size
+            buffer.putInt(data.length);
+            buffer.flip();
+            channel.write(buffer);
+
+            baos.write(data);
+
+        }
+
+        //Set the payload
+        setRawPayload(baos.toByteArray());
+    }
+
+
+    /**
+     * Returns a normal map made up of objects that have meaning to the client from the payload.
+     * This is used primarily by the client since the server only cares about the raw bytes.
+     * @return
+     * @throws IOException
+     */
+    public Map getSessionFromPayload() throws IOException{
+
+        Map<Object, Object> sessionMap = Collections.synchronizedMap(new HashMap<Object,
Object>());
+        ByteBuffer buffer = ByteBuffer.allocateDirect(4);
+        ByteArrayInputStream bais = new ByteArrayInputStream(getRawPayload());
+        ReadableByteChannel channel = Channels.newChannel(bais);
+        int keySize;
+        int dataSize;
+        int read;
+
+        for (int i = 0; i < this.entryCount; i++) {
+            buffer.rewind();
+
+            //Get the key Size
+            read = channel.read(buffer);
+            if (read == 4) {
+                buffer.flip();
+                keySize = buffer.getInt();
+            } else {
+                throw new IOException("Could not read the key length from record(" + i +
")");
+            }
+
+            //Read the key
+            byte [] key = new byte[keySize];
+            if (bais.read(key) != keySize)
+                throw new IOException("Could not read key from record(" + i + ")");
+            Object objKey = convertObjectFromBytes(key);
+
+            //Get the data Size
+            buffer.rewind();
+            read = channel.read(buffer);
+            if (read == 4) {
+                buffer.flip();
+                dataSize = buffer.getInt();
+            } else {
+                throw new IOException("Could not read the data length from record(" + i +
")");
+            }
+
+
+            //Get the data
+            byte [] data = new byte[dataSize];
+            if (bais.read(data) != dataSize)
+                throw new IOException("Could not read data from record(" + i + ")");
+            Object objData = convertObjectFromBytes(data);
+
+            //Place the objects in the map
+            sessionMap.put(objKey, objData);
+        }
+
+        return sessionMap;
+    }
+    /**
+     * Sets the payload bytes from a raw session map, or one that is made up of byes.
+     * This is primarily used on the server and client's shouldn't access this method.
+     * @param session
+     * @throws IOException
+     */
+    public void setPayloadFromRawSession(Map session) throws IOException {
+
+        Set set = session.keySet();
+        entryCount = set.size();
+
+        //Set up the output stream
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ByteBuffer buffer = ByteBuffer.allocateDirect(4);
+        WritableByteChannel channel = Channels.newChannel(baos);
+
+        for (Object aSet : set) {
+
+            buffer.rewind();
+
+            ByteArray key = (ByteArray) aSet;
+            byte rawKey[] = key.getBytes();
+
+            //Keysize
+            buffer.putInt(rawKey.length);
+            buffer.flip();
+            channel.write(buffer);
+
+            baos.write(rawKey);
+
+            //Data
+            buffer.rewind();
+            byte data[] = (byte []) session.get(key);
+
+            //Data Size
+            buffer.putInt(data.length);
+            buffer.flip();
+            channel.write(buffer);
+
+            baos.write(data);
+
+        }
+
+        //Set the payload
+        setRawPayload(baos.toByteArray());
+    }
+
+    /**
+     * Will create a raw session from the payload that is used for server side storage.
+     * This method is used primarily for the server and should not be used by the client.
+     * @return
+     * @throws IOException
+     */
+    public Map getRawSessionFromPayload() throws IOException{
+
+        Map<ByteArray, byte[]> sessionMap = Collections.synchronizedMap(new HashMap<ByteArray,
byte[]>());
+        ByteBuffer buffer = ByteBuffer.allocateDirect(4);
+        ByteArrayInputStream bais = new ByteArrayInputStream(getRawPayload());
+        ReadableByteChannel channel = Channels.newChannel(bais);
+        int keySize;
+        int dataSize;
+        int read;
+
+        for (int i = 0; i < this.entryCount; i++) {
+            buffer.rewind();
+
+            //Get the key Size
+            read = channel.read(buffer);
+            if (read == 4) {
+                buffer.flip();
+                keySize = buffer.getInt();
+            } else {
+                throw new IOException("Could not read the key length from record(" + i +
")");
+            }
+
+            //Read the key
+            byte [] key = new byte[keySize];
+            if (bais.read(key) != keySize)
+                throw new IOException("Could not read key from record(" + i + ")");
+
+            //Get the data Size
+            buffer.rewind();
+            read = channel.read(buffer);
+            if (read == 4) {
+                buffer.flip();
+                dataSize = buffer.getInt();
+            } else {
+                throw new IOException("Could not read the data length from record(" + i +
")");
+            }
+
+
+            //Get the data
+            byte [] data = new byte[dataSize];
+            if (bais.read(data) != dataSize)
+                throw new IOException("Could not read data from record(" + i + ")");
+
+            sessionMap.put(new ByteArray(key), data);
+        }
+
+        return sessionMap;
+    }
+
+    public void execute(CacheInfoHolder info) throws IOException {
+        Cache cache = info.getCache(getCacheName(), true);
+
+        //Place the raw session in the cache
+        cache.put(new Element(getSessionId(), getRawSessionFromPayload()));
+
+        //Notify peers
+        info.cacheNotifier.notifyPutSession(this);
+    }
+}

Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/PutSessionCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/PutSessionCommand.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/PutSessionCommand.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/RemoveEntryCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/RemoveEntryCommand.java?view=diff&rev=450195&r1=450194&r2=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/RemoveEntryCommand.java
(original)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/RemoveEntryCommand.java
Tue Sep 26 13:43:52 2006
@@ -24,12 +24,13 @@
 import java.util.Map;
 import java.util.HashMap;
 import java.util.Collections;
+import java.io.IOException;
 
 import net.sf.ehcache.Element;
 import net.sf.ehcache.Cache;
 
 public class RemoveEntryCommand extends CacheBaseCommand{
-    public void execute(CacheInfoHolder info) {
+    public void execute(CacheInfoHolder info) throws IOException {
         Cache cache = info.getCache(getCacheName(), true);
 
         //Check if we are using sessions

Added: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/RemoveSessionCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/RemoveSessionCommand.java?view=auto&rev=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/RemoveSessionCommand.java
(added)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/RemoveSessionCommand.java
Tue Sep 26 13:43:52 2006
@@ -0,0 +1,41 @@
+/*
+ * 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.geronimo.gcache.command;
+
+import net.sf.ehcache.Cache;
+import org.apache.geronimo.gcache.CacheInfoHolder;
+
+import java.io.IOException;
+
+public class RemoveSessionCommand extends CacheBaseCommand{
+
+    public void execute(CacheInfoHolder info) throws IOException {
+        Cache cache = info.getCache(getCacheName(), true);
+
+        //Be sure a session was sent
+        if (!this.hasSession()){
+            throw new IOException("Session does not exist.");
+        }
+
+        cache.remove(this.getSessionId());
+
+        //Notify peers
+        info.getCacheNotifier().notifyRemoveSession(this);
+    }
+}

Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/RemoveSessionCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/RemoveSessionCommand.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/command/RemoveSessionCommand.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/server/listeners/CacheNotifier.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/server/listeners/CacheNotifier.java?view=diff&rev=450195&r1=450194&r2=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/server/listeners/CacheNotifier.java
(original)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/server/listeners/CacheNotifier.java
Tue Sep 26 13:43:52 2006
@@ -21,11 +21,17 @@
 import org.apache.geronimo.gcache.command.ClearCacheCommand;
 import org.apache.geronimo.gcache.command.PutEntryCommand;
 import org.apache.geronimo.gcache.command.RemoveEntryCommand;
+import org.apache.geronimo.gcache.command.RemoveSessionCommand;
+import org.apache.geronimo.gcache.command.PutSessionCommand;
 
 public interface CacheNotifier {
     public void notifyClearCache(ClearCacheCommand command);
-    
+
     public void notifyPut(PutEntryCommand command);
 
+    public void notifyPutSession(PutSessionCommand command);
+
     public void notifyRemove(RemoveEntryCommand command);
+
+    public void notifyRemoveSession(RemoveSessionCommand command);
 }

Modified: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/server/listeners/DefaultCacheNotifier.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/server/listeners/DefaultCacheNotifier.java?view=diff&rev=450195&r1=450194&r2=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/server/listeners/DefaultCacheNotifier.java
(original)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/server/listeners/DefaultCacheNotifier.java
Tue Sep 26 13:43:52 2006
@@ -21,6 +21,8 @@
 import org.apache.geronimo.gcache.command.PutEntryCommand;
 import org.apache.geronimo.gcache.command.RemoveEntryCommand;
 import org.apache.geronimo.gcache.command.ClearCacheCommand;
+import org.apache.geronimo.gcache.command.RemoveSessionCommand;
+import org.apache.geronimo.gcache.command.PutSessionCommand;
 
 /**
  * Default implementation of a cache notifier that does nothing
@@ -33,6 +35,12 @@
     public void notifyPut(PutEntryCommand command) {
     }
 
+    public void notifyRemoveSession(RemoveSessionCommand command) {
+    }
+
     public void notifyRemove(RemoveEntryCommand command) {
+    }
+
+    public void notifyPutSession(PutSessionCommand command) {
     }
 }

Modified: geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArray.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArray.java?view=diff&rev=450195&r1=450194&r2=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArray.java
(original)
+++ geronimo/sandbox/gcache/server/src/main/java/org/apache/geronimo/gcache/util/ByteArray.java
Tue Sep 26 13:43:52 2006
@@ -57,6 +57,10 @@
         this.hash = 0;
     }
 
+    public byte[] getBytes(){
+       return array; 
+    }
+
     public void setBytes(byte[] array, int offset, int length) {
         this.array = array;
         this.offset = offset;

Added: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/BulkSendCommandTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/BulkSendCommandTest.java?view=auto&rev=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/BulkSendCommandTest.java
(added)
+++ geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/BulkSendCommandTest.java
Tue Sep 26 13:43:52 2006
@@ -0,0 +1,47 @@
+/*
+ * 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.geronimo.gcache.command;
+
+import org.testng.annotations.Test;
+
+import java.io.ByteArrayInputStream;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.Channels;
+
+public class BulkSendCommandTest {
+    
+    @Test
+    public void testBulkSendCommand() throws Exception {
+
+        BulkSendCommand command = (BulkSendCommand) CommandTypes.createCommand(CommandTypes.BULK_SEND_COMMAND);
+        command.setNumberOfCommands(8);
+        long commandId = command.getCommandId();
+
+        //Convert the command to bytes
+        byte[] marshalled = command.marshal();
+
+        ByteArrayInputStream bias = new ByteArrayInputStream(marshalled);
+        ReadableByteChannel channel = Channels.newChannel(bias);
+        BulkSendCommand readCommand = new BulkSendCommand();
+        readCommand.readExternal(channel);
+        assert readCommand.getCommandId() == commandId;
+        assert readCommand.getNumberOfCommands() == 8;
+
+    }
+}

Propchange: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/BulkSendCommandTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/BulkSendCommandTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/BulkSendCommandTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/PutSessionCommandTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/PutSessionCommandTest.java?view=auto&rev=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/PutSessionCommandTest.java
(added)
+++ geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/PutSessionCommandTest.java
Tue Sep 26 13:43:52 2006
@@ -0,0 +1,161 @@
+/*
+ * 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.geronimo.gcache.command;
+
+import org.testng.annotations.Test;
+import org.testng.annotations.BeforeSuite;
+import org.apache.geronimo.gcache.CacheInfoHolder;
+import org.apache.geronimo.gcache.util.ByteArray;
+import net.sf.ehcache.CacheManager;
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.Element;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.HashMap;
+import java.util.Collections;
+
+public class PutSessionCommandTest {
+
+    private String cacheName = "Test";
+    private String sessionId = "My Session";
+
+    private String key1 = "My Key 1";
+    private String data1 = "My Data 1";
+    private String key2 = "My Key 2";
+    private String data2 = "My Data 2";
+    private String key3 = "My Key 3";
+    private String data3 = "My Data 3";
+
+    private CacheManager cacheMgr = CacheManager.create();
+    private CacheInfoHolder info = new CacheInfoHolder(cacheMgr);
+
+    @Test
+    public void testPutSessionCommand_raw() throws Exception {
+
+        //Wipe the cache
+        cacheMgr.removalAll();
+
+        //Add the entries the way the server would and the cache will be in a raw format
+        addEntry(cacheName, sessionId, key1, data1, info);
+        addEntry(cacheName, sessionId, key2, data2, info);
+        addEntry(cacheName, sessionId, key3, data3, info);
+
+        //Pull the cache
+        Cache cache = cacheMgr.getCache(cacheName);
+        assert cache != null;
+
+        //Ensure the session is in the cache
+        assert cache.getSize() == 1;
+        Element element = cache.get(sessionId);
+        assert element != null;
+        Map rawSession = (Map) element.getObjectValue();
+
+        //The session should have 3 entries
+        assert rawSession.size() == 3;
+
+        //The map is already in raw formet, so use it
+        PutSessionCommand command = (PutSessionCommand) CommandTypes.createCommand(CommandTypes.PUT_SESSION_COMMAND);
+        command.setCacheName(cacheName);
+        command.setSessionId(sessionId);
+        command.setPayloadFromRawSession(rawSession);
+
+        //Wipe out the cache
+        cache.removeAll();
+
+        assert cache.getSize() == 0;
+
+        //Execute the command and it should repopulate the cache
+        command.execute(info);
+
+        //Test the cache it repoulated
+        runTest();
+
+    }
+
+    @Test
+    public void testPutSessionCommand_object() throws Exception {
+
+        //Wipe the cache
+        cacheMgr.removalAll();
+
+        //Create a new empty cache
+        cacheMgr.addCache(cacheName);
+
+        //Create and load the session
+        Map<Object, Object> sessionMap = Collections.synchronizedMap(new HashMap<Object,
Object>());
+        sessionMap.put(key1, data1);
+        sessionMap.put(key2, data2);
+        sessionMap.put(key3, data3);
+
+        PutSessionCommand command = (PutSessionCommand) CommandTypes.createCommand(CommandTypes.PUT_SESSION_COMMAND);
+        command.setCacheName(cacheName);
+        command.setSessionId(sessionId);
+        command.setPayloadFromSession(sessionMap);
+
+        command.execute(info);
+
+        //Test the cache got populated
+        runTest();
+
+    }
+
+    private void runTest() throws Exception{
+
+        //Pull the cache
+        Cache cache = cacheMgr.getCache(cacheName);
+        assert cache != null;
+        assert cache.getSize() == 1;
+
+        Element element = cache.get(sessionId);
+        assert element != null;
+
+        //Since we are pulling from the server cache it should be raw
+        Map newRawSession = (Map) element.getObjectValue();
+
+        //Be sure 3 elements exist
+        assert newRawSession.size() == 3;
+        PutSessionCommand util = (PutSessionCommand) CommandTypes.createCommand(CommandTypes.PUT_SESSION_COMMAND);
+        util.setPayloadFromRawSession(newRawSession);
+
+        //Pull a readable Map from it (what the client will see)
+        Map clientSession = util.getSessionFromPayload();
+        assert clientSession.size() == 3;
+
+        //Ensure all of the data is here
+        assert ((String) clientSession.get(key1)).equals(data1);
+        assert ((String) clientSession.get(key2)).equals(data2);
+        assert ((String) clientSession.get(key3)).equals(data3);
+    }
+
+    private void addEntry(String cacheName, String sessionId, String key, Object data, CacheInfoHolder
info) throws IOException {
+
+        PutEntryCommand command = new PutEntryCommand();
+
+        command.setCacheName(cacheName);
+        command.setKey(key);
+        command.setSessionId(sessionId);
+        command.setPayload(data);
+
+        command.execute(info);
+
+    }
+}

Propchange: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/PutSessionCommandTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/PutSessionCommandTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/PutSessionCommandTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/RemoveSessionCommandTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/RemoveSessionCommandTest.java?view=auto&rev=450195
==============================================================================
--- geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/RemoveSessionCommandTest.java
(added)
+++ geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/RemoveSessionCommandTest.java
Tue Sep 26 13:43:52 2006
@@ -0,0 +1,67 @@
+/*
+ * 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.geronimo.gcache.command;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.CacheManager;
+import net.sf.ehcache.Element;
+import org.apache.geronimo.gcache.CacheInfoHolder;
+import org.apache.geronimo.gcache.util.ByteArray;
+import org.testng.annotations.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class RemoveSessionCommandTest {
+        @Test
+    public void testRemoveEntry() throws Exception {
+        String cacheName = "Test";
+        String key = "My Key";
+        String sessionId = "My Session";
+        String data = "My Data";
+
+        //Set up a cache
+        CacheManager cacheMgr = CacheManager.create();
+        cacheMgr.addCache(cacheName);
+        Cache cache = cacheMgr.getCache(cacheName);
+        assert cache != null;
+
+        //Create a session
+        Map sessionMap = new HashMap();
+        sessionMap.put(new ByteArray(BaseCommand.convertObjectToBytes(key)), BaseCommand.convertObjectToBytes(data));
+
+        //Add an entry to the session in the cache
+        cache.put(new Element(sessionId, sessionMap));
+
+        //Setup the remove command
+        RemoveSessionCommand command = (RemoveSessionCommand) CommandTypes.createCommand(CommandTypes.REMOVE_SESSION_COMMAND);
+
+        command.setCacheName(cacheName);
+        command.setSessionId(sessionId);
+
+        //Execute the command
+        CacheInfoHolder info = new CacheInfoHolder(cacheMgr);
+        command.execute(info);
+
+        //Check the cache
+        Element element = cache.get(sessionId);
+        assert element == null;
+
+    }
+}

Propchange: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/RemoveSessionCommandTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/RemoveSessionCommandTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/command/RemoveSessionCommandTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message