/*
* 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 org.gradle.internal.concurrent.CompositeStoppable;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public abstract class ClassLoaderUtils {
/**
* Returns the ClassLoader that contains the Java platform classes only. This is different to {@link ClassLoader#getSystemClassLoader()}, which includes the application classes in addition to the platform classes.
*/
public static ClassLoader getPlatformClassLoader() {
return ClassLoader.getSystemClassLoader().getParent();
}
public static void tryClose(@Nullable ClassLoader classLoader) {
CompositeStoppable.stoppable(classLoader).stop();
}
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);
}
}
}