/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collection;
import java.util.LinkedHashSet;

/**
 * @author max
 */
public class ExternalClasspathClassLoader extends URLClassLoader {
  static {
    registerAsParallelCapable();
  }

  private ExternalClasspathClassLoader(URL[] urls) {
    super(urls, Thread.currentThread().getContextClassLoader());
  }

  private static String[] parseUrls(String classpathFilePath) {
    Collection<String> roots = new LinkedHashSet<>();
    File file = new File(classpathFilePath);
    try {
      final BufferedReader reader = new BufferedReader(new FileReader(file));
      try {
        while (reader.ready()) {
          roots.add(reader.readLine());
        }
      }
      finally {
        reader.close();
      }

      //noinspection SSBasedInspection
      return roots.toArray(new String[roots.size()]);
    }
    catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  public static String[] getRoots() {
    final String classPathFilePath = System.getProperty("classpath.file");
    return classPathFilePath != null ? parseUrls(classPathFilePath) : null;
  }

  public static String[] getExcludeRoots() {
    try {
      final String classPathFilePath = System.getProperty("exclude.tests.roots.file");
      return classPathFilePath != null ? parseUrls(classPathFilePath) : null;
    }
    catch (Exception e) {
      //noinspection SSBasedInspection
      return new String[0];
    }
  }

  public static void install() {
    try {
      URL[] urls = parseUrls();
      if (urls != null) {
        Thread.currentThread().setContextClassLoader(new ExternalClasspathClassLoader(urls));
      }
    }
    catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  private static URL[] parseUrls() {
    try {
      String[] roots = getRoots();
      if (roots == null) return null;

      URL[] urls = new URL[roots.length];
      for (int i = 0; i < urls.length; i++) {
        urls[i] = new File(roots[i]).toURI().toURL();
      }
      return urls;
    }
    catch (MalformedURLException e) {
      throw new RuntimeException(e);
    }
  }
}

@@ -953,203 +953,9 @@
 der 
-extends URLClassLoader %7B%0A  static %7B%0A    registerAsParallelCapable();%0A  %7D%0A%0A  private ExternalClasspathClassLoader(URL%5B%5D urls) %7B%0A    super(urls, Thread.currentThread().getContextClassLoader());%0A  %7D
+%7B
 %0A%0A  
@@ -2162,16 +2162,68 @@
 %0A       
+ URLClassLoader auxLoader = new URLClassLoader(urls,
  Thread.
@@ -2238,17 +2238,17 @@
 hread().
-s
+g
 etContex
@@ -2264,131 +2264,936 @@
 der(
-new ExternalClasspathClassLoader(urls));%0A      %7D%0A    %7D%0A    catch (Exception e) %7B%0A      throw new RuntimeException(e);%0A    %7D
+));%0A        Thread.currentThread().setContextClassLoader(auxLoader);%0A        Thread.currentThread().setContextClassLoader(loadOptimizedLoader(urls, auxLoader));%0A      %7D%0A    %7D%0A    catch (Exception e) %7B%0A      throw new RuntimeException(e);%0A    %7D%0A  %7D%0A%0A  private static ClassLoader loadOptimizedLoader(Object urls, URLClassLoader auxLoader) throws Exception %7B%0A    Object builder = auxLoader.loadClass(%22com.intellij.util.lang.UrlClassLoader%22).getMethod(%22build%22).invoke(null);%0A    builder.getClass().getMethod(%22urls%22, URL%5B%5D.class).invoke(builder, urls);%0A    builder.getClass().getMethod(%22useCache%22).invoke(builder);%0A    builder.getClass().getMethod(%22allowLock%22).invoke(builder);%0A    builder.getClass().getMethod(%22allowBootstrapResources%22).invoke(builder);%0A    builder.getClass().getMethod(%22parent%22, ClassLoader.class).invoke(builder, auxLoader.getParent());%0A    return (ClassLoader)builder.getClass().getMethod(%22get%22).invoke(builder);
 %0A  %7D

use UrlClassLoader for speedier teamcity test initialization