Search in sources :

Example 21 with ResourceCollection

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

the class JettyRunForkedMojo method prepareConfiguration.

public File prepareConfiguration() throws MojoExecutionException {
    try {
        //work out the configuration based on what is configured in the pom
        File propsFile = new File(target, "fork.props");
        if (propsFile.exists())
            propsFile.delete();
        propsFile.createNewFile();
        //propsFile.deleteOnExit();
        Properties props = new Properties();
        //web.xml
        if (webApp.getDescriptor() != null) {
            props.put("web.xml", webApp.getDescriptor());
        }
        if (webApp.getQuickStartWebDescriptor() != null) {
            props.put("quickstart.web.xml", webApp.getQuickStartWebDescriptor().getFile().getAbsolutePath());
        }
        //sort out the context path
        if (webApp.getContextPath() != null) {
            props.put("context.path", webApp.getContextPath());
        }
        //tmp dir
        props.put("tmp.dir", webApp.getTempDirectory().getAbsolutePath());
        props.put("tmp.dir.persist", Boolean.toString(originalPersistTemp));
        //send over the original base resources before any overlays were added
        if (originalBaseResource instanceof ResourceCollection)
            props.put("base.dirs.orig", toCSV(((ResourceCollection) originalBaseResource).getResources()));
        else
            props.put("base.dirs.orig", originalBaseResource.toString());
        //send over the calculated resource bases that includes unpacked overlays, but none of the
        //meta-inf resources
        Resource postOverlayResources = (Resource) webApp.getAttribute(MavenWebInfConfiguration.RESOURCE_BASES_POST_OVERLAY);
        if (postOverlayResources instanceof ResourceCollection)
            props.put("base.dirs", toCSV(((ResourceCollection) postOverlayResources).getResources()));
        else
            props.put("base.dirs", postOverlayResources.toString());
        //web-inf classes
        if (webApp.getClasses() != null) {
            props.put("classes.dir", webApp.getClasses().getAbsolutePath());
        }
        if (useTestScope && webApp.getTestClasses() != null) {
            props.put("testClasses.dir", webApp.getTestClasses().getAbsolutePath());
        }
        //web-inf lib
        List<File> deps = webApp.getWebInfLib();
        StringBuffer strbuff = new StringBuffer();
        for (int i = 0; i < deps.size(); i++) {
            File d = deps.get(i);
            strbuff.append(d.getAbsolutePath());
            if (i < deps.size() - 1)
                strbuff.append(",");
        }
        props.put("lib.jars", strbuff.toString());
        //any war files
        List<Artifact> warArtifacts = getWarArtifacts();
        for (int i = 0; i < warArtifacts.size(); i++) {
            strbuff.setLength(0);
            Artifact a = warArtifacts.get(i);
            strbuff.append(a.getGroupId() + ",");
            strbuff.append(a.getArtifactId() + ",");
            strbuff.append(a.getFile().getAbsolutePath());
            props.put("maven.war.artifact." + i, strbuff.toString());
        }
        //any overlay configuration
        WarPluginInfo warPlugin = new WarPluginInfo(project);
        //add in the war plugins default includes and excludes
        props.put("maven.war.includes", toCSV(warPlugin.getDependentMavenWarIncludes()));
        props.put("maven.war.excludes", toCSV(warPlugin.getDependentMavenWarExcludes()));
        List<OverlayConfig> configs = warPlugin.getMavenWarOverlayConfigs();
        int i = 0;
        for (OverlayConfig c : configs) {
            props.put("maven.war.overlay." + (i++), c.toString());
        }
        try (OutputStream out = new BufferedOutputStream(new FileOutputStream(propsFile))) {
            props.store(out, "properties for forked webapp");
        }
        return propsFile;
    } catch (Exception e) {
        throw new MojoExecutionException("Prepare webapp configuration", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Resource(org.eclipse.jetty.util.resource.Resource) Properties(java.util.Properties) Artifact(org.apache.maven.artifact.Artifact) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Example 22 with ResourceCollection

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

the class Starter method configureWebApp.

public void configureWebApp() throws Exception {
    if (props == null)
        return;
    //apply a properties file that defines the things that we configure in the jetty:run plugin:
    // - the context path
    String str = (String) props.get("context.path");
    if (str != null)
        webApp.setContextPath(str);
    // - web.xml
    str = (String) props.get("web.xml");
    if (str != null)
        webApp.setDescriptor(str);
    str = (String) props.get("quickstart.web.xml");
    if (str != null)
        webApp.setQuickStartWebDescriptor(Resource.newResource(new File(str)));
    // - the tmp directory
    str = (String) props.getProperty("tmp.dir");
    if (str != null)
        webApp.setTempDirectory(new File(str.trim()));
    str = (String) props.getProperty("tmp.dir.persist");
    if (str != null)
        webApp.setPersistTempDirectory(Boolean.valueOf(str));
    //Get the calculated base dirs which includes the overlays
    str = (String) props.getProperty("base.dirs");
    if (str != null && !"".equals(str.trim())) {
        ResourceCollection bases = new ResourceCollection(StringUtil.csvSplit(str));
        webApp.setWar(null);
        webApp.setBaseResource(bases);
    }
    //Get the original base dirs without the overlays
    str = (String) props.get("base.dirs.orig");
    if (str != null && !"".equals(str.trim())) {
        ResourceCollection bases = new ResourceCollection(StringUtil.csvSplit(str));
        webApp.setAttribute("org.eclipse.jetty.resources.originalBases", bases);
    }
    //For overlays
    str = (String) props.getProperty("maven.war.includes");
    List<String> defaultWarIncludes = fromCSV(str);
    str = (String) props.getProperty("maven.war.excludes");
    List<String> defaultWarExcludes = fromCSV(str);
    //List of war artifacts
    List<Artifact> wars = new ArrayList<Artifact>();
    //List of OverlayConfigs
    TreeMap<String, OverlayConfig> orderedConfigs = new TreeMap<String, OverlayConfig>();
    Enumeration<String> pnames = (Enumeration<String>) props.propertyNames();
    while (pnames.hasMoreElements()) {
        String n = pnames.nextElement();
        if (n.startsWith("maven.war.artifact")) {
            Artifact a = new Artifact((String) props.get(n));
            a.resource = Resource.newResource("jar:" + Resource.toURL(new File(a.path)).toString() + "!/");
            wars.add(a);
        } else if (n.startsWith("maven.war.overlay")) {
            OverlayConfig c = new OverlayConfig((String) props.get(n), defaultWarIncludes, defaultWarExcludes);
            orderedConfigs.put(n, c);
        }
    }
    Set<Artifact> matchedWars = new HashSet<Artifact>();
    //process any overlays and the war type artifacts
    List<Overlay> overlays = new ArrayList<Overlay>();
    for (OverlayConfig config : orderedConfigs.values()) {
        //overlays can be individually skipped
        if (config.isSkip())
            continue;
        //an empty overlay refers to the current project - important for ordering
        if (config.isCurrentProject()) {
            Overlay overlay = new Overlay(config, null);
            overlays.add(overlay);
            continue;
        }
        //if a war matches an overlay config
        Artifact a = getArtifactForOverlayConfig(config, wars);
        if (a != null) {
            matchedWars.add(a);
            SelectiveJarResource r = new SelectiveJarResource(new URL("jar:" + Resource.toURL(new File(a.path)).toString() + "!/"));
            r.setIncludes(config.getIncludes());
            r.setExcludes(config.getExcludes());
            Overlay overlay = new Overlay(config, r);
            overlays.add(overlay);
        }
    }
    //iterate over the left over war artifacts and unpack them (without include/exclude processing) as necessary
    for (Artifact a : wars) {
        if (!matchedWars.contains(a)) {
            Overlay overlay = new Overlay(null, a.resource);
            overlays.add(overlay);
        }
    }
    webApp.setOverlays(overlays);
    // - the equivalent of web-inf classes
    str = (String) props.getProperty("classes.dir");
    if (str != null && !"".equals(str.trim())) {
        webApp.setClasses(new File(str));
    }
    str = (String) props.getProperty("testClasses.dir");
    if (str != null && !"".equals(str.trim())) {
        webApp.setTestClasses(new File(str));
    }
    // - the equivalent of web-inf lib
    str = (String) props.getProperty("lib.jars");
    if (str != null && !"".equals(str.trim())) {
        List<File> jars = new ArrayList<File>();
        String[] names = StringUtil.csvSplit(str);
        for (int j = 0; names != null && j < names.length; j++) jars.add(new File(names[j].trim()));
        webApp.setWebInfLib(jars);
    }
}
Also used : Enumeration(java.util.Enumeration) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) URL(java.net.URL) File(java.io.File) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection) HashSet(java.util.HashSet)

Example 23 with ResourceCollection

use of org.eclipse.jetty.util.resource.ResourceCollection in project dropwizard by dropwizard.

the class ServletEnvironmentTest method setsBaseResourceList.

@Test
public void setsBaseResourceList() throws Exception {
    Resource wooResource = Resource.newResource(tempDir.newFolder());
    Resource fooResource = Resource.newResource(tempDir.newFolder());
    final Resource[] testResources = new Resource[] { wooResource, fooResource };
    environment.setBaseResource(testResources);
    ArgumentCaptor<Resource> captor = ArgumentCaptor.forClass(Resource.class);
    verify(handler).setBaseResource(captor.capture());
    Resource actualResource = captor.getValue();
    assertThat(actualResource).isInstanceOf(ResourceCollection.class);
    ResourceCollection actualResourceCollection = (ResourceCollection) actualResource;
    assertThat(actualResourceCollection.getResources()).contains(wooResource, fooResource);
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection) Test(org.junit.Test)

Example 24 with ResourceCollection

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

the class OverlayedAppProvider method createTemplateContext.

/* ------------------------------------------------------------ */
private TemplateContext createTemplateContext(final String key, Webapp webapp, Template template, Node node, ClassLoader parent) throws Exception {
    __log.info("created {}", key);
    // look for libs
    // If we have libs directories, create classloader and make it available to
    // the XMLconfiguration
    List<URL> libs = new ArrayList<URL>();
    for (Resource lib : getLayeredResources(LIB, node, template)) {
        for (String jar : lib.list()) {
            if (!jar.toLowerCase(Locale.ENGLISH).endsWith(".jar"))
                continue;
            libs.add(lib.addPath(jar).getURL());
        }
    }
    final ClassLoader libLoader;
    if (libs.size() > 0) {
        __log.debug("{}: libs={}", key, libs);
        libLoader = new URLClassLoader(libs.toArray(new URL[] {}), parent) {

            public String toString() {
                return "libLoader@" + Long.toHexString(hashCode()) + "-lib-" + key;
            }
        };
    } else
        libLoader = parent;
    Thread.currentThread().setContextClassLoader(libLoader);
    // Make the shared resourceBase
    List<Resource> bases = new ArrayList<Resource>();
    for (Resource wa : getLayers(node, template)) bases.add(wa);
    if (webapp != null)
        bases.add(webapp.getBaseResource());
    Resource baseResource = bases.size() == 1 ? bases.get(0) : new ResourceCollection(bases.toArray(new Resource[bases.size()]));
    __log.debug("{}: baseResource={}", key, baseResource);
    // Make the shared context
    TemplateContext shared = new TemplateContext(key, getDeploymentManager().getServer(), baseResource, libLoader);
    _shared.put(key, shared);
    // Create properties to be shared by overlay.xmls
    Map<String, Object> idMap = new HashMap<String, Object>();
    idMap.put(_serverID, getDeploymentManager().getServer());
    // otherwise create an instance ourselves
    for (Resource template_xml : getLayeredResources(TEMPLATE_XML, template, node)) {
        __log.debug("{}: template.xml={}", key, template_xml);
        XmlConfiguration xmlc = newXmlConfiguration(template_xml.getURL(), idMap, template, null);
        xmlc.getIdMap().putAll(idMap);
        xmlc.configure(shared);
        idMap = xmlc.getIdMap();
    }
    shared.setIdMap(idMap);
    shared.start();
    return shared;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) JarResource(org.eclipse.jetty.util.resource.JarResource) Resource(org.eclipse.jetty.util.resource.Resource) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) JettyWebXmlConfiguration(org.eclipse.jetty.webapp.JettyWebXmlConfiguration) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) WebAppClassLoader(org.eclipse.jetty.webapp.WebAppClassLoader) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Example 25 with ResourceCollection

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

