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 You can build a class path by repeatedly calling the addXxx methods,
- * then use the getXxx methods to get the accumulated class path.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();
- }
-}