ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seanfi...@apache.org
Subject svn commit: r1666555 - /ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/resource/FileLocator.java
Date Fri, 13 Mar 2015 19:37:07 GMT
Author: seanfinan
Date: Fri Mar 13 19:37:06 2015
New Revision: 1666555

URL: http://svn.apache.org/r1666555
Log:
CTAKES-345 Added functionality to utilize $CTAKES_HOME and the current working directory when
searching for files

Modified:
    ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/resource/FileLocator.java

Modified: ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/resource/FileLocator.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/resource/FileLocator.java?rev=1666555&r1=1666554&r2=1666555&view=diff
==============================================================================
--- ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/resource/FileLocator.java
(original)
+++ ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/resource/FileLocator.java
Fri Mar 13 19:37:06 2015
@@ -18,10 +18,9 @@
  */
 package org.apache.ctakes.core.resource;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
+import org.apache.log4j.Logger;
+
+import java.io.*;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
@@ -31,8 +30,19 @@ import java.net.URL;
  * 
  * @author Mayo Clinic
  */
-public class FileLocator
-{
+final public class FileLocator {
+
+   static private final Logger LOGGER = Logger.getLogger( "FileLocator" );
+
+   /**
+    * $CTAKES_HOME is an environment variable that may be set to indicate
+    * the absolute directory path of the ctakes installation
+    */
+   static private final String CTAKES_HOME = "CTAKES_HOME";
+
+   private FileLocator() {
+   }
+
     public static InputStream getAsStream(String location) throws FileNotFoundException
     {
         try
@@ -53,35 +63,122 @@ public class FileLocator
     }
 	
     /**
-     * @Deprecated  As of release 3.1, replaced by {@link #getAsStream()}
-     */    
-    public static File locateFile(String location)
-            throws FileNotFoundException
-    {
-        try
-        {
-        	//System.out.println("cwd=" + new File(".").getAbsolutePath());
-            return locateOnClasspath(location);
-        }
-        catch (Exception e)
-        {
-            return locateExplicitly(location);
+     *  @deprecated As of release 3.1, replaced by {@link #getAsStream(String)} and {@link
#getFullPath(String)}
+     */
+    @Deprecated
+    public static File locateFile( final String location ) throws FileNotFoundException {
+       try {
+          return locateOnClasspath( location );
+       } catch ( Exception e ) {
+          return locateExplicitly( location );
         }
     }
 
-    private static File locateOnClasspath(String cpLocation)
-            throws FileNotFoundException, URISyntaxException
-    {
-        ClassLoader cl = FileLocator.class.getClassLoader();
-        URL indexUrl = cl.getResource(cpLocation);
-        if (indexUrl == null)
-        {
-            throw new FileNotFoundException(cpLocation);
-        }
+   /**
+    * Logs a debug message before returning the absolute path of a file derived from some
relative path
+    *
+    * @param relativePath relative path of some file
+    * @param file         the actual file addressed by relative path
+    * @param locationText description of where file exists relative to relativePath
+    * @return the canonical path of file or the absolute path of file if the canonical cannot
be made
+    */
+   static private String createDiscoveredPath( final String relativePath, final File file,
final String locationText ) {
+      try {
+         LOGGER.debug( relativePath + " discovered " + locationText + " as: " + file.getCanonicalPath()
);
+         return file.getCanonicalPath();
+      } catch ( IOException ioE ) {
+         LOGGER.debug( relativePath + " discovered " + locationText + " as: " + file.getPath()
);
+         return file.getPath();
+      }
+   }
+
+   /**
+    * Attempts to discover the real location of a file pointed to by relativePath.
+    * The search will be performed in the following order:
+    * <p>
+    * 1. By checking to see if the provided relative path is actually an absolute path
+    * 2. By checking within the ClassPath
+    * 3. By checking directly under the current working directory
+    * 4. By checking under $CTAKES_HOME
+    * 5. By traversing above the current working directory.  Useful when running under a
module directory in an IDE
+    * Example:  cwd = /usr/bin/ctakes/ctakes-module , relativePath = ctakes-other-module/more/file.ext
+    * The directory above cwd /usr/bin/ctakes will be checked for containment of the relative
path
+    * If /usr/bin/ctakes/ctakes-other-module/more/file.txt exists then that is returned
+    * 6. By traversing above the current working directory and under a subdirectory ctakes/
+    * Example: cwd = /usr/bin/my_custom_ctakes/my_ctakes-module , relativePath = ctakes-other-module/more/file.ext
+    * The directory above cwd /usr/bin will be checked for containment of ctakes/ plus the
relative path
+    * If /usr/bin/ctakes/ctakes-other-module/more/file.txt exists then that is returned
+    * </p>
+    *
+    * @param relativePath some relative path to a file
+    * @return the canonical path of the file or the absolute path of the file if the canonical
cannot be made
+    * @throws FileNotFoundException if the file cannot be found
+    */
+   static public String getFullPath( final String relativePath ) throws FileNotFoundException
{
+      File file = new File( relativePath );
+      if ( file.exists() ) {
+         return createDiscoveredPath( relativePath, file, "without adjustment" );
+      }
+      // check in the classpath
+      try {
+         file = locateOnClasspath( relativePath );
+         if ( file.exists() ) {
+            return createDiscoveredPath( relativePath, file, "under Classpath" );
+         }
+      } catch ( FileNotFoundException | URISyntaxException multiE ) {
+         // the locateOnClasspath method throws exceptions if the file isn't found.  Ignore
and continue
+      }
+      // check for relative directly under current working directory
+      final String cwd = System.getProperty( "user.dir" );
+      file = new File( cwd, relativePath );
+      if ( file.exists() ) {
+         return createDiscoveredPath( relativePath, file, "under Working Directory" );
+      }
+      // Check under the $CTAKES_HOME location  Do this before messing with relative path
traversal
+      final String cTakesHome = System.getenv( CTAKES_HOME );
+      if ( cTakesHome != null && !cTakesHome.isEmpty() ) {
+         file = new File( cTakesHome, relativePath );
+         if ( file.exists() ) {
+            return createDiscoveredPath( relativePath, file, "under $CTAKES_HOME" );
+         }
+      }
+      // Users running projects out of an ide may have the module directory as cwd
+      // OR in a personal project directory parallel to that of the ctakes installation
+      File cwdDerived = new File( cwd );
+      while ( cwdDerived.getParentFile() != null ) {
+         cwdDerived = cwdDerived.getParentFile();
+         file = new File( cwdDerived, relativePath );
+         if ( file.exists() ) {
+            return createDiscoveredPath( relativePath, file, "above Working Directory" );
+         }
+         file = new File( cwdDerived, "ctakes/" + relativePath );
+         if ( file.exists() ) {
+            return createDiscoveredPath( relativePath, file, "above Working Directory /ctakes"
);
+         }
+      }
+      LOGGER.error( "Could not find " + relativePath + " as absolute or in \n" + cwd
+                    + " or in any parent thereof or in $CTAKES_HOME \n" + cTakesHome );
+      throw new FileNotFoundException( "No File exists at " + relativePath );
+   }
+
 
-        URI indexUri = new URI(indexUrl.toExternalForm());
-        File f = new File(indexUri);
-        return f;
+   /**
+    * Check the java classpath for the presence of a file pointed to by relativePath
+    *
+    * @param relativePath some relative path to a file
+    * @return a file in the classpath pointed to by relativePath - if found
+    * @throws FileNotFoundException if the file is not found in the classpath
+    * @throws URISyntaxException    if the discovered file cannot be converted into a URI
+    */
+   private static File locateOnClasspath( final String relativePath )
+         throws FileNotFoundException, URISyntaxException {
+      final ClassLoader classLoader = FileLocator.class.getClassLoader();
+      final URL indexUrl = classLoader.getResource( relativePath );
+      if ( indexUrl == null ) {
+         throw new FileNotFoundException( relativePath );
+      }
+      final URI indexUri = new URI( indexUrl.toExternalForm() );
+      return new File( indexUri );
     }
 
     private static File locateExplicitly(String explicitLocation)



Mime
View raw message