Skip to content
Permalink
Browse files

Improved: Rewrite ‘Classpath’ class

(OFBIZ-11237)

This class has been moved to ‘org.apache.ofbiz.base.container’ to be
able to reduce its visibility to the “package” level.

The API has been change to avoid unnecessary checks and simpler dataflow.

There was no need to provide thread safety so the ‘synchronized’
blocks have been removed.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1868027 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Oct 5, 2019
1 parent 4ee6633 commit c982df72c27d5f18ef33a3aeb40d1f1d64bdf6eb
@@ -0,0 +1,84 @@
/*
* 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.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* A class path object.
*
* <p>This reifies the notion of a Java class path to be able to manipulate them programmatically.
*/
final class Classpath {
/** {@code .jar} and {@code .zip} files matcher. */
private static final PathMatcher JAR_ZIP_FILES = FileSystems.getDefault().getPathMatcher("glob:*.{java,zip}");

/** A sequence of unique path elements. */
private final LinkedHashSet<Path> elements = new LinkedHashSet<>();

/**
* Adds a directory or a file to the class path.
*
* In the directory case, all files ending with ".jar" or ".zip" inside this directory
* are added to the class path.
*
* @param file the absolute normalized file name of a directory or a file that must exist
* @param type either "dir" or "jar"
* @throws NullPointerException when {@code file} is {@code null}.
*/
void add(Path file, String type) {
elements.add(file);
if (Files.isDirectory(file) && "dir".equals(type)) {
try (Stream<Path> innerFiles = Files.list(file)) {
innerFiles.filter(JAR_ZIP_FILES::matches).forEach(elements::add);
} catch (IOException e) {
String fmt = "Warning : Module classpath component '%s' is not valid and will be ignored...";
System.err.println(String.format(fmt, file));
}
}
}

@Override
public String toString() {
return elements.stream()
.map(Path::toString)
.collect(Collectors.joining(File.pathSeparator));
}

/**
* Returns the list of class path component URIs.
*
* @return a list of class path component URIs
*/
List<URI> toUris() {
return elements.stream()
.map(Path::toUri)
.collect(Collectors.toList());
}
}
@@ -20,6 +20,7 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
@@ -39,7 +40,6 @@
import org.apache.ofbiz.base.component.ComponentConfig;
import org.apache.ofbiz.base.component.ComponentException;
import org.apache.ofbiz.base.component.ComponentLoaderConfig;
import org.apache.ofbiz.base.start.Classpath;
import org.apache.ofbiz.base.start.Start;
import org.apache.ofbiz.base.start.StartupCommand;
import org.apache.ofbiz.base.util.Debug;
@@ -96,12 +96,13 @@ public boolean start() {
*/
private static void loadClassPathForAllComponents(List<Classpath> componentsClassPath) {
List<URL> allComponentUrls = new ArrayList<>();
for(Classpath classPath : componentsClassPath) {
for (Classpath classPath : componentsClassPath) {
try {
allComponentUrls.addAll(Arrays.asList(classPath.getUrls()));
for (URI uri : classPath.toUris()) {
allComponentUrls.add(uri.toURL());
}
} catch (MalformedURLException e) {
Debug.logError("Unable to load component classpath" + classPath.toString(), module);
Debug.logError(e.getMessage(), module);
Debug.logError(e, "Unable to load component classpath %s", module, classPath);
}
}
URL[] componentURLs = allComponentUrls.toArray(new URL[allComponentUrls.size()]);
@@ -347,15 +348,11 @@ private static Classpath buildClasspathFromComponentConfig(ComponentConfig confi

location = location.startsWith("/") ? location.substring(1) : location;
String dirLoc = location.endsWith("/*") ? location.substring(0, location.length() - 2) : location;
Path path = Paths.get(configRoot + dirLoc);

Path path = Paths.get(configRoot + dirLoc).toAbsolutePath().normalize();
if (Files.exists(path)) {
classPath.addComponent(configRoot + location);
if (Files.isDirectory(path) && "dir".equals(cp.type)) {
classPath.addFilesFromPath(path.toFile());
}
classPath.add(path, cp.type);
} else {
Debug.logWarning("Location '" + configRoot + dirLoc + "' does not exist", module);
Debug.logWarning("Location '" + path + "' does not exist", module);
}
}
return classPath;

This file was deleted.

0 comments on commit c982df7

Please sign in to comment.
You can’t perform that action at this time.