package org.liquibase.grails;

import liquibase.FileOpener;

import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

public class GrailsFileOpener implements FileOpener {
    public InputStream getResourceAsStream(String file) throws IOException {
        return getClass().getClassLoader().getResourceAsStream("grails-app/migrations/"+file);
    }

    public Enumeration<URL> getResources(String packageName) throws IOException {
        return getClass().getClassLoader().getResources("grails-app/migrations/"+packageName);
    }

    public ClassLoader toClassLoader() {
        return getClass().getClassLoader();
    }
}

@@ -55,16 +55,57 @@
 Opener;%0D
+%0Aimport liquibase.ClassLoaderFileOpener;%0D
 %0A%0D%0Aimpor
@@ -216,141 +216,945 @@
 %0D%0A%0D%0A
-public class GrailsFileOpener implements FileOpener %7B%0D%0A    public InputStream getResourceAsStream(String file) throws IOException %7B%0D%0A
+import org.codehaus.groovy.grails.commons.ApplicationHolder;%0D%0A%0D%0Apublic class GrailsFileOpener implements FileOpener %7B%0D%0A    %0D%0A    private FileOpener fileOpener;%0D%0A%0D%0A    public GrailsFileOpener() %7B%0D%0A        if (ApplicationHolder.getApplication().isWarDeployed()) %7B%0D%0A            fileOpener = new ClassLoaderFileOpener();%0D%0A        %7D else %7B%0D%0A            fileOpener = new DevFileOpener();%0D%0A        %7D%0D%0A    %7D%0D%0A%0D%0A    public InputStream getResourceAsStream(String file) throws IOException %7B%0D%0A        return fileOpener.getResourceAsStream(file);%0D%0A    %7D%0D%0A%0D%0A    public Enumeration%3CURL%3E getResources(String packageName) throws IOException %7B%0D%0A        return fileOpener.getResources(packageName);%0D%0A    %7D%0D%0A%0D%0A    public ClassLoader toClassLoader() %7B%0D%0A        return fileOpener.toClassLoader();%0D%0A    %7D%0D%0A%0D%0A    private static class DevFileOpener implements FileOpener %7B%0D%0A%0D%0A        public InputStream getResourceAsStream(String file) throws IOException %7B%0D%0A    
     
@@ -1237,32 +1237,36 @@
 tions/%22+file);%0D%0A
+    
     %7D%0D%0A%0D%0A    pub
@@ -1254,32 +1254,36 @@
        %7D%0D%0A%0D%0A    
+ 
+   
 public Enumerati
@@ -1345,32 +1345,36 @@
 tion %7B%0D%0A        
+   
+ 
 return getClass(
@@ -1437,32 +1437,36 @@
 +packageName);%0D%0A
+    
     %7D%0D%0A%0D%0A    pub
@@ -1450,32 +1450,36 @@
 ;%0D%0A        %7D%0D%0A%0D%0A
+    
     public Class
@@ -1496,32 +1496,36 @@
 lassLoader() %7B%0D%0A
+    
         return g
@@ -1549,22 +1549,35 @@
 sLoader();%0D%0A
+        %7D%0D%0A%0D%0A
     %7D%0D%0A%7D%0D%0A

handle war-deployed grails apps