/
ContainerLoader.java
180 lines (160 loc) · 7.8 KB
/
ContainerLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*******************************************************************************
* 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.container;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
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.util.Debug;
import org.apache.ofbiz.base.util.UtilValidate;
/**
* An object that loads containers (background processes).
*
* Normally, instances of this class are created by OFBiz startup code, and
* client code should not create instances of this class. Client code is
* responsible for making sure containers are shut down properly.
* <p>
* When OFBiz starts, the main thread will create the {@code ContainerLoader} instance and
* then call the loader's {@code load} method.
* After this instance has been created and initialized, the main thread will call the
* {@code start} method. When OFBiz shuts down, a separate shutdown thread will call
* the {@code unload} method.
*/
public class ContainerLoader {
public static final String module = ContainerLoader.class.getName();
private final Deque<Container> loadedContainers = new LinkedList<>();
/**
* Starts the containers.
*
* @param config Startup config
* @param ofbizCommands Command-line arguments
* @throws StartupException If an error was encountered. Throwing this exception
* will halt loader loading, so it should be thrown only when OFBiz can't
* operate without it.
*/
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 = config.loaders;
// 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));
// 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 static Collection<ContainerConfig.Configuration> retrieveOfbizContainers(String configFile)
throws StartupException {
try {
return ContainerConfig.getConfigurations(configFile);
} catch (ContainerException e) {
throw new StartupException(e);
}
}
private static List<ContainerConfig.Configuration> filterContainersHavingMatchingLoaders(List<String> loaders,
Collection<ContainerConfig.Configuration> containerConfigs) {
return containerConfigs.stream()
.filter(containerCfg ->
UtilValidate.isEmpty(containerCfg.loaders) &&
UtilValidate.isEmpty(loaders) ||
containerCfg.loaders.stream().anyMatch(loader -> loaders.contains(loader)))
.collect(Collectors.toList());
}
private static List<Container> loadContainersFromConfigurations(List<ContainerConfig.Configuration> containerConfigs,
Config config, List<StartupCommand> ofbizCommands) throws StartupException {
List<Container> loadContainers = new ArrayList<>();
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 static Container loadContainer(String configFile, ContainerConfig.Configuration containerCfg,
List<StartupCommand> ofbizCommands) throws StartupException {
// load the container class
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class<?> containerClass;
try {
containerClass = loader.loadClass(containerCfg.className);
} catch (ClassNotFoundException e) {
throw new StartupException("Cannot locate container class", e);
}
if (containerClass == null) {
throw new StartupException("Component container class not loaded");
}
// create a new instance of the container object
Container containerObj;
try {
containerObj = (Container) containerClass.getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException 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(ofbizCommands, containerCfg.name, configFile);
} catch (ContainerException e) {
throw new StartupException("Cannot init() " + containerCfg.name, e);
}
return containerObj;
}
private void startLoadedContainers() throws StartupException {
Debug.logInfo("[Startup] Starting containers...", module);
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);
}
Debug.logInfo("Started container " + container.getName(), module);
}
}
/**
* Stops the containers.
*/
public synchronized void unload() {
Debug.logInfo("Shutting down containers", module);
loadedContainers.descendingIterator().forEachRemaining(loadedContainer -> {
Debug.logInfo("Stopping container " + loadedContainer.getName(), module);
try {
loadedContainer.stop();
} catch (ContainerException e) {
Debug.logError(e, module);
}
Debug.logInfo("Stopped container " + loadedContainer.getName(), module);
});
}
}