Help! My Mind is MUSH

Anselm Baird writes:
 |
 |Now 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.

Thanks for the hints.  It helped a lot.  I think that I tried to do
what you suggested.  The CgiDirectory works.  The sub directories of a
CgiDirectory work.  The CgiPassDirectory works.  But I can not get the
cloning of any sub directories that the CgiPassDirectory points to!!

What hints can you give me?

Also, what would I do to set up an editor of this CgiDirectory?  It
would be nice to have an editor like the extension editor so I can
edit the "Child Template" from a form.

Oh yea, the CgiPassDirectory is equal to the PassDirectory except for
the class name & who it extends.

Here is the code:

// CgiDirectory.java
// $Id: CgiDirectory.java,v 1.0  $

package w3c.jigsaw.resources ;

import java.io.* ;
import java.util.* ;

import w3c.tools.sorter.*;
import w3c.jigsaw.http.* ;
import w3c.jigsaw.indexer.* ;
import w3c.jigsaw.html.* ;
import w3c.jigsaw.resources.* ;
import w3c.jigsaw.forms.* ;
import w3c.www.http.*;

/**
 * A simple, and reasonably efficient Cgi directory resource.
 * This directory resource embeds its own resource store object to keep
 * track of its next children (wich might themselves be CgiDirectory). It
 * is reasonably efficient in the sense that it won't overload the memory with 
 * unused informations. However, stay tuned for a <em>really</em> efficient
 * file based directory resource (tuned to serve only files).
 */

public class CgiDirectory extends DirectoryResource
{

    /**
     * Attribute index - Does this applies to directory below this one.
     */
    protected static int ATTR_GENERIC = -1 ;

    /**
     * Attribute index - The template for this directory.
     */
    protected static int ATTR_TEMPLATE = -1;

    /**
     * Attribute index - The template for the child.
     */
    protected static int ATTR_CHILD_TEMPLATE = -1;

    static {
		Attribute a = null ;
		Class     cls = null ;
		try {
			cls   = Class.forName("w3c.jigsaw.resources.CgiDirectory") ;
		} catch (Exception ex) {
			ex.printStackTrace() ;
			System.exit(1) ;
		}
		// The generic flag
		a = new BooleanAttribute("generic"
								 , Boolean.FALSE
								 , Attribute.EDITABLE) ;
		ATTR_GENERIC = AttributeRegistry.registerAttribute(cls, a) ;
		// The template:
		a = new AttributeHolderAttribute("template"
										 , null
										 , Attribute.EDITABLE);
		ATTR_TEMPLATE = AttributeRegistry.registerAttribute(cls, a);
		// The child:
		a = new AttributeHolderAttribute("child_template"
										 , null
										 , Attribute.EDITABLE);
		ATTR_CHILD_TEMPLATE = AttributeRegistry.registerAttribute(cls, a);
    }

 	protected HTTPResource cloneDirectory(String name) {
		HTTPResource template = getTemplate();
		if ( template == null ) {
			// Sever config error, track ity:
			getServer().errlog(getClass().getName()+
							   "@"+
							   getURL()+
							   ": no template !");
			return null;
		}
		File file = new File(getDirectory(), name) ;
		Hashtable defs = new Hashtable(13);
		defs.put("parent", this);
		defs.put("directory", file) ;
		defs.put("identifier", name);
		defs.put("resource-store", children) ;
		defs.put("server", getServer()) ;
		defs.put("url", getURL() + name);
		try {
			return (CgiDirectory) getClone(defs);
		} catch (Exception ex) {
			ex.printStackTrace() ;
			return null ;
		}
	}
	
	protected synchronized HTTPResource cloneTemplate(String name) {
		HTTPResource template = getChildTemplate();
		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("directory", getDirectory());
		defs.put("identifier", name);
		defs.put("resource-store", children) ;
		defs.put("server", getServer()) ;
		defs.put("url", getURL() + name);
		try {
			return (HTTPResource) template.getClone(defs);
		} catch (Exception ex) {
			ex.printStackTrace() ;
			return null ;
		}
	}
	
