26
26
import java .nio .file .Files ;
27
27
import java .nio .file .Path ;
28
28
import java .util .ArrayList ;
29
- import java .util .HashSet ;
30
- import java .util .LinkedHashSet ;
31
29
import java .util .List ;
32
- import java .util .Map ;
33
- import java .util .Objects ;
34
- import java .util .Set ;
35
- import java .util .concurrent .ConcurrentHashMap ;
36
30
import java .util .concurrent .atomic .AtomicBoolean ;
37
31
import java .util .stream .Collectors ;
38
32
import java .util .stream .Stream ;
44
38
import org .apache .ofbiz .base .start .Start ;
45
39
import org .apache .ofbiz .base .start .StartupCommand ;
46
40
import org .apache .ofbiz .base .util .Debug ;
47
- import org .apache .ofbiz .base .util .UtilValidate ;
48
41
49
42
/**
50
43
* ComponentContainer - StartupContainer implementation for Components
@@ -62,9 +55,6 @@ public class ComponentContainer implements Container {
62
55
63
56
private String name ;
64
57
private final AtomicBoolean loaded = new AtomicBoolean (false );
65
- /** The set of ready components in their inverse dependency order. */
66
- private final LinkedHashSet <ComponentConfig > readyComponents = new LinkedHashSet <>();
67
- private static Map <String , List <String >> toBeLoadedComponents = new ConcurrentHashMap <>();
68
58
69
59
@ Override
70
60
public void init (List <StartupCommand > ofbizCommands , String name , String configFile ) throws ContainerException {
@@ -89,11 +79,12 @@ void init(String name, Path ofbizHome) throws ContainerException {
89
79
for (ComponentDef def : ComponentLoaderConfig .getRootComponents ()) {
90
80
loadComponent (ofbizHome , def );
91
81
}
82
+ ComponentConfig .sortDependencies ();
92
83
} catch (IOException | ComponentException e ) {
93
84
throw new ContainerException (e );
94
85
}
95
86
String fmt = "Added class path for component : [%s]" ;
96
- List <Classpath > componentsClassPath = readyComponents . stream ()
87
+ List <Classpath > componentsClassPath = ComponentConfig . components ()
97
88
.peek (cmpnt -> Debug .logInfo (String .format (fmt , cmpnt .getComponentName ()), module ))
98
89
.map (cmpnt -> buildClasspathFromComponentConfig (cmpnt ))
99
90
.collect (Collectors .toList ());
@@ -134,19 +125,15 @@ private static void loadClassPathForAllComponents(List<Classpath> componentsClas
134
125
* @param dir the location where the component should be loaded
135
126
* @param component a single component or a component directory definition
136
127
* @throws IOException when component directory loading fails.
137
- * @throws ComponentException when retrieving component configuration files fails.
138
128
*/
139
- private void loadComponent (Path dir , ComponentDef component ) throws IOException , ComponentException {
129
+ private void loadComponent (Path dir , ComponentDef component ) throws IOException {
140
130
Path location = component .location .isAbsolute () ? component .location : dir .resolve (component .location );
141
131
switch (component .type ) {
142
132
case COMPONENT_DIRECTORY :
143
133
loadComponentDirectory (location );
144
134
break ;
145
135
case SINGLE_COMPONENT :
146
- ComponentConfig config = retrieveComponentConfig (null , location );
147
- if (config != null ) {
148
- loadSingleComponent (config );
149
- }
136
+ retrieveComponentConfig (null , location );
150
137
break ;
151
138
}
152
139
}
@@ -157,9 +144,8 @@ private void loadComponent(Path dir, ComponentDef component) throws IOException,
157
144
*
158
145
* @param directoryName the name of component directory to load
159
146
* @throws IOException
160
- * @throws ComponentException
161
147
*/
162
- private void loadComponentDirectory (Path directoryName ) throws IOException , ComponentException {
148
+ private void loadComponentDirectory (Path directoryName ) throws IOException {
163
149
Debug .logInfo ("Auto-Loading component directory : [" + directoryName + "]" , module );
164
150
if (Files .exists (directoryName ) && Files .isDirectory (directoryName )) {
165
151
Path componentLoad = directoryName .resolve (ComponentLoaderConfig .COMPONENT_LOAD_XML_FILENAME );
@@ -205,56 +191,15 @@ private void loadComponentsInDirectoryUsingLoadFile(Path directoryPath, Path com
205
191
* for loading purposes
206
192
*
207
193
* @param directoryPath a valid absolute path of a component directory
208
- * @throws IOException
209
- * @throws ComponentException
194
+ * @throws IOException if an I/O error occurs when opening the directory
210
195
*/
211
- private void loadComponentsInDirectory (Path directoryPath ) throws IOException , ComponentException {
196
+ private static void loadComponentsInDirectory (Path directoryPath ) throws IOException {
212
197
try (Stream <Path > paths = Files .list (directoryPath )) {
213
- List < ComponentConfig > componentConfigs = paths .sorted ()
198
+ paths .sorted ()
214
199
.map (cmpnt -> directoryPath .resolve (cmpnt ).toAbsolutePath ().normalize ())
215
200
.filter (Files ::isDirectory )
216
201
.filter (dir -> Files .exists (dir .resolve (ComponentConfig .OFBIZ_COMPONENT_XML_FILENAME )))
217
- .map (componentDir -> retrieveComponentConfig (null , componentDir ))
218
- .filter (Objects ::nonNull )
219
- .collect (Collectors .toList ());
220
-
221
- for (ComponentConfig cmpnt : componentConfigs ) {
222
- loadSingleComponent (cmpnt );
223
- }
224
- }
225
- loadComponentWithDependency ();
226
- }
227
-
228
- /**
229
- * Checks dependency for unloaded components and add them into
230
- * componentsClassPath
231
- *
232
- * @throws IOException
233
- * @throws ComponentException
234
- */
235
- private void loadComponentWithDependency () throws IOException , ComponentException {
236
- while (true ) {
237
- if (UtilValidate .isEmpty (toBeLoadedComponents )) {
238
- return ;
239
- } else {
240
- for (Map .Entry <String , List <String >> entries : toBeLoadedComponents .entrySet ()) {
241
- ComponentConfig config = retrieveComponentConfig (entries .getKey (), null );
242
- if (config .enabled ()) {
243
- List <String > dependencyList = checkDependencyForComponent (config );
244
- if (UtilValidate .isNotEmpty (dependencyList )) {
245
- toBeLoadedComponents .replace (config .getComponentName (), dependencyList );
246
- String msg = "Not loading component [" + config .getComponentName () + "] because it's dependent Component is not loaded [ " + dependencyList + "]" ;
247
- Debug .logInfo (msg , module );
248
- }
249
- if (UtilValidate .isEmpty (dependencyList )) {
250
- readyComponents .add (config );
251
- toBeLoadedComponents .replace (config .getComponentName (), dependencyList );
252
- }
253
- } else {
254
- Debug .logInfo ("Not loading component [" + config .getComponentName () + "] because it's disabled" , module );
255
- }
256
- }
257
- }
202
+ .forEach (componentDir -> retrieveComponentConfig (null , componentDir ));
258
203
}
259
204
}
260
205
@@ -278,53 +223,6 @@ private static ComponentConfig retrieveComponentConfig(String name, Path locatio
278
223
return config ;
279
224
}
280
225
281
- /**
282
- * Load a single component by adding all its classpath entries to
283
- * the list of classpaths to be loaded
284
- *
285
- * @param config the component configuration
286
- * @throws ComponentException
287
- */
288
- private void loadSingleComponent (ComponentConfig config ) throws ComponentException {
289
- if (config .enabled ()) {
290
- List <String > dependencyList = checkDependencyForComponent (config );
291
- if (UtilValidate .isEmpty (dependencyList )) {
292
- readyComponents .add (config );
293
- }
294
- } else {
295
- Debug .logInfo ("Not loading component [" + config .getComponentName () + "] because it's disabled" , module );
296
- }
297
- }
298
-
299
- /**
300
- * Check for components loaded and Removes loaded components dependency
301
- * from list of unloaded components
302
- *
303
- * @param config the component configuration
304
- * @throws ComponentException
305
- */
306
- private List <String > checkDependencyForComponent (ComponentConfig config ) throws ComponentException {
307
- List <String > dependencyList = new ArrayList <>(config .getDependsOn ());
308
- if (UtilValidate .isNotEmpty (dependencyList )) {
309
- Set <String > resolvedDependencyList = new HashSet <>();
310
- for (String dependency : dependencyList ) {
311
- Debug .logInfo ("Component : " + config .getComponentName () + " is Dependent on " + dependency , module );
312
- ComponentConfig componentConfig = ComponentConfig .getComponentConfig (String .valueOf (dependency ));
313
- if (readyComponents .contains (componentConfig )) {
314
- resolvedDependencyList .add (dependency );
315
- }
316
- }
317
- resolvedDependencyList .forEach (resolvedDependency -> Debug .logInfo ("Resolved : " + resolvedDependency + " Dependency for Component " + config .getComponentName (), module ));
318
- dependencyList .removeAll (resolvedDependencyList );
319
- if (UtilValidate .isEmpty (dependencyList )) {
320
- toBeLoadedComponents .remove (config .getComponentName ());
321
- } else {
322
- toBeLoadedComponents .put (config .getComponentName (), dependencyList );
323
- }
324
- }
325
- return dependencyList ;
326
- }
327
-
328
226
/**
329
227
* Constructs a {@code Classpath} object for a specific component definition.
330
228
*
0 commit comments