Skip to content

Commit

Permalink
Fixed StackOverflow in WebappClassLoader.getURLs
Browse files Browse the repository at this point in the history
- seen in tests when logging used toString after the CL closed.
- Now when it is used after the closure, returns an empty array
  instead of exception

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Feb 27, 2023
1 parent 8ef5403 commit 147c401
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,9 @@ protected PermissionCollection getPermissions(CodeSource codeSource) {

@Override
public synchronized URL[] getURLs() {
checkStatus(LifeCycleStatus.RUNNING);
if (this.status == LifeCycleStatus.CLOSED) {
return new URL[0];
}
if (repositoryURLs != null) {
return repositoryURLs.toArray(URL[]::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@

import com.sun.enterprise.util.io.FileUtils;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.naming.resources.WebDirContext;
import org.glassfish.common.util.GlassfishUrlClassLoader;
import org.hamcrest.core.StringEndsWith;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.hasLength;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.hamcrest.core.StringEndsWith.endsWith;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -73,12 +79,7 @@ public void isParallel() {

@Test
public void getResource() {
WebappClassLoader classLoader = new WebappClassLoader(WebappClassLoaderTest.class.getClassLoader());
WebDirContext webDirContext = new WebDirContext();
webDirContext.setDocBase(docBase.toFile().getAbsolutePath());
classLoader.setResources(webDirContext);
classLoader.addRepository(repo.getFileName().toString() + "/", repo.toFile());
classLoader.setDelegate(false);
WebappClassLoader classLoader = createCL();
classLoader.start();
assertAll(
() -> assertThat(classLoader.getResource(".").toExternalForm(), endsWith("/repo1/")),
Expand All @@ -92,12 +93,7 @@ public void getResource() {

@Test
public void getResourceAsStream() {
WebappClassLoader classLoader = new WebappClassLoader(WebappClassLoaderTest.class.getClassLoader());
WebDirContext webDirContext = new WebDirContext();
webDirContext.setDocBase(docBase.toFile().getAbsolutePath());
classLoader.setResources(webDirContext);
classLoader.addRepository(repo.getFileName().toString() + "/", repo.toFile());
classLoader.setDelegate(false);
WebappClassLoader classLoader = createCL();
classLoader.start();
assertAll(
// streams of 4 bytes returned by the delegate CL
Expand All @@ -109,4 +105,26 @@ public void getResourceAsStream() {
() -> assertNotNull(classLoader.getResourceAsStream("Dir1/fileInDir1.txt"), "Dir1/fileInDir1.txt")
);
}


@Test
public void getURLs() throws Exception {
WebappClassLoader classLoader = createCL();
assertThat(classLoader.getURLs(), arrayWithSize(1));
classLoader.start();
assertThat(classLoader.getURLs(), arrayWithSize(1));
classLoader.close();
assertThat(classLoader.getURLs(), arrayWithSize(0));
}


private WebappClassLoader createCL() {
WebappClassLoader classLoader = new WebappClassLoader(WebappClassLoaderTest.class.getClassLoader());
WebDirContext webDirContext = new WebDirContext();
webDirContext.setDocBase(docBase.toFile().getAbsolutePath());
classLoader.setResources(webDirContext);
classLoader.addRepository(repo.getFileName().toString() + "/", repo.toFile());
classLoader.setDelegate(false);
return classLoader;
}
}

0 comments on commit 147c401

Please sign in to comment.