You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2014/03/24 17:18:42 UTC

svn commit: r1580900 - in /jena/branches/jena-fuseki-new-ui/src/main: java/org/apache/jena/fuseki/server/ShiroEnvironmentLoader.java webapp/WEB-INF/web.xml

Author: andy
Date: Mon Mar 24 16:18:42 2014
New Revision: 1580900

URL: http://svn.apache.org/r1580900
Log:
Look for Shiro config file in a few places.

Modified:
    jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/server/ShiroEnvironmentLoader.java
    jena/branches/jena-fuseki-new-ui/src/main/webapp/WEB-INF/web.xml

Modified: jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/server/ShiroEnvironmentLoader.java
URL: http://svn.apache.org/viewvc/jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/server/ShiroEnvironmentLoader.java?rev=1580900&r1=1580899&r2=1580900&view=diff
==============================================================================
--- jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/server/ShiroEnvironmentLoader.java (original)
+++ jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/server/ShiroEnvironmentLoader.java Mon Mar 24 16:18:42 2014
@@ -20,7 +20,8 @@ package org.apache.jena.fuseki.server;
 
 import java.io.IOException ;
 import java.io.InputStream ;
-import java.net.URL ;
+import java.nio.file.Path ;
+import java.nio.file.Paths ;
 
 import javax.servlet.ServletContext ;
 import javax.servlet.ServletContextEvent ;
@@ -31,9 +32,10 @@ import org.apache.shiro.io.ResourceUtils
 import org.apache.shiro.web.env.EnvironmentLoader ;
 import org.apache.shiro.web.env.ResourceBasedWebEnvironment ;
 import org.apache.shiro.web.env.WebEnvironment ;
-import org.apache.shiro.web.util.WebUtils ;
 import org.slf4j.Logger ;
 
+import com.hp.hpl.jena.util.FileUtils ;
+
 /** A place to perform Fuseki-specific initialization of Apache Shiro.
  *  This means finding shiro.ini in multiple possible places, based on
  *  different deployment setups.
@@ -69,39 +71,88 @@ public class ShiroEnvironmentLoader exte
         if ( environment instanceof ResourceBasedWebEnvironment ) {
             ResourceBasedWebEnvironment env = (ResourceBasedWebEnvironment)environment ;
             String[] locations = env.getConfigLocations() ;
-            if ( locations.length > 1 ) {
-                for ( String loc : locations ) {
-                    if ( resourceExists(loc) ) {
-                        locations = new String[] {loc} ;
-                        env.setConfigLocations(locations);
-                        return ;
-                    }
-                }
-            }
+            String loc = huntForShiroIni(locations) ;
+            if (loc != null )
+                locations = new String[] {loc} ;
+            env.setConfigLocations(locations);
         }
     }
-
-    /** 
-     * Test whether a name identified an existing resource
-     * @param resource    A String in Shiro-resource name format (e.g. URL scheme names) 
-     * @return True/false as to whether the resource can be found or not. 
-     */
     
-    private boolean resourceExists(String resource) {
-        try {
-            // See IniWebEnvironment.convertPathToIni
-            if (!ResourceUtils.hasResourcePrefix(resource)) {
-                //Sort out "path" and open as a webapp resource.
-                resource = WebUtils.normalize(resource);
-                URL url = servletContext.getResource(resource) ;
-                return ( url == null ) ;
-            } else {
-                // Treat as a plain name. 
-                InputStream is = ResourceUtils.getInputStreamForPath(resource);
+    private static final String FILE = "file" ;
+    
+    //Siro needs a URL, or a resource name.
+    // TODO Log choice.
+    // TODO check file: works.
+    
+    /** Look for a Shiro ini file, or return null */
+    private static String huntForShiroIni(String[] locations) {
+        for ( String loc : locations ) {
+            // If file:, look for that file.
+            // If a relative name without scheme, look in FUSEKI_BASE, FUSEKI_HOME, webapp. 
+            String scheme = FileUtils.getScheme(loc) ;
+            
+            // Covers C:\\ as a "scheme name"
+            if ( scheme != null ) {
+                if ( scheme.equalsIgnoreCase(FILE)) {
+                    // Test file: for exists
+                    Path p = Paths.get(loc.substring(FILE.length()+1)) ;
+                    if ( ! p.toFile().exists() )
+                        continue ;
+                    // Fall through.
+                }
+                // Can't test - try 
+                return loc ;
+            }
+            // No scheme .
+            Path p = Paths.get(loc) ;
+            String fn = resolve(FusekiServer.FUSEKI_BASE, p) ;
+            if ( fn != null )
+                return "file:/"+fn ;
+            fn = resolve(FusekiServer.FUSEKI_HOME, p) ;
+            if ( fn != null )
+                return "file:/"+fn ;
+            
+            // Try in webapp.
+            
+            try {
+                InputStream is = ResourceUtils.getInputStreamForPath(loc);
                 boolean exists = (is != null ) ;
                 is.close() ;
-                return exists ;
-            }
-        } catch (IOException e) { return false ; }
+                return loc ;
+            } catch (IOException e) { }
+        }
+        return null ;
     }
+    
+    /** Directory + name -> filename if it exists */ 
+    private static String resolve(Path dir, Path file) {
+        Path p = dir.resolve(file) ;
+        if ( p.toFile().exists() )
+            return p.toString() ;
+        return null ;
+    }
+
+//    /** 
+//     * Test whether a name identified an existing resource
+//     * @param resource    A String in Shiro-resource name format (e.g. URL scheme names) 
+//     * @return True/false as to whether the resource can be found or not. 
+//     */
+//    
+//    private boolean resourceExists(String resource) {
+//        try {
+//            // See IniWebEnvironment.convertPathToIni
+//            if (!ResourceUtils.hasResourcePrefix(resource)) {
+//                //Sort out "path" and open as a webapp resource.
+//                resource = WebUtils.normalize(resource);
+//                URL url = servletContext.getResource(resource) ;
+//                return ( url == null ) ;
+//            } else {
+//                // Treat as a plain name. 
+//                InputStream is = ResourceUtils.getInputStreamForPath(resource);
+//                boolean exists = (is != null ) ;
+//                is.close() ;
+//                return exists ;
+//            }
+//        } catch (IOException e) { return false ; }
+//    }
 }

Modified: jena/branches/jena-fuseki-new-ui/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/jena/branches/jena-fuseki-new-ui/src/main/webapp/WEB-INF/web.xml?rev=1580900&r1=1580899&r2=1580900&view=diff
==============================================================================
--- jena/branches/jena-fuseki-new-ui/src/main/webapp/WEB-INF/web.xml (original)
+++ jena/branches/jena-fuseki-new-ui/src/main/webapp/WEB-INF/web.xml Mon Mar 24 16:18:42 2014
@@ -31,8 +31,8 @@
 
   <context-param>
     <param-name>shiroConfigLocations</param-name>
-    <!-- Try : cwd, FUSEKI_BASE, FUSEKI_HOME, war resource -->
-    <param-value>file:shiro.ini,shiro.ini</param-value>
+    <!-- Try : FUSEKI_BASE, FUSEKI_HOME, war resource -->
+    <param-value>shiro.ini</param-value>
   </context-param>
 
   <!-- Apache Jena Fuseki setup -->