Search in sources :

Example 6 with PathResource

use of org.eclipse.jetty.util.resource.PathResource in project jetty.project by eclipse.

the class WebAppClassLoaderTest method init.

@Before
public void init() throws Exception {
    this.testWebappDir = MavenTestingUtils.getProjectDirPath("src/test/webapp");
    Resource webapp = new PathResource(testWebappDir);
    _context = new WebAppContext();
    _context.setBaseResource(webapp);
    _context.setContextPath("/test");
    _loader = new WebAppClassLoader(_context);
    _loader.addJars(webapp.addPath("WEB-INF/lib"));
    _loader.addClassPath(webapp.addPath("WEB-INF/classes"));
    _loader.setName("test");
    _context.loadSystemClasses();
    _context.loadServerClasses();
}
Also used : PathResource(org.eclipse.jetty.util.resource.PathResource) PathResource(org.eclipse.jetty.util.resource.PathResource) Resource(org.eclipse.jetty.util.resource.Resource) Before(org.junit.Before)

Example 7 with PathResource

use of org.eclipse.jetty.util.resource.PathResource in project jetty.project by eclipse.

the class DefaultFileLocatorHelper method getBundleInstallLocation.

/**
     * Works with equinox, felix, nuxeo and probably more. Not exactly in the
     * spirit of OSGi but quite necessary to support self-contained webapps and
     * other situations.
     * 
     * @param bundle The bundle
     * @return Its installation location as a file.
     * @throws Exception if unable to get the bundle install location
     */
public File getBundleInstallLocation(Bundle bundle) throws Exception {
    // String installedBundles = System.getProperty("osgi.bundles");
    // grab the MANIFEST.MF's url
    // and then do what it takes.
    URL url = bundle.getEntry("/META-INF/MANIFEST.MF");
    if (url.getProtocol().equals("file")) {
        // File: URL#toURI is broken
        return new PathResource(url).getFile().getParentFile().getParentFile();
    } else if (url.getProtocol().equals("bundleentry")) {
        // say hello to equinox who has its own protocol.
        // we use introspection like there is no tomorrow to get access to
        // the File
        URLConnection con = url.openConnection();
        // work around
        con.setUseCaches(Resource.getDefaultUseCaches());
        if (BUNDLE_ENTRY_FIELD == null) {
            BUNDLE_ENTRY_FIELD = con.getClass().getDeclaredField("bundleEntry");
            BUNDLE_ENTRY_FIELD.setAccessible(true);
        }
        Object bundleEntry = BUNDLE_ENTRY_FIELD.get(con);
        if (match(bundleEntry.getClass().getName(), FILE_BUNDLE_ENTRY_CLASSES)) {
            if (FILE_FIELD == null) {
                FILE_FIELD = bundleEntry.getClass().getDeclaredField("file");
                FILE_FIELD.setAccessible(true);
            }
            File f = (File) FILE_FIELD.get(bundleEntry);
            return f.getParentFile().getParentFile();
        } else if (match(bundleEntry.getClass().getName(), ZIP_BUNDLE_ENTRY_CLASSES)) {
            url = bundle.getEntry("/");
            con = url.openConnection();
            con.setDefaultUseCaches(Resource.getDefaultUseCaches());
            if (BUNDLE_ENTRY_FIELD == null) {
                // this one will be a DirZipBundleEntry
                BUNDLE_ENTRY_FIELD = con.getClass().getDeclaredField("bundleEntry");
                BUNDLE_ENTRY_FIELD.setAccessible(true);
            }
            bundleEntry = BUNDLE_ENTRY_FIELD.get(con);
            if (BUNDLE_FILE_FIELD_FOR_DIR_ZIP_BUNDLE_ENTRY == null) {
                BUNDLE_FILE_FIELD_FOR_DIR_ZIP_BUNDLE_ENTRY = bundleEntry.getClass().getDeclaredField("bundleFile");
                BUNDLE_FILE_FIELD_FOR_DIR_ZIP_BUNDLE_ENTRY.setAccessible(true);
            }
            Object zipBundleFile = BUNDLE_FILE_FIELD_FOR_DIR_ZIP_BUNDLE_ENTRY.get(bundleEntry);
            if (ZIP_FILE_FILED_FOR_ZIP_BUNDLE_FILE == null) {
                ZIP_FILE_FILED_FOR_ZIP_BUNDLE_FILE = zipBundleFile.getClass().getDeclaredField("zipFile");
                ZIP_FILE_FILED_FOR_ZIP_BUNDLE_FILE.setAccessible(true);
            }
            ZipFile zipFile = (ZipFile) ZIP_FILE_FILED_FOR_ZIP_BUNDLE_FILE.get(zipBundleFile);
            return new File(zipFile.getName());
        } else if (match(bundleEntry.getClass().getName(), DIR_ZIP_BUNDLE_ENTRY_CLASSES)) {
        // that will not happen as we did ask for the manifest not a
        // directory.
        }
    } else if ("bundle".equals(url.getProtocol())) {
        // observed this on felix-2.0.0
        String location = bundle.getLocation();
        if (location.startsWith("file:/")) {
            URI uri = new URI(URIUtil.encodePath(location));
            return new File(uri);
        } else if (location.startsWith("file:")) {
            // location defined in the BundleArchive m_bundleArchive
            // it is relative to relative to the BundleArchive's
            // m_archiveRootDir
            File res = new File(location.substring("file:".length()));
            if (!res.exists()) {
                return null;
            // Object bundleArchive = getFelixBundleArchive(bundle);
            // File archiveRoot =
            // getFelixBundleArchiveRootDir(bundleArchive);
            // String currentLocation =
            // getFelixBundleArchiveCurrentLocation(bundleArchive);
            // System.err.println("Got the archive root " +
            // archiveRoot.getAbsolutePath()
            // + " current location " + currentLocation +
            // " is directory ?");
            // res = new File(archiveRoot, currentLocation != null
            // ? currentLocation : location.substring("file:".length()));
            }
            return res;
        } else if (location.startsWith("reference:file:")) {
            location = URLDecoder.decode(location.substring("reference:".length()), "UTF-8");
            File file = new File(location.substring("file:".length()));
            return file;
        }
    }
    return null;
}
Also used : ZipFile(java.util.zip.ZipFile) PathResource(org.eclipse.jetty.util.resource.PathResource) File(java.io.File) ZipFile(java.util.zip.ZipFile) URI(java.net.URI) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 8 with PathResource

