Skip to content

Commit

Permalink
Fix leaked resources in jetty maven plugin (#10888)
Browse files Browse the repository at this point in the history
  • Loading branch information
janbartel authored Nov 14, 2023
1 parent 4dd8bc9 commit 9bf6229
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.eclipse.jetty.ee10.webapp.WebAppContext;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.resource.MountedPathResource;
import org.eclipse.jetty.util.resource.Resource;
Expand All @@ -45,9 +47,10 @@ public OverlayManager(WarPluginInfo warPlugin)

public void applyOverlays(MavenWebAppContext webApp) throws IOException
{
Objects.requireNonNull(webApp);
List<Resource> resourceBases = new ArrayList<Resource>();

for (Overlay o : getOverlays())
for (Overlay o : getOverlays(webApp))
{
//can refer to the current project in list of overlays for ordering purposes
if (o.getConfig() != null && o.getConfig().isCurrentProject() && webApp.getBaseResource().exists())
Expand All @@ -56,7 +59,7 @@ public void applyOverlays(MavenWebAppContext webApp) throws IOException
continue;
}
//add in the selectively unpacked overlay in the correct order to the webapp's resource base
resourceBases.add(unpackOverlay(o));
resourceBases.add(unpackOverlay(webApp, o));
}

if (!resourceBases.contains(webApp.getBaseResource()) && webApp.getBaseResource().exists())
Expand All @@ -73,7 +76,7 @@ public void applyOverlays(MavenWebAppContext webApp) throws IOException
/**
* Generate an ordered list of overlays
*/
protected List<Overlay> getOverlays()
protected List<Overlay> getOverlays(WebAppContext webApp)
{
Set<Artifact> matchedWarArtifacts = new HashSet<Artifact>();
List<Overlay> overlays = new ArrayList<Overlay>();
Expand All @@ -98,7 +101,7 @@ protected List<Overlay> getOverlays()
if (a != null)
{
matchedWarArtifacts.add(a);
Resource resource = ResourceFactory.root().newJarFileResource(a.getFile().toPath().toUri()); // TODO leak
Resource resource = webApp.getResourceFactory().newJarFileResource(a.getFile().toPath().toUri());
SelectiveJarResource r = new SelectiveJarResource(resource);
r.setIncludes(config.getIncludes());
r.setExcludes(config.getExcludes());
Expand All @@ -112,7 +115,7 @@ protected List<Overlay> getOverlays()
{
if (!matchedWarArtifacts.contains(a))
{
Resource resource = ResourceFactory.root().newJarFileResource(a.getFile().toPath().toUri()); // TODO leak
Resource resource = webApp.getResourceFactory().newJarFileResource(a.getFile().toPath().toUri());
Overlay overlay = new Overlay(null, resource);
overlays.add(overlay);
}
Expand All @@ -127,9 +130,11 @@ protected List<Overlay> getOverlays()
* @return the location to which it was unpacked
* @throws IOException if there is an IO problem
*/
protected Resource unpackOverlay(Overlay overlay)
protected Resource unpackOverlay(WebAppContext webApp, Overlay overlay)
throws IOException
{
Objects.requireNonNull(webApp);

if (overlay.getResource() == null)
return null; //nothing to unpack

Expand All @@ -153,6 +158,6 @@ protected Resource unpackOverlay(Overlay overlay)
overlay.unpackTo(unpackDir);

//use top level of unpacked content
return ResourceFactory.root().newResource(unpackDir.getCanonicalPath()); // TODO leak
return webApp.getResourceFactory().newResource(unpackDir.getCanonicalPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.eclipse.jetty.ee9.webapp.WebAppContext;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.resource.MountedPathResource;
import org.eclipse.jetty.util.resource.Resource;
Expand All @@ -36,7 +38,7 @@
*/
public class OverlayManager
{
private WarPluginInfo warPlugin;
private final WarPluginInfo warPlugin;

public OverlayManager(WarPluginInfo warPlugin)
{
Expand All @@ -48,7 +50,7 @@ public void applyOverlays(MavenWebAppContext webApp)
{
List<Resource> resourceBases = new ArrayList<Resource>();

for (Overlay o : getOverlays())
for (Overlay o : getOverlays(webApp))
{
//can refer to the current project in list of overlays for ordering purposes
if (o.getConfig() != null && o.getConfig().isCurrentProject() && webApp.getBaseResource().exists())
Expand All @@ -57,7 +59,7 @@ public void applyOverlays(MavenWebAppContext webApp)
continue;
}
//add in the selectively unpacked overlay in the correct order to the webapp's resource base
resourceBases.add(unpackOverlay(o));
resourceBases.add(unpackOverlay(webApp, o));
}

if (!resourceBases.contains(webApp.getBaseResource()) && webApp.getBaseResource().exists())
Expand All @@ -74,9 +76,11 @@ public void applyOverlays(MavenWebAppContext webApp)
/**
* Generate an ordered list of overlays
*/
protected List<Overlay> getOverlays()
protected List<Overlay> getOverlays(WebAppContext webApp)
throws Exception
{
{
Objects.requireNonNull(webApp);

Set<Artifact> matchedWarArtifacts = new HashSet<Artifact>();
List<Overlay> overlays = new ArrayList<Overlay>();

Expand All @@ -100,7 +104,7 @@ protected List<Overlay> getOverlays()
if (a != null)
{
matchedWarArtifacts.add(a);
Resource resource = ResourceFactory.root().newJarFileResource(a.getFile().toPath().toUri()); // TODO leak
Resource resource = webApp.getResourceFactory().newJarFileResource(a.getFile().toPath().toUri());
SelectiveJarResource r = new SelectiveJarResource(resource);
r.setIncludes(config.getIncludes());
r.setExcludes(config.getExcludes());
Expand All @@ -114,7 +118,7 @@ protected List<Overlay> getOverlays()
{
if (!matchedWarArtifacts.contains(a))
{
Resource resource = ResourceFactory.root().newJarFileResource(a.getFile().toPath().toUri()); // TODO leak
Resource resource = webApp.getResourceFactory().newJarFileResource(a.getFile().toPath().toUri());
Overlay overlay = new Overlay(null, resource);
overlays.add(overlay);
}
Expand All @@ -129,9 +133,12 @@ protected List<Overlay> getOverlays()
* @return the location to which it was unpacked
* @throws IOException if there is an IO problem
*/
protected Resource unpackOverlay(Overlay overlay)
protected Resource unpackOverlay(WebAppContext webApp, Overlay overlay)
throws IOException
{
Objects.requireNonNull(webApp);
Objects.requireNonNull(overlay);

if (overlay.getResource() == null)
return null; //nothing to unpack

Expand All @@ -154,6 +161,6 @@ protected Resource unpackOverlay(Overlay overlay)
overlay.unpackTo(unpackDir);

//use top level of unpacked content
return ResourceFactory.root().newResource(unpackDir.getCanonicalPath()); // TODO leak
return webApp.getResourceFactory().newResource(unpackDir.getCanonicalPath());
}
}

0 comments on commit 9bf6229

Please sign in to comment.