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

📄 filedircontext.java

📁 Tomcat 4.1与WebServer集成组件的源代码包.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999 The Apache Software Foundation.  All rights  * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer.  * * 2. Rey appear in the software itself, *    if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software *    Foundation" must not be used to endorse or promote products derived *    from this software without prior written permission. For written  *    permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" *    nor may "Apache" appear in their names without prior written *    permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * [Additional notices, if required by prior licensing conditions] * */ package org.apache.naming.modules.fs;import java.util.Arrays;import java.util.Hashtable;import java.util.Vector;import java.util.Date;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.io.FileOutputStream;import java.io.FileNotFoundException;import java.io.OutputStream;import java.io.IOException;import javax.naming.Context;import javax.naming.Name;import javax.naming.NameParser;import javax.naming.NamingEnumeration;import javax.naming.NamingException;import javax.naming.CompositeName;import javax.naming.NameParser;import javax.naming.OperationNotSupportedException;import javax.naming.NameAlreadyBoundException;import javax.naming.directory.DirContext;import javax.naming.directory.Attributes;import javax.naming.directory.Attribute;import javax.naming.directory.ModificationItem;import javax.naming.directory.SearchControls;import org.apache.tomcat.util.res.StringManager;import org.apache.naming.core.*;/** * DirContext for a filesystem directory. * * The 'bind' operation will accept an InputStream ( TODO: File, any * resource with content ) * and create the file. ( TODO: what attributes can we support ? ) * * The lookup operation will return a FileDirContext or a File. * * Supported attributes: (TODO: lastModified, size, ...) * * Note that JNDI allows memory-efficient style, without having one wrapper * object for each real resource. * * @author Remy Maucherat * @author Costin Manolache */public class FileDirContext extends BaseDirContext {        private static org.apache.commons.logging.Log log=        org.apache.commons.logging.LogFactory.getLog( FileDirContext.class );    // -------------------------------------------------------------- Constants    protected StringManager sm =        StringManager.getManager("org.apache.naming.res");    protected static final int BUFFER_SIZE = 2048;    // ----------------------------------------------------------- Constructors    /**     * Builds a file directory context using the given environment.     */    public FileDirContext() {        super();    }    /**     * Builds a file directory context using the given environment.     */    public FileDirContext(Hashtable env) {        super(env);    }    // ----------------------------------------------------- Instance Variables    /**     * The document base directory.     */    protected File base = null;    /**     * Absolute normalized filename of the base.     */    protected String absoluteBase = null;    /**     * Case sensitivity.     */    protected boolean caseSensitive = true;    /**     * The document base path.     */    protected String docBase = null;    // ------------------------------------------------------------- Properties    /**     * Set the document root.     *      * @param docBase The new document root     *      * @exception IllegalArgumentException if the specified value is not     *  supported by this implementation     * @exception IllegalArgumentException if this would create a     *  malformed URL     */    public void setDocBase(String docBase) {	// Validate the format of the proposed document root	if (docBase == null)	    throw new IllegalArgumentException		(sm.getString("resources.null"));	// Calculate a File object referencing this document base directory	base = new File(docBase);        try {            base = base.getCanonicalFile();        } catch (IOException e) {            // Ignore        }	// Validate that the document base is an existing directory	if (!base.exists() || !base.isDirectory() || !base.canRead())	    throw new IllegalArgumentException		(sm.getString("fileResources.base", docBase));        this.absoluteBase = base.getAbsolutePath();	// Change the document root property	this.docBase = docBase;    }    /**     * Return the document root for this component.     */    public String getDocBase() {	return (this.docBase);    }    /**     * Set case sensitivity.     */    public void setCaseSensitive(boolean caseSensitive) {        this.caseSensitive = caseSensitive;    }    /**     * Is case sensitive ?     */    public boolean isCaseSensitive() {        return caseSensitive;    }    // --------------------------------------------------------- Public Methods    /**     * Release any resources allocated for this directory context.     */    public void release() {        caseSensitive = true;        absoluteBase = null;        base = null;        super.release();    }    // -------------------- BaseDirContext implementation --------------------     /**     * Retrieves the named object. The result is a File relative to the docBase     * or a FileDirContext for directories.     *      * @param name the name of the object to look up     * @return the object bound to name     * @exception NamingException if a naming exception is encountered     */    public Object lookup(Name nameObj, boolean resolveLinkx)        throws NamingException    {        if( log.isDebugEnabled() ) log.debug( "lookup " + nameObj );        System.out.println("XXX " + nameObj.get(0));        if( "fs:".equals( nameObj.get(0).toString() ))            nameObj=nameObj.getSuffix(1);                String name=nameObj.toString(); // we need to convert anyway, for File constructor                Object result = null;        File file = file(name);                if (file == null)            throw new NamingException                (sm.getString("resources.notFound", name));                if (file.isDirectory()) {            FileDirContext tempContext = new FileDirContext(env);            tempContext.setDocBase(file.getPath());            result = tempContext;        } else {            // TODO: based on the name, return various 'styles' of            // content            // TODO: use lazy streams, cacheable            result = file; //new FileResource(file);        }                return result;    }    /**     * Unbinds the named object. Removes the terminal atomic name in name      * from the target context--that named by all but the terminal atomic      * part of name.     * <p>     * This method is idempotent. It succeeds even if the terminal atomic      * name is not bound in the target context, but throws      * NameNotFoundException if any of the intermediate contexts do not exist.      *      * @param name the name to bind; may not be empty     * @exception NameNotFoundException if an intermediate context does not      * exist     * @exception NamingException if a naming exception is encountered     */    public void unbind(Name nameObj)        throws NamingException    {        if( "fs:".equals( nameObj.get(0).toString() ))            nameObj=nameObj.getSuffix(1);        String name=nameObj.toString();        if( log.isDebugEnabled() ) log.debug( "unbind " + name );        File file = file(name);        if (file == null)            throw new NamingException                (sm.getString("resources.notFound", name));        if (!file.delete())            throw new NamingException                (sm.getString("resources.unbindFailed", name));    }    /**     * Binds a new name to the object bound to an old name, and unbinds the      * old name. Both names are relative to this context. Any attributes      * associated with the old name become associated with the new name.      * Intermediate contexts of the old name are not changed.     *      * @param oldName the name of the existing binding; may not be empty     * @param newName the name of the new binding; may not be empty     * @exception NameAlreadyBoundException if newName is already bound     * @exception NamingException if a naming exception is encountered     */    public void rename(Name oldNameO, Name newNameO)        throws NamingException    {        String oldName=oldNameO.toString();        String newName=newNameO.toString();        File file = file(oldName);        if (file == null)            throw new NamingException                (sm.getString("resources.notFound", oldName));        File newFile = new File(base, newName);                file.renameTo(newFile);            }    /**     * Enumerates the names bound in the named context, along with the class      * names of objects bound to them. The contents of any subcontexts are      * not included.     * <p>     * If a binding is added to or removed from this context, its effect on      * an enumeration previously returned is undefined.     *      * @param name the name of the context to list     * @return an enumeration of the names and class names of the bindings in      * this context. Each element of the enumeration is of type NameClassPair.     * @exception NamingException if a naming exception is encountered     */    public NamingEnumeration list(String name)        throws NamingException    {        if( log.isDebugEnabled() ) log.debug( "list " + name );        File file = file(name);        if (file == null)            throw new NamingException                (sm.getString("resources.notFound", name));        Vector entries = list(file);        return new NamingContextEnumeration(entries.elements(), this, false);    }    /**     * Enumerates the names bound in the named context, along with the      * objects bound to them. The contents of any subcontexts are not      * included.     * <p>     * If a binding is added to or removed from this context, its effect on      * an enumeration previously returned is undefined.     *      * @param name the name of the context to list     * @return an enumeration of the bindings in this context.      * Each element of the enumeration is of type Binding.     * @exception NamingException if a naming exception is encountered     */    public NamingEnumeration listBindings(String name)        throws NamingException    {        if( log.isDebugEnabled() ) log.debug( "listBindings " + name );        File file = file(name);        if (file == null)            throw new NamingException

⌨️ 快捷键说明

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