Package org.mortbay.util

Examples of org.mortbay.util.Resource


     * @param useDevConfig <code>true</code> if running in development mode
     * @throws Exception on any error
     */
    public CustomWebApplicationContext(boolean useDevConfig, ClassLoader bootLoader) throws Exception {
        super("webapp");
        Resource webInf = getWebInf();
        additionalClasspath = "";
        resourceCacheMap = new HashMap<String, ResourceCache>();
        reverseCaches = new ArrayList<ResourceCache>();
        cacheState = new HashMap<String, CacheState>();
        setContextPath("/");
View Full Code Here


     * @see org.mortbay.http.ResourceCache#getResource(java.lang.String)
     */
    public Resource getResource(String pathInContext) throws IOException {
        boolean fullResourceCache = SystemProperties.get("adito.fullResourceCache",
            String.valueOf(!(SystemProperties.get("adito.useDevConfig", "false").equals("true")))).equals("true");
        Resource r;
        if (log.isDebugEnabled())
            log.debug("Request for " + pathInContext);

        // This is a work around to prevent WEB-INF getting listed by using the
        // path //WEB-INF
        if (pathInContext.indexOf("//WEB-INF") != -1) {
            return null;
        }

        /*
         * When in 'Full resource cache' mode, check if we already have cached
         * the resource
         */
        if (fullResourceCache && cacheState.containsKey(pathInContext)) {
            r = cacheState.get(pathInContext).getResource();
            if (log.isDebugEnabled())
                if (r == null)
                    log.debug("Resource " + pathInContext + " is permanently as missing.");
                else
                    log.debug("Resource " + pathInContext + " found in permanent cache");
            return r;
        }

        /*
         * Determine if the resource has already been found in a resource cache
         * (be it an extensions resource cache or the cores)
         */

        ResourceCache o = fullResourceCache ? null : (ResourceCache) resourceCacheMap.get(pathInContext);
        if (o == null) {

            /*
             * The existence of the resource has not yet been determined. Search
             * all resource caches in reverse until the extension is found. When
             * found, store which cache it was found in for quick look up in the
             * future.
             */
            if (log.isDebugEnabled())
                log.debug("Resource " + pathInContext + " not found in any resource cache, checking in plugins");

            for (Iterator i = reverseCaches.iterator(); i.hasNext();) {
                ResourceCache cache = (ResourceCache) i.next();
                r = cache.getResource(pathInContext);
                if (r != null && r.exists() && !r.isDirectory()) {
                    if (fullResourceCache) {
                        if (log.isDebugEnabled())
                            log.debug("    Found in " + cache.getBaseResource().toString());
                        cacheState.put(pathInContext, new CacheState(CacheState.FOUND, pathInContext, r));
                    } else {
                        if (log.isDebugEnabled())
                            log.debug("    Found in " + cache.getBaseResource().toString());
                        resourceCacheMap.put(pathInContext, cache);
                    }
                    return r;
                }
            }

            /*
             * The resource cannot be found in this caches base directory
             */
            if (log.isDebugEnabled())
                log.debug("   Not found");
        } else {
            /*
             * We know what cache the resource came from so check it still
             * exists and return. This will only happen when not in full cache
             * mode
             */
            r = o.getResource(pathInContext);
            if (r != null && r.exists() && !r.isDirectory()) {
                if (log.isDebugEnabled())
                    log.debug("    Found in " + o.getBaseResource().toString());
                return r;
            }
        }

        if (log.isDebugEnabled())
            log.debug("Checking for alias in plugins");
        String resourceAlias = getResourceAlias(pathInContext);
        if (resourceAlias != null) {

            /*
             * The resource was not found with its real name in any caches base
             * directory, so repeat the operation but look for the alias
             */

            if (log.isDebugEnabled())
                log.debug("    Found alias of " + resourceAlias + ", checking in plugins");
            for (Iterator i = reverseCaches.iterator(); i.hasNext();) {
                ResourceCache cache = (ResourceCache) i.next();
                r = cache.getResource(resourceAlias);

                /*
                 * When checking for resource modification, check for existence
                 * of file. This allows file to be removed at runtime without
                 * adding overhead when used on deployed server
                 */

                if (r != null && r.exists() && !r.isDirectory()) {
                    if (fullResourceCache) {
                        if (log.isDebugEnabled())
                            log.debug("    Found in " + cache.getBaseResource().toString());
                        cacheState.put(pathInContext, new CacheState(CacheState.FOUND, pathInContext, r));
                        return r;
                    } else {
                        if (log.isDebugEnabled())
                            log.debug("    Found in " + cache.getBaseResource().toString());
                        resourceCacheMap.put(pathInContext, cache);
                        return r;
                    }
                }
            }
            if (log.isDebugEnabled())
                log.debug("   Not found");
        }

        /*
         * The resource could not be found in any caches base directory, so pass
         * to the main webapp
         */

        if (log.isDebugEnabled())
            log.debug("Passing to main webapp");
        r = super.getResource(pathInContext);
        if (r != null && r.exists() && !r.isDirectory()) {

            /*
             * The resource has been found in the main webapps base directory,
             * store where it was found for quick lookup in future requests
             */
 
View Full Code Here

      if (log.isDebugEnabled())
        log.debug("Getting resource " + pathInContext + ", checking in plugins");
        // First try all the plugins with the path for an overidden resource
        for (Iterator i = resourceCaches.iterator(); i.hasNext();) {
            ResourceCache cache = (ResourceCache) i.next();
            Resource r = cache.getResource(pathInContext);
            if (r != null && r.exists() && !r.isDirectory()) {
              if (log.isDebugEnabled())
                log.debug("Found in " + cache.getBaseResource().toString());
                return r;
            }
        }
View Full Code Here

    resourceLocator = new FsResourceLocator(tempFile.getParentFile());
    context = new HttpContext();
  }

  public void testShouldGetResourceFromRootDir() throws Exception {
    Resource resource = resourceLocator.getResource(context, tempFile.getName());
    assertTrue(resource.exists());
    assertNotNull(resource.getInputStream());
    assertEquals(tempFile.getAbsolutePath(), resource.getFile().getAbsolutePath());
  }
View Full Code Here

      throws Exception {
    assertFalse(resourceLocator.getResource(context, "not_exists").exists());
  }

  public void testShouldReturnFilePathFromToString() throws Exception {
    Resource resource = resourceLocator.getResource(context, tempFile.getName());
    assertTrue("toString() must end with filename, because Jetty used this method to determine file type",
        resource.toString().endsWith(tempFile.getName()));
  }
View Full Code Here

  public void testHackForJsUserExtensionsLocating() throws Exception {
    File extension = new File("user-extensions.js").getAbsoluteFile();
    extension.createNewFile();
    extension.deleteOnExit();
    FsResourceLocator extensionLocator = new FsResourceLocator(extension.getParentFile());
    Resource resource = extensionLocator.getResource(context, "some/path/user-extensions.js");
    assertTrue(resource.exists());
    assertEquals(extension.getAbsolutePath(), resource.getFile().getAbsolutePath());
  }
View Full Code Here

import java.io.IOException;

public class ClasspathResourceLocatorUnitTest extends TestCase {
    public void testShouldGetResourceFromClasspath() throws Exception {
        Resource resource = getResourceFromClasspath("ClasspathResourceLocatorUnitTest.class");
        assertNotNull(resource.getInputStream());
    }
View Full Code Here

        Resource resource = getResourceFromClasspath("ClasspathResourceLocatorUnitTest.class");
        assertNotNull(resource.getInputStream());
    }

    public void testShouldReturnMissingResourceWhenResourceNotFound() throws Exception {
        Resource resource = getResourceFromClasspath("not_exists");
        assertFalse(resource.exists());
        assertNull(resource.getInputStream());
    }
View Full Code Here

        assertNull(resource.getInputStream());
    }

    public void testShouldStoreFileNameInMetaData() throws Exception {
      String filename = "ClasspathResourceLocatorUnitTest.class";
        Resource resource = getResourceFromClasspath(filename);
        assertEquals("toString() must end with filename, because Jetty used this method to determine file type",
            filename, resource.toString());   
  }
View Full Code Here

        });
        assertEquals(file, handler.getResource(file.toURI().toURL().toString()).getFile());
    }

    public void testShouldReturnMissingResourceIfNoResourceLocated() throws Exception {
        Resource resource = handler.getResource("not exists path");
        assertFalse(resource.exists());
    }
View Full Code Here

TOP

Related Classes of org.mortbay.util.Resource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.