[Webfunds-commits] java/webfunds/client/plugins WalletContext.java WalletManager.java

Ian Grigg iang@cypherpunks.ai
Sun, 17 Sep 2000 16:43:33 -0400 (AST)


iang        00/09/17 16:43:33

  Modified:    webfunds/client Core.java WalletInterface.java
  Added:       webfunds/client/plugins WalletContext.java
                        WalletManager.java
  Removed:     webfunds/client WalletContext.java WalletManager.java
  Log:
  1. moved WalletContext and WalletManager into plugins, as there is much
  in common.  Not much code commonality at the moment, but that can come later.
  2. got 3rd party plugin wallets going via the PluginClassLoader (that calls
  DirClassLoader).  Core now calls this on the "plugins/wallets" directory,
  and the WalletManager then scans that for extra wallets.
  3. added a getHome call to WalletContext so that a plugin wallet can see
  things like files - contracts and properties - that might have been
  delivered with the 3rd party package.  This is probably not the way to
  do it, but something is needed in the short term.

Revision  Changes    Path
1.62      +6 -6      java/webfunds/client/Core.java

Index: Core.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/client/Core.java,v
retrieving revision 1.61
retrieving revision 1.62
diff -u -r1.61 -r1.62
--- Core.java	2000/08/18 00:21:00	1.61
+++ Core.java	2000/09/17 20:43:31	1.62
@@ -1,4 +1,4 @@
-/* $Id: Core.java,v 1.61 2000/08/18 00:21:00 gelderen Exp $
+/* $Id: Core.java,v 1.62 2000/09/17 20:43:31 iang Exp $
  *
  * Copyright (c) Systemics Inc. 1995-2000 on behalf of
  * The WebFunds Development Team.  All rights reserved.
@@ -27,6 +27,7 @@
 import webfunds.client.contracts.ChangeContractStore;
 import webfunds.client.plugins.PluginException;
 import webfunds.client.plugins.PluginManager;
+import webfunds.client.plugins.WalletManager;
 import webfunds.store.Store;
 import webfunds.store.StoreException;
 import webfunds.store.*;
@@ -43,7 +44,7 @@
     /** customisable brand name */
     public static String me        = "WebFunds";
 
-    /** turn on developer test mode to do wierd things */
+    /** turn on developer test mode to do weird things */
     public static boolean testMode = false;
 
     /** take stuff from the defaults directory */
@@ -224,8 +225,6 @@
 //        //
 //        // Adding 3rd party file plugins
 //        //
-//        String plugin3rdParty = null;
-//
 //        logmsg("3rd Party Plugins: ==========================");
 //
 //        String[] plugs = VersionNumbers.getListOfAddOns(pluginsDir, bug);
@@ -246,8 +245,9 @@
         //
         logmsg("Wallets: SOX - - - - - - - - - - - - - - - - - - - - - - -");
         wm.addWallet("webfunds.client.sox.SOXWallet");
-        logmsg("         Mkt - - - - - - - - - - - - - - - - - - - - - - -");
-        wm.addWallet("systemics.trader.MarketWallet");
+        wm.add3rdPartyWallets(new File(pluginsDir, "wallets"));
+//        logmsg("         Mkt - - - - - - - - - - - - - - - - - - - - - - -");
+//        wm.addWallet("systemics.trader.MarketWallet");
         logmsg("wallets open. ============================================");
 
 



1.37      +4 -2      java/webfunds/client/WalletInterface.java

Index: WalletInterface.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/client/WalletInterface.java,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -r1.36 -r1.37
--- WalletInterface.java	2000/07/16 19:27:23	1.36
+++ WalletInterface.java	2000/09/17 20:43:32	1.37
@@ -1,14 +1,16 @@
 /*
- * $Id: WalletInterface.java,v 1.36 2000/07/16 19:27:23 iang Exp $
+ * $Id: WalletInterface.java,v 1.37 2000/09/17 20:43:32 iang Exp $
  *
  * Copyright (c) 1995-2000 Systemics Ltd on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
  */
 package webfunds.client;
 
+import java.io.File;
 import java.io.PrintWriter;
 import java.util.Date;
-import webfunds.sox.*;
+import webfunds.sox.ItemId;
+import webfunds.client.plugins.WalletContext;
 
 /**
  *  The Wallet.  Implementation dependant, as this interface



1.1                  java/webfunds/client/plugins/WalletContext.java

Index: WalletContext.java
===================================================================
/* $Id: WalletContext.java,v 1.1 2000/09/17 20:43:32 iang Exp $
 *
 * Copyright (c) 2000 Systemics Inc. All rights reserved.
 */

package webfunds.client.plugins;


import java.io.File;
import java.security.SecureRandom;

import webfunds.client.Addressbook;
import webfunds.client.WalletInterface;
import webfunds.client.UInterface;
import webfunds.client.contracts.ChangeContractStore;
// import webfunds.client.plugins.PluginManager;
import webfunds.store.Store;
import webfunds.store.StoreException;



public interface WalletContext
{
    /** how to interpret low level names to printable names */
    Addressbook getAddressbook(WalletInterface wi);
    SecureRandom getSecureRandom();
    /** the persistant storage object available from client */
    Store getStore(WalletInterface wi) throws StoreException;
    /** the GUI */
    UInterface getUInterface(WalletInterface wi);
    /** the PluginManager for modifying the GUI's menus */
    PluginManager getPluginManager(WalletInterface wi);

    /** static, read-only data that is supplied with a third-party wallet */
    // this is something we should change to a local Store like class
    File getHome(WalletInterface wi);


    // XXX: ChangeContractStore is misnamed and prolly too specific.
    ChangeContractStore getContractStore(WalletInterface wi);

}



1.1                  java/webfunds/client/plugins/WalletManager.java

Index: WalletManager.java
===================================================================
/*
 * $Id: WalletManager.java,v 1.1 2000/09/17 20:43:33 iang Exp $
 *
 * Copyright (c) Systemics Ltd 1995-1999 on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.client.plugins;

import java.beans.*;
import java.util.Vector;
import java.util.Hashtable;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.File;
import java.security.SecureRandom;

import webfunds.ricardian.IniFileReader;
import webfunds.ricardian.ContractException;

import webfunds.client.Addressbook;
import webfunds.client.WalletInterface;
import webfunds.client.Manager;
import webfunds.client.CorePart;
import webfunds.client.AccountBrowser;
import webfunds.client.UInterface;
import webfunds.client.contracts.ChangeContractStore;

import webfunds.store.Store;
import webfunds.store.StoreException;

import webfunds.utils.VersionNumbers;

public class WalletManager
    extends Manager implements CorePart, WalletContext
{
    
    private Vector       wallets;
    private Hashtable    homes;
    
    public WalletManager(PrintWriter bug)
    {
        debug(bug, "WM ");
        Beans.setGuiAvailable(true);
        wallets = new Vector();
        homes = new Hashtable();
    }

    public String getType()
    {
        return "WalletManager";
    }

    // what is this for?  array is out of date if wallets is changed.
    protected void updateAccountBrowser()
    {
        WalletInterface[] array = new WalletInterface[ wallets.size() ];
        wallets.copyInto(array);
        core.getAccountBrowser(this).setWallets(array);
    }
    

    public void addWallet(String classname)
    {
        try
        {
            //Class clas = Class.forName(classname);
            WalletInterface wallet;
            //wallet = (WalletInterface)clas.newInstance();
            logmsg("instantiating " + classname);
            wallet = (WalletInterface)Beans.instantiate(classloader, classname);

            logmsg("debug");
            PrintWriter bug = getDebug();
            //bug.println("setting debug for " + classname);
            wallet.debug(bug);
            homes.put(wallet, ".");       // access to static info in dir
            wallet.setWalletContext(this);

            wallets.addElement(wallet);
            updateAccountBrowser();
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        
    }
    
    // what is this for?  array is out of date if wallets is changed.
    public WalletInterface[] getWallets()
    {
        WalletInterface[] array = new WalletInterface[wallets.size()];
        wallets.copyInto(array);
        return array;
    }
    
    public void removeWallet(WalletInterface wallet)
    {
        wallets.removeElement(wallet);
        homes.remove(wallet);
        updateAccountBrowser();
    }

    public UInterface getUInterface(WalletInterface wi)
    {
        return core.getUInterface(this);
    }
    
    public ChangeContractStore getContractStore(WalletInterface wi)
    {
        return core.getContractStore(this);
    }
    
    public Store getStore(WalletInterface wi)
        throws StoreException
    {
        return core.getStore(this).getStore(wi.getShortName());
    }
    
    public Addressbook getAddressbook(WalletInterface wi)
    {
        return core.getAddressbook(this);
    }
    
    public SecureRandom getSecureRandom()
    {
        // XXX: should we be getting it from Core?
        return core.getSecureRandom(); 
    }

    public PluginManager getPluginManager(WalletInterface wi)
    {
        return core.getPluginManager(this);
    }

    public File getHome(WalletInterface wi)
    {
        return new File( (String) homes.get(wi) );
    }


    /**
     *  Plug in the 3rd party wallets out of the directory dir.
     */
    public void add3rdPartyWallets(File walletDir)
    {
        logmsg("3rd Party Wallets: ==========================");

        String[] plugs = VersionNumbers.getListOfAddOns(walletDir, bug);

        for (int i = 0; i < plugs.length; i++)
        {
            String plug = plugs[i];
            try {
                logmsg("    " + plug + " - - - - - - - - - - - - - - - -");
                add3rdPartyWallet(walletDir, plug);
            } catch (PluginException ex) {
                logmsg("        Failed: " + ex + "\n" + vvv);
            }   
        }
        updateAccountBrowser();
    }

    static final String spec  = "wallet.ini";
    static final String vvv = "Contact your Vendor!";

    public void add3rdPartyWallet(File top, String wal)
        throws PluginException
    {
        File dir = new File(top, wal);
        File pugSpec = new File(dir, spec);
        logmsg("reading " + pugSpec);
        IniFileReader ini = readAsIniFile(pugSpec);
        String s = "Wallet " + wal + " failed.\n";

        String main = ini.getSectionItemValue("java", "main");
        if (main == null || main.length() == 0)
            throw new PluginException(s + "no [java] main in " + pugSpec);
        String root = ini.getSectionItemValue("java", "root");
        if (root == null || root.length() == 0)
            root = top.getPath() + File.separator + wal;

        logmsg("new PCL ... " + root);
        PluginClassLoader cl;
        try {
            cl = new PluginClassLoader(root);
        } catch (FileNotFoundException ex) {
            s += "Ex: " + ex + "\n" + "directory " + wal +
                   " not a valid plugin home!";
            throw new PluginException(s) ;
        }

        String tail = "\n" + "Main " + main +
                   " not a valid Main entry point!";
        logmsg("ini says " + main);
        WalletInterface wallet;
        try {
            Class cuss = cl.loadClass(main);
            wallet = (WalletInterface)cuss.newInstance();

            // another method:
            //WalletInterface wallet;
            //logmsg("instantiating " + classname);
            //wallet = (WalletInterface)Beans.instantiate(classloader, classname);
        } catch (IllegalAccessException ex) {
            s += "Ex: " + ex + "\n" + tail;
            throw new PluginException(s) ;
        } catch (InstantiationException ex) {
            s += "Ex: " + ex + "\n" + tail;
            throw new PluginException(s) ;
        } catch (ClassNotFoundException ex) {
            s += "Ex: " + ex + "\n" + tail;
            throw new PluginException(s) ;
        }

        //loaders.addElement(cl);          // not sure whether this is needed...


        //
        // from now on, we have no idea what this code can
        // do / throw so we need to catch everything ?!?
        //
        try
        {
            logmsg("debug");
            //bug.println("setting debug for " + classname);
            wallet.debug(getDebug());
            homes.put(wallet, dir.getPath());  // access to static info in dir
            wallet.setWalletContext(this);

            // ? prepare(wallet, plugins);
        }
        catch (Throwable ex)
        {
            throw new PluginException("badly behaved, ditching");
        }

        wallets.addElement(wallet);
    }

    public IniFileReader readAsIniFile(File pugSpec)
        throws PluginException
    {

        // Read Plugin Spec
        byte[] buf;
        try {
            FileInputStream fis = new FileInputStream(pugSpec);
            buf = new byte[fis.available()];
            fis.read(buf);
            fis.close();
        } catch (IOException ex) {
            throw new PluginException("IOEx: " + ex + "\n" + "Spec " + spec +
                   " not readable!");
        }

        IniFileReader ini;
        try {
            ini = new IniFileReader(buf);
        } catch (ContractException ex) {
            throw new PluginException("Ex: " + ex + "\n" + "Spec " + spec +
                   " not a valid Ini file!");
        }
        return ini;
    }


}