|
18 | 18 | *******************************************************************************/
|
19 | 19 | package org.ofbiz.base.start;
|
20 | 20 |
|
| 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; |
21 | 31 | import java.io.File;
|
| 32 | +import java.io.FileFilter; |
22 | 33 | import java.io.FileInputStream;
|
23 | 34 | import java.io.FileNotFoundException;
|
24 | 35 | import java.io.IOException;
|
@@ -291,47 +302,118 @@ private Properties getPropertiesFile(String config) throws IOException {
|
291 | 302 | return props;
|
292 | 303 | }
|
293 | 304 |
|
294 |
| - void initClasspath(Classpath classPath) throws IOException { |
| 305 | + void initClasspath(Classpath classPath, Classpath libraryPath) throws Exception { |
295 | 306 | // add OFBIZ_HOME to class path
|
296 | 307 | 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()); |
316 | 314 | classPath.instrument(this.instrumenterFile, this.instrumenterClassName);
|
317 | 315 | }
|
318 | 316 |
|
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"))) { |
326 | 366 | continue;
|
327 | 367 | }
|
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 | + } |
333 | 414 | }
|
334 | 415 | }
|
335 | 416 | }
|
336 | 417 | }
|
| 418 | + |
337 | 419 | }
|
0 commit comments