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

📄 directoryresource.java

📁 很棒的web服务器源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			break;		} catch (InvalidResourceException ex) {		    resource = null;		} finally {		    rr_indexer.unlock();		}	    }	}	return resource;    }    /**     * Get the name of the resource relative to the given filename.     * @param name The name of the file.     * @return a String, the resource name.     * @see org.w3c.tools.resources.indexer.SampleResourceIndexer     */    protected String getIndexedName(String name) {	ResourceContext   context     = getContext();	String            indexed     = 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));	    if ((rr_lastidx = rr_indexer) != null ) {		try {		    ResourceIndexer indexer = 			(ResourceIndexer)rr_indexer.lock();		    indexed = indexer.getIndexedName(getDirectory(), name);		    if ( indexed != null ) 			break;		} catch (InvalidResourceException ex) {		    indexed = null;		} finally {		    rr_indexer.unlock();		}	    }	}	return ((indexed == null) ? name : indexed);    }    public synchronized ResourceReference createDefaultResource(String name) {	return createDefaultResource(name, null);    }    /**     * 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.     * @param req The incomming request     * @return A Resource instance, if possible, <strong>null</strong>     *    otherwise.     */    protected synchronized 	ResourceReference createDefaultResource(String name,						RequestInterface req)     {	// Don't automagically create resources of name '..' or '.'	if (name.equals("..") || name.equals(".") 	    || (name.indexOf('\\') >= 0 )) {	    return null ;	}	Hashtable defs = new Hashtable(10) ;	Resource resource = index(name, defs, req);	// Did we finally create a resource ?	ResourceReference rr = null;	if ( resource != null ) {	    // Register this child in our store:	    rr = addResource(resource, defs) ;	    markModified() ;	}	return rr ;    }    /**     * Initialize and register a new resource into this directory.     * @param resource The uninitialized resource to be added.     */    protected ResourceContext updateDefaultChildAttributes(Hashtable attrs) {	ResourceContext context = null;	context = super.updateDefaultChildAttributes(attrs);	String name = (String) attrs.get(id);	if (( name != null ) && (getDirectory() !=  null)) {	    attrs.put(di, new File(getDirectory(), name));	}	return context;    }    /**     * Reindex recursivly all the resources from this DirectoryResource.     * @param rec recursivly?     */    public synchronized void reindex(boolean rec) {	if (getExtensibleFlag()) {	    Enumeration       e    = enumerateAllResourceIdentifiers();	    String            name = null;	    ResourceReference rr   = null;	    Resource          r    = null;	    while (e.hasMoreElements()) {		name = (String) e.nextElement();		rr = lookup(name);		if (rr != null) {		    try {			r = rr.lock();			// forbid cycles			if (r == this)			    continue;			if (r instanceof DirectoryResource) {			    //launch reindex			    DirectoryResource dir = (DirectoryResource) r;			    //reindex directory itself			    //the new diretory must have the same context			    Hashtable defs = new Hashtable(5);			    defs.put(co, dir.getContext());			    //indexing ...			    Resource newdir = index(name, defs);			    // do we want it to keep its indexer?			    if ((newdir == null) && rec) {				dir.reindex(true);			    } else {				if (! (newdir instanceof DirectoryResource)) {				    throw new RuntimeException(					       "Reindex Error : "+					       name+" can't be reindexed. "+					       "The reindexed resource is "+					       "no more a DirectoryResource.");				}				DirectoryResource reindexed = 				    (DirectoryResource) newdir;				String indexer = 				    reindexed.getString(ATTR_INDEXER, "");				if (indexer.equals("")) {				    if (rec) {					dir.reindex(true);				    }				    indexer = 					dir.getString(ATTR_INDEXER, null);				    reindexed.setValue(ATTR_INDEXER, indexer);				} else {				    dir.setValue(ATTR_INDEXER, indexer);				    if (rec) {					dir.reindex(true);				    }				}				//move children to the reindexed directory				reindexed.setValue(ATTR_KEY, dir.getKey());				dir.setValue(ATTR_IDENTIFIER, 					     name+"-bakindex");				addResource(reindexed, defs);				// Now replace the old DirectoryResource 				// by the new one				try {				    dir.replace(reindexed);				} catch (MultipleLockException ex) {				    throw new RuntimeException(							   "Reindex Error : "+							   ex.getMessage());				}			    }			} else if (! (r instanceof AbstractContainer)) {			    //leaf			    Hashtable resdefs = new Hashtable(10);			    Resource resource = index(name, resdefs);			    if (resource != null) {				try {				    r.delete();				} catch (MultipleLockException ex) {				    throw new RuntimeException(							    "Reindex Error : "+							    ex.getMessage());				}				addResource(resource, resdefs);			    }			}		    } catch (InvalidResourceException ex) {			System.out.println(ex.getMessage());		    } finally {			rr.unlock();		    }		}	    }	    markModified();	}    }    /**     * Enumerate all available children resource identifiers.      * This method <em>requires</em> that we create all our pending resources.     * @return An enumeration of all our resources.     */    protected synchronized Enumeration enumerateAllResourceIdentifiers() {	File directory = getDirectory() ;	if ( directory != null ) {	    synchronized(this) {		String lst[] = directory.list() ;		if ( lst != null ) {		    for (int i = 0 ; i < lst.length ; i++) {			if (lst[i].equals(".") || lst[i].equals(".."))			    continue ;			if (super.lookup(lst[i]) == null) {			    String indexed = getIndexedName(lst[i]);			    if (indexed.equals(lst[i]))				createDefaultResource(lst[i]) ;			    else if (super.lookup(indexed) == null)		 		createDefaultResource(lst[i]) ;			}		    }		}	    }	}	return super.enumerateResourceIdentifiers(true);     }    /**     * Enumerate all available children resource identifiers.      * This method <em>requires</em> that we create all our pending resources     * if we are in the extensible mode...too bad !     * @return An enumeration of all our resources.     */    public synchronized Enumeration enumerateResourceIdentifiers(boolean all) {	// If extensible, update if needed:	if (all && getExtensibleFlag() ) {	    File directory = getDirectory() ;	    if ( directory != null ) {		synchronized(this) {		    long dirstamp  = directory.lastModified() ;		    if ( dirstamp > getDirStamp() ) {			String lst[] = directory.list() ;			if ( lst != null ) {			    for (int i = 0 ; i < lst.length ; i++) {				if (lst[i].equals(".") || lst[i].equals(".."))				    continue ;				if (super.lookup(lst[i]) == null) {				    String indexed = getIndexedName(lst[i]);				    if (indexed.equals(lst[i]))					createDefaultResource(lst[i]) ;				    else if (super.lookup(indexed) == null)					createDefaultResource(lst[i]) ;				}			    }			}			setLong(ATTR_DIRSTAMP, dirstamp) ;		    }		}	    }	}	return super.enumerateResourceIdentifiers(all);    }    /**     * Lookup the resource having the given name in this directory.     * @param name The name of the resource.     * @return A resource instance, or <strong>null</strong>.     */    public ResourceReference lookup(String name)     {	ResourceReference rr = null;	// Try our store:	rr = super.lookup(name);	if (rr != null)	    return rr;	// If allowed, than try a default fallback:	return getExtensibleFlag() ? createDefaultResource(name) : null ;    }    /**     * Delete this directory resource, for ever.     * This method will delete the directory resource, and its associated      * store, <strong>along</strong> with any of the sub-resources it contains.     * Deleting the root directory of your server might take sometime...     * <p>Once the resource is deleted, it isx1 removed from its inital store     * and will not be unpickleable any more.     * @exception MultipleLockException if someone has locked this resource.     */    public synchronized void delete() 	throws MultipleLockException    {	disableEvent();	// Remove all the defined resources in this directory	// Set the extensible flag to false, otherwise, the directory grows	// as we shrink it :-)	setBoolean(ATTR_EXTENSIBLE, false);	super.delete();    }    /**     * Was return false (don't khow why)     */    public synchronized boolean verify() {	return getDirectory().exists();    }    /**     * Initialize this directory resource with the given set of attributes.     * @param values The attribute values.     */    public void initialize(Object values[]) {	super.initialize(values) ;	disableEvent();	// Get our parent resource and compute our directory:	File dir = null ;	if ( ! definesAttribute(ATTR_DIRECTORY) ) {	    // Get our parent:	    ResourceReference rr = getParent();	    if (rr != null) {		try {		    Resource parent = rr.lock();		    if (parent.definesAttribute(di)) {			File pdir = (File) parent.getValue(di, null);			if ( pdir != null ) {			    // Compute and set our directory attribute:			    dir = new File(pdir, getIdentifier()) ;			    setValue(ATTR_DIRECTORY, dir) ;			}		    }		} catch (InvalidResourceException ex) {	  		} finally {		    rr.unlock();		}	    }	} else {	    dir = getDirectory();	}	// Register our specific indexer, if any:	ResourceContext c = getContext();	String indexer = getString(ATTR_INDEXER, null);	if (( indexer != null ) && (!indexer.equals(""))) {	    IndexerModule   m = (IndexerModule)c.getModule(IndexerModule.NAME);	    m.registerIndexer(c, indexer);	}	enableEvent();    }}

⌨️ 快捷键说明

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