[Webfunds-commits] java/webfunds/client SepFileStore.java

Ian Grigg iang@cypherpunks.ai
Sat, 30 Sep 2000 14:08:26 -0400 (AST)


iang        00/09/30 14:08:26

  Modified:    webfunds/store SepFileStore.java
  Removed:     webfunds/client SepFileStore.java
  Log:
  MOVED to store from client (repository copied to preserve history).

Revision  Changes    Path
1.42      +137 -25   java/webfunds/store/SepFileStore.java

Index: SepFileStore.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/store/SepFileStore.java,v
retrieving revision 1.41
retrieving revision 1.42
diff -u -r1.41 -r1.42
--- SepFileStore.java	2000/07/10 17:39:42	1.41
+++ SepFileStore.java	2000/09/30 18:08:25	1.42
@@ -1,20 +1,20 @@
 /*
- * $Id: SepFileStore.java,v 1.41 2000/07/10 17:39:42 iang Exp $
+ * $Id: SepFileStore.java,v 1.42 2000/09/30 18:08:25 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
  */
-package webfunds.client;
+package webfunds.store;
 
 import java.io.*;
 import java.util.*;
 import java.lang.reflect.InvocationTargetException;
 
 import webfunds.utils.Diagnostics;
-import webfunds.store.AppendFileStore;
-import webfunds.store.Store;
-import webfunds.store.StoreException;
-import webfunds.store.*;
+// import webfunds.store.AppendFileStore;
+// import webfunds.store.Store;
+// import webfunds.store.StoreException;
+// import webfunds.store.*;
 import webfunds.sox.Encodable;
 import webfunds.sox.SOXPacketException;
 
@@ -23,7 +23,7 @@
  * Every piece of data is stored in a separate file, hence, 'SepFile.'
  */
 public class SepFileStore
-    extends Store implements CorePart, Diagnostics
+    extends Store implements Diagnostics //, CorePart
 {
     /**
      * The version of the stored format for objects.
@@ -31,7 +31,7 @@
     public static final int VERSION = 1;
     //protected int version = VERSION;
 
-    protected   Core  c = null;
+//    protected   Core  c = null;
     protected   File dir;
     protected   boolean syncbool = false;
     protected   boolean error = false;
@@ -50,7 +50,19 @@
     public PrintWriter err()      { return (bug == null) ? new PrintWriter(System.err, true) : bug ; }
 
     
+
+/////////  Construction  //////////////////////////////////
+
     /**
+     * Open an empty, new SepFileStore with nothing set.
+     * Note this is probably incorrect, for ClassLoader use only.
+     */
+    public SepFileStore()
+    {
+        super();
+    }
+    
+    /**
      * Open an empty, new SepFileStore.
      * Note this is probably incorrect, getInstance should be used instead.
      */
@@ -67,6 +79,14 @@
     public SepFileStore(File dir, PrintWriter pw, String logfix)
     {
         super();
+        init(dir, pw, logfix);
+    }
+    
+    /**
+     * Open an empty, new SepFileStore with diags
+     */
+    public void init(File dir, PrintWriter pw, String logfix)
+    {
         this.dir = dir;
         this.name = dir.getPath();
         this.bug  = new PrintWriter(pw, true);
@@ -74,13 +94,15 @@
     }
     
 
+
+/////////  Initialisation  //////////////////////////////////
+
     // XXX: should use Store.getInstance(type, loc) or just (loc)
     public static Store getInstance(File dir)
     {
         return getInstance(dir, new PrintWriter(System.err), "ignore: ");
     }
 
-
     /**
      * Returns an existing SepFileStore, with files in dir
      * read in as objects in the hashtable.
@@ -89,23 +111,34 @@
     public static SepFileStore getInstance(File dir,
                                PrintWriter pw, String logfix)
     {
+        checkDir(dir);
 
+        SepFileStore store = new SepFileStore();
+        store.init(dir, pw, logfix);
+
+        store.initFiles();
+        return store ;
+    }
+    
+    protected static void checkDir(File dir)
+    {
         if (!dir.exists())
             dir.mkdirs();
         if (!dir.isDirectory() || !dir.canRead())
             throw new RuntimeException("Cannot read as directory: " + dir);
-
-        SepFileStore store = new SepFileStore(dir, pw, logfix);            
-        store.setSync(false);
+    }
+    
+    protected void initFiles()
+    {
+        setSync(false);
         String[] files = dir.list();
         File file = null;
         for (int i = 0; i < files.length; i++)
         {
             file = new File(dir, files[i]);
-            store.readFile(file);
+            readFile(file);
         }
-        store.setSync(true);
-        return store;
+        setSync(true);
     }
     
     protected void readFile(File file)
@@ -181,9 +214,13 @@
             throw new RuntimeException("upgrade required: ");
 
         String className = new String(b);
+        ClassLoader cl = getClassLoader();
         Class clss;
         try {
-            clss = Class.forName(className);
+            if (cl == null)
+                clss = Class.forName(className);
+            else        // must be a 3rd party plugin
+                clss = cl.loadClass(className);
         } catch (ClassNotFoundException ex) {
             return false ;
         }
@@ -232,10 +269,10 @@
 
 
 
-    /** All Core components (CorePart) need the parent Core set.  */
-    public void   setCore(Core core)      { if (c == null) { c = core; } }
-    /** Another CorePart requirement.  */
-    public String getType()               { return "Store"; }
+//    /** All Core components (CorePart) need the parent Core set.  */
+//    public void   setCore(Core core)      { if (c == null) { c = core; } }
+//    /** Another CorePart requirement.  */
+//    public String getType()               { return "Store"; }
         
 
 
@@ -255,7 +292,11 @@
     {
         return syncbool;
     }
-    
+
+
+
+/////////  Get New Store  //////////////////////////////////
+
 //    /**
 //     * Open up a subStore - a store on a subdirectory.
 //     * If not currently present in stores, creates a new one.
@@ -276,11 +317,18 @@
 //        return getOldStore(name);
 //    }
 //
+
     protected Store getOldStore(String name)
-        // throws IOException
     {
-        SepFileStore st = getInstance(new File(dir, name), bug, fix);
-        st.setCore(c);
+        File d = new File(dir, name);
+        SepFileStore st; //  = getInstance(d, bug, fix);
+        checkDir(d);
+        st = new SepFileStore();
+//        st.setCore(c);
+logmsg(name + ": setting (OLD) class loader " + getClassLoader());
+        st.setClassLoader(getClassLoader());
+        st.init(d, bug, fix);
+        st.initFiles();
         stores.put(name, st);
         return st;
     }
@@ -304,12 +352,15 @@
             return st;
         }
 
