BugFix: Kryo instance leak on Exception.

- Kryo instance will leak when there was an Exception thrown
  while deserializing etc. (ONOS-1343)

Change-Id: I70067e4a7111f0d44cef8ebf8ee33f9e02c600b6
diff --git a/src/main/java/net/onrc/onos/core/datagrid/HazelcastEventChannel.java b/src/main/java/net/onrc/onos/core/datagrid/HazelcastEventChannel.java
index 00ff7a8..c3a35cf 100644
--- a/src/main/java/net/onrc/onos/core/datagrid/HazelcastEventChannel.java
+++ b/src/main/java/net/onrc/onos/core/datagrid/HazelcastEventChannel.java
@@ -337,21 +337,24 @@
             //
             byte[] valueBytes = event.getValue();
             Kryo kryo = kryoFactory.newKryo();
-            V value = deserializeValue(kryo, valueBytes);
+            try {
+                V value = deserializeValue(kryo, valueBytes);
 
-            //
-            // Deliver the notification
-            //
-            int index = 0;
-            for (IEventChannelListener<K, V> listener : listeners) {
-                V copyValue = value;
-                if (index++ > 0) {
-                    // Each listener should get a deep copy of the value
-                    copyValue = kryo.copy(value);
+                //
+                // Deliver the notification
+                //
+                int index = 0;
+                for (IEventChannelListener<K, V> listener : listeners) {
+                    V copyValue = value;
+                    if (index++ > 0) {
+                        // Each listener should get a deep copy of the value
+                        copyValue = kryo.copy(value);
+                    }
+                    listener.entryAdded(copyValue);
                 }
-                listener.entryAdded(copyValue);
+            } finally {
+                kryoFactory.deleteKryo(kryo);
             }
-            kryoFactory.deleteKryo(kryo);
         }
 
         /**
@@ -366,21 +369,24 @@
             //
             byte[] valueBytes = event.getValue();
             Kryo kryo = kryoFactory.newKryo();
-            V value = deserializeValue(kryo, valueBytes);
+            try {
+                V value = deserializeValue(kryo, valueBytes);
 
-            //
-            // Deliver the notification
-            //
-            int index = 0;
-            for (IEventChannelListener<K, V> listener : listeners) {
-                V copyValue = value;
-                if (index++ > 0) {
-                    // Each listener should get a deep copy of the value
-                    copyValue = kryo.copy(value);
+                //
+                // Deliver the notification
+                //
+                int index = 0;
+                for (IEventChannelListener<K, V> listener : listeners) {
+                    V copyValue = value;
+                    if (index++ > 0) {
+                        // Each listener should get a deep copy of the value
+                        copyValue = kryo.copy(value);
+                    }
+                    listener.entryRemoved(copyValue);
                 }
-                listener.entryRemoved(copyValue);
+            } finally {
+                kryoFactory.deleteKryo(kryo);
             }
-            kryoFactory.deleteKryo(kryo);
         }
 
         /**
@@ -395,21 +401,24 @@
             //
             byte[] valueBytes = event.getValue();
             Kryo kryo = kryoFactory.newKryo();
-            V value = deserializeValue(kryo, valueBytes);
+            try {
+                V value = deserializeValue(kryo, valueBytes);
 
-            //
-            // Deliver the notification
-            //
-            int index = 0;
-            for (IEventChannelListener<K, V> listener : listeners) {
-                V copyValue = value;
-                if (index++ > 0) {
-                    // Each listener should get a deep copy of the value
-                    copyValue = kryo.copy(value);
+                //
+                // Deliver the notification
+                //
+                int index = 0;
+                for (IEventChannelListener<K, V> listener : listeners) {
+                    V copyValue = value;
+                    if (index++ > 0) {
+                        // Each listener should get a deep copy of the value
+                        copyValue = kryo.copy(value);
+                    }
+                    listener.entryUpdated(copyValue);
                 }
-                listener.entryUpdated(copyValue);
+            } finally {
+                kryoFactory.deleteKryo(kryo);
             }
-            kryoFactory.deleteKryo(kryo);
         }
 
         /**