org.springframework.boot.loader.LaunchedURLClassLoader Java Examples

The following examples show how to use org.springframework.boot.loader.LaunchedURLClassLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ArquillianSyncBootJarLauncher.java    From camel-spring-boot with Apache License 2.0 7 votes vote down vote up
@Override
protected ClassLoader createClassLoader(URL[] urls) throws Exception {
    // The spring classloader should not be built on top of the current classloader, it should just share the test classes if available
    List<URL> parentUrls = Arrays.asList(urlsFromClassLoader(this.getClassLoader()));
    List<URL> additionalURLs = parentUrls.stream().filter(u -> u.toString().startsWith("file") && !u.toString().endsWith(".jar")).collect(Collectors.toList());

    ArrayList<URL> newURLs = new ArrayList(Arrays.asList(urls));
    newURLs.addAll(additionalURLs);

    ClassLoader appClassLoader = null;
    // Until https://github.com/spring-projects/spring-boot/issues/12832 is resolved
    if (getJavaMajorVersion() >= 9) {
        // use the Platform classloader to resolve classes on the module path
        appClassLoader = getClass().getClassLoader().getParent();
    }
    LaunchedURLClassLoader launchedURLClassLoader = new LaunchedURLClassLoader(newURLs.toArray(new URL[0]), appClassLoader);
    return launchedURLClassLoader;
}
 
Example #2
Source File: UndertowStartFilter.java    From spring-cloud-formula with Apache License 2.0 5 votes vote down vote up
@Override
public boolean process(ApplicationExchange application, LauncherFilterChain filterChain) {

    int port = application.getServerPort();

    ClassLoader classLoader = new LaunchedURLClassLoader(application.getClassPathUrls(), deduceParentClassLoader());

    DeploymentInfo servletBuilder = Servlets.deployment()
                   .setClassLoader(classLoader)
                   .setContextPath(application.getContextPath())
                   .setDeploymentName(application.getApplication().getPath());

    DeploymentManager manager = Servlets.defaultContainer().addDeployment(servletBuilder);
 
    manager.deploy();

    Undertow server = null;
    try {
        server = Undertow.builder()
                .addHttpListener(port, "localhost")
                .setHandler(manager.start()).build();
        server.start();
    } catch (ServletException e) {
        e.printStackTrace();
    }

    return false;
}
 
Example #3
Source File: SpringBootRuleITest.java    From java-agent with Apache License 2.0 5 votes vote down vote up
@Test
public void testFindOpenTracingAgentRules() throws Exception {
    File f = new File(System.getProperty("rules.path"));
    URL[] urls = new URL[1];
    urls[0] = f.toURI().toURL();

    try (LaunchedURLClassLoader loader = new LaunchedURLClassLoader(urls,
            ClassLoader.getSystemClassLoader())) {

        assertNotNull(loader.getResource("test.rule"));
    }
}
 
Example #4
Source File: ScriptUtil.java    From util4j with Apache License 2.0 5 votes vote down vote up
public Class<?> buildClass(String className,String script)throws Exception{
    URL[] urls=new URL[paths.size()];
    for (int i = 0; i < paths.size(); i++) {
        String path=paths.get(i);
        URL url=new File(path).toURI().toURL();
        urls[i]=url;
    }
    LaunchedURLClassLoader classLoader = new LaunchedURLClassLoader(urls, Thread.currentThread().getContextClassLoader());
    DynamicCompiler dynamicCompiler = new DynamicCompiler(classLoader);
    dynamicCompiler.addSource(className,script);
    Map<String,Class<?>> map=dynamicCompiler.build();
    Class<?> clazz=map.get(className);
    return clazz;
}
 
Example #5
Source File: ScriptUtil.java    From util4j with Apache License 2.0 5 votes vote down vote up
public byte[] buildClassBytes(String className,String script)throws Exception{
    URL[] urls=new URL[paths.size()];
    for (int i = 0; i < paths.size(); i++) {
        String path=paths.get(i);
        URL url=new File(path).toURI().toURL();
        urls[i]=url;
    }
    LaunchedURLClassLoader classLoader = new LaunchedURLClassLoader(urls, Thread.currentThread().getContextClassLoader());
    DynamicCompiler dynamicCompiler = new DynamicCompiler(classLoader);
    dynamicCompiler.addSource(className,script);
    Map<String, byte[]> map = dynamicCompiler.buildByteCodes();
    byte[] clazz=map.get(className);
    return clazz;
}
 
