BugFix: Potential Kryo instance leak on Exception.

- Add try-finally to all other newKryo() calls

Change-Id: I1e3b331e058f511fc541d8146deeb470d6820e88
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 fbc67a4..4afd144 100644
--- a/src/main/java/net/onrc/onos/core/datagrid/HazelcastEventChannel.java
+++ b/src/main/java/net/onrc/onos/core/datagrid/HazelcastEventChannel.java
@@ -184,12 +184,14 @@
         //
         byte[] buffer = new byte[MAX_BUFFER_SIZE];
         Kryo kryo = kryoFactory.newKryo();
-        Output output = new Output(buffer, -1);
-        kryo.writeClassAndObject(output, value);
-        byte[] valueBytes = output.toBytes();
-        kryoFactory.deleteKryo(kryo);
-
-        return valueBytes;
+        try {
+            Output output = new Output(buffer, -1);
+            kryo.writeClassAndObject(output, value);
+            byte[] valueBytes = output.toBytes();
+            return valueBytes;
+        } finally {
+            kryoFactory.deleteKryo(kryo);
+        }
     }
 
     /**
@@ -262,10 +264,12 @@
         // Decode the value
         //
         Kryo kryo = kryoFactory.newKryo();
-        V value = deserializeValue(kryo, valueBytes);
-        kryoFactory.deleteKryo(kryo);
-
-        return value;
+        try {
+            V value = deserializeValue(kryo, valueBytes);
+            return value;
+        } finally {
+            kryoFactory.deleteKryo(kryo);
+        }
     }
 
     /**
@@ -287,14 +291,17 @@
         //
         Collection<byte[]> values = channelMap.values();
         Kryo kryo = kryoFactory.newKryo();
-        for (byte[] valueBytes : values) {
-            //
-            // Decode the value
-            //
-            V value = deserializeValue(kryo, valueBytes);
-            allEntries.add(value);
+        try {
+            for (byte[] valueBytes : values) {
+                //
+                // Decode the value
+                //
+                V value = deserializeValue(kryo, valueBytes);
+                allEntries.add(value);
+            }
+        } finally {
+            kryoFactory.deleteKryo(kryo);
         }
-        kryoFactory.deleteKryo(kryo);
 
         return allEntries;
     }
diff --git a/src/main/java/net/onrc/onos/core/intent/runtime/PersistIntent.java b/src/main/java/net/onrc/onos/core/intent/runtime/PersistIntent.java
index f27c855..9a9d59c 100644
--- a/src/main/java/net/onrc/onos/core/intent/runtime/PersistIntent.java
+++ b/src/main/java/net/onrc/onos/core/intent/runtime/PersistIntent.java
@@ -43,7 +43,8 @@
         table = DataStoreClient.getClient().getTable(INTENT_JOURNAL);
         stream = new ByteArrayOutputStream(1024);
         output = new Output(stream);
-        kryo = (new KryoFactory()).newKryo();
+        // FIXME Using KryoFactory only to register classes and not using the pool.
+        kryo = new KryoFactory(1).newKryo();
     }
 
     public long getKey() {