⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 directoryresource.java

📁 很棒的web服务器源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// DirectoryResource.java// $Id: DirectoryResource.java,v 1.24 2003/06/06 14:23:14 ylafon Exp $// (c) COPYRIGHT MIT and INRIA, 1996.// Please first read the full copyright statement in file COPYRIGHT.htmlpackage org.w3c.tools.resources ;import java.util.Enumeration;import java.util.Hashtable;import java.io.File;import java.io.PrintStream;import java.io.RandomAccessFile;import org.w3c.tools.resources.indexer.IndexerModule;import org.w3c.tools.resources.indexer.ResourceIndexer;import org.w3c.tools.resources.event.StructureChangedEvent;/** * A simple, and reasonably efficient directory resource. */public class DirectoryResource extends ContainerResource {    /**     * Attribute index - The index for our directory attribute.     */    protected static int ATTR_DIRECTORY = -1 ;    /**     * Attribute index - The last time we physically visited the directory.     */    protected static int ATTR_DIRSTAMP = -1 ;    /**     * Attribute index - The indexer to use for that directory, if any.     */    protected static int ATTR_INDEXER = -1;    /**     * Attribute index - The index of wether we are extensible.     */    protected static int ATTR_EXTENSIBLE = -1 ;    /**     * Attribute index - The index of wether we can be shrinked.     */    protected static int ATTR_SHRINKABLE = -1 ;    static String di = "directory".intern();    static {	Attribute a   = null ;	Class     cls = null ;	// Get a pointer to our class.	try {	    cls = Class.forName("org.w3c.tools.resources.DirectoryResource") ;	} catch (Exception ex) {	    ex.printStackTrace() ;	    System.exit(1) ;	}	// The directory attribute.	a = new FileAttribute("directory"			      , null			      , Attribute.COMPUTED|Attribute.DONTSAVE);	ATTR_DIRECTORY = AttributeRegistry.registerAttribute(cls, a) ;	// The last time we visited the directory	a = new DateAttribute("dirstamp"			      , null			      , Attribute.COMPUTED) ;	ATTR_DIRSTAMP = AttributeRegistry.registerAttribute(cls, a) ;	// Our indexer name (optional).	a = new StringAttribute("indexer"				, null				, Attribute.EDITABLE) ;	ATTR_INDEXER = AttributeRegistry.registerAttribute(cls, a) ;	// Are we extensible (can we create resources on the fly):	a = new BooleanAttribute("extensible"				 , Boolean.TRUE				 , Attribute.EDITABLE) ;	ATTR_EXTENSIBLE = AttributeRegistry.registerAttribute(cls, a) ;	// Are we shrinkable (can we delete resources on the fly):	a = new BooleanAttribute("shrinkable"				 , Boolean.TRUE				 , Attribute.EDITABLE) ;	ATTR_SHRINKABLE = AttributeRegistry.registerAttribute(cls, a) ;    }    /**     * Get the indexer out of the given context.     * @return A ResourceIndexer instance, guaranteeed not to be <strong>     * null</strong>.     */    protected ResourceReference getIndexer(ResourceContext c) {	IndexerModule   m = (IndexerModule) c.getModule(IndexerModule.NAME);	ResourceReference rr = m.getIndexer(c);	return rr;    }    public void setValue(int idx, Object value) {	super.setValue(idx, value);	if ( idx == ATTR_INDEXER ) {	    String indexer = getString(ATTR_INDEXER, null);	    	    if ( indexer != null ) {		ResourceContext c = null;		IndexerModule   m = null;		c = getContext();		m = (IndexerModule) c.getModule(IndexerModule.NAME);		m.registerIndexer(c, indexer);	    }	}    }    /**     * Get the physical directory exported by this resource.     * @return A non-null File object giving the directory of this resource.     */    public File getDirectory() {	return (File) getValue(ATTR_DIRECTORY, null) ;    }    /**     * Get the physical directory exported by this resource.     * @return A non-null File object giving the directory of this resource.     */    public File unsafeGetDirectory() {	return (File) unsafeGetValue(ATTR_DIRECTORY, null) ;    }    /**     * Get the absolute time at which we examined the physicall directory.     * @return The date (as a long number of ms since Java epoch), or     * <strong>-1</strong> if we never examined it before.     */    public long getDirStamp() {	return getLong(ATTR_DIRSTAMP, -1) ;    }    /**     * Get the extensible flag value.     * A DirectoryResource is extensible, if it is allowed to create new     * resources out of the file system knowledge on the fly.     * <p>Setting this flag might slow down the server. It unfortunatelly     * defaults to <strong>true</strong> until I have a decent admin     * program.     * @return A boolean <strong>true</strong> if the directory is     *    extensible.     */    public boolean getExtensibleFlag() {	return getBoolean(ATTR_EXTENSIBLE, true) ;    }    /**     * Get the extensible flag value.     * A DirectoryResource is extensible, if it is allowed to create new     * resources out of the file system knowledge on the fly.     * <p>Setting this flag might slow down the server. It unfortunatelly     * defaults to <strong>true</strong> until I have a decent admin     * program.     * @return A boolean <strong>true</strong> if the directory is     *    extensible.     */    public boolean getShrinkableFlag() {	return getBoolean(ATTR_SHRINKABLE, true) ;    }    /**     * Get the extensible flag value.     * A DirectoryResource is extensible, if it is allowed to create new     * resources out of the file system knowledge on the fly.     * <p>Setting this flag might slow down the server. It unfortunatelly     * defaults to <strong>true</strong> until I have a decent admin     * program.     * @return A boolean <strong>true</strong> if the directory is     *    extensible.     */    public boolean unsafeGetShrinkableFlag() {	Object value = unsafeGetValue(ATTR_SHRINKABLE, null) ;	if (value == null) {	    return true;	} else if ( value instanceof Boolean ) {	    return ((Boolean) value).booleanValue() ;	} else {	    throw new IllegalAttributeAccess(this					     , attributes[ATTR_SHRINKABLE]					     , "getBoolean") ;	}    }    /**     * A resource is about to be removed     * This handles the <code>RESOURCE_REMOVED</code> kind of events.     * @param evt The event describing the change.     */    public void resourceRemoved(StructureChangedEvent evt) {	super.resourceRemoved(evt);	if (! isUnloaded())	    markModified();    }    /**     * Create a DirectoryResource and the physical directory too.     * @param name the name of the resource.     * @return A ResourceReference instance.     */    public ResourceReference createDirectoryResource(String name) {	// Create an empty file:	File    file          = new File(getDirectory(), name) ;	boolean created       = false ;	boolean exists_before = false ;	try {	    if (file.exists()) {		if (! file.isDirectory())		    created = false;		else		    exists_before = true;	    } else {		file.mkdir();		created = true;	    }	} catch (Exception ex) {	    created = false;	}	if (! created)	    return null;	ResourceReference rr = createDefaultResource(name);	if (rr == null) {	    if (!exists_before)		file.delete();	    return null;	}	try {	    Resource r = rr.lock();	    if (! (r instanceof DirectoryResource)) {		try {		    r.delete();		} catch (MultipleLockException ex) {		    //OUCH!		    //manual delete		}		if (!exists_before)		    file.delete();		return null;	    }	} catch (InvalidResourceException ex) {	    if (!exists_before)		file.delete();	    return null;	} finally {	    rr.unlock();	}	return rr;    }    /**     * Create a Resource and the physical file too.     * @param name the name of the resource.     * @return A ResourceReference instance.     */    public ResourceReference createResource(String name) {	return createResource(name, null);    }    /**     * Create a Resource and the physical file too.     * @param name the name of the resource.     * @param req the protocol request.     * @return A ResourceReference instance.     */    public ResourceReference createResource(String name, 					    RequestInterface req)     {	// Create an empty file:	File    file    = new File(getDirectory(), name) ;	boolean created = false ;	if ( ! file.exists() ) {	    try {		(new RandomAccessFile(file, "rw")).close() ;		created = true ;	    } catch (Exception ex) {		created = false ;	    }	}	if (! created) 	    return null;	ResourceReference rr = createDefaultResource(name, req);	//if (rr == null)	file.delete();	return rr;    }    /**     * Index a Resource. Call the indexer.     * @param name The name of the resource to index.     * @param defs The defaults attributes.     * @return A resource instance.     * @see org.w3c.tools.resources.indexer.SampleResourceIndexer     */    private Resource index(String name, Hashtable defs) {	return index(name, defs, null);    }    /**     * Index a Resource. Call the indexer.     * @param name The name of the resource to index.     * @param defs The defaults attributes.     * @param req The protocol request.     * @return A resource instance.     * @see org.w3c.tools.resources.indexer.SampleResourceIndexer     */    protected Resource index(String name, 			     Hashtable defs, 			     RequestInterface req)     {	// Prepare a set of default parameters for the resource:	defs.put(id, name);	updateDefaultChildAttributes(defs);	ResourceContext context = getContext();	// Try to get the indexer to create the resource:	Resource    resource = null;	ResourceReference rr_indexer  = null;	ResourceReference rr_lastidx  = null;	while ( context != null ) {	    // Lookup for next indexer in hierarchy:	    do {		rr_indexer = getIndexer(context);		context = context.getParent();	    } while ((rr_indexer == rr_lastidx) && (context != null));	    // Is this a useful indexer ?	    if ((rr_lastidx = rr_indexer) != null ) {		try {		    ResourceIndexer indexer = 			(ResourceIndexer)rr_indexer.lock();		    resource = indexer.createResource(this,						      req,						      getDirectory(),						      name,						      defs) ;		    if ( resource != null ) 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -