keycloak-memoizeit

Details

diff --git a/testsuite/integration/src/main/java/org/keycloak/testutils/KeycloakServer.java b/testsuite/integration/src/main/java/org/keycloak/testutils/KeycloakServer.java
index afcedbe..6dd52b7 100644
--- a/testsuite/integration/src/main/java/org/keycloak/testutils/KeycloakServer.java
+++ b/testsuite/integration/src/main/java/org/keycloak/testutils/KeycloakServer.java
@@ -23,17 +23,12 @@ package org.keycloak.testutils;
 
 import io.undertow.Undertow;
 import io.undertow.Undertow.Builder;
-import io.undertow.server.handlers.resource.Resource;
-import io.undertow.server.handlers.resource.ResourceManager;
-import io.undertow.server.handlers.resource.URLResource;
+import io.undertow.server.handlers.resource.*;
 import io.undertow.servlet.Servlets;
 import io.undertow.servlet.api.DeploymentInfo;
 import io.undertow.servlet.api.FilterInfo;
 
-import java.io.ByteArrayOutputStream;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.net.URL;
 
 import javax.servlet.DispatcherType;
@@ -66,6 +61,7 @@ public class KeycloakServer {
     public static class KeycloakServerConfig {
         private String host = "localhost";
         private int port = 8081;
+        private String resourcesHome;
 
         public String getHost() {
             return host;
@@ -75,6 +71,10 @@ public class KeycloakServer {
             return port;
         }
 
+        public String getResourcesHome() {
+            return resourcesHome;
+        }
+
         public void setHost(String host) {
             this.host = host;
         }
@@ -82,6 +82,10 @@ public class KeycloakServer {
         public void setPort(int port) {
             this.port = port;
         }
+
+        public void setResourcesHome(String resourcesHome) {
+            this.resourcesHome = resourcesHome;
+        }
     }
 
     private static <T> T loadJson(InputStream is, Class<T> type) {
@@ -111,6 +115,19 @@ public class KeycloakServer {
             }
         }
 
+        if (System.getProperties().containsKey("resources")) {
+            String resources = System.getProperty("resources");
+            if (resources == null || resources.equals("")) {
+                for (String c : System.getProperty("java.class.path").split(File.pathSeparator)) {
+                    if (c.contains("keycloak" + File.separator + "testsuite" + File.separator + "integration")) {
+                        config.setResourcesHome(c.replaceFirst("testsuite.integration.*", ""));
+                    }
+                }
+            } else {
+                config.setResourcesHome(resources);
+            }
+        }
+
         final KeycloakServer keycloak = new KeycloakServer(config);
         keycloak.sysout = true;
         keycloak.start();
@@ -232,7 +249,7 @@ public class KeycloakServer {
         di.setClassLoader(getClass().getClassLoader());
         di.setContextPath("/auth-server");
         di.setDeploymentName("Keycloak");
-        di.setResourceManager(new KeycloakResourceManager());
+        di.setResourceManager(new KeycloakResourceManager(config.getResourcesHome()));
 
         FilterInfo filter = Servlets.filter("SessionFilter", KeycloakSessionServletFilter.class);
         di.addFilter(filter);
@@ -244,6 +261,10 @@ public class KeycloakServer {
 
         setupDefaultRealm();
 
+        if (config.getResourcesHome() != null) {
+            info("Loading resources from " + config.getResourcesHome());
+        }
+
         info("Started Keycloak (http://" + config.getHost() + ":" + config.getPort() + "/auth-server) in "
                 + (System.currentTimeMillis() - start) + " ms\n");
     }
@@ -264,17 +285,50 @@ public class KeycloakServer {
     }
 
     public static class KeycloakResourceManager implements ResourceManager {
+
+        private String resourcesHome;
+
+        public KeycloakResourceManager(String resourcesHome) {
+            this.resourcesHome = resourcesHome;
+        }
+
         @Override
         public Resource getResource(String path) throws IOException {
-            String realPath = "META-INF/resources" + path;
-
-            if (realPath.endsWith("/admin/")) {
-                realPath += "index.html";
+            if (resourcesHome == null) {
+                String realPath = "META-INF/resources" + path;
+
+                if (realPath.endsWith("/admin/")) {
+                    realPath += "index.html";
+                }
+
+                URL url = getClass().getClassLoader().getResource(realPath);
+
+                return new URLResource(url, url.openConnection(), path);
+            } else {
+                File file;
+                if (path.startsWith("/forms")) {
+                    file = file(resourcesHome, "forms", "src", "main", "resources", "META-INF", "resources", path.replace('/', File.separatorChar));
+                } else if (path.startsWith("/admin")) {
+                    file =  file(resourcesHome, "admin-ui", "src", "main", "resources", "META-INF", "resources", path.replace('/', File.separatorChar));
+                    if (!file.isFile()) {
+                        file =  file(resourcesHome, "admin-ui-styles", "src", "main", "resources", "META-INF", "resources", path.replace('/', File.separatorChar));
+                    }
+                } else {
+                    throw new IOException("Unknown resource " + path);
+                }
+                return new FileResource(file, new FileResourceManager(file.getParentFile(), 1), path);
             }
+        }
+    }
 
-            URL url = getClass().getClassLoader().getResource(realPath);
-            return new URLResource(url, url.openConnection(), path);
+    private static File file(String... path) {
+        StringBuilder s = new StringBuilder();
+        s.append(path[0]);
+        for (int i = 1; i < path.length; i++) {
+            s.append(File.separator);
+            s.append(path[i]);
         }
+        return new File(s.toString());
     }
 
 }