Skip to content
This repository was archived by the owner on May 9, 2020. It is now read-only.

Commit 2b05aea

Browse files
committed
Cleaned up the way the classloaders are setup at startup by removing duplicate logic and responsibilities between the start and base components.
Before this change the following classloader hierarchy was setup: 1) the "application" classloader for the "start" component 2) one "native lib" classloader for the "base" component 3) one "native lib" classloader for the all the other (non "base") framework/application/specialpurpose/hot-deploy components 4) one "web app" classloader for each of the webapplications After this commit the hierarchy is simplified by merging #2 and #3: a) the "application" classloader for the "start" component b) one "native lib" classloader for the all the framework/application/specialpurpose/hot-deploy components (including the "base" component) c) one "web app" classloader for each of the webapplications This has been achieved by merging the code that was setting up classloader #2 in the start component (for the "base" component) with the code that was setting up the classloader #3 in the base component: now all the code is in the start component. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1633182 13f79535-47bb-0310-9956-ffa450edef68
1 parent e8c39f8 commit 2b05aea

File tree

5 files changed

+127
-131
lines changed

5 files changed

+127
-131
lines changed

framework/base/ofbiz-component.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ under the License.
2222
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2323
xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/ofbiz-component.xsd">
2424
<resource-loader name="main" type="component"/>
25+
<classpath type="dir" location="dtd"/>
2526
<classpath type="jar" location="build/lib/*"/>
2627
<classpath type="dir" location="config"/>
2728
<classpath type="jar" location="lib/*"/>
29+
<classpath type="jar" location="lib/ant/*"/>
30+
<classpath type="jar" location="lib/commons/*"/>
31+
<classpath type="jar" location="lib/j2eespecs/*"/>
32+
<classpath type="jar" location="lib/scripting/*"/>
2833

2934
<test-suite loader="main" location="testdef/basetests.xml"/>
3035

framework/base/src/org/ofbiz/base/container/ComponentContainer.java

