Author: mthl
Date: Sat Oct  5 15:44:21 2019
New Revision: 1868027

URL: http://svn.apache.org/viewvc?rev=1868027&view=rev
Log:
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.

Added:
    
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/container/Classpath.java
   (with props)
Removed:
    
ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Classpath.java
Modified:
    
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/container/ComponentContainer.java

Added: 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/container/Classpath.java
URL: 
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/container/Classpath.java?rev=1868027&view=auto
==============================================================================
--- 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/container/Classpath.java
 (added)
+++ 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/container/Classpath.java
 Sat Oct  5 15:44:21 2019
@@ -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());
+     }
+}

Propchange: 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/container/Classpath.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/container/Classpath.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/container/Classpath.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/container/ComponentContainer.java
URL: 
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/container/ComponentContainer.java?rev=1868027&r1=1868026&r2=1868027&view=diff
==============================================================================
--- 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/container/ComponentContainer.java
 (original)
+++ 
ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/container/ComponentContainer.java
 Sat Oct  5 15:44:21 2019
@@ -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 implemen
      */
     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 @@ public class ComponentContainer implemen
 
             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;


Reply via email to