use of org.eclipse.jetty.util.resource.PathResource in project yacy_grid_mcp by yacy.

the class FileHandler method getResource.

@Override
public Resource getResource(String path) {
    try {
        Resource resource = super.getResource(path);
        if (resource == null)
            return null;
        if (!(resource instanceof PathResource) || !resource.exists())
            return resource;
        File f = resource.getFile();
        if (f.isDirectory() && !path.equals("/"))
            return resource;
        CacheResource cache = resourceCache.get(f);
        if (cache != null)
            return cache;
        if (f.length() < CACHE_LIMIT || f.getName().endsWith(".html") || path.equals("/")) {
            cache = new CacheResource((PathResource) resource);
            resourceCache.put(f, cache);
            return cache;
        }
        return resource;
    } catch (IOException e) {
        Data.logger.warn("", e);
    }
    return null;
}
Also used : PathResource(org.eclipse.jetty.util.resource.PathResource) PathResource(org.eclipse.jetty.util.resource.PathResource) Resource(org.eclipse.jetty.util.resource.Resource) IOException(java.io.IOException) File(java.io.File)

Aggregations

PathResource (org.eclipse.jetty.util.resource.PathResource)8 File (java.io.File)3 IOException (java.io.IOException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Path (java.nio.file.Path)2 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)2 Resource (org.eclipse.jetty.util.resource.Resource)2 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 ZipFile (java.util.zip.ZipFile)1 LocalConnector (org.eclipse.jetty.server.LocalConnector)1 Request (org.eclipse.jetty.server.Request)1 Server (org.eclipse.jetty.server.Server)1 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 Before (org.junit.Before)1 Test (org.junit.Test)1 BaseObject (php.runtime.lang.BaseObject)1