Example #6
Source File: FunctionArchiveDeployer.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Override
protected ClassLoader createClassLoader(URL[] urls) throws Exception {
	String classAsPath = DeployerContextUtils.class.getName().replace('.', '/') + ".class";
	byte[] deployerContextUtilsBytes = StreamUtils
			.copyToByteArray(DeployerContextUtils.class.getClassLoader().getResourceAsStream(classAsPath));
	/*
	 * While LaunchedURLClassLoader is completely disconnected with the current
	 * class loader, this will ensure that certain classes (e.g., org.reactivestreams.* see #shouldLoadViaDeployerLoader() )
	 * are shared across two class loaders.
	 */
	final ClassLoader deployerClassLoader = getClass().getClassLoader();
	this.archiveLoader = new LaunchedURLClassLoader(urls, deployerClassLoader.getParent()) {
		@Override
		public Class<?> loadClass(String name) throws ClassNotFoundException {
			Class<?> clazz = null;
			if (shouldLoadViaDeployerLoader(name)) {
				clazz = deployerClassLoader.loadClass(name);
			}
			else if (name.equals(DeployerContextUtils.class.getName())) {
				/*
				 * This will ensure that `DeployerContextUtils` is available to
				 * foreign class loader for cases where foreign JAR does not
				 * have SCF dependencies.
				 */
				try {
					clazz = super.loadClass(name, false);
				}
				catch (Exception e) {
					clazz = defineClass(name, deployerContextUtilsBytes, 0, deployerContextUtilsBytes.length);
				}
			}
			else {
				clazz = super.loadClass(name, false);
			}
			return clazz;
		}
	};
	return this.archiveLoader;
}
 
Example #7
Source File: FormulaLauncher.java    From spring-cloud-formula with Apache License 2.0 4 votes vote down vote up
@Override
protected ClassLoader createClassLoader(URL[] urls) {
    logger.info("creating class loader with urls: {}", Arrays.asList(urls));
    return new LaunchedURLClassLoader(urls, deduceParentClassLoader());
}
 
Example #8
Source File: ApplicationRunner.java    From liiklus with MIT License 4 votes vote down vote up
@SneakyThrows
public ConfigurableApplicationContext run() {
    System.setProperty("plugins.dir", findPluginsDir().getAbsolutePath());
    System.setProperty("plugins.pathMatcher", "*/build/libs/*.jar");

    var tempFile = Files.createTempFile("app", ".jar");
    tempFile.toFile().deleteOnExit();
    try (var appJarStream = getClass().getClassLoader().getResourceAsStream("app-boot.jar")) {
        Files.copy(appJarStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
    }

    var launcher = new JarLauncher(new JarFileArchive(tempFile.toFile(), tempFile.toUri().toURL())) {

        ClassLoader createClassLoader() throws Exception {
            return super.createClassLoader(getClassPathArchives());
        }

        @Override
        protected ClassLoader createClassLoader(URL[] urls) throws Exception {
            var systemClassLoader = ClassLoader.getSystemClassLoader();
            return new LaunchedURLClassLoader(urls, systemClassLoader.getParent()) {

                @Override
                protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
                    var classFile = findResource(name.replace(".", "/") + ".class");
                    if (classFile != null) {
                        // If exists in the app.jar, load it from the system classloader instead
                        log.debug("Loading class '{}' from the system ClassLoader instead", name);
                        return systemClassLoader.loadClass(name);
                    }
                    return super.loadClass(name, resolve);
                }
            };
        }
    };

    var currentClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        var appClassLoader = launcher.createClassLoader();
        Thread.currentThread().setContextClassLoader(appClassLoader);

        var applicationClass = appClassLoader.loadClass("com.github.bsideup.liiklus.Application");

        var createSpringApplicationMethod = applicationClass.getDeclaredMethod("createSpringApplication", String[].class);

        var application = (SpringApplication) createSpringApplicationMethod.invoke(null, (Object) new String[0]);
        application.setDefaultProperties(properties);
        return application.run();
    } finally {
        Thread.currentThread().setContextClassLoader(currentClassLoader);
    }
}
 
Example #9
Source File: BootClassLoaderFactory.java    From spring-cloud-dashboard with Apache License 2.0 4 votes vote down vote up
@Override
protected ClassLoader createClassLoader(URL[] urls) throws Exception {
	return new LaunchedURLClassLoader(urls, parent);
}
 
Example #10
Source File: BootClassLoaderFactory.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
@Override
protected ClassLoader createClassLoader(URL[] urls) throws Exception {
	return new LaunchedURLClassLoader(urls, parent);
}