Search in sources :

Example 6 with Resource

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

the class AnnotationConfiguration method parseContainerPath.

/**
     * Scan jars on container path.
     * 
     * @param context the context for the scan
     * @param parser the parser to scan with
     * @throws Exception if unable to scan
     */
public void parseContainerPath(final WebAppContext context, final AnnotationParser parser) throws Exception {
    //always parse for discoverable annotations as well as class hierarchy and servletcontainerinitializer related annotations
    final Set<Handler> handlers = new HashSet<Handler>();
    handlers.addAll(_discoverableAnnotationHandlers);
    handlers.addAll(_containerInitializerAnnotationHandlers);
    if (_classInheritanceHandler != null)
        handlers.add(_classInheritanceHandler);
    _containerPathStats = new CounterStatistic();
    for (Resource r : context.getMetaData().getContainerResources()) {
        //queue it up for scanning if using multithreaded mode
        if (_parserTasks != null) {
            ParserTask task = new ParserTask(parser, handlers, r);
            _parserTasks.add(task);
            _containerPathStats.increment();
            if (LOG.isDebugEnabled())
                task.setStatistic(new TimeStatistic());
        }
    }
}
Also used : CounterStatistic(org.eclipse.jetty.util.statistic.CounterStatistic) Resource(org.eclipse.jetty.util.resource.Resource) Handler(org.eclipse.jetty.annotations.AnnotationParser.Handler) ConcurrentHashSet(org.eclipse.jetty.util.ConcurrentHashSet) HashSet(java.util.HashSet)

Example 7 with Resource

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

the class AnnotationParser method parse.

/**
     * Parse a given class
     * 
     * @param handlers the set of handlers to find class
     * @param className the class name to parse
     * @throws Exception if unable to parse
     */
public void parse(Set<? extends Handler> handlers, String className) throws Exception {
    if (className == null)
        return;
    if (!isParsed(className)) {
        className = className.replace('.', '/') + ".class";
        URL resource = Loader.getResource(className);
        if (resource != null) {
            Resource r = Resource.newResource(resource);
            try (InputStream is = r.getInputStream()) {
                scanClass(handlers, null, is);
            }
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) Resource(org.eclipse.jetty.util.resource.Resource) URL(java.net.URL)

Example 8 with Resource

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

the class AnnotationParser method parseDir.

/**
     * Parse all classes in a directory
     * 
     * @param handlers the set of handlers to look for classes in 
     * @param dir the resource directory to look for classes
     * @throws Exception if unable to parse
     */
protected void parseDir(Set<? extends Handler> handlers, Resource dir) throws Exception {
    // skip dirs whose name start with . (ie hidden)
    if (!dir.isDirectory() || !dir.exists() || dir.getName().startsWith("."))
        return;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Scanning dir {}", dir);
    }
    ;
    MultiException me = new MultiException();
    String[] files = dir.list();
    for (int f = 0; files != null && f < files.length; f++) {
        Resource res = dir.addPath(files[f]);
        if (res.isDirectory())
            parseDir(handlers, res);
        else {
            //we've already verified the directories, so just verify the class file name
            File file = res.getFile();
            if (isValidClassFileName((file == null ? null : file.getName()))) {
                try {
                    String name = res.getName();
                    if (!isParsed(name)) {
                        Resource r = Resource.newResource(res.getURL());
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Scanning class {}", r);
                        }
                        ;
                        try (InputStream is = r.getInputStream()) {
                            scanClass(handlers, dir, is);
                        }
                    }
                } catch (Exception ex) {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Error scanning file " + files[f], ex);
                    me.add(new RuntimeException("Error scanning file " + files[f], ex));
                }
            } else {
                if (LOG.isDebugEnabled())
                    LOG.debug("Skipping scan on invalid file {}", res);
            }
        }
    }
    me.ifExceptionThrow();
}
Also used : JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) Resource(org.eclipse.jetty.util.resource.Resource) MultiException(org.eclipse.jetty.util.MultiException) File(java.io.File) MultiException(org.eclipse.jetty.util.MultiException) IOException(java.io.IOException)

Example 9 with Resource

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

the class JarServer method main.

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    ServletContextHandler context = new ServletContextHandler();
    Resource.setDefaultUseCaches(true);
    Resource base = Resource.newResource("jar:file:src/main/resources/content.jar!/");
    context.setBaseResource(base);
    context.addServlet(new ServletHolder(new DefaultServlet()), "/");
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { context, new DefaultHandler() });
    server.setHandler(handlers);
    server.start();
    server.join();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Resource(org.eclipse.jetty.util.resource.Resource) DefaultServlet(org.eclipse.jetty.servlet.DefaultServlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 10 with Resource

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

the class GlobalWebappConfigBinding method processBinding.

public void processBinding(Node node, App app) throws Exception {
    ContextHandler handler = app.getContextHandler();
    if (handler == null) {
        throw new NullPointerException("No Handler created for App: " + app);
    }
    if (handler instanceof WebAppContext) {
        WebAppContext context = (WebAppContext) handler;
        if (LOG.isDebugEnabled()) {
            LOG.debug("Binding: Configuring webapp context with global settings from: " + _jettyXml);
        }
        if (_jettyXml == null) {
            LOG.warn("Binding: global context binding is enabled but no jetty-web.xml file has been registered");
        }
        Resource globalContextSettings = Resource.newResource(_jettyXml);
        if (globalContextSettings.exists()) {
            XmlConfiguration jettyXmlConfig = new XmlConfiguration(globalContextSettings.getInputStream());
            Resource resource = Resource.newResource(app.getOriginId());
            File file = resource.getFile();
            jettyXmlConfig.getIdMap().put("Server", app.getDeploymentManager().getServer());
            jettyXmlConfig.getProperties().put("jetty.home", System.getProperty("jetty.home", "."));
            jettyXmlConfig.getProperties().put("jetty.base", System.getProperty("jetty.base", "."));
            jettyXmlConfig.getProperties().put("jetty.webapp", file.getCanonicalPath());
            jettyXmlConfig.getProperties().put("jetty.webapps", file.getParentFile().getCanonicalPath());
            jettyXmlConfig.configure(context);
        } else {
            LOG.info("Binding: Unable to locate global webapp context settings: " + _jettyXml);
        }
    }
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Resource(org.eclipse.jetty.util.resource.Resource) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) File(java.io.File)

Aggregations

Resource (org.eclipse.jetty.util.resource.Resource)196 Test (org.junit.Test)79 File (java.io.File)46 URL (java.net.URL)39 ArrayList (java.util.ArrayList)38 Matchers.containsString (org.hamcrest.Matchers.containsString)31 IOException (java.io.IOException)28 ResourceCollection (org.eclipse.jetty.util.resource.ResourceCollection)18 JarResource (org.eclipse.jetty.util.resource.JarResource)16 XmlConfiguration (org.eclipse.jetty.xml.XmlConfiguration)16 Server (org.eclipse.jetty.server.Server)13 HashSet (java.util.HashSet)12 InputStream (java.io.InputStream)9 HashMap (java.util.HashMap)9 URI (java.net.URI)8 MalformedURLException (java.net.MalformedURLException)7 StringTokenizer (java.util.StringTokenizer)7 URISyntaxException (java.net.URISyntaxException)6 Properties (java.util.Properties)6 Set (java.util.Set)6