You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by re...@apache.org on 2021/10/07 10:04:25 UTC

[uima-uimaj] branch feature/UIMA-6358-ResourceManager_impl-should-provide-a-more-robust-way-to-register-multiple-datapaths created (now 0787986)

This is an automated email from the ASF dual-hosted git repository.

rec pushed a change to branch feature/UIMA-6358-ResourceManager_impl-should-provide-a-more-robust-way-to-register-multiple-datapaths
in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git.


      at 0787986  [UIMA-6358] ResourceManager_impl should provide a more robust way to register multiple datapaths

This branch includes the following new commits:

     new 0787986  [UIMA-6358] ResourceManager_impl should provide a more robust way to register multiple datapaths

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[uima-uimaj] 01/01: [UIMA-6358] ResourceManager_impl should provide a more robust way to register multiple datapaths

Posted by re...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rec pushed a commit to branch feature/UIMA-6358-ResourceManager_impl-should-provide-a-more-robust-way-to-register-multiple-datapaths
in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git

commit 07879861f734d9dc851111715936716d40f1e0c1
Author: Richard Eckart de Castilho <re...@apache.org>
AuthorDate: Thu Oct 7 12:04:12 2021 +0200

    [UIMA-6358] ResourceManager_impl should provide a more robust way to register multiple datapaths
    
    - Added new methods setDataPathElements(File/String...) and getDataPathElements -> String[]
    - Deprecated current setDataPath and getDataPath methods
    - WIP (no tests yet)
---
 .../apache/uima/resource/RelativePathResolver.java | 38 ++++++++++-
 .../org/apache/uima/resource/ResourceManager.java  | 39 ++++++++++-
 .../resource/impl/RelativePathResolver_impl.java   | 77 +++++++++++++++++-----
 .../uima/resource/impl/ResourceManager_impl.java   | 19 +++++-
 .../impl/RelativePathResolver_implTest.java        | 55 +++++++---------
 5 files changed, 176 insertions(+), 52 deletions(-)

diff --git a/uimaj-core/src/main/java/org/apache/uima/resource/RelativePathResolver.java b/uimaj-core/src/main/java/org/apache/uima/resource/RelativePathResolver.java
index 279bb94..5aa5501 100644
--- a/uimaj-core/src/main/java/org/apache/uima/resource/RelativePathResolver.java
+++ b/uimaj-core/src/main/java/org/apache/uima/resource/RelativePathResolver.java
@@ -19,6 +19,7 @@
 
 package org.apache.uima.resource;
 
+import java.io.File;
 import java.net.MalformedURLException;
 import java.net.URL;
 
@@ -37,10 +38,20 @@ public interface RelativePathResolver {
    * UNIX). Elements of this path may be absolute or relative file paths.
    * 
    * @return the data path
+   * @deprecated Use {@link #getDataPathElements} instead.
    */
+  @Deprecated
   String getDataPath();
 
   /**
+   * Gets the data path used to resolve relative paths. Elements of this path may be absolute or
+   * relative file paths.
+   * 
+   * @return the data path
+   */
+  String[] getDataPathElements();
+
+  /**
    * Sets the data path used to resolve relative paths. More than one directory may be specified by
    * separating them with the System <code>path.separator</code> character (; on windows, : on
    * UNIX). Elements of this path may be absolute or relative file paths.
@@ -50,10 +61,36 @@ public interface RelativePathResolver {
    * 
    * @throws MalformedURLException
    *           if a file path could not be converted to a URL
+   * @deprecated Use {@link #setDataPathElements} instead.
    */
+  @Deprecated
   void setDataPath(String aPath) throws MalformedURLException;
 
   /**
+   * Sets the data path elements used to resolve relative paths. Elements of this path may be
+   * absolute or relative file paths.
+   * 
+   * @param aElements
+   *          the data path elements
+   * 
+   * @throws MalformedURLException
+   *           if a file path could not be converted to a URL
+   */
+  void setDataPathElements(String... aElements) throws MalformedURLException;
+
+  /**
+   * Sets the data path elements used to resolve relative paths. Elements of this path may be
+   * absolute or relative file paths.
+   * 
+   * @param aElements
+   *          the data path elements
+   * 
+   * @throws MalformedURLException
+   *           if a file path could not be converted to a URL
+   */
+  void setDataPathElements(File... aElements) throws MalformedURLException;
+
+  /**
    * Resolves a relative URL to an absolute URL. This will attempt to resolve the URL relative to
    * each element of the data path, sequentially starting with the first element. If this results in
    * an absolute URL at which a file actually exists, that absolute URL is returned. If no file
@@ -75,5 +112,4 @@ public interface RelativePathResolver {
    *          the ClassLoader that should be used to resolve the resources.
    */
   void setPathResolverClassLoader(ClassLoader aClassLoader);
-
 }
