📄 filedircontext.java
字号:
/*
* $Header: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/naming/resources/FileDirContext.java,v 1.3 2003/09/02 21:22:03 remm Exp $
* $Revision: 1.3 $
* $Date: 2003/09/02 21:22:03 $
*
* ====================================================================
*
* 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. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may 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.resources;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;
import javax.naming.NameAlreadyBoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.OperationNotSupportedException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchControls;
import org.apache.naming.NamingContextBindingsEnumeration;
import org.apache.naming.NamingContextEnumeration;
import org.apache.naming.NamingEntry;
/**
* Filesystem Directory Context implementation helper class.
*
* @author Remy Maucherat
* @version $Revision: 1.3 $ $Date: 2003/09/02 21:22:03 $
*/
public class FileDirContext extends BaseDirContext {
// -------------------------------------------------------------- Constants
/**
* The descriptive information string for this implementation.
*/
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;
/**
* Allow linking.
*/
protected boolean allowLinking = false;
// ------------------------------------------------------------- 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();
super.setDocBase(docBase);
}
/**
* Set case sensitivity.
*/
public void setCaseSensitive(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}
/**
* Is case sensitive ?
*/
public boolean isCaseSensitive() {
return caseSensitive;
}
/**
* Set allow linking.
*/
public void setAllowLinking(boolean allowLinking) {
this.allowLinking = allowLinking;
}
/**
* Is linking allowed.
*/
public boolean getAllowLinking() {
return allowLinking;
}
// --------------------------------------------------------- Public Methods
/**
* Release any resources allocated for this directory context.
*/
public void release() {
caseSensitive = true;
allowLinking = false;
absoluteBase = null;
base = null;
super.release();
}
// -------------------------------------------------------- Context Methods
/**
* Retrieves the named object.
*
* @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(String name)
throws NamingException {
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 {
result = 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(String name)
throws NamingException {
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(String oldName, String newName)
throws NamingException {
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 {
File file = file(name);
if (file == null)
throw new NamingException
(sm.getString("resources.notFound", name));
Vector entries = list(file);
return new NamingContextEnumeration(entries);
}
/**
* 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 {
File file = file(name);
if (file == null)
throw new NamingException
(sm.getString("resources.notFound", name));
Vector entries = list(file);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -