Improved: Refactor and simplify the startup sequence in OFBiz

(OFBIZ-8337)

This is another major commit to the refactoring of the startup sequence in
OFBiz with the following highlighted changes:

- Delete the NativeLibClassLoader.java
- Delete the native classpath logic in Classpath.java
- Refactor the ComponentContainer and StartupControlPanel to operate without
  the NativeLibClassLoader. This substantially simplifies the code
- Declare a URLClassLoader in ComponentContainer that is instantiated upon
  building the classpath for all components. This makes the classloader start
  in one shot
- Simplify the Config file and remove fields that are not used. Also
  refactor some messy logic for loading the props file and other code
  improvements
- Refactor all the .properties files for startup to have a consistent
  structure that clearly documents all available properties and the
  default value for each property. This change is also related to the
  changes applied on Config.java
- Remove the declaration of the StartupLoader implementation class
  from all startup .properties files because only one implementation exists
  and it should be the default always.
- Refactor the loaders retrieval code (main, rmi, test, load-data) defined
  in the startup .properties files
- Refactor some switch statements to comply with java coding standards
- Add the DTDs defined in base through Gradle because we removed the
  NativeLibClassLoader and the classpath buildup logic in StartupControlPanel

Thanks: Jacopo Cappellato for reviewing the work


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1772879 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Taher A. Alkhateeb
2016-12-06 12:31:00 +00:00
parent 03bb6055d6
commit 8074a9b3c6
12 changed files with 241 additions and 382 deletions
@@ -22,6 +22,8 @@ import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -30,7 +32,6 @@ import org.apache.ofbiz.base.component.ComponentConfig;
import org.apache.ofbiz.base.component.ComponentException;
import org.apache.ofbiz.base.component.ComponentLoaderConfig;
import org.apache.ofbiz.base.start.Classpath;
import org.apache.ofbiz.base.start.NativeLibClassLoader;
import org.apache.ofbiz.base.start.Start;
import org.apache.ofbiz.base.start.StartupCommand;
import org.apache.ofbiz.base.util.Debug;
@@ -43,7 +44,7 @@ import org.apache.ofbiz.base.util.FileUtil;
* defined in OFBiz. This container must run before any other containers to
* allow components to access any necessary resources. Furthermore, the
* ComponentContainer also builds up the <code>ComponentConfigCache</code>
* to keep track of all loaded components
* defined in <code>ComponentConfig</code> to keep track of loaded components
*
*/
public class ComponentContainer implements Container {
@@ -52,6 +53,7 @@ public class ComponentContainer implements Container {
private String name;
private final AtomicBoolean loaded = new AtomicBoolean(false);
private final List<Classpath> componentsClassPath = new ArrayList<Classpath>();
@Override
public void init(List<StartupCommand> ofbizCommands, String name, String configFile) throws ContainerException {
@@ -68,6 +70,7 @@ public class ComponentContainer implements Container {
} catch (IOException | ComponentException e) {
throw new ContainerException(e);
}
loadClassPathForAllComponents(componentsClassPath);
Debug.logInfo("All components loaded", module);
}
@@ -78,6 +81,28 @@ public class ComponentContainer implements Container {
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
* @throws ContainerException
*/
private void loadClassPathForAllComponents(List<Classpath> componentsClassPath) throws ContainerException {
List<URL> allComponentUrls = new ArrayList<URL>();
for(Classpath classPath : componentsClassPath) {
try {
allComponentUrls.addAll(Arrays.asList(classPath.getUrls()));
} catch (MalformedURLException e) {
Debug.logError("Unable to load component classpath" + classPath.toString(), module);
Debug.logError(e.getMessage(), module);
}
}
URL[] componentURLs = allComponentUrls.toArray(new URL[allComponentUrls.size()]);
URLClassLoader classLoader = new URLClassLoader(componentURLs, Thread.currentThread().getContextClassLoader());
Thread.currentThread().setContextClassLoader(classLoader);
}
/**
* Checks if <code>ComponentDef.type</code> is a directory or a single component.
* If it is a directory, load the directory, otherwise load a single component
@@ -197,28 +222,19 @@ public class ComponentContainer implements Container {
/**
* Load a single component by adding all its classpath entries to
* the classloader
* the list of classpaths to be loaded
*
* @param config: the component configuration
* @throws IOException
*/
private void loadComponent(ComponentConfig config) throws IOException {
if (!config.enabled()) {
if (config.enabled()) {
Classpath classpath = buildClasspathFromComponentConfig(config);
componentsClassPath.add(classpath);
Debug.logInfo("Added class path for component : [" + config.getComponentName() + "]", module);
} else {
Debug.logInfo("Not loading component [" + config.getComponentName() + "] because it is disabled", module);
return;
}
NativeLibClassLoader classloader = (NativeLibClassLoader) Thread.currentThread().getContextClassLoader();
Classpath classPath = buildClasspathFromComponentConfig(config);
for (URL url : classPath.getUrls()) {
classloader.addURL(url);
}
for (File folder : classPath.getNativeFolders()) {
classloader.addNativeClassPath(folder);
}
Debug.logInfo("Loaded component : [" + config.getComponentName() + "]", module);
}
/**
@@ -22,7 +22,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.ofbiz.base.component.ComponentConfig;
@@ -31,7 +30,6 @@ import org.apache.ofbiz.base.start.StartupCommand;
import org.apache.ofbiz.base.start.StartupException;
import org.apache.ofbiz.base.start.StartupLoader;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.StringUtil;
import org.apache.ofbiz.base.util.UtilValidate;
import edu.emory.mathcs.backport.java.util.Collections;
@@ -57,12 +55,7 @@ public class ContainerLoader implements StartupLoader {
public synchronized void load(Config config, List<StartupCommand> ofbizCommands) throws StartupException {
// loaders defined in startup (e.g. main, test, load-data, etc ...)
List<String> loaders = null;
for (Map<String,String> loaderMap: config.loaders) {
if (module.equals(loaderMap.get("class"))) {
loaders = StringUtil.split((String)loaderMap.get("profiles"), ",");
}
}
List<String> loaders = config.loaders;
// load containers defined in ofbiz-containers.xml
Debug.logInfo("[Startup] Loading containers...", module);