1. xitiomet
  2. placebohttp

Commits

xitiomet  committed 9e6ab8a

keystore timeout

  • Participants
  • Parent commits 400f90c
  • Branches default

Comments (0)

Files changed (1)

File src/org/openstatic/placebo/plugins/KeyStore.java

View file
 
 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())
         {
                 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);
                 }
                 
     
     public boolean shutdown()
     {
+        this.keep_running = false;
         System.err.println("KeyStore: Shutdown");
         return true;
     }