package org.mule.tooling.esb.launcher.configuration.archive;


import com.intellij.execution.ExecutionException;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.CompilerModuleExtension;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FilenameFilter;

public class MuleAppMavenHandler implements MuleAppHandler
{

    @NotNull
    @Override
    public File getMuleApp(final Module module) throws ExecutionException
    {
        final CompilerModuleExtension compilerModuleExtension = CompilerModuleExtension.getInstance(module);
        final VirtualFile moduleFile = module.getModuleFile();
        if (compilerModuleExtension != null && moduleFile != null)
        {
            //Refresh file system
            module.getModuleFile().refresh(false, true);
            VirtualFile compilerOutputPath = compilerModuleExtension.getCompilerOutputPath();
            if (compilerOutputPath == null)
            {
                compilerOutputPath = moduleFile.findChild("target");
            }
            if (compilerOutputPath != null && compilerOutputPath.getParent() != null)
            {
                final File outputDir = new File(compilerOutputPath.getParent().getCanonicalPath());
                File applicationZip = null;
                final File[] zips = outputDir.listFiles(new FilenameFilter()
                {
                    @Override
                    public boolean accept(File dir, String name)
                    {
                        return name.endsWith("zip");
                    }
                });
                if (zips.length > 0)
                {
                    applicationZip = zips[0];
                }
                if (applicationZip == null)
                {
                    throw new ExecutionException("Unable to create application. Application was not found at " + compilerOutputPath.getParent().getCanonicalPath());
                }
                return applicationZip;
            }
            else
            {
                throw new ExecutionException("Unable to create application. No output path was found.");
            }
        }
        else
        {
            throw new ExecutionException("Unable to create application. No module found.");
        }
    }


}

@@ -514,24 +514,89 @@
 ption%0A    %7B%0A
+        String outputPath = null;%0A        final File outputDir;%0A%0A
         fina
@@ -1013,24 +1013,25 @@
 tputPath();%0A
+%0A
             
@@ -1053,36 +1053,66 @@
 putPath 
-=
+!
 = null
-)%0A           
+ && compilerOutputPath.getParent() != null)
  %7B%0A     
@@ -1121,16 +1121,29 @@
         
+ outputPath =
  compile
@@ -1157,40 +1157,88 @@
 Path
- = moduleFile.findChild(%22target%22
+.getParent().getCanonicalPath();%0A                outputDir = new File(outputPath
 );%0A 
@@ -1241,32 +1241,39 @@
 );%0A            %7D
+ else %7B
 %0A            if 
@@ -1265,37 +1265,29 @@
             
-if (compilerO
+    o
 utputPath !=
@@ -1288,72 +1288,92 @@
 ath 
-!
 = 
-null && compilerOutputPath.getParent() != null)%0A            %7B
+module.getModuleFile().getParent().getCanonicalPath() + File.separator + %22target%22;
 %0A   
@@ -1381,35 +1381,24 @@
             
-final File 
 outputDir = 
@@ -1406,34 +1406,38 @@
 ew File(
-compilerOutputPath
+module.getModuleFile()
 .getPare
@@ -1451,39 +1451,60 @@
 tCanonicalPath()
-);%0A    
+, %22target%22);%0A            %7D%0A%0A
             File
@@ -1523,28 +1523,24 @@
 Zip = null;%0A
-    
             
@@ -1608,34 +1608,26 @@
             
-  
-  %7B%0A    
+%7B%0A
             
@@ -1640,20 +1640,16 @@
 verride%0A
-    
         
@@ -1701,20 +1701,16 @@
 g name)%0A
-    
         
@@ -1735,28 +1735,24 @@
             
-    
 return name.
@@ -1780,34 +1780,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -1810,28 +1810,24 @@
             
-  
-  
 if (zips.len
@@ -1839,36 +1839,32 @@
  0)%0A            
-    
 %7B%0A              
@@ -1865,20 +1865,16 @@
         
- 
-   
 applicat
@@ -1891,20 +1891,16 @@
 ips%5B0%5D;%0A
-    
         
@@ -1913,28 +1913,24 @@
             
-    
 if (applicat
@@ -1953,34 +1953,26 @@
             
-    %7B%0A    
+%7B%0A
             
@@ -2072,64 +2072,21 @@
 %22 + 
-compilerOutputPath.getParent().getCanonicalPath());%0A    
+outputPath);%0A
     
@@ -2103,28 +2103,24 @@
             
- 
-   
 return appli
@@ -2130,16 +2130,18 @@
 ionZip;%0A
+//
         
@@ -2138,32 +2138,34 @@
 //            %7D%0A
+//
             else
@@ -2157,32 +2157,34 @@
            else%0A
+//
             %7B%0A  
@@ -2173,32 +2173,34 @@
 //            %7B%0A
+//
                 
@@ -2280,32 +2280,34 @@
 h was found.%22);%0A
+//
             %7D%0A  

Fix for issue #72