Lines changed: 4 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,9 @@ public class ComponentContainer implements Container {
4646

4747
public static final String module = ComponentContainer.class.getName();
4848

49-
//protected static List loadedComponents2 = null;
50-
protected Classpath classPath = new Classpath();
51-
protected Classpath libraryPath = new Classpath(System.getProperty("java.library.path"));
5249
protected String configFileLocation = null;
5350
private String name;
5451
private boolean loaded = false;
55-
private String instrumenterClassName;
56-
private String instrumenterFile;
5752

5853
@Override
5954
public void init(String[] args, String name, String configFile) throws ContainerException {
@@ -69,25 +64,9 @@ public void init(String[] args, String name, String configFile) throws Container
6964
loaderConfig = cc.getProperty("loader-config").value;
7065
}
7166

72-
// check for en override update classpath
73-
boolean updateClassPath = true;
74-
if (cc.getProperty("update-classpath") != null) {
75-
updateClassPath = "true".equalsIgnoreCase(cc.getProperty("update-classpath").value);
76-
}
77-
if (cc.getProperty("ofbiz.instrumenterClassName") != null) {
78-
instrumenterClassName = cc.getProperty("ofbiz.instrumenterClassName").value;
79-
} else {
80-
instrumenterClassName = null;
81-
}
82-
if (cc.getProperty("ofbiz.instrumenterFile") != null) {
83-
instrumenterFile = cc.getProperty("ofbiz.instrumenterFile").value;
84-
} else {
85-
instrumenterFile = null;
86-
}
87-
8867
// load the components
8968
try {
90-
loadComponents(loaderConfig, updateClassPath, instrumenterClassName, instrumenterFile);
69+
loadComponents(loaderConfig);
9170
} catch (AlreadyLoadedException e) {
9271
throw new ContainerException(e);
9372
} catch (ComponentException e) {
@@ -102,10 +81,8 @@ public boolean start() throws ContainerException {
10281
return true;
10382
}
10483

105-
public synchronized void loadComponents(String loaderConfig, boolean updateClasspath, String instrumenterClassName, String instrumenterFile) throws AlreadyLoadedException, ComponentException {
84+
public synchronized void loadComponents(String loaderConfig) throws AlreadyLoadedException, ComponentException {
10685
// set the loaded list; and fail if already loaded
107-
//if (loadedComponents == null) {
108-
// loadedComponents = new LinkedList();
10986
if (!loaded) {
11087
loaded = true;
11188
} else {
@@ -129,18 +106,6 @@ public synchronized void loadComponents(String loaderConfig, boolean updateClass
129106
this.loadComponentFromConfig(parentPath, def);
130107
}
131108
}
132-
133-
// set the new classloader/classpath on the current thread
134-
if (updateClasspath) {
135-
if (UtilValidate.isNotEmpty(instrumenterFile) && UtilValidate.isNotEmpty(instrumenterClassName)) {
136-
classPath.instrument(instrumenterFile, instrumenterClassName);
137-
}
138-
139-
System.setProperty("java.library.path", libraryPath.toString());
140-
ClassLoader cl = classPath.getClassLoader();
141-
Thread.currentThread().setContextClassLoader(cl);
142-
}
143-
144109
Debug.logInfo("All components loaded", module);
145110
}
146111

@@ -229,67 +194,10 @@ private void loadComponentDirectory(String directoryName) {
229194
private void loadComponent(ComponentConfig config) {
230195
// make sure the component is enabled
231196
if (!config.enabled()) {
232-
Debug.logInfo("Not Loading component : [" + config.getComponentName() + "] (disabled)", module);
197+
Debug.logInfo("Not Loaded component : [" + config.getComponentName() + "] (disabled)", module);
233198
return;
234199
}
235-
236-
Debug.logInfo("Loading component : [" + config.getComponentName() + "]", module);
237-
boolean isBaseComponent = "base".equals(config.getComponentName());
238-
List<ComponentConfig.ClasspathInfo> classpathInfos = config.getClasspathInfos();
239-
String configRoot = config.getRootLocation();
240-
configRoot = configRoot.replace('\\', '/');
241-
// set the root to have a trailing slash
242-
if (!configRoot.endsWith("/")) {
243-
configRoot = configRoot + "/";
244-
}
245-
if (classpathInfos != null) {
246-
String nativeLibExt = System.mapLibraryName("someLib").replace("someLib", "").toLowerCase();
247-
for (ComponentConfig.ClasspathInfo cp: classpathInfos) {
248-
String location = cp.location.replace('\\', '/');
249-
// set the location to not have a leading slash
250-
if (location.startsWith("/")) {
251-
location = location.substring(1);
252-
}
253-
if (!"jar".equals(cp.type) && !"dir".equals(cp.type)) {
254-
Debug.logError("Classpath type '" + cp.type + "' is not supported; '" + location + "' not loaded", module);
255-
continue;
256-
}
257-
String dirLoc = location;
258-
if (dirLoc.endsWith("/*")) {
259-
// strip off the slash splat
260-
dirLoc = location.substring(0, location.length() - 2);
261-
}
262-
File path = FileUtil.getFile(configRoot + dirLoc);
263-
if (path.exists()) {
264-
if (path.isDirectory()) {
265-
if ("dir".equals(cp.type)) {
266-
if (!isBaseComponent)
267-
classPath.addComponent(configRoot + location);
268-
}
269-
// load all .jar, .zip files and native libs in this directory
270-
boolean containsNativeLibs = false;
271-
for (File file: path.listFiles()) {
272-
String fileName = file.getName().toLowerCase();
273-
if (fileName.endsWith(".jar") || fileName.endsWith(".zip")) {
274-
if (!isBaseComponent)
275-
classPath.addComponent(file);
276-
} else if (fileName.endsWith(nativeLibExt)) {
277-
containsNativeLibs = true;
278-
}
279-
}
280-
if (containsNativeLibs) {
281-
libraryPath.addComponent(path);
282-
}
283-
} else {
284-
// add a single file
285-
if (!isBaseComponent)
286-
classPath.addComponent(configRoot + location);
287-
}
288-
} else {
289-
Debug.logWarning("Location '" + configRoot + dirLoc + "' does not exist", module);
290-
}
291-
}
292-
}
200+
Debug.logInfo("Loaded component : [" + config.getComponentName() + "]", module);
293201
}
294202

295203
/**

framework/base/src/org/ofbiz/base/container/JustLoadComponentsContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void init(String[] args, String name, String configFile) {
3636
this.name = name;
3737
try {
3838
ComponentContainer cc = new ComponentContainer();
39-
cc.loadComponents(null, true, null, null);
39+
cc.loadComponents(null);
4040
} catch (AlreadyLoadedException e) {
4141
Debug.logError(e, module);
4242
} catch (ComponentException e) {

framework/start/src/org/ofbiz/base/start/Config.java

Lines changed: 114 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,18 @@
1818
*******************************************************************************/
1919
package org.ofbiz.base.start;
2020

21+
import org.w3c.dom.Document;
22+
import org.w3c.dom.Element;
23+
import org.w3c.dom.NamedNodeMap;
24+
import org.w3c.dom.Node;
25+
import org.w3c.dom.NodeList;
26+
import org.xml.sax.SAXException;
27+
28+
import javax.xml.parsers.DocumentBuilder;
29+
import javax.xml.parsers.DocumentBuilderFactory;
30+
import javax.xml.parsers.ParserConfigurationException;
2131
import java.io.File;
32+
import java.io.FileFilter;
2233
import java.io.FileInputStream;
2334
import java.io.FileNotFoundException;
2435
import java.io.IOException;
@@ -291,47 +302,118 @@ private Properties getPropertiesFile(String config) throws IOException {
291302
return props;
292303
}
293304

294-
void initClasspath(Classpath classPath) throws IOException {
305+
void initClasspath(Classpath classPath, Classpath libraryPath) throws Exception {
295306
// add OFBIZ_HOME to class path
296307
classPath.addClasspath(this.ofbizHome);
297-
298-
// load all the resources from the framework base component
299-
// load all the jars from the base lib directory
300-
if (this.baseLib != null) {
301-
loadLibs(classPath, this.baseLib, true);
302-
}
303-
// load the ofbiz-base.jar and the ofbiz-base-test.jar
304-
if (this.baseJar != null) {
305-
classPath.addComponent(this.baseJar);
306-
classPath.addComponent(this.baseJar.substring(0, this.baseJar.indexOf(".jar")) + "-test.jar");
307-
}
308-
// load the base schema directory
309-
if (this.baseDtd != null) {
310-
classPath.addComponent(this.baseDtd);
311-
}
312-
// load the config directory
313-
if (this.baseConfig != null) {
314-
classPath.addComponent(this.baseConfig);
315-
}
308+
File home = new File(this.ofbizHome);
309+
collectClasspathEntries(new File(home, "framework"), classPath, libraryPath);
310+
collectClasspathEntries(new File(home, "applications"), classPath, libraryPath);
311+
collectClasspathEntries(new File(home, "specialpurpose"), classPath, libraryPath);
312+
collectClasspathEntries(new File(home, "hot-deploy"), classPath, libraryPath);
313+
System.setProperty("java.library.path", libraryPath.toString());
316314
classPath.instrument(this.instrumenterFile, this.instrumenterClassName);
317315
}
318316

319-
private void loadLibs(Classpath classPath, String path, boolean recurse) throws IOException {
320-
File libDir = new File(path);
321-
if (libDir.exists()) {
322-
File files[] = libDir.listFiles();
323-
for (File file: files) {
324-
String fileName = file.getName();
325-
if (file.isHidden()) {
317+
private void collectClasspathEntries(File folder, Classpath classpath, Classpath libraryPath) throws ParserConfigurationException, IOException, SAXException {
318+
if (!folder.exists() && !folder.isDirectory()) {
319+
return;
320+
}
321+
FileFilter componentLoadFilter = new FileFilter() {
322+
public boolean accept(File pathname) {
323+
return "component-load.xml".equals(pathname.getName());
324+
}
325+
};
326+
FileFilter folderFilter = new FileFilter() {
327+
public boolean accept(File pathname) {
328+
return pathname.isDirectory();
329+
}
330+
};
331+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
332+
DocumentBuilder builder = factory.newDocumentBuilder();
333+
File[] componentLoadFiles;
334+
List<File> ofbizComponents = new ArrayList<File>();
335+
componentLoadFiles = folder.listFiles(componentLoadFilter);
336+
if (componentLoadFiles != null && componentLoadFiles.length == 1) {
337+
File componentLoadFile = componentLoadFiles[0];
338+
// parse and get folder names to be processed
339+
Document document = builder.parse(componentLoadFile);
340+
Element element = document.getDocumentElement();
341+
NodeList loadComponents = element.getElementsByTagName("load-component");
342+
for (int i = 0; i < loadComponents.getLength(); i++) {
343+
Node loadComponent = loadComponents.item(i);
344+
NamedNodeMap attributes = loadComponent.getAttributes();
345+
Node componentLocation = attributes.getNamedItem("component-location");
346+
if (componentLocation == null) {
347+
continue;
348+
}
349+
ofbizComponents.add(new File(new File(folder, componentLocation.getNodeValue()), "ofbiz-component.xml"));
350+
}
351+
} else {
352+
File[] componentFolders = folder.listFiles(folderFilter);
353+
for (File componentFolder: componentFolders) {
354+
File ofbizComponent = new File(componentFolder, "ofbiz-component.xml");
355+
if (ofbizComponent.exists()) {
356+
ofbizComponents.add(ofbizComponent);
357+
}
358+
}
359+
}
360+
String nativeLibExt = System.mapLibraryName("someLib").replace("someLib", "").toLowerCase();
361+
for (File ofbizComponent: ofbizComponents) {
362+
Document document = builder.parse(ofbizComponent);
363+
Element element = document.getDocumentElement();
364+
if (element.hasAttribute("enabled")) {
365+
if ("false".equals(element.getAttribute("enabled"))) {
326366
continue;
327367
}
328-
// FIXME: filter out other files?
329-
if (file.isDirectory() && !"CVS".equals(fileName) && !".svn".equals(fileName) && recurse) {
330-
loadLibs(classPath, file.getCanonicalPath(), recurse);
331-
} else if (fileName.endsWith(".jar") || fileName.endsWith(".zip")) {
332-
classPath.addComponent(file);
368+
}
369+
NodeList classpathEntries = element.getElementsByTagName("classpath");
370+
for (int i = 0; i < classpathEntries.getLength(); i++) {
371+
Node classpathEntry = classpathEntries.item(i);
372+
NamedNodeMap attributes = classpathEntry.getAttributes();
373+
Node type = attributes.getNamedItem("type");
374+
if (type == null || !("jar".equals(type.getNodeValue()) || "dir".equals(type.getNodeValue()))) {
375+
continue;
376+
}
377+
Node location = attributes.getNamedItem("location");
378+
String locationValue = location.getNodeValue();
379+
locationValue = locationValue.replace('\\', '/');
380+
// set the location to not have a leading slash
381+
if (locationValue.startsWith("/")) {
382+
locationValue = locationValue.substring(1);
383+
}
384+
String dirLoc = locationValue;
385+
if (dirLoc.endsWith("/*")) {
386+
// strip off the slash splat
387+
dirLoc = locationValue.substring(0, locationValue.length() - 2);
388+
}
389+
390+
String fileNameSeparator = ("\\".equals(File.separator) ? "\\" + File.separator : File.separator);
391+
dirLoc = dirLoc.replaceAll("/+|\\\\+", fileNameSeparator);
392+
File path = new File(ofbizComponent.getParent(), dirLoc);
393+
if (path.exists()) {
394+
if (path.isDirectory()) {
395+
if ("dir".equals(type.getNodeValue())) {
396+
classpath.addComponent(path.toString());
397+
}
398+
// load all .jar, .zip files and native libs in this directory
399+
boolean containsNativeLibs = false;
400+
for (File file: path.listFiles()) {
401+
String fileName = file.getName().toLowerCase();
402+
if (fileName.endsWith(".jar") || fileName.endsWith(".zip")) {
403+
classpath.addComponent(file);
404+
} else if (fileName.endsWith(nativeLibExt)) {
405+
containsNativeLibs = true;
406+
}
407+
}
408+
if (containsNativeLibs) {
409+
libraryPath.addComponent(path);
410+
}
411+
} else {
412+
classpath.addComponent(path.toString());
413+
}
333414
}
334415
}
335416
}
336417
}
418+
337419
}

framework/start/src/org/ofbiz/base/start/Start.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,10 @@ public void run() {
241241

242242
private void initStartLoaders() throws StartupException {
243243
Classpath classPath = new Classpath();
244+
Classpath libraryPath = new Classpath(System.getProperty("java.library.path"));
244245
try {
245-
this.config.initClasspath(classPath);
246-
} catch (IOException e) {
246+
this.config.initClasspath(classPath, libraryPath);
247+
} catch (Exception e) {
247248
throw (StartupException) new StartupException("Couldn't initialized classpath").initCause(e);
248249
}
249250
ClassLoader classloader = classPath.getClassLoader();

0 commit comments

Comments
 (0)