# HG changeset patch # User xitiomet # Date 1327347574 18000 # Node ID 9e6ab8ab1d739ceff54c0991f19770c0a8bf126e # Parent 400f90c8aa46e8cdaad366b1639581726d2b7dcd keystore timeout diff -r 400f90c8aa46e8cdaad366b1639581726d2b7dcd -r 9e6ab8ab1d739ceff54c0991f19770c0a8bf126e src/org/openstatic/placebo/plugins/KeyStore.java --- a/src/org/openstatic/placebo/plugins/KeyStore.java Fri Dec 09 13:38:58 2011 -0500 +++ b/src/org/openstatic/placebo/plugins/KeyStore.java Mon Jan 23 14:39:34 2012 -0500 @@ -36,14 +36,63 @@ public class KeyStore implements PlaceboPlugin { - private JSONObject store; + protected JSONObject store; private JTextPane json_box; private boolean swing_mode; + protected boolean keep_running; + private Thread key_gc; + private int key_timeout; public boolean startup(CoreServer core, String mount_location) { System.err.println("KeyStore: Initializing Keystore"); + this.keep_running = true; + this.key_timeout = Integer.valueOf(core.getSettings().getProperty("keystore_timeout", "3600")).intValue(); store = new JSONObject(); + key_gc = new Thread() + { + public void run() + { + while(KeyStore.this.keep_running) + { + try + { + expire_check(KeyStore.this.store); + Thread.sleep(1000); + } catch (Exception e) {} + } + } + + public void expire_check(JSONObject jo) throws Exception + { + String[] fnames = JSONObject.getNames(jo); + if (fnames != null) + { + for (int i = 0; i < fnames.length; i++) + { + Object value = jo.get(fnames[i]); + if (value instanceof JSONObject) + { + JSONObject nd = (JSONObject) value; + boolean expired = false; + try + { + int exp = nd.getInt("_expires"); + exp--; + if (exp <= 0) + expired = true; + nd.put("_expires", exp); + } catch (Exception e) {} + if (expired) + jo.remove(fnames[i]); + else + expire_check(nd); + } + } + } + } + }; + key_gc.start(); if (core.isSwingOk()) { @@ -89,8 +138,11 @@ try { current_object = current_object.getJSONObject(current_node); + current_object.put("_expires", this.key_timeout); } catch (Exception js_exc) { - current_object.put(current_node, new JSONObject()); + JSONObject new_obj = new JSONObject(); + new_obj.put("_expires", this.key_timeout); + current_object.put(current_node, new_obj); current_object = current_object.getJSONObject(current_node); } @@ -139,6 +191,7 @@ public boolean shutdown() { + this.keep_running = false; System.err.println("KeyStore: Shutdown"); return true; }