the class QuickStartDescriptorProcessor method visitMetaInfResource.

public void visitMetaInfResource(WebAppContext context, Resource dir) {
    Collection<Resource> metaInfResources = (Collection<Resource>) context.getAttribute(MetaInfConfiguration.METAINF_RESOURCES);
    if (metaInfResources == null) {
        metaInfResources = new HashSet<Resource>();
        context.setAttribute(MetaInfConfiguration.METAINF_RESOURCES, metaInfResources);
    }
    metaInfResources.add(dir);
    //also add to base resource of webapp
    Resource[] collection = new Resource[metaInfResources.size() + 1];
    int i = 0;
    collection[i++] = context.getBaseResource();
    for (Resource resource : metaInfResources) collection[i++] = resource;
    context.setBaseResource(new ResourceCollection(collection));
}
Also used : Resource(org.eclipse.jetty.util.resource.Resource) Collection(java.util.Collection) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Aggregations

ResourceCollection (org.eclipse.jetty.util.resource.ResourceCollection)30 Resource (org.eclipse.jetty.util.resource.Resource)22 File (java.io.File)13 ArrayList (java.util.ArrayList)8 URL (java.net.URL)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 JarResource (org.eclipse.jetty.util.resource.JarResource)5 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)4 MimeTypes (org.eclipse.jetty.http.MimeTypes)3 Server (org.eclipse.jetty.server.Server)3 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)3 MalformedURLException (java.net.MalformedURLException)2 URLClassLoader (java.net.URLClassLoader)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Set (java.util.Set)2 TreeMap (java.util.TreeMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2