/*
 * Copyright 2015 the original author or authors.
 *
 * 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 org.gradle.internal.classloader;

import org.gradle.api.Nullable;
import org.gradle.internal.UncheckedException;

import java.io.Closeable;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public abstract class ClassLoaderUtils {
    public static void tryClose(@Nullable ClassLoader classLoader) {
        if (classLoader instanceof Closeable) {
            try {
                ((Closeable) classLoader).close();
            } catch (IOException ignore) {
                // do nothing.
            }
        }
    }

    public static void disableUrlConnectionCaching() {
        // fix problems in updating jar files by disabling default caching of URL connections.
        // URLConnection default caching should be disabled since it causes jar file locking issues and JVM crashes in updating jar files.
        // Changes to jar files won't be noticed in all cases when caching is enabled.
        // sun.net.www.protocol.jar.JarURLConnection leaves the JarFile instance open if URLConnection caching is enabled.
        try {
            URL url = new URL("jar:file://valid_jar_url_syntax.jar!/");
            URLConnection urlConnection = url.openConnection();
            urlConnection.setDefaultUseCaches(false);
        } catch (MalformedURLException e) {
            throw UncheckedException.throwAsUncheckedException(e);
        } catch (IOException e) {
            throw UncheckedException.throwAsUncheckedException(e);
        }
    }
}

@@ -734,35 +734,67 @@
 on;%0A
-%0A
 import 
-java.io.Close
+org.gradle.internal.concurrent.CompositeStopp
 able;%0A
+%0A
 impo
@@ -1031,95 +1031,37 @@
     
-if (classLoader instanceof Closeable) %7B%0A            try %7B%0A                ((Close
+CompositeStoppable.stopp
 able
-) 
+(
 clas
@@ -1073,114 +1073,15 @@
 er).
-close();%0A            %7D catch (IOException ignore) %7B%0A                // do nothing.%0A            %7D%0A        %7D
+stop();
 %0A   

Use `CompositeStoppable` to take care of closing `ClassLoader` instances.