FileDocCategorySizeDatePackage
VirtualDirContext.javaAPI DocApache Tomcat 6.0.147842Fri Jul 20 04:20:36 BST 2007org.apache.naming.resources

VirtualDirContext

public class VirtualDirContext extends FileDirContext
Extended FileDirContext implementation that will allow loading of tld files from the META-INF directory (or subdirectories) in classpath. This will fully mimic the behavior of compressed jars also when using unjarred resources. Tld files can be loaded indifferently from WEB-INF webapp dir (or subdirs) or from META-INF dir from jars available in the classpath: using this DirContext implementation you will be able to use unexpanded jars during development and to make any tld in them virtually available to the webapp. Sample context xml configuration: <Context docBase="\webapps\mydocbase"> <Resources className="org.apache.naming.resources.VirtualDirContext" virtualClasspath="\dir\classes;\somedir\somejar.jar"/> </Resources> This is not meant to be used for production. Its meant to ease development with IDE's without the need for fully republishing jars in WEB-INF/lib
author
Fabrizio Giustina
version
$Id: $

Fields Summary
private Map
virtualMappings
Map containing generated virtual names for tld files under WEB-INF and the actual file reference.
private Map
tagfileMappings
Map containing a mapping for tag files that should be loaded from the META-INF dir of referenced jar files.
private String
virtualClasspath
; separated list of virtual path elements.
Constructors Summary
Methods Summary
public voidallocate()
{@inheritDoc}

        super.allocate();

        virtualMappings = new Hashtable<String, File>();
        tagfileMappings = new Hashtable<String, File>();

        // looks into any META-INF dir found in classpath entries for tld files.
        StringTokenizer tkn = new StringTokenizer(virtualClasspath, ";");
        while (tkn.hasMoreTokens()) {
            File file = new File(tkn.nextToken(), "META-INF");

            if (!file.exists() || !file.isDirectory()) {
                continue;
            }
            scanForTlds(file);
        }
    
public javax.naming.directory.AttributesgetAttributes(java.lang.String name)


        // handle "virtual" tlds
        if (name.startsWith("/WEB-INF/") && name.endsWith(".tld")) {
            String tldName = name.substring(name.lastIndexOf("/") + 1);
            if (virtualMappings.containsKey(tldName)) {
                return new FileResourceAttributes(virtualMappings.get(tldName));
            }
        } else if (name.startsWith("/META-INF/tags") && name.endsWith(".tag")
                || name.endsWith(".tagx")) {

            // already loaded tag file
            if (tagfileMappings.containsKey(name)) {
                return new FileResourceAttributes(tagfileMappings.get(name));
            }

            // unknown tagfile, search for it in virtualClasspath
            StringTokenizer tkn = new StringTokenizer(virtualClasspath, ";");
            while (tkn.hasMoreTokens()) {
                File file = new File(tkn.nextToken(), name);
                if (file.exists()) {
                    tagfileMappings.put(name, file);
                    return new FileResourceAttributes(file);
                }
            }
        }

        return super.getAttributes(name);
    
private java.util.ListgetVirtualNamingEntries()
Returns a list of virtual naming entries.

return
list of naming entries, containing tlds in virtualMappings

        List<NamingEntry> virtual = new ArrayList<NamingEntry>();

        for (String name : virtualMappings.keySet()) {

            File file = virtualMappings.get(name);
            NamingEntry entry = new NamingEntry(name, new FileResource(file),
                    NamingEntry.ENTRY);
            virtual.add(entry);
        }
        return virtual;
    
protected java.util.ArrayListlist(java.io.File file)

        ArrayList entries = super.list(file);

        // adds virtual tlds for WEB-INF listing
        if ("WEB-INF".equals(file.getName())) {
            entries.addAll(getVirtualNamingEntries());
        }

        return entries;
    
public java.lang.Objectlookup(java.lang.String name)


        // handle "virtual" tlds
        if (name.startsWith("/WEB-INF/") && name.endsWith(".tld")) {
            String tldName = name.substring(name.lastIndexOf("/") + 1);
            if (virtualMappings.containsKey(tldName)) {
                return new FileResource(virtualMappings.get(tldName));
            }
        } else if (name.startsWith("/META-INF/tags") && name.endsWith(".tag")
                || name.endsWith(".tagx")) {

            // already loaded tag file: we are sure that getAttributes() has
            // already been called if we are here
            File tagFile = tagfileMappings.get(name);
            if (tagFile != null) {
                return new FileResource(tagFile);
            }
        }

        return super.lookup(name);
    
public voidrelease()
{@inheritDoc}

        super.release();
        virtualMappings = null;
    
private voidscanForTlds(java.io.File dir)
Scan a given dir for tld files. Any found tld will be added to the virtualMappings.

param
dir Dir to scan for tlds


        File[] files = dir.listFiles();
        for (int j = 0; j < files.length; j++) {
            File file = files[j];

            if (file.isDirectory()) {
                scanForTlds(file);
            } else if (file.getName().endsWith(".tld")) {
                // just generate a random name using the current timestamp, name
                // doesn't matter since it needs to be referenced by URI
                String virtualTldName = "~" + System.currentTimeMillis() + "~"
                        + file.getName();
                virtualMappings.put(virtualTldName, file);
            }
        }

    
public voidsetVirtualClasspath(java.lang.String path)
virtualClasspath attribute that will be automatically set from the Context virtualClasspath attribute from the context xml file.

param
path ; separated list of path elements.

        virtualClasspath = path;