Skip to content
Permalink
Browse files
Improved: Do not add classpath info to the classloader classpath (OFB…
…IZ-11161)

Those directories are already added in the classpath by the build
system. The classpath info defined in “ofbiz-component.xml” files are
now only used to retrieve label files.
  • Loading branch information
mthl committed Dec 7, 2019
1 parent 98bfafd commit 376b81846436e291263b3f45e4aeb1b9257c685d

This file was deleted.

@@ -20,15 +20,11 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.ofbiz.base.component.ComponentConfig;
@@ -83,12 +79,6 @@ void init(String name, Path ofbizHome) throws ContainerException {
} catch (IOException | ComponentException e) {
throw new ContainerException(e);
}
String fmt = "Added class path for component : [%s]";
List<Classpath> componentsClassPath = ComponentConfig.components()
.peek(cmpnt -> Debug.logInfo(String.format(fmt, cmpnt.getComponentName()), module))
.map(cmpnt -> buildClasspathFromComponentConfig(cmpnt))
.collect(Collectors.toList());
loadClassPathForAllComponents(componentsClassPath);
Debug.logInfo("All components loaded", module);
}

@@ -97,28 +87,6 @@ public boolean start() {
return loaded.get();
}

/**
* Iterate over all the components and load their classpath URLs into the classloader
* and set the classloader as the context classloader
*
* @param componentsClassPath a list of classpaths for all components
*/
private static void loadClassPathForAllComponents(List<Classpath> componentsClassPath) {
List<URL> allComponentUrls = new ArrayList<>();
for (Classpath classPath : componentsClassPath) {
try {
for (URI uri : classPath.toUris()) {
allComponentUrls.add(uri.toURL());
}
} catch (MalformedURLException e) {
Debug.logError(e, "Unable to load component classpath %s", module, classPath);
}
}
URL[] componentURLs = allComponentUrls.toArray(new URL[allComponentUrls.size()]);
URLClassLoader classLoader = new URLClassLoader(componentURLs, Thread.currentThread().getContextClassLoader());
Thread.currentThread().setContextClassLoader(classLoader);
}

/**
* Loads any kind of component definition.
*
@@ -223,19 +191,6 @@ private static ComponentConfig retrieveComponentConfig(String name, Path locatio
return config;
}

/**
* Constructs a {@code Classpath} object for a specific component definition.
*
* @param config the component configuration
* @return the associated class path information
* @see ComponentConfig
*/
private static Classpath buildClasspathFromComponentConfig(ComponentConfig config) {
Classpath res = new Classpath();
config.getClasspathInfos().forEach(res::add);
return res;
}

@Override
public void stop() {
}
@@ -22,8 +22,6 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -69,15 +67,6 @@ public void testCheckDependencyForComponent() throws ContainerException, Startup
List<String> loadedComponents = ComponentConfig.components()
.map(c -> c.getGlobalName())
.collect(Collectors.toList());
// we can cast ContextClassLoader since ComponentContainer.loadClassPathForAllComponents has called
// setContextClassLoader with an URLClassLoader instance
URL[] classpath = ((URLClassLoader) Thread.currentThread().getContextClassLoader()).getURLs();
List<URL> actualClasspath = Arrays.asList(classpath);
List<URL> expectedClasspath = Arrays.asList(
ofbizHome.resolve(ORDER_CONFIG).toUri().toURL(),
ofbizHome.resolve(ACCOUNTING_CONFIG).toUri().toURL());

assertEquals(expectedClasspath, actualClasspath);
assertEquals(Arrays.asList("order", "accounting"), loadedComponents);
}
}

0 comments on commit 376b818

Please sign in to comment.