+logmsg("asked to open " + name + " flags == " + flags);
         if (flags == 0)
                 return getOldStore(name) ;
 
+logmsg("testing if flags == APPEND: " + flags);
         if (flags != APPEND)
             throw new StoreException("flag unknown: " + flags + " ("+name+")");
             
+logmsg("return getAppendStore(name);");
         return getAppendStore(name);
     }
     
@@ -317,12 +368,73 @@
         throws StoreException
     {
         AppendFileStore st;
-        st = AppendFileStore.getInstance(new File(dir, name), bug, fix);
+        // st = AppendFileStore.getInstance(new File(dir, name), bug, fix);
+        st = new AppendFileStore();
+logmsg("(APPEND) has class loader " + st.getClassLoader());
+        st.setClassLoader(getClassLoader());
+        st.init(new File(dir, name), bug, fix);
         //st.setCore(c);
         stores.put(name, st);
         return st;
     }
 
+    /**
+     *  Open up a new subStore, with ClassLoader set.
+     *  Experimental.
+     *  Not compatible with getStores (which is not used).
+     */
+    public Store getStore(String name, ClassLoader cl)
+        throws StoreException
+    {
+        if (cl == null)
+            throw new IllegalArgumentException("cl==null");
+
+logmsg("asked to open " + name + " CL == " + cl);
+        Object obj = stores.get(name);
+        SepFileStore st;
+        if (obj != null)
+        {
+            st = (SepFileStore)obj;
+            ClassLoader cl2 = st.getClassLoader();
+logmsg("store already open: " + st.getClass() + " with " + st.getClassLoader());
+            if (!cl.equals(cl2))
+                throw new StoreException(name + " already open without " +
+                                         cl + " but with " + cl2);
+            return st;
+        }
+
+// Probably not necessary...
+        String myClassName = this.getClass().getName();
+        logmsg("my class is " + myClassName);
+        Class clss;
+        try {
+            clss = cl.loadClass(myClassName);
+        } catch (ClassNotFoundException ex) {
+            throw new StoreException("CNFEx: " + ex.getMessage());
+        }
+
+        ClassLoader cl3 = clss.getClassLoader();
+logmsg("open class: " + clss.getClass() + " with " + cl3);
+        try {
+            st = (SepFileStore)clss.newInstance();
+        } catch (IllegalAccessException ex) {
+            throw new StoreException("IAEx: " + ex.getMessage());
+        } catch (InstantiationException ex) {
+            throw new StoreException("InEx: " + ex.getMessage());
+        }
+// End Probably...
+
+        ClassLoader cl4 = st.getClass().getClassLoader();
+logmsg("instantiated: " + st.getClass() + " with " + cl4);
+        st.setClassLoader(cl);
+
+        st.init(new File(dir, name), bug, " (cl): ");
+logmsg("init()d");
+        st.initFiles();
+logmsg("initFiles()d");
+        return st;
+    }
+    
     /**
      * Within this current store, all directories are ignored
      * by the creating getInstance() method.