Improved: Refactor and simplify the startup sequence in OFBiz
(OFBIZ-8337) This is a major refactoring exercise to simplify the startup sequence in OFBiz. There are no functional changes, however, both the logic and the API signatures of the core classes in OFBiz are changed to help simplify the startup sequence. The changes insofar are summarized as: - Rename ContainerConfig.Container to ContainerConfig.Configuration, the old nameing was very confusing as it confuses it with actual containers - Change the signature of ContainerLoader and Container to accept List<StartupCommand> instead of String[]. And also migrate the adapter for converting these args into a standalone class called StartupCommandsToArgsAdapter - Remove the LockedBy annotation as it is not used - Substantially simplify the ContainerLoader load and unload logic and delete the start logic (merge it with load). The interface StartupLoader was updated accordingly - Remove old logic to load containers in hot-deploy components - Remove printThreadDump output from ContainerLoader which used to exist for debugging purposes and is no longer necessary - Cleanup the AdminServer startup logic - Change the StartupControlPanel logic to use only one StartupLoader instead of a list. The only implementation is ContainerLoader - Remove the synchronized blocks in StartupControlPanel due to having only one loader - Update the startup .properties files to reflect the changes in the startup logic - Update the TestRunContainer to use List<StartupCommand> in its logic We expect more commits (specifically to the containers) to complete this refactoring initiative git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1765127 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -33,7 +33,7 @@ import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.apache.ofbiz.base.container.ContainerConfig;
|
||||
import org.apache.ofbiz.base.container.ContainerConfig.Container;
|
||||
import org.apache.ofbiz.base.container.ContainerConfig.Configuration;
|
||||
import org.apache.ofbiz.base.container.ContainerException;
|
||||
import org.apache.ofbiz.base.location.FlexibleLocation;
|
||||
import org.apache.ofbiz.base.util.Assert;
|
||||
@@ -85,18 +85,18 @@ public final class ComponentConfig {
|
||||
return componentConfigCache.values();
|
||||
}
|
||||
|
||||
public static List<ContainerConfig.Container> getAllContainers() {
|
||||
return getAllContainers(null);
|
||||
public static List<ContainerConfig.Configuration> getAllConfigurations() {
|
||||
return getAllConfigurations(null);
|
||||
}
|
||||
|
||||
public static List<ContainerConfig.Container> getAllContainers(String componentName) {
|
||||
List<ContainerConfig.Container> containers = new ArrayList<ContainerConfig.Container>();
|
||||
public static List<ContainerConfig.Configuration> getAllConfigurations(String componentName) {
|
||||
List<ContainerConfig.Configuration> configurations = new ArrayList<ContainerConfig.Configuration>();
|
||||
for (ComponentConfig cc : getAllComponents()) {
|
||||
if (componentName == null || componentName.equals(cc.getComponentName())) {
|
||||
containers.addAll(cc.getContainers());
|
||||
configurations.addAll(cc.getConfigurations());
|
||||
}
|
||||
}
|
||||
return containers;
|
||||
return configurations;
|
||||
}
|
||||
|
||||
public static List<EntityResourceInfo> getAllEntityResourceInfos(String type) {
|
||||
@@ -346,7 +346,7 @@ public final class ComponentConfig {
|
||||
private final List<TestSuiteInfo> testSuiteInfos;
|
||||
private final List<KeystoreInfo> keystoreInfos;
|
||||
private final List<WebappInfo> webappInfos;
|
||||
private final List<ContainerConfig.Container> containers;
|
||||
private final List<ContainerConfig.Configuration> configurations;
|
||||
|
||||
private ComponentConfig(String globalName, String rootLocation) throws ComponentException {
|
||||
if (!rootLocation.endsWith("/")) {
|
||||
@@ -463,16 +463,16 @@ public final class ComponentConfig {
|
||||
} else {
|
||||
this.webappInfos = Collections.emptyList();
|
||||
}
|
||||
// containers
|
||||
// configurations
|
||||
try {
|
||||
Collection<Container> containers = ContainerConfig.getContainers(xmlUrl);
|
||||
if (!containers.isEmpty()) {
|
||||
this.containers = Collections.unmodifiableList(new ArrayList<ContainerConfig.Container>(containers));
|
||||
Collection<Configuration> configurations = ContainerConfig.getConfigurations(xmlUrl);
|
||||
if (!configurations.isEmpty()) {
|
||||
this.configurations = Collections.unmodifiableList(new ArrayList<ContainerConfig.Configuration>(configurations));
|
||||
} else {
|
||||
this.containers = Collections.emptyList();
|
||||
this.configurations = Collections.emptyList();
|
||||
}
|
||||
} catch (ContainerException ce) {
|
||||
throw new ComponentException("Error reading containers for component: " + this.globalName, ce);
|
||||
throw new ComponentException("Error reading container configurations for component: " + this.globalName, ce);
|
||||
}
|
||||
if (Debug.verboseOn())
|
||||
Debug.logVerbose("Read component config : [" + rootLocation + "]", module);
|
||||
@@ -490,8 +490,8 @@ public final class ComponentConfig {
|
||||
return this.componentName;
|
||||
}
|
||||
|
||||
public List<ContainerConfig.Container> getContainers() {
|
||||
return this.containers;
|
||||
public List<ContainerConfig.Configuration> getConfigurations() {
|
||||
return this.configurations;
|
||||
}
|
||||
|
||||
public List<EntityResourceInfo> getEntityResourceInfos() {
|
||||
|
||||
+3
-2
@@ -31,6 +31,7 @@ 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.StartupCommand;
|
||||
import org.apache.ofbiz.base.util.Debug;
|
||||
import org.apache.ofbiz.base.util.FileUtil;
|
||||
import org.apache.ofbiz.base.util.UtilValidate;
|
||||
@@ -52,7 +53,7 @@ public class ComponentContainer implements Container {
|
||||
private final AtomicBoolean loaded = new AtomicBoolean(false);
|
||||
|
||||
@Override
|
||||
public void init(String[] args, String name, String configFile) throws ContainerException {
|
||||
public void init(List<StartupCommand> ofbizCommands, String name, String configFile) throws ContainerException {
|
||||
if (!loaded.compareAndSet(false, true)) {
|
||||
throw new ContainerException("Components already loaded, cannot start");
|
||||
}
|
||||
@@ -60,7 +61,7 @@ public class ComponentContainer implements Container {
|
||||
this.configFileLocation = configFile;
|
||||
|
||||
// get the config for this container
|
||||
ContainerConfig.Container cc = ContainerConfig.getContainer(name, configFileLocation);
|
||||
ContainerConfig.Configuration cc = ContainerConfig.getConfiguration(name, configFileLocation);
|
||||
|
||||
// check for an override loader config
|
||||
String loaderConfig = null;
|
||||
|
||||
@@ -18,6 +18,10 @@
|
||||
*******************************************************************************/
|
||||
package org.apache.ofbiz.base.container;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.ofbiz.base.start.StartupCommand;
|
||||
|
||||
/**
|
||||
* An OFBiz container. A container can be thought of as a background process.
|
||||
*
|
||||
@@ -39,14 +43,14 @@ public interface Container {
|
||||
/** Initialize the container. This method must not block - implementations
|
||||
* should initialize internal structures and then return.
|
||||
*
|
||||
* @param args Command-line arguments.
|
||||
* @param ofbizCommands Command-line arguments.
|
||||
* @param name Unique name of the container's instance.
|
||||
* @param configFile Location of the configuration file used to load this container.
|
||||
* @throws ContainerException If an error was encountered. Throwing this exception
|
||||
* will halt container loading, so it should be thrown only when other containers
|
||||
* might depend on this one.
|
||||
*/
|
||||
public void init(String[] args, String name, String configFile) throws ContainerException;
|
||||
public void init(List<StartupCommand> ofbizCommands, String name, String configFile) throws ContainerException;
|
||||
|
||||
/**
|
||||
* Start the container process. This method must not block - implementations
|
||||
|
||||
@@ -29,7 +29,6 @@ import java.util.Map;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import org.apache.ofbiz.base.lang.LockedBy;
|
||||
import org.apache.ofbiz.base.util.StringUtil;
|
||||
import org.apache.ofbiz.base.util.UtilURL;
|
||||
import org.apache.ofbiz.base.util.UtilValidate;
|
||||
@@ -44,24 +43,25 @@ import org.xml.sax.SAXException;
|
||||
*/
|
||||
public class ContainerConfig {
|
||||
|
||||
private ContainerConfig() {}
|
||||
|
||||
public static final String module = ContainerConfig.class.getName();
|
||||
|
||||
@LockedBy("ContainerConfig.class")
|
||||
private static Map<String, Container> containers = new LinkedHashMap<String, Container>();
|
||||
private static Map<String, Configuration> configurations = new LinkedHashMap<String, Configuration>();
|
||||
|
||||
public static Container getContainer(String containerName, String configFile) throws ContainerException {
|
||||
Container container = containers.get(containerName);
|
||||
if (container == null) {
|
||||
getContainers(configFile);
|
||||
container = containers.get(containerName);
|
||||
public static Configuration getConfiguration(String containerName, String configFile) throws ContainerException {
|
||||
Configuration configuration = configurations.get(containerName);
|
||||
if (configuration == null) {
|
||||
getConfigurations(configFile);
|
||||
configuration = configurations.get(containerName);
|
||||
}
|
||||
if (container == null) {
|
||||
if (configuration == null) {
|
||||
throw new ContainerException("No container found with the name : " + containerName);
|
||||
}
|
||||
return container;
|
||||
return configuration;
|
||||
}
|
||||
|
||||
public static Collection<Container> getContainers(String configFile) throws ContainerException {
|
||||
public static Collection<Configuration> getConfigurations(String configFile) throws ContainerException {
|
||||
if (UtilValidate.isEmpty(configFile)) {
|
||||
throw new ContainerException("configFile argument cannot be null or empty");
|
||||
}
|
||||
@@ -69,24 +69,24 @@ public class ContainerConfig {
|
||||
if (xmlUrl == null) {
|
||||
throw new ContainerException("Could not find container config file " + configFile);
|
||||
}
|
||||
return getContainers(xmlUrl);
|
||||
return getConfigurations(xmlUrl);
|
||||
}
|
||||
|
||||
public static Collection<Container> getContainers(URL xmlUrl) throws ContainerException {
|
||||
public static Collection<Configuration> getConfigurations(URL xmlUrl) throws ContainerException {
|
||||
if (xmlUrl == null) {
|
||||
throw new ContainerException("xmlUrl argument cannot be null");
|
||||
}
|
||||
Collection<Container> result = getContainerPropsFromXml(xmlUrl);
|
||||
Collection<Configuration> result = getConfigurationPropsFromXml(xmlUrl);
|
||||
synchronized (ContainerConfig.class) {
|
||||
for (Container container : result) {
|
||||
containers.put(container.name, container);
|
||||
for (Configuration container : result) {
|
||||
configurations.put(container.name, container);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String getPropertyValue(ContainerConfig.Container parentProp, String name, String defaultValue) {
|
||||
ContainerConfig.Container.Property prop = parentProp.getProperty(name);
|
||||
public static String getPropertyValue(ContainerConfig.Configuration parentProp, String name, String defaultValue) {
|
||||
ContainerConfig.Configuration.Property prop = parentProp.getProperty(name);
|
||||
if (prop == null || UtilValidate.isEmpty(prop.value)) {
|
||||
return defaultValue;
|
||||
} else {
|
||||
@@ -94,8 +94,8 @@ public class ContainerConfig {
|
||||
}
|
||||
}
|
||||
|
||||
public static int getPropertyValue(ContainerConfig.Container parentProp, String name, int defaultValue) {
|
||||
ContainerConfig.Container.Property prop = parentProp.getProperty(name);
|
||||
public static int getPropertyValue(ContainerConfig.Configuration parentProp, String name, int defaultValue) {
|
||||
ContainerConfig.Configuration.Property prop = parentProp.getProperty(name);
|
||||
if (prop == null || UtilValidate.isEmpty(prop.value)) {
|
||||
return defaultValue;
|
||||
} else {
|
||||
@@ -109,8 +109,8 @@ public class ContainerConfig {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean getPropertyValue(ContainerConfig.Container parentProp, String name, boolean defaultValue) {
|
||||
ContainerConfig.Container.Property prop = parentProp.getProperty(name);
|
||||
public static boolean getPropertyValue(ContainerConfig.Configuration parentProp, String name, boolean defaultValue) {
|
||||
ContainerConfig.Configuration.Property prop = parentProp.getProperty(name);
|
||||
if (prop == null || UtilValidate.isEmpty(prop.value)) {
|
||||
return defaultValue;
|
||||
} else {
|
||||
@@ -118,8 +118,8 @@ public class ContainerConfig {
|
||||
}
|
||||
}
|
||||
|
||||
public static String getPropertyValue(ContainerConfig.Container.Property parentProp, String name, String defaultValue) {
|
||||
ContainerConfig.Container.Property prop = parentProp.getProperty(name);
|
||||
public static String getPropertyValue(ContainerConfig.Configuration.Property parentProp, String name, String defaultValue) {
|
||||
ContainerConfig.Configuration.Property prop = parentProp.getProperty(name);
|
||||
if (prop == null || UtilValidate.isEmpty(prop.value)) {
|
||||
return defaultValue;
|
||||
} else {
|
||||
@@ -127,8 +127,8 @@ public class ContainerConfig {
|
||||
}
|
||||
}
|
||||
|
||||
public static int getPropertyValue(ContainerConfig.Container.Property parentProp, String name, int defaultValue) {
|
||||
ContainerConfig.Container.Property prop = parentProp.getProperty(name);
|
||||
public static int getPropertyValue(ContainerConfig.Configuration.Property parentProp, String name, int defaultValue) {
|
||||
ContainerConfig.Configuration.Property prop = parentProp.getProperty(name);
|
||||
if (prop == null || UtilValidate.isEmpty(prop.value)) {
|
||||
return defaultValue;
|
||||
} else {
|
||||
@@ -142,8 +142,8 @@ public class ContainerConfig {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean getPropertyValue(ContainerConfig.Container.Property parentProp, String name, boolean defaultValue) {
|
||||
ContainerConfig.Container.Property prop = parentProp.getProperty(name);
|
||||
public static boolean getPropertyValue(ContainerConfig.Configuration.Property parentProp, String name, boolean defaultValue) {
|
||||
ContainerConfig.Configuration.Property prop = parentProp.getProperty(name);
|
||||
if (prop == null || UtilValidate.isEmpty(prop.value)) {
|
||||
return defaultValue;
|
||||
} else {
|
||||
@@ -151,9 +151,7 @@ public class ContainerConfig {
|
||||
}
|
||||
}
|
||||
|
||||
private ContainerConfig() {}
|
||||
|
||||
private static Collection<Container> getContainerPropsFromXml(URL xmlUrl) throws ContainerException {
|
||||
private static Collection<Configuration> getConfigurationPropsFromXml(URL xmlUrl) throws ContainerException {
|
||||
Document containerDocument = null;
|
||||
try {
|
||||
containerDocument = UtilXml.readXmlDocument(xmlUrl, true);
|
||||
@@ -165,20 +163,20 @@ public class ContainerConfig {
|
||||
throw new ContainerException("Error reading the container config file: " + xmlUrl, e);
|
||||
}
|
||||
Element root = containerDocument.getDocumentElement();
|
||||
List<Container> result = new ArrayList<Container>();
|
||||
List<Configuration> result = new ArrayList<Configuration>();
|
||||
for (Element curElement: UtilXml.childElementList(root, "container")) {
|
||||
result.add(new Container(curElement));
|
||||
result.add(new Configuration(curElement));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static class Container {
|
||||
public static class Configuration {
|
||||
public final String name;
|
||||
public final String className;
|
||||
public final List<String> loaders;
|
||||
public final Map<String, Property> properties;
|
||||
|
||||
public Container(Element element) {
|
||||
public Configuration(Element element) {
|
||||
this.name = element.getAttribute("name");
|
||||
this.className = element.getAttribute("class");
|
||||
this.loaders = StringUtil.split(element.getAttribute("loaders"), ",");
|
||||
|
||||
+74
-170
@@ -18,23 +18,23 @@
|
||||
*******************************************************************************/
|
||||
package org.apache.ofbiz.base.container;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.ofbiz.base.component.ComponentConfig;
|
||||
import org.apache.ofbiz.base.start.Config;
|
||||
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;
|
||||
|
||||
/**
|
||||
* An object that loads containers (background processes).
|
||||
*
|
||||
@@ -47,115 +47,70 @@ public class ContainerLoader implements StartupLoader {
|
||||
|
||||
public static final String module = ContainerLoader.class.getName();
|
||||
|
||||
private String configFile = null;
|
||||
private final List<Container> loadedContainers = new LinkedList<Container>();
|
||||
private boolean unloading = false;
|
||||
private boolean loaded = false;
|
||||
|
||||
/**
|
||||
* @see org.apache.ofbiz.base.start.StartupLoader#load(Config, String[])
|
||||
*/
|
||||
@Override
|
||||
public synchronized void load(Config config, String args[]) throws StartupException {
|
||||
if (this.loaded || this.unloading) {
|
||||
return;
|
||||
}
|
||||
this.loadedContainers.clear();
|
||||
// get this loader's configuration file
|
||||
this.configFile = config.containerConfig;
|
||||
public synchronized void load(Config config, List<StartupCommand> ofbizCommands) throws StartupException {
|
||||
|
||||
List<String> loaders = null;
|
||||
for (Map loaderMap: config.loaders) {
|
||||
if (module.equals(loaderMap.get("class"))) {
|
||||
loaders = StringUtil.split((String)loaderMap.get("profiles"), ",");
|
||||
}
|
||||
}
|
||||
// loaders defined in startup (e.g. main, test, load-data, etc ...)
|
||||
List<String> loaders = StringUtil.split((String) config.loader.get("profiles"), ",");
|
||||
|
||||
Debug.logInfo("[Startup] Loading containers from " + configFile + " for loaders " + loaders, module);
|
||||
Collection<ContainerConfig.Container> containers = null;
|
||||
try {
|
||||
containers = ContainerConfig.getContainers(configFile);
|
||||
} catch (ContainerException e) {
|
||||
throw new StartupException(e);
|
||||
}
|
||||
for (ContainerConfig.Container containerCfg : containers) {
|
||||
if (this.unloading) {
|
||||
return;
|
||||
}
|
||||
boolean matchingLoaderFound = false;
|
||||
if (UtilValidate.isEmpty(containerCfg.loaders) && UtilValidate.isEmpty(loaders)) {
|
||||
matchingLoaderFound = true;
|
||||
} else {
|
||||
for (String loader: loaders) {
|
||||
if (UtilValidate.isEmpty(containerCfg.loaders) || containerCfg.loaders.contains(loader)) {
|
||||
matchingLoaderFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (matchingLoaderFound) {
|
||||
Debug.logInfo("Loading container: " + containerCfg.name, module);
|
||||
Container tmpContainer = loadContainer(containerCfg, args);
|
||||
this.loadedContainers.add(tmpContainer);
|
||||
Debug.logInfo("Loaded container: " + containerCfg.name, module);
|
||||
}
|
||||
}
|
||||
if (this.unloading) {
|
||||
return;
|
||||
}
|
||||
// load containers defined in ofbiz-containers.xml
|
||||
Debug.logInfo("[Startup] Loading containers...", module);
|
||||
List<ContainerConfig.Configuration> ofbizContainerConfigs = filterContainersHavingMatchingLoaders(
|
||||
loaders, retrieveOfbizContainers(config.containerConfig));
|
||||
loadedContainers.addAll(loadContainersFromConfigurations(ofbizContainerConfigs, config, ofbizCommands));
|
||||
|
||||
List<ContainerConfig.Container> containersDefinedInComponents = ComponentConfig.getAllContainers();
|
||||
for (ContainerConfig.Container containerCfg: containersDefinedInComponents) {
|
||||
boolean matchingLoaderFound = false;
|
||||
if (UtilValidate.isEmpty(containerCfg.loaders) && UtilValidate.isEmpty(loaders)) {
|
||||
matchingLoaderFound = true;
|
||||
} else {
|
||||
for (String loader: loaders) {
|
||||
if (UtilValidate.isEmpty(containerCfg.loaders) || containerCfg.loaders.contains(loader)) {
|
||||
matchingLoaderFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (matchingLoaderFound) {
|
||||
Debug.logInfo("Loading component's container: " + containerCfg.name, module);
|
||||
Container tmpContainer = loadContainer(containerCfg, args);
|
||||
this.loadedContainers.add(tmpContainer);
|
||||
Debug.logInfo("Loaded component's container: " + containerCfg.name, module);
|
||||
}
|
||||
}
|
||||
// Get hot-deploy container configuration files
|
||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||
Enumeration<URL> resources;
|
||||
try {
|
||||
resources = loader.getResources("hot-deploy-containers.xml");
|
||||
while (resources.hasMoreElements() && !this.unloading) {
|
||||
URL xmlUrl = resources.nextElement();
|
||||
Debug.logInfo("Loading hot-deploy containers from " + xmlUrl, module);
|
||||
Collection<ContainerConfig.Container> hotDeployContainers = ContainerConfig.getContainers(xmlUrl);
|
||||
for (ContainerConfig.Container containerCfg : hotDeployContainers) {
|
||||
if (this.unloading) {
|
||||
return;
|
||||
}
|
||||
Container tmpContainer = loadContainer(containerCfg, args);
|
||||
this.loadedContainers.add(tmpContainer);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Debug.logError(e, "Could not load hot-deploy-containers.xml", module);
|
||||
throw new StartupException(e);
|
||||
}
|
||||
loaded = true;
|
||||
// load containers defined in components
|
||||
Debug.logInfo("[Startup] Loading component containers...", module);
|
||||
List<ContainerConfig.Configuration> componentContainerConfigs = filterContainersHavingMatchingLoaders(
|
||||
loaders, ComponentConfig.getAllConfigurations());
|
||||
loadedContainers.addAll(loadContainersFromConfigurations(componentContainerConfigs, config, ofbizCommands));
|
||||
|
||||
// Start all containers loaded from above steps
|
||||
startLoadedContainers();
|
||||
}
|
||||
|
||||
private Container loadContainer(ContainerConfig.Container containerCfg, String[] args) throws StartupException {
|
||||
private Collection<ContainerConfig.Configuration> retrieveOfbizContainers(String configFile) throws StartupException {
|
||||
try {
|
||||
return ContainerConfig.getConfigurations(configFile);
|
||||
} catch (ContainerException e) {
|
||||
throw new StartupException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private List<ContainerConfig.Configuration> filterContainersHavingMatchingLoaders(List<String> loaders,
|
||||
Collection<ContainerConfig.Configuration> containerConfigs) throws StartupException {
|
||||
return containerConfigs.stream()
|
||||
.filter(containerCfg ->
|
||||
UtilValidate.isEmpty(containerCfg.loaders) &&
|
||||
UtilValidate.isEmpty(loaders) ||
|
||||
containerCfg.loaders.stream().anyMatch(loader -> loaders.contains(loader)))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<Container> loadContainersFromConfigurations(List<ContainerConfig.Configuration> containerConfigs,
|
||||
Config config, List<StartupCommand> ofbizCommands) throws StartupException {
|
||||
|
||||
List<Container> loadContainers = new ArrayList<Container>();
|
||||
for (ContainerConfig.Configuration containerCfg : containerConfigs) {
|
||||
Debug.logInfo("Loading container: " + containerCfg.name, module);
|
||||
Container tmpContainer = loadContainer(config.containerConfig, containerCfg, ofbizCommands);
|
||||
loadContainers.add(tmpContainer);
|
||||
Debug.logInfo("Loaded container: " + containerCfg.name, module);
|
||||
}
|
||||
return loadContainers;
|
||||
}
|
||||
|
||||
private Container loadContainer(String configFile,
|
||||
ContainerConfig.Configuration containerCfg,
|
||||
List<StartupCommand> ofbizCommands) throws StartupException {
|
||||
// load the container class
|
||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||
if (loader == null) {
|
||||
Debug.logWarning("Unable to get context classloader; using system", module);
|
||||
loader = ClassLoader.getSystemClassLoader();
|
||||
}
|
||||
Class<?> containerClass = null;
|
||||
Class<?> containerClass;
|
||||
try {
|
||||
containerClass = loader.loadClass(containerCfg.className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
@@ -166,79 +121,34 @@ public class ContainerLoader implements StartupLoader {
|
||||
}
|
||||
|
||||
// create a new instance of the container object
|
||||
Container containerObj = null;
|
||||
Container containerObj;
|
||||
try {
|
||||
containerObj = (Container) containerClass.newInstance();
|
||||
} catch (InstantiationException e) {
|
||||
throw new StartupException("Cannot create " + containerCfg.name, e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new StartupException("Cannot create " + containerCfg.name, e);
|
||||
} catch (ClassCastException e) {
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
throw new StartupException("Cannot create " + containerCfg.name, e);
|
||||
}
|
||||
|
||||
if (containerObj == null) {
|
||||
throw new StartupException("Unable to create instance of component container");
|
||||
}
|
||||
|
||||
// initialize the container object
|
||||
try {
|
||||
containerObj.init(args, containerCfg.name, configFile);
|
||||
containerObj.init(ofbizCommands, containerCfg.name, configFile);
|
||||
} catch (ContainerException e) {
|
||||
throw new StartupException("Cannot init() " + containerCfg.name, e);
|
||||
} catch (java.lang.AbstractMethodError e) {
|
||||
throw new StartupException("Cannot init() " + containerCfg.name, e);
|
||||
}
|
||||
|
||||
return containerObj;
|
||||
}
|
||||
|
||||
private void printThreadDump() {
|
||||
Thread currentThread = Thread.currentThread();
|
||||
ThreadGroup group = currentThread.getThreadGroup();
|
||||
while (group.getParent() != null) {
|
||||
group = group.getParent();
|
||||
}
|
||||
Thread threadArr[] = new Thread[1000];
|
||||
group.enumerate(threadArr);
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
PrintWriter out = new PrintWriter(writer);
|
||||
out.println("Thread dump:");
|
||||
for (Thread t: threadArr) {
|
||||
if (t != null) {
|
||||
ThreadGroup g = t.getThreadGroup();
|
||||
out.println("Thread: " + t.getName() + " [" + t.getId() + "] @ " + (g != null ? g.getName() : "[none]") + " : " + t.getPriority() + " [" + t.getState().name() + "]");
|
||||
out.println("--- Alive: " + t.isAlive() + " Daemon: " + t.isDaemon());
|
||||
for (StackTraceElement stack: t.getStackTrace()) {
|
||||
out.println("### " + stack.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Debug.logInfo(writer.toString(), module);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.ofbiz.base.start.StartupLoader#start()
|
||||
*/
|
||||
@Override
|
||||
public synchronized void start() throws StartupException {
|
||||
if (!this.loaded || this.unloading) {
|
||||
throw new IllegalStateException("start() called on unloaded containers");
|
||||
}
|
||||
private void startLoadedContainers() throws StartupException {
|
||||
Debug.logInfo("[Startup] Starting containers...", module);
|
||||
// start each container object
|
||||
for (Container container: this.loadedContainers) {
|
||||
if (this.unloading) {
|
||||
return;
|
||||
}
|
||||
for (Container container: loadedContainers) {
|
||||
Debug.logInfo("Starting container " + container.getName(), module);
|
||||
try {
|
||||
container.start();
|
||||
} catch (ContainerException e) {
|
||||
throw new StartupException("Cannot start() " + container.getClass().getName(), e);
|
||||
} catch (java.lang.AbstractMethodError e) {
|
||||
throw new StartupException("Cannot start() " + container.getClass().getName(), e);
|
||||
}
|
||||
Debug.logInfo("Started container " + container.getName(), module);
|
||||
}
|
||||
@@ -248,26 +158,20 @@ public class ContainerLoader implements StartupLoader {
|
||||
* @see org.apache.ofbiz.base.start.StartupLoader#unload()
|
||||
*/
|
||||
@Override
|
||||
public void unload() throws StartupException {
|
||||
if (!this.unloading) {
|
||||
this.unloading = true;
|
||||
synchronized (this) {
|
||||
Debug.logInfo("Shutting down containers", module);
|
||||
if (Debug.verboseOn()) {
|
||||
printThreadDump();
|
||||
}
|
||||
// shutting down in reverse order
|
||||
for (int i = this.loadedContainers.size(); i > 0; i--) {
|
||||
Container container = this.loadedContainers.get(i-1);
|
||||
Debug.logInfo("Stopping container " + container.getName(), module);
|
||||
try {
|
||||
container.stop();
|
||||
} catch (ContainerException e) {
|
||||
Debug.logError(e, module);
|
||||
}
|
||||
Debug.logInfo("Stopped container " + container.getName(), module);
|
||||
}
|
||||
public synchronized void unload() throws StartupException {
|
||||
Debug.logInfo("Shutting down containers", module);
|
||||
|
||||
List<Container> reversedContainerList = new ArrayList<Container>(loadedContainers);
|
||||
Collections.reverse(reversedContainerList);
|
||||
|
||||
for(Container loadedContainer : reversedContainerList) {
|
||||
Debug.logInfo("Stopping container " + loadedContainer.getName(), module);
|
||||
try {
|
||||
loadedContainer.stop();
|
||||
} catch (ContainerException e) {
|
||||
Debug.logError(e, module);
|
||||
}
|
||||
Debug.logInfo("Stopped container " + loadedContainer.getName(), module);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-1
@@ -18,10 +18,14 @@
|
||||
*******************************************************************************/
|
||||
package org.apache.ofbiz.base.container;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.ofbiz.base.component.AlreadyLoadedException;
|
||||
import org.apache.ofbiz.base.component.ComponentException;
|
||||
import org.apache.ofbiz.base.start.StartupCommand;
|
||||
import org.apache.ofbiz.base.util.Debug;
|
||||
|
||||
|
||||
/**
|
||||
* A Container implementation to run the tests configured through this testtools stuff.
|
||||
*/
|
||||
@@ -32,7 +36,7 @@ public class JustLoadComponentsContainer implements Container {
|
||||
private String name;
|
||||
|
||||
@Override
|
||||
public void init(String[] args, String name, String configFile) {
|
||||
public void init(List<StartupCommand> ofbizCommands, String name, String configFile) {
|
||||
this.name = name;
|
||||
try {
|
||||
ComponentContainer cc = new ComponentContainer();
|
||||
|
||||
+6
-4
@@ -24,8 +24,10 @@ import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.ofbiz.base.start.Start;
|
||||
import org.apache.ofbiz.base.start.StartupCommand;
|
||||
import org.apache.ofbiz.base.util.RMIExtendedSocketFactory;
|
||||
|
||||
/**
|
||||
@@ -47,15 +49,15 @@ public class NamingServiceContainer implements Container {
|
||||
|
||||
private String name;
|
||||
|
||||
public void init(String[] args, String name, String configFile) throws ContainerException {
|
||||
public void init(List<StartupCommand> ofbizCommands, String name, String configFile) throws ContainerException {
|
||||
this.name =name;
|
||||
this.configFileLocation = configFile;
|
||||
|
||||
ContainerConfig.Container cfg = ContainerConfig.getContainer(name, configFileLocation);
|
||||
ContainerConfig.Configuration cfg = ContainerConfig.getConfiguration(name, configFileLocation);
|
||||
|
||||
// get the naming (JNDI) port
|
||||
|
||||
ContainerConfig.Container.Property port = cfg.getProperty("port");
|
||||
ContainerConfig.Configuration.Property port = cfg.getProperty("port");
|
||||
if (port.value != null) {
|
||||
try {
|
||||
this.namingPort = Integer.parseInt(port.value) + Start.getInstance().getConfig().portOffset;
|
||||
@@ -65,7 +67,7 @@ public class NamingServiceContainer implements Container {
|
||||
}
|
||||
|
||||
// get the naming (JNDI) server
|
||||
ContainerConfig.Container.Property host = cfg.getProperty("host");
|
||||
ContainerConfig.Configuration.Property host = cfg.getProperty("host");
|
||||
if (host != null && host.value != null) {
|
||||
this.namingHost = host.value ;
|
||||
}
|
||||
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package org.apache.ofbiz.base.container;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.ofbiz.base.start.StartupCommand;
|
||||
import org.apache.ofbiz.base.start.StartupCommandUtil;
|
||||
|
||||
/**
|
||||
* Temporary class. This class is responsible for converting
|
||||
* StartupCommand instances to String[]. This is a workaround until all
|
||||
* containers have fully changed their signatures to adopt the StartupCommand
|
||||
* object
|
||||
*/
|
||||
public class StartupCommandToArgsAdapter {
|
||||
/**
|
||||
* Generates the loaderArgs with arguments as expected by the
|
||||
* containers that will receive them.
|
||||
*
|
||||
* TODO A better solution is to change the signature of all
|
||||
* containers to receive a <tt>List</tt> of <tt>StartupCommand</tt>s
|
||||
* instead and delete the methods adaptStartupCommandsToLoaderArgs
|
||||
* and retrieveCommandArguments along with the loaderArgs list.
|
||||
*/
|
||||
public static String[] adaptStartupCommandsToLoaderArgs(List<StartupCommand> ofbizCommands) {
|
||||
List<String> loaderArgs = new ArrayList<String>();
|
||||
final String LOAD_DATA = StartupCommandUtil.StartupOption.LOAD_DATA.getName();
|
||||
final String TEST = StartupCommandUtil.StartupOption.TEST.getName();
|
||||
|
||||
if(ofbizCommands.stream().anyMatch(command -> command.getName().equals(LOAD_DATA))) {
|
||||
retrieveCommandArguments(ofbizCommands, LOAD_DATA).entrySet().stream().forEach(entry ->
|
||||
loaderArgs.add("-" + entry.getKey() + "=" + entry.getValue()));
|
||||
} else if(ofbizCommands.stream().anyMatch(command -> command.getName().equals(TEST))) {
|
||||
retrieveCommandArguments(ofbizCommands, TEST).entrySet().stream().forEach(entry ->
|
||||
loaderArgs.add("-" + entry.getKey() + "=" + entry.getValue()));
|
||||
}
|
||||
return loaderArgs.toArray(new String[loaderArgs.size()]);
|
||||
}
|
||||
|
||||
private static Map<String,String> retrieveCommandArguments(List<StartupCommand> ofbizCommands, String commandName) {
|
||||
return ofbizCommands.stream()
|
||||
.filter(option-> option.getName().equals(commandName))
|
||||
.collect(Collectors.toList()).get(0).getProperties();
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.ofbiz.base.lang;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/** This annotation is meant to be used to describe how some variable
|
||||
* or method is protected by some other locking mechanism. Its value
|
||||
* is free-form, and can contain anything. It will generally contain
|
||||
* the name of an instance variable, including <code>this</code>.
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@Target({ElementType.FIELD, ElementType.LOCAL_VARIABLE, ElementType.METHOD})
|
||||
public @interface LockedBy {
|
||||
String value();
|
||||
}
|
||||
@@ -31,7 +31,6 @@ package org.apache.ofbiz.base.metrics;
|
||||
import java.util.Collection;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.apache.ofbiz.base.lang.LockedBy;
|
||||
import org.apache.ofbiz.base.lang.ThreadSafe;
|
||||
import org.apache.ofbiz.base.util.Assert;
|
||||
import org.apache.ofbiz.base.util.UtilProperties;
|
||||
@@ -138,17 +137,11 @@ public final class MetricsFactory {
|
||||
}
|
||||
|
||||
private static final class MetricsImpl implements Metrics, Comparable<Metrics> {
|
||||
@LockedBy("this")
|
||||
private int count = 0;
|
||||
@LockedBy("this")
|
||||
private long lastTime = System.currentTimeMillis();
|
||||
@LockedBy("this")
|
||||
private double serviceRate = 0.0;
|
||||
@LockedBy("this")
|
||||
private long totalServiceTime = 0;
|
||||
@LockedBy("this")
|
||||
private long totalEvents = 0;
|
||||
@LockedBy("this")
|
||||
private long cumulativeEvents = 0;
|
||||
private final String name;
|
||||
private final int estimationSize;
|
||||
|
||||
Reference in New Issue
Block a user