All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.xtext.util.internal.AlternateJdkLoader Maven / Gradle / Ivy

/**
 * Copyright (c) 2014, 2019 itemis AG (http://www.itemis.eu) and others.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package org.eclipse.xtext.util.internal;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.google.common.collect.Lists;

public class AlternateJdkLoader extends URLClassLoader {
	private final ConcurrentMap locks = new ConcurrentHashMap<>();

	public AlternateJdkLoader(Iterable files) {
		super(mapToURLs(files));
	}
	
	private static URL[] mapToURLs (Iterable files) {
		ArrayList fileList = Lists.newArrayList(files);
		URL[] result = new URL[fileList.size()];
		for (int i = 0; i < fileList.size(); i++) {
			try {
				result[i] = fileList.get(i).toURI().toURL();
			} catch (MalformedURLException e) {
				throw new RuntimeException(e);
			}
		}
		return result;
	}

	@Override
	protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
		synchronized (getClassLoadingLockJdk5(name)) {
			Class c = findLoadedClass(name);
			if (c == null) {
				c = findClass(name);
			}
			if (resolve) {
				resolveClass(c);
			}
			return c;
		}
	}

	@Override
	public URL getResource(String name) {
		return findResource(name);
	}

	@Override
	public Enumeration getResources(String name) throws IOException {
		return findResources(name);
	}

	private Object getClassLoadingLockJdk5(String className) {
		Object newLock = new Object();
		Object existingLock = locks.putIfAbsent(className, newLock);
		return existingLock != null ? existingLock : newLock;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy