Print

Print


Sean,

Thanks for the prompt reply,I knew there had to be an easy way to do it.  I
only needed to make one change in the constructor:


 public ParameterDatabase(java.io.InputStream stream)


rather than the line:

  parents.addElement(new ParameterDatabase(new File(s)));

I used:

     parents.addElement(new ParameterDatabase(
            this.getClass().getResourceAsStream(s)));

Each parameter file should have a heading of the form

parent.0 = /ec/simple/simple.params

but I am sure other forms could be accommodated.


As for out.stat I am planning at the moment to only write out statistics if
the user requires them.  In this case web start can get permission from the
user to write to their disk.

Regards,

Laurie






-----Original Message-----
From: Sean Luke [mailto:[log in to unmask]]
Sent: 17 July 2005 18:25
To: [log in to unmask]
Subject: Re: ECJ as Web Start App

On Jul 17, 2005, at 7:32 AM, Hirsch Laurence wrote:

> I am now considering deploying my ECJ app using web start.  The first
> problem I have is with the parameter files as I would like to open a
> default file without prompting the user. As I understand it I need to
> use something like
>
> this.getClass().getClassLoader().getResourceAsStream....
>
> to open a text file from within the jar.  But ParameterDatabase is
> expecting java File. I am sure there must be an easy way to do this but
> my java skills are lacking.

ParameterDatabases by normally are loaded from files; but there is a
default constructor which can create an empty ParameterDatabase.  From
there you can add parameters directly with the set(Parameter,String)
method.

But it sounds like what you need is the ability to load a
ParameterDatabase either from a flat Dictionary (a HashMap, say) or from
an InputStream that you would hook up to your resource.  Both are pretty
easy to add to the ParameterDatabase class.  I list some code you can add
to ParameterDatabase below, which compiles but I make no promises about
correctness.  :-)

Some gotchas: ParameterDatabases typically have a 'filename' internal
variable set from which relative filenames can be defined.  For example,
if you say:

stat.file = ../../foo

...this means "starting at the ParameterDatabase's 'filename', go to
../../foo".  Since you will not have a 'filename' defined if you're just
loading from an InputStream, the behavior of such parameters is undefined.
Your big gotchas may be in out.stat and in parent.0, parent.1, etc.

I'm interested in your attempts here because one of the several reasons we
modified ECJ 13 to be fully encapsulated was to make possible an ECJ
applet.

Sean


    /** Creates a new parameter database from the given Dictionary.  Both
the keys and values will be run through toString() before adding to the
dataase.   Keys are parameters.  Values are the values of the parameters.
Beware that a ParameterDatabase is itself a Dictionary; but if you pass
one in here you will only get the lowest-level elements.  If parent.n are
defined, parents will be attempted to be loaded -- that's the reason for
the FileNotFoundException and IOException.  */
    public ParameterDatabase(java.util.Dictionary map) throws
FileNotFoundException, IOException
        {
        this();
        java.util.Enumeration keys = map.keys();
        while(keys.hasMoreElements())
            {
            Object obj = keys.nextElement();
            set(new Parameter(""+obj),""+map.get(obj));
            }

        // load parents
        for (int x = 0;; x++)
            {
            String s = getProperty("parent." + x);
            if (s == null)
                return; // we're done

            if (new File(s).isAbsolute()) // it's an absolute file
definition
                parents.addElement(new ParameterDatabase(new File(s)));
            else throw new FileNotFoundException("Attempt to load a relative
file, but there's no parent file: " + s);
            }
        }

    /** Creates a new parameter database loaded from the given stream. If
parent.n are defined, parents will be attempted to be loaded -- that's the
reason for the FileNotFoundException and IOException. */

    public ParameterDatabase(java.io.InputStream stream) throws
FileNotFoundException, IOException
        {
        this();
        load(stream);

        listeners = new Vector();

        // load parents
        for (int x = 0;; x++)
            {
            String s = getProperty("parent." + x);
            if (s == null)
                return; // we're done
            if (new File(s).isAbsolute()) // it's an absolute file
definition
                parents.addElement(new ParameterDatabase(new File(s)));
            else throw new FileNotFoundException("Attempt to load a relative
file, but there's no parent file: " + s);
            }
    }