Help! My Mind is MUSH

bpm@terraweb.com writes:
 > 
 > Yep, my mind has turned to a large mushy melon!  I'm working on
 > creating a CGIDirectoryResource and a PassCGIDirectory that will work
 > as the equivalent of NCSA ScriptAlias directive.  So far, it is going
 > well.  I have created a CGIDirectoryResource which extends the
 > DirectoryResource.  I rewrote/shadowed the createDefaultResource
 > method to create a new CGIDirectoryResource or a new CgiResource.   It
 > works just great!  I can create the PassCGIDirectory and have it point
 > to my old cgi-bin directory and all of the CGIs are usable.

Cool :-)

 > So you ask "What's the problem?".  The current way is that in
 > DirectoryResource there is a method 'createDefaultResource'.  In it,
 > indexer.createResource is invoked.  indexer.createResource checks to
 > see if the new file is a directory or a file.  If it is a directory,
 > then createDirectoryResource is called.  If it is a file, then
 > createFileResource is called.  createFileResource creates a new
 > resource by matching the filename extension with existing defined
 > extensions. (Shouldn't that be a hash lookup instead of a for loop?)
 > When it matches, it gets a template of the resource matching the
 > extension and clones the template into a nice resource.

It can be a hastable lookup: if you look at the code, you will see
that in fact, all matching extensions attributes are set in the newly
created resource, this means that if you index foo.en.html, the newly
created resources will get default attribute values from en first and
then from html.
So if html defines "content-type", and en defines "content-language",
your new resource will get both attribute values. Note that ordre of
extensions matters: if html and en defines the same attribute values,
then the html setting will override the en setting (if I remember
correctly)

 > Well the manor that I am creating the CGIResource is a lobotamized
 > version of the createFileResource method which creates a new file
 > resource based on the extension of the file.  I have added a "cgi"
 > extension that is a template for the CgiResource.  If the "cgi"
 > extension does not exist, then I get a null returned (Duhh).  I want
 > to be able to create a new CgiResource, but I can not find the
 > constructor for the CgiResource class.  Is the compiler creating one
 > by default?  Should I be doing this some other way?  I just KNOW that
 > the way that I am doing it is not the best way.  Below is the code
 > that I wrote.

I am not sure what the problem is: are you comlpaining about the null
returned value when cgi is not defined ? I guess so...

Anyway, your code is nice (it shouldn't span 80 columns thgouh ;-) but
there is a much smarter way of fullfiliing your functionnality (I know
I am the one who recommended starting the way you went...)

What I am proposing is the following: sub-class directory resource (as
you did), but make it have one special children say "template". Then:

a) override createDefaultResource, so that if a file is to be indexed, a
   clone of the current "template" child is created
b) override createDefaultResource, so that if a directory is to be
   created, a clone of the current directory is created.

That new resource could be called hum...HomogeneousDirectory (?) It
would only create one kind of file resource, and itself. Here is a
very rough sketch of the code, ask for more details (if needed):

a) Overide createDefaultResource:

   HTTPResource createDefaultResource(String name) {
       .... various checks...
       File file = new File(getDirectory(), name);
       if ( ! file.isDirectory() )
           return cloneTemplate();
       else
           return cloneDirectory();
   }

   protected HTTPResource cloneDirectory() {
       Hashtable defs = new Hashtable(11);
       defs.put("parent", this);
       defs.put("resource-store", children) ;
       defs.put("server", getServer()) ;
       defs.put("url", getURL() + name);
       HomogeneousDirectory d = (HomogeneousDirectory) getClone(defs);
   }

   protected synchronized HTTPResource cloneTemplate() {
       HTTPResource template = lookup("template");
       if ( template == null ) {
           // Sever config error, track ity:
           getServer().errlog(getClass().getName()+"@"+getURL()+": no template !");
           return null;
       }
       Hashtable defs = new Hashtable(11);
       defs.put("parent", this);
       defs.put("resource-store", children) ;
       defs.put("server", getServer()) ;
       defs.put("url", getURL() + name);
       return template.getClone(defs);
   }

   public void initialize(Object values[]) {
       // Normal stuff:
       super.initialize(values);
       // Make sure we have a template, or get default one:
       if ( lookup("template") == null ) {
           // Get out parent, if it's a HomogeneousDirectory, clone its template
           // as one of our child.
           ...
   }

[This is email quality code, hopefully it has enough hints...]

Noew the idea is that in your case:
a) You create a HomogeneousDirectory to export cgi-bin,
b) You create (as a child of it) a "template" resource, that is a
   CgiResource

Then, you're all set :-) This will be recursive (could be an attribute
to control wether the directory should clone itself to index sub-dirs,
or not).


Anselm.

Received on Tuesday, 12 November 1996 05:59:31 UTC