
org.fabric3.plugin.util.ClassLoaderHelper Maven / Gradle / Ivy
The newest version!
/*
* Fabric3
* Copyright (c) 2009-2015 Metaform Systems
*
* 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.fabric3.plugin.util;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.eclipse.aether.artifact.Artifact;
import org.fabric3.api.host.classloader.DelegatingResourceClassLoader;
/**
* Creates runtime classloaders.
*/
public class ClassLoaderHelper {
/**
* Creates the classloader to boot the runtime.
*
* @param parent the parent classloader
* @param artifacts the set of artifacts to include on the boot classpath
* @return the boot classloader
*/
public static ClassLoader createBootClassLoader(ClassLoader parent, Set artifacts) {
URL[] urls = new URL[artifacts.size()];
int i = 0;
for (Artifact artifact : artifacts) {
File file = artifact.getFile();
assert file != null;
try {
urls[i++] = file.toURI().toURL();
} catch (MalformedURLException e) {
// toURI should have made this valid
throw new AssertionError(e);
}
}
return new DelegatingResourceClassLoader(urls, parent);
}
/**
* Creates the host classloader based on the given set of artifacts.
*
* @param parent the parent classloader
* @param hostArtifacts the artifacts
* @return the host classloader
*/
public static ClassLoader createHostClassLoader(ClassLoader parent, Set hostArtifacts) {
List urls = new ArrayList<>(hostArtifacts.size());
for (Artifact artifact : hostArtifacts) {
try {
File pathElement = artifact.getFile();
URL url = pathElement.toURI().toURL();
urls.add(url);
} catch (MalformedURLException e) {
// toURI should have encoded the URL
throw new AssertionError(e);
}
}
return new DelegatingResourceClassLoader(urls.toArray(new URL[urls.size()]), parent);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy