package org.scalaide.ew.launcher;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;

public class EquinoxWeavingLauncherConfigurationHelper {

  public static void updateConfiguration(File configFile) throws CoreException {
    FileInputStream in = null;
    Properties props = new Properties();
    try {
      in = new FileInputStream(configFile);
      props.load(in);
    } catch (IOException ex) {
      EquinoxWeavingLauncherPlugin.plugin().logError(ex);
      if (in != null) try {
        in.close();
      } catch (IOException ex2) {
        // Deliberately ignored
      }
      return;
    }
    
    // The is the shared install location
    File pluginDirFile = new File(Platform.getInstallLocation().getURL().getFile(), "plugins");
    File pluginDirOption = pluginDirFile.exists() ? pluginDirFile : null;

    // This is the per-user install location
    File pluginAuxDirFile = new File(new File(Platform.getConfigurationLocation().getURL().getFile()).getParentFile(), "plugins");
    File pluginAuxDirOption = pluginAuxDirFile.exists() ? pluginAuxDirFile : null;
    
    String weavingAspectJBundleLocation = scanPluginDirForUniqueJar(pluginDirOption, "org.eclipse.equinox.weaving.aspectj_.+\\.jar");
    if (weavingAspectJBundleLocation == null) {
      weavingAspectJBundleLocation = scanPluginDirForUniqueJar(pluginAuxDirOption, "org.eclipse.equinox.weaving.aspectj_.+\\.jar");
      if (weavingAspectJBundleLocation == null)
        return;
    }

    // See section "Installing JDT Weaving in a non-default location",
    //   http://wiki.eclipse.org/JDT_weaving_features
    props.put("osgi.framework.extensions", "org.eclipse.equinox.weaving.hook");

    String bundles = props.getProperty("osgi.bundles");
    props.put("osgi.bundles", "reference:file:"+weavingAspectJBundleLocation+"@2:start,"+bundles);
    
    props.put("aj.weaving.verbose", "true");
    props.put("org.aspectj.weaver.showWeaveInfo", "true");
    props.put("org.aspectj.osgi.verbose", "true");

    FileOutputStream out = null;
    try {
      out = new FileOutputStream(configFile);
      props.store(out, "Configuration File");
    } catch (IOException ex) {
      EquinoxWeavingLauncherPlugin.plugin().logError(ex);
    } finally {
      if (in != null) try {
        in.close();
      } catch (IOException ex) {
        // Deliberately ignored
      }
      if (out != null) try {
        out.close();
      } catch (IOException ex) {
        // Deliberately ignored
      }
    }
  }
  
  private static String scanPluginDirForUniqueJar(File pluginDirOption, final String filePattern) {
    if (pluginDirOption != null) {
      File[] files = pluginDirOption.listFiles(new FileFilter() {
          public boolean accept(File file) { return file.getName().matches(filePattern) && file.isFile(); }
      });
      if (files.length > 0)
        return files[0].getAbsolutePath();
    }
    return null;
  }
}

@@ -53,35 +53,8 @@
 le;%0A
-import java.io.FileFilter;%0A
 impo
@@ -73,32 +73,32 @@
 ileInputStream;%0A
+
 import java.io.F
@@ -215,24 +215,69 @@
 eException;%0A
+import org.eclipse.core.runtime.FileLocator;%0A
 import org.e
@@ -305,16 +305,50 @@
 latform;
+%0Aimport org.osgi.framework.Bundle;
 %0A%0Apublic
@@ -872,746 +872,72 @@
     
-// The is the shared install location%0A    File pluginDirFile = new File(Platform.getInstallLocation().getURL().getFile(), %22plugins%22);%0A    File pluginDirOption = pluginDirFile.exists() ? pluginDirFile : null;%0A%0A    // This is the per-user install location%0A    File pluginAuxDirFile = new File(new File(Platform.getConfigurationLocation().getURL().getFile()).getParentFile(), %22plugins%22);%0A    File pluginAuxDirOption = pluginAuxDirFile.exists() ? pluginAuxDirFile : null;%0A    %0A    String weavingAspectJBundleLocation = scanPluginDirForUniqueJar(pluginDirOption, %22org.eclipse.equinox.weaving.aspectj_.+%5C%5C.jar%22);%0A    if (weavingAspectJBundleLocation == null) %7B%0A      weavingAspectJBundleLocation = scanPluginDirForUniqueJar(pluginAuxDirOption, 
+String weavingAspectJBundleLocation = getPlatformBundleLocation(
 %22org
@@ -972,25 +972,16 @@
 ectj
-_.+%5C%5C.jar
 %22);%0A    
   if
@@ -972,26 +972,24 @@
 ectj%22);%0A    
-  
 if (weavingA
@@ -1024,31 +1024,23 @@
 )%0A      
-  
 return;
-%0A    %7D
 %0A%0A    //
@@ -2080,300 +2080,271 @@
 ing 
-scanPluginDirForUniqueJar(File pluginDirOption, final String filePattern) %7B%0A    if (pluginDirOption != null) %7B%0A      File%5B%5D files = pluginDirOption.listFiles(new FileFilter() %7B%0A          public boolean accept(File file) %7B return file.getName().matches(filePattern) && file.isFile
+getPlatformBundleLocation(String symbolicName) %7B%0A    Bundle bundle = Platform.getBundle(symbolicName);%0A    if (bundle != null) %7B%0A      try %7B%0A        File bundleLocation = FileLocator.getBundleFile(bundle);%0A        return bundleLocation.getCanonicalPath
 ();
- %7D
 %0A      %7D
 );%0A 
@@ -2343,81 +2343,73 @@
    %7D
-);%0A      if (files.length %3E 0)%0A        return files%5B0%5D.getAbsolutePath();
+ catch (IOException ex) %7B%0A        // Deliberately ignored%0A      %7D
 %0A   

Use org.eclipse.core.runtime.platform to locate bundles