Skip to content

Commit

Permalink
GEODE-10407: check if class allready loaded (apache#7832)
Browse files Browse the repository at this point in the history
* GEODE-10407: check if class allready loaded
  • Loading branch information
mivanac authored Aug 24, 2022
1 parent 6c257d7 commit de7834a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,10 @@ public Enumeration<URL> getResources(String name) throws IOException {
public JarDeploymentService getJarDeploymentService() {
return jarDeploymentService;
}

public ClassLoader getClassloaderForArtifact(String artifactId) {
return classPathService.getClassloaderForArtifact(artifactId);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ public interface ClasspathService {
InputStream getResourceAsStream(final String name);

Enumeration<URL> getResources(final String name) throws IOException;

ClassLoader getClassloaderForArtifact(String artifactId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ protected Class loadClass(String name, boolean resolve) throws ClassNotFoundExce
for (DeployJarChildFirstClassLoader sibling : artifactIdsToClassLoader.values().stream()
.filter(Objects::nonNull).collect(Collectors.toList())) {
try {
if (sibling.thisIsOld()) {
continue;
}
c = sibling.findLoadedClass(name);
if (c != null) {
break;
}
c = sibling.findClass(name);
if (c != null) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class JarDeployerIntegrationTest {
private static File plainJarVersion1, plainJarVersion1b, plainJarVersion2, semanticJarVersion1,
semanticJarVersion2, semanticJarVersion1b, semanticJarVersion1c;

private static File baseJar;

private File deployedDir;

@BeforeClass
Expand All @@ -74,6 +76,10 @@ public static void createStagedJars() throws IOException {
jarBuilder.buildJar(semanticJarVersion1b, createClassContent("version1b", "Def"));
semanticJarVersion1c = new File(stagedTempDir.newFolder("v1c"), "def.jar");
jarBuilder.buildJar(semanticJarVersion1c, createClassContent("version1c", "Def"));

baseJar = new File(stagedDir, "base.jar");
jarBuilder.buildJar(baseJar, create1ClassContent("ExceptionA"),
create2ClassContent("ExceptionB", "ExceptionA"));
}

@Before
Expand Down Expand Up @@ -202,6 +208,31 @@ public void undeploy() throws Exception {
.isInstanceOf(ClassNotFoundException.class);
}

@Test
public void deploy2JarsSetCurrentClassloaderTo1stAndLoadClassFrom2nd() throws Exception {

// deploy def-1.0.jar
jarDeployer.deploy(semanticJarVersion1);

// deploy base.jar
DeployedJar deployedJar = jarDeployer.deploy(baseJar);

assertThat(deployedJar).isNotNull();

ClassPathLoader oldLoader = ClassPathLoader.getLatest();

// set current classloader to 1st
ClassLoader cl = oldLoader.getClassloaderForArtifact("def");

// load extended class and base class
cl.loadClass("jddunit.function2.ExceptionB");

// load base class
cl.loadClass("jddunit.function1.ExceptionA");

}


private String getVersion(String classname) throws Exception {
Class<?> def = ClassPathLoader.getLatest().forName(classname);
assertThat(def).isNotNull();
Expand All @@ -216,4 +247,27 @@ private static String createClassContent(String version, String functionName) {
+ "public void execute(FunctionContext context) {context.getResultSender().lastResult(\""
+ version + "\");}}";
}

private static String create1ClassContent(String className1) {
return "package jddunit.function1;"
+ "public class "
+ className1 + " extends Exception {"
+ "private static final long serialVersionUID = 1L;"
+ "public " + className1 + "(String message) {"
+ " super(message);"
+ "}"
+ "}";
}

private static String create2ClassContent(String className1, String className2) {
return "package jddunit.function2;" + "import jddunit.function1." + className2 + ";"
+ "public class "
+ className1 + " extends " + className2 + " {"
+ "private static final long serialVersionUID = 1L;"
+ "public " + className1 + "(String message) {"
+ " super(message);"
+ "}"
+ "}";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ public Class<?> getProxyClass(final Class<?>... classObjs) {
return null;
}

public ClassLoader getClassloaderForArtifact(String artifactId) {
return artifactIdsToClassLoader.get(artifactId);
}

private synchronized void rebuildClassLoaderForDeployedJars() {
leafLoader = null;
List<Deployment> deployments = jarDeploymentService.listDeployed();
Expand Down

0 comments on commit de7834a

Please sign in to comment.