Skip to content

Commit

Permalink
Improved: Do not add classpath info to the classloader classpath (OFB…
Browse files Browse the repository at this point in the history
…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 376b818
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 147 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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.
*
Expand Down Expand Up @@ -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() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.