Author: adrianc
Date: Sun Mar 20 20:02:48 2011 New Revision: 1083554 URL: http://svn.apache.org/viewvc?rev=1083554&view=rev Log: Some bug fixes and improvements to ComponentContainer.java and Classpath.java: 1. <classpath type="dir" .../> did not add jar files to the classpath. That has been fixed. 2. <classpath type="dir" .../> that contained an invalid path was added to the classpath anyway. That has been fixed. 3. Added ability to load native libraries (*.dll, *.so). Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/container/ComponentContainer.java ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Classpath.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/container/ComponentContainer.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/container/ComponentContainer.java?rev=1083554&r1=1083553&r2=1083554&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/container/ComponentContainer.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/container/ComponentContainer.java Sun Mar 20 20:02:48 2011 @@ -48,6 +48,7 @@ public class ComponentContainer implemen //protected static List loadedComponents2 = null; protected Classpath classPath = new Classpath(System.getProperty("java.class.path")); + protected Classpath libraryPath = new Classpath(System.getProperty("java.library.path")); protected String configFileLocation = null; private boolean loaded = false; private String instrumenterClassName; @@ -137,6 +138,7 @@ public class ComponentContainer implemen if (updateClasspath) { classPath.instrument(instrumenterFile, instrumenterClassName); System.setProperty("java.class.path", classPath.toString()); + System.setProperty("java.library.path", libraryPath.toString()); ClassLoader cl = classPath.getClassLoader(); Thread.currentThread().setContextClassLoader(cl); } @@ -242,39 +244,47 @@ public class ComponentContainer implemen configRoot = configRoot + "/"; } if (classpathInfos != null) { + String nativeLibExt = System.mapLibraryName("someLib").replace("someLib", "").toLowerCase(); for (ComponentConfig.ClasspathInfo cp: classpathInfos) { String location = cp.location.replace('\\', '/'); // set the location to not have a leading slash if (location.startsWith("/")) { location = location.substring(1); } - if ("dir".equals(cp.type)) { - classPath.addComponent(configRoot + location); - } else if ("jar".equals(cp.type)) { - String dirLoc = location; - if (dirLoc.endsWith("/*")) { - // strip off the slash splat - dirLoc = location.substring(0, location.length() - 2); - } - File path = FileUtil.getFile(configRoot + dirLoc); - if (path.exists()) { - if (path.isDirectory()) { - // load all .jar and .zip files in this directory - for (File file: path.listFiles()) { - String fileName = file.getName(); - if (fileName.endsWith(".jar") || fileName.endsWith(".zip")) { - classPath.addComponent(file); - } - } - } else { - // add a single file + if (!"jar".equals(cp.type) && !"dir".equals(cp.type)) { + Debug.logError("Classpath type '" + cp.type + "' is not supported; '" + location + "' not loaded", module); + continue; + } + String dirLoc = location; + if (dirLoc.endsWith("/*")) { + // strip off the slash splat + dirLoc = location.substring(0, location.length() - 2); + } + File path = FileUtil.getFile(configRoot + dirLoc); + if (path.exists()) { + if (path.isDirectory()) { + if ("dir".equals(cp.type)) { classPath.addComponent(configRoot + location); } + // load all .jar, .zip files and native libs in this directory + boolean containsNativeLibs = false; + for (File file: path.listFiles()) { + String fileName = file.getName().toLowerCase(); + if (fileName.endsWith(".jar") || fileName.endsWith(".zip")) { + classPath.addComponent(file); + } else if (fileName.endsWith(nativeLibExt)) { + containsNativeLibs = true; + } + } + if (containsNativeLibs) { + libraryPath.addComponent(path); + } } else { - Debug.logWarning("Location '" + configRoot + dirLoc + "' does not exist", module); + // add a single file + classPath.addComponent(configRoot + location); } } else { - Debug.logError("Classpath type '" + cp.type + "' is not supported; '" + location + "' not loaded", module); + Debug.logWarning("Location '" + configRoot + dirLoc + "' does not exist", module); } } } Modified: ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Classpath.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Classpath.java?rev=1083554&r1=1083553&r2=1083554&view=diff ============================================================================== --- ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Classpath.java (original) +++ ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Classpath.java Sun Mar 20 20:02:48 2011 @@ -133,12 +133,36 @@ public class Classpath { } public ClassLoader getClassLoader(ClassLoader parent) { - URL[] urls = getUrls(); - - return new URLClassLoader(urls, parent); + return new NativeLibClassLoader(getUrls(), parent); } public List<File> getElements() { return _elements; } + + /* + * Native library class loader. This class is necessary because the + * bootstrap ClassLoader caches the native library path - so any + * changes to the library path are ignored (changes that might have + * been made by loading OFBiz components). + */ + private class NativeLibClassLoader extends URLClassLoader { + + private NativeLibClassLoader(URL[] urls, ClassLoader parent) { + super(urls, parent); + } + + @Override + protected String findLibrary(String libname) { + String[] libPaths = System.getProperty("java.library.path").split(File.pathSeparator); + String libFileName = System.mapLibraryName(libname); + for (String path : libPaths) { + File libFile = new File(path, libFileName); + if (libFile.exists()) { + return libFile.getAbsolutePath(); + } + } + return null; + } + } } |
Free forum by Nabble | Edit this page |