diff --git a/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java b/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java
index 774feeb..990d917 100644
--- a/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java
+++ b/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java
@@ -19,6 +19,7 @@
 
 package org.apache.uima.resource;
 
+import java.io.File;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -43,23 +44,59 @@ public interface ResourceManager {
    * UNIX).
    * 
    * @return the data path
+   * 
+   * @deprecated Use {@link #getDataPathElements()} instead.
    */
+  @Deprecated
   String getDataPath();
 
   /**
+   * Gets the data path elements used to resolve relative paths.
+   * 
+   * @return the data path elements
+   */
+  String[] getDataPathElements();
+
+  /**
    * Sets the data path used to resolve relative paths. More than one directory may be specified by
    * separating them with the System <code>path.separator</code> character (; on windows, : on
-   * UNIX). The elements of this path may be URLs or File paths.
+   * UNIX). Elements of this path may be absolute or relative file paths.
    * 
    * @param aPath
    *          the data path
    * 
    * @throws MalformedURLException
    *           if an element of the path is neither a valid URL or a valid file path
+   * @deprecated Use {@link #setDataPathElements} instead.
    */
+  @Deprecated
   void setDataPath(String aPath) throws MalformedURLException;
 
   /**
+   * Sets the data path elements used to resolve relative paths. Elements of this path may be
+   * absolute or relative file paths.
+   * 
+   * @param aElements
+   *          the data path
+   * 
+   * @throws MalformedURLException
+   *           if an element of the path is neither a valid URL or a valid file path
+   */
+  void setDataPathElements(String... aElements) throws MalformedURLException;
+
+  /**
+   * Sets the data path elements used to resolve relative paths. Elements of this path may be
+   * absolute or relative file paths.
+   * 
+   * @param aElements
+   *          the data path
+   * 
+   * @throws MalformedURLException
+   *           if an element of the path is neither a valid URL or a valid file path
+   */
+  void setDataPathElements(File... aElements) throws MalformedURLException;
+
+  /**
    * Attempts to resolve a relative path to an absolute path using the same mechanism that the
    * ResourceManager uses to find resources -- a lookup in the datapath followed by a lookup in the
    * classpath.
diff --git a/uimaj-core/src/main/java/org/apache/uima/resource/impl/RelativePathResolver_impl.java b/uimaj-core/src/main/java/org/apache/uima/resource/impl/RelativePathResolver_impl.java
index 3abd3fe..ef88b02 100644
--- a/uimaj-core/src/main/java/org/apache/uima/resource/impl/RelativePathResolver_impl.java
+++ b/uimaj-core/src/main/java/org/apache/uima/resource/impl/RelativePathResolver_impl.java
@@ -19,12 +19,16 @@
 
 package org.apache.uima.resource.impl;
 
+import static java.util.Arrays.stream;
+import static java.util.stream.Collectors.joining;
+
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.StringTokenizer;
 
@@ -33,13 +37,11 @@ import org.apache.uima.util.impl.Constants;
 
 /**
  * Reference implementation of {@link RelativePathResolver}.
- * 
- * 
  */
 public class RelativePathResolver_impl implements RelativePathResolver {
 
   /** Data path as a string. */
-  private String mDataPath;
+  private String[] mDataPath;
 
   /** Array of base URLs parsed from the data path. */
   private URL[] mBaseUrls;
@@ -80,32 +82,76 @@ public class RelativePathResolver_impl implements RelativePathResolver {
       setDataPath(dataPath);
     } catch (MalformedURLException e) {
       // initialize to empty path
-      mDataPath = "";
+      mDataPath = Constants.EMPTY_STRING_ARRAY;
       mBaseUrls = Constants.EMPTY_URL_ARRAY;
     }
     mClassLoader = aClassLoader;
   }
 
-  /**
-   * @see org.apache.uima.resource.RelativePathResolver#getDataPath()
-   */
   @Override
+  @Deprecated
   public String getDataPath() {
+    String pathSepChar = System.getProperty("path.separator");
+    return stream(mDataPath).collect(joining(pathSepChar));
+  }
+
+  @Override
+  public String[] getDataPathElements() {
     return mDataPath;
   }
 
-  /**
-   * @see org.apache.uima.resource.RelativePathResolver#setDataPath(java.lang.String)
-   */
   @Override
+  public void setDataPathElements(File... aPaths) throws MalformedURLException {
+    if (aPaths == null) {
+      mDataPath = Constants.EMPTY_STRING_ARRAY;
+      mBaseUrls = Constants.EMPTY_URL_ARRAY;
+      return;
+    }
+
+    mDataPath = Arrays.stream(aPaths) //
+            .map(File::getPath) //
+            .toArray(String[]::new);
+    mBaseUrls = new URL[aPaths.length];
+    for (int i = 0; i < aPaths.length; i++) {
+      // Note, this URL can contain space characters if there were spaces in the
+      // datapath. This may not be ideal but we're keeping that behavior for
+      // backwards compatibility. Some components relied on this (e.g. by calling
+      // URL.getFile() and expecting it to be a valid file name).
+      mBaseUrls[i] = aPaths[i].toURL();
+    }
+  }
+
+  @Override
+  public void setDataPathElements(String... aPaths) throws MalformedURLException {
+    if (aPaths == null) {
+      mDataPath = null;
+      mBaseUrls = null;
+      return;
+    }
+
+    mDataPath = Arrays.copyOf(aPaths, aPaths.length);
+    mBaseUrls = new URL[aPaths.length];
+    for (int i = 0; i < aPaths.length; i++) {
+      // Note, this URL can contain space characters if there were spaces in the
+      // datapath. This may not be ideal but we're keeping that behavior for
+      // backwards compatibility. Some components relied on this (e.g. by calling
+      // URL.getFile() and expecting it to be a valid file name).
+      mBaseUrls[i] = new File(aPaths[i]).toURL();
+    }
+  }
+
+  @Override
+  @Deprecated
   public void setDataPath(String aPath) throws MalformedURLException {
     List<URL> urls = new ArrayList<>();
+    List<String> paths = new ArrayList<>();
 
     // tokenize based on path.separator system property
     String pathSepChar = System.getProperty("path.separator");
     StringTokenizer tokenizer = new StringTokenizer(aPath, pathSepChar);
     while (tokenizer.hasMoreTokens()) {
       String tok = tokenizer.nextToken();
+      paths.add(tok);
       URL url = new File(tok).toURL();
       urls.add(url);
       // Note, this URL can contain space characters if there were spaces in the
@@ -113,14 +159,10 @@ public class RelativePathResolver_impl implements RelativePathResolver {
       // backwards compatibility. Some components relied on this (e.g. by calling
       // URL.getFile() and expecting it to be a valid file name).
     }
-    mBaseUrls = new URL[urls.size()];
-    urls.toArray(mBaseUrls);
-    mDataPath = aPath;
+    mBaseUrls = urls.toArray(new URL[urls.size()]);
+    mDataPath = paths.toArray(new String[paths.size()]);
   }
 
-  /**
-   * @see org.apache.uima.resource.RelativePathResolver#resolveRelativePath(java.net.URL)
-   */
   @Override
   public URL resolveRelativePath(URL aRelativeUrl) {
     // try each base URL
@@ -129,8 +171,9 @@ public class RelativePathResolver_impl implements RelativePathResolver {
       try {
         URL absUrl = new URL(baseUrls[i], aRelativeUrl.toString());
         // if file exists here, return this URL
-        if (fileExistsAtUrl(absUrl))
+        if (fileExistsAtUrl(absUrl)) {
           return absUrl;
+        }
       } catch (MalformedURLException e) {
         // ignore and move on to next base URL
       }
diff --git a/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java b/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java
index 1e5d848..0146bbf 100644
--- a/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java
+++ b/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java
@@ -19,6 +19,7 @@
 
 package org.apache.uima.resource.impl;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
@@ -77,7 +78,7 @@ public class ResourceManager_impl implements ResourceManager {
    *   These are used to validate multiple declarations, and to get
    *   a resource to tie it to a binding
    */
- // @formatter:on
+  // @formatter:on
   static protected class ResourceRegistration { // make protected
                                                 // https://issues.apache.org/jira/browse/UIMA-2102
     /**
@@ -111,6 +112,7 @@ public class ResourceManager_impl implements ResourceManager {
   private static final URL[] emptyURLarray = new URL[0];
 
   private AtomicBoolean isDestroyed = new AtomicBoolean(false);
+
   /**
    * a monitor lock for synchronizing get/set of casManager ref
    */
@@ -365,6 +367,11 @@ public class ResourceManager_impl implements ResourceManager {
     return getRelativePathResolver().getDataPath();
   }
 
+  @Override
+  public String[] getDataPathElements() {
+    return getRelativePathResolver().getDataPathElements();
+  }
+
   /**
    * @see org.apache.uima.resource.ResourceManager#setDataPath(String)
    */
@@ -373,6 +380,16 @@ public class ResourceManager_impl implements ResourceManager {
     getRelativePathResolver().setDataPath(aPath);
   }
 
+  @Override
+  public void setDataPathElements(String... aElements) throws MalformedURLException {
+    getRelativePathResolver().setDataPathElements(aElements);
+  }
+
+  @Override
+  public void setDataPathElements(File... aElements) throws MalformedURLException {
+    getRelativePathResolver().setDataPathElements(aElements);
+  }
+
   /*
    * (non-Javadoc)
    * 
diff --git a/uimaj-core/src/test/java/org/apache/uima/resource/impl/RelativePathResolver_implTest.java b/uimaj-core/src/test/java/org/apache/uima/resource/impl/RelativePathResolver_implTest.java
index 67d4ef6..b5bf848 100644
--- a/uimaj-core/src/test/java/org/apache/uima/resource/impl/RelativePathResolver_implTest.java
+++ b/uimaj-core/src/test/java/org/apache/uima/resource/impl/RelativePathResolver_implTest.java
@@ -31,44 +31,35 @@ import org.junit.jupiter.api.Test;
 public class RelativePathResolver_implTest {
   @Test
   public void testSetDataPath() throws Exception {
-    try {
-      RelativePathResolver_impl resolver = new RelativePathResolver_impl();
-      // specify path by file names
-      String path = "/this/is/a/test" + System.getProperty("path.separator") + "/another/test";
-      resolver.setDataPath(path);
-      Assert.assertEquals(path, resolver.getDataPath());
-      URL[] urls = resolver.getBaseUrls();
-      Assert.assertEquals(2, urls.length);
-      Assert.assertEquals(new File("/this/is/a/test").toURL(), urls[0]);
-      Assert.assertEquals(new File("/another/test").toURL(), urls[1]);
-    } catch (Exception e) {
-      JUnitExtension.handleException(e);
-    }
+    RelativePathResolver_impl resolver = new RelativePathResolver_impl();
+    // specify path by file names
+    String path = "/this/is/a/test" + System.getProperty("path.separator") + "/another/test";
+    resolver.setDataPath(path);
+    Assert.assertEquals(path, resolver.getDataPath());
+    URL[] urls = resolver.getBaseUrls();
+    Assert.assertEquals(2, urls.length);
+    Assert.assertEquals(new File("/this/is/a/test").toURL(), urls[0]);
+    Assert.assertEquals(new File("/another/test").toURL(), urls[1]);
   }
 
   @Test
   public void testResolveRelativePath() throws Exception {
-    try {
-      RelativePathResolver_impl resolver = new RelativePathResolver_impl();
-      // file should not be found
-      URL absUrl = resolver.resolveRelativePath(new URL("file:test/relativePathTest.dat"));
-      Assert.assertNull(absUrl);
+    RelativePathResolver_impl resolver = new RelativePathResolver_impl();
+    // file should not be found
+    URL absUrl = resolver.resolveRelativePath(new URL("file:test/relativePathTest.dat"));
+    Assert.assertNull(absUrl);
 
-      // specify path
-      String path = JUnitExtension.getFile("ResourceTest/subdir").getAbsolutePath();
-      resolver.setDataPath(path);
+    // specify path
+    String path = JUnitExtension.getFile("ResourceTest/subdir").getAbsolutePath();
+    resolver.setDataPath(path);
 
-      // now file should be found
-      absUrl = resolver.resolveRelativePath(new URL("file:test/relativePathTest.dat"));
-      Assert.assertNotNull(absUrl);
+    // now file should be found
+    absUrl = resolver.resolveRelativePath(new URL("file:test/relativePathTest.dat"));
+    Assert.assertNotNull(absUrl);
 
-      // try resolving an absolute path even with no data path
-      resolver.setDataPath("");
-      URL newUrl = resolver.resolveRelativePath(absUrl);
-      assertEquals(absUrl, newUrl);
-    } catch (Exception e) {
-      JUnitExtension.handleException(e);
-    }
+    // try resolving an absolute path even with no data path
+    resolver.setDataPath("");
+    URL newUrl = resolver.resolveRelativePath(absUrl);
+    assertEquals(absUrl, newUrl);
   }
-
 }