diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/container/Classpath.java b/framework/base/src/main/java/org/apache/ofbiz/base/container/Classpath.java new file mode 100644 index 0000000000..5d097607c1 --- /dev/null +++ b/framework/base/src/main/java/org/apache/ofbiz/base/container/Classpath.java @@ -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. + * + *

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 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 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 toUris() { + return elements.stream() + .map(Path::toUri) + .collect(Collectors.toList()); + } +} diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/container/ComponentContainer.java b/framework/base/src/main/java/org/apache/ofbiz/base/container/ComponentContainer.java index 5c787327da..b11df68e1c 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/container/ComponentContainer.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/container/ComponentContainer.java @@ -20,6 +20,7 @@ package org.apache.ofbiz.base.container; 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 java.util.stream.Collectors; 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 class ComponentContainer implements Container { */ private static void loadClassPathForAllComponents(List componentsClassPath) { List 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 @@ public class ComponentContainer implements Container { 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; diff --git a/framework/start/src/main/java/org/apache/ofbiz/base/start/Classpath.java b/framework/start/src/main/java/org/apache/ofbiz/base/start/Classpath.java deleted file mode 100644 index 188913ba6b..0000000000 --- a/framework/start/src/main/java/org/apache/ofbiz/base/start/Classpath.java +++ /dev/null @@ -1,169 +0,0 @@ -/******************************************************************************* - * 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. - *******************************************************************************/ - -// NOTE: Portions Originally Copyright 2002 Mort Bay Consulting (Australia) Pty. Ltd. (this was taken from a code base released under the Apache License, though no header was on this file) - -package org.apache.ofbiz.base.start; - -import java.io.File; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; - -/** - * A class path accumulator. - *

You can build a class path by repeatedly calling the addXxx methods, - * then use the getXxx methods to get the accumulated class path.

- */ -public final class Classpath { - - private List elements = new ArrayList<>(); - - /** - * Default constructor. - */ - public Classpath() { - } - - /** - * Adds a class path component. The component may be a directory or a file. - * If component does not exist, the method does nothing. - * - * @param component The class path component to add - * @return true if the component was added - * @throws IOException if there was a problem parsing the component - * @throws IllegalArgumentException if component is null - */ - public boolean addComponent(File component) throws IOException { - if (component == null) { - throw new IllegalArgumentException("component cannot be null"); - } - if (component.exists()) { - File key = component.getCanonicalFile(); - synchronized (elements) { - if (!elements.contains(key)) { - elements.add(key); - return true; - } - } - } else { - System.out.println("Warning : Module classpath component '" + component + "' is not valid and will be ignored..."); - } - return false; - } - - /** - * Adds a class path component. The component may be a directory or a file. - * If component does not exist, the method does nothing. - * - * @param component The class path component to add - * @return true if the component was added - * @throws IOException if there was a problem parsing the component - * @throws IllegalArgumentException if component is null or empty - */ - public boolean addComponent(String component) throws IOException { - if (component == null || component.isEmpty()) { - throw new IllegalArgumentException("component cannot be null or empty"); - } - return addComponent(new File(component)); - } - - /** - * Scans a directory and adds all files ending with ".jar" or ".zip" to - * the class path. - * If path is not a directory, the method does nothing. - * - * @param path the directory to scan - * @throws IOException if there was a problem processing the directory - * @throws IllegalArgumentException if path is null - */ - public void addFilesFromPath(File path) throws IOException { - if (path == null) { - throw new IllegalArgumentException("path cannot be null"); - } - File[] listedFiles; - if (path.isDirectory() && path.exists()) { - // load all .jar, .zip files and native libs in this directory - listedFiles = path.listFiles(); - } else { - listedFiles = null; - } - if (listedFiles != null) { - for (File file : listedFiles) { - String fileName = file.getName().toLowerCase(Locale.getDefault()); - if (fileName.endsWith(".jar") || fileName.endsWith(".zip")) { - File key = file.getCanonicalFile(); - synchronized (elements) { - if (!elements.contains(key)) { - elements.add(key); - } - } - } - } - } else { - System.out.println("Warning : Module classpath component '" + path + "' is not valid and will be ignored..."); - } - } - - private static void appendPath(StringBuilder cp, String path) { - if (path.indexOf(' ') >= 0) { - cp.append('\"'); - cp.append(path); - cp.append('"'); - } else { - cp.append(path); - } - } - - /** - * Returns a list of class path component URLs. - * - * @return A list of class path component URLs - * @throws MalformedURLException - */ - public URL[] getUrls() throws MalformedURLException { - synchronized (elements) { - int cnt = elements.size(); - URL[] urls = new URL[cnt]; - for (int i = 0; i < cnt; i++) { - urls[i] = elements.get(i).toURI().toURL(); - } - return urls; - } - } - - @Override - public String toString() { - StringBuilder cp = new StringBuilder(1024); - synchronized (elements) { - int cnt = elements.size(); - if (cnt >= 1) { - cp.append(elements.get(0).getPath()); - } - for (int i = 1; i < cnt; i++) { - cp.append(File.pathSeparatorChar); - appendPath(cp, elements.get(i).getPath()); - } - } - return cp.toString(); - } -}