	/**
     * Get the default cgi child target class.
     */
	protected HTTPResource getChildTemplate() {
		return (HTTPResource) getValue(ATTR_CHILD_TEMPLATE, null);
    }

	/**
     * Get the default cgi child target class.
     */
	protected HTTPResource getTemplate() {
		return (HTTPResource) getValue(ATTR_TEMPLATE, null);
    }

	/**
     * Set the default directory class.
     */
	
    protected void setTemplate(HTTPResource template) {
		setValue(ATTR_TEMPLATE, template);
    }

	/**
     * Set the default cgi child target class.
     */
	
    protected void setChildTemplate(HTTPResource template) {
		setValue(ATTR_CHILD_TEMPLATE, template);
    }

    /**
     * Get the generic flag value.
     */

    protected boolean getGenericFlag() {
		return getBoolean(ATTR_GENERIC, false) ;
    }
	
    /**
     * Initialize a template.
     **/
	protected
	HTTPResource
	initTemplate( String className, HTTPResource template ) {

		if ( template == null ) {
			// This would be a very nice place to get a member value that is
			// the class that we want to clone.  Must have some editor like the
			// extensions editor resource.
			try {
				Class cls = Class.forName(className);
				try {
					template = (HTTPResource) cls.newInstance();
					template.setValue("identifier", getIdentifier());
					return template;
				} catch (Exception ex) {
					String msg = ("Internal server error while "
								  + "creating template "
								  + " of class " + cls.getName()
								  + ": " + ex.getMessage());
					getServer().errlog(msg);
					System.exit(1);
				}
			} catch (Exception ex) {
				ex.printStackTrace();
				System.exit(1);
			}
		}
		return template;
	}
	
	public void initialize(Object values[]) {
 		super.initialize(values);
		String childClassName = "w3c.jigsaw.forms.CgiResource";
		String dirClassName = "w3c.jigsaw.resources.CgiDirectory";
		
		HTTPResource template = initTemplate(childClassName,
											 getChildTemplate());
		setChildTemplate(template);
		
		template = initTemplate(dirClassName, getTemplate());
		setTemplate(template);
	}


    /**
     * Try creating a default resource having the given name.
     * This method will make its best effort to create a default resource
     * having this name in the directory. If a file with this name exists,
     * it will check the pre-defined admin extensions and look for a match.
     * If a directory with this name exists, and admin allows to do so, it
     * will create a sub-directory resource.
     * @param name The name of the resource to try to create.
     * @return A Resource instance, if possible, <strong>null</strong>
     *    otherwise.
     */

    public synchronized HTTPResource createDefaultResource(String name) {

		HTTPResource resource;
		
		// Don't automagically create resources of name '..' or '.'
		if (name.equals("..") || name.equals("."))
			return null ;
		// Try building a default resource for it:
		ResourceIndexer indexer = getServer().getIndexer() ;
		// Is there a file with such a name ?
		File file = new File(getDirectory(), name) ;
		if ( ! file.exists() )
			return null ;
		// If the file system is not case sensitive, emulate it :-(
		if ( getServer().checkFileSystemSensitivity() ) {
			File directory = getDirectory();
			if ( directory == null )
				return null;
			String  files[] = directory.list();
			boolean found   = false;
			for (int i = 0 ; i < files.length ; i++) {
				if (found = files[i].equals(name))
					break;
			}
			if ( ! found )
				return null;
		}
		// Prepare a set of default parameters for the resource:
		acquireChildren() ;
		
		// Okay, dispatch on wether it is a file or a directory.
		if ( file.isDirectory() )
			resource = cloneDirectory(name);
		else
			resource = cloneTemplate(name);
		
		if ( resource != null ) {
			// Register this child in our store:
			children.addResource(resource) ;
			// Update or create any relevant negotiable resource:
			if ( getNegotiableFlag() ) 
				updateNegotiableResource(name) ;
			markModified() ;
		}
		return resource ;
		
    }

}

-- 
---
Brian Millett                    
Technology Applications Inc.     "Heaven can not exist,
(314) 530-1981                          If the family is not eternal"
bpm@techapp.com                   F. Ballard Washburn

Received on Monday, 18 November 1996 10:28:22 UTC