📄 wardircontext.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.naming.resources;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import javax.naming.CompositeName;
import javax.naming.Name;
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;
/**
* WAR Directory Context implementation.
*
* @author Remy Maucherat
* @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
*/
public class WARDirContext extends BaseDirContext {
private static org.apache.juli.logging.Log log=
org.apache.juli.logging.LogFactory.getLog( WARDirContext.class );
// ----------------------------------------------------------- Constructors
/**
* Builds a WAR directory context using the given environment.
*/
public WARDirContext() {
super();
}
/**
* Builds a WAR directory context using the given environment.
*/
public WARDirContext(Hashtable env) {
super(env);
}
/**
* Constructor used for returning fake subcontexts.
*/
protected WARDirContext(ZipFile base, Entry entries) {
this.base = base;
this.entries = entries;
}
// ----------------------------------------------------- Instance Variables
/**
* The WAR file.
*/
protected ZipFile base = null;
/**
* WAR entries.
*/
protected Entry entries = 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"));
if (!(docBase.endsWith(".war")))
throw new IllegalArgumentException
(sm.getString("warResources.notWar"));
// Calculate a File object referencing this document base directory
File base = new File(docBase);
// Validate that the document base is an existing directory
if (!base.exists() || !base.canRead() || base.isDirectory())
throw new IllegalArgumentException
(sm.getString("warResources.invalidWar", docBase));
try {
this.base = new ZipFile(base);
} catch (Exception e) {
throw new IllegalArgumentException
(sm.getString("warResources.invalidWar", e.getMessage()));
}
super.setDocBase(docBase);
loadEntries();
}
// --------------------------------------------------------- Public Methods
/**
* Release any resources allocated for this directory context.
*/
public void release() {
entries = null;
if (base != null) {
try {
base.close();
} catch (IOException e) {
log.warn
("Exception closing WAR File " + base.getName(), e);
}
}
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 {
return lookup(new CompositeName(name));
}
/**
* Retrieves the named object. If name is empty, returns a new instance
* of this context (which represents the same naming context as this
* context, but its environment may be modified independently and it may
* be accessed concurrently).
*
* @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 name)
throws NamingException {
if (name.isEmpty())
return this;
Entry entry = treeLookup(name);
if (entry == null)
throw new NamingException
(sm.getString("resources.notFound", name));
ZipEntry zipEntry = entry.getEntry();
if (zipEntry.isDirectory())
return new WARDirContext(base, entry);
else
return new WARResource(entry.getEntry());
}
/**
* 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 {
throw new OperationNotSupportedException();
}
/**
* 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 {
throw new OperationNotSupportedException();
}
/**
* 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 {
return list(new CompositeName(name));
}
/**
* 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(Name name)
throws NamingException {
if (name.isEmpty())
return new NamingContextEnumeration(list(entries).iterator());
Entry entry = treeLookup(name);
if (entry == null)
throw new NamingException
(sm.getString("resources.notFound", name));
return new NamingContextEnumeration(list(entry).iterator());
}
/**
* 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 {
return listBindings(new CompositeName(name));
}
/**
* 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -