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

org.apache.commons.javaflow.spi.ExtendedClasspathResourceLoader Maven / Gradle / Ivy

There is a newer version: 2.7.8
Show newest version
/**
 * Copyright 2013-2022 Valery Silaev (http://vsilaev.com)
 *
 * 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.apache.commons.javaflow.spi;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

public class ExtendedClasspathResourceLoader extends ClasspathResourceLoader {
    private static final ThreadLocal> IN_MEMORY_RESOURCES = new ThreadLocal>();

    public ExtendedClasspathResourceLoader(ClassLoader classLoader) {
        super(classLoader);
    }

    public static void runWithInMemoryResources(final Runnable block, Map inMemoryResources) {
        runWithInMemoryResources(new Callable() {
            public Void call() {
                block.run();
                return null;
            }
        }, inMemoryResources);
    }

    public static  V runWithInMemoryResources(Callable block, Map inMemoryResources) {
        Map resources = new HashMap(inMemoryResources);

        Map previous = IN_MEMORY_RESOURCES.get();
        if (null != previous) {
            // Merge with previous ones for recursive calls
            resources.putAll(previous);
        }
        IN_MEMORY_RESOURCES.set(resources);

        try {
            return block.call();
        } catch (RuntimeException ex) {
            throw ex;
        } catch (Error ex) {
            throw ex;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        } finally {
            if (null != previous) { 
                IN_MEMORY_RESOURCES.set(previous);
            } else {
                IN_MEMORY_RESOURCES.remove();
            }
        }

    }
    
    @Override
    public boolean hasResource(String name) {
        Map inMemoryResources = IN_MEMORY_RESOURCES.get();
        if (null != inMemoryResources && inMemoryResources.containsKey(name)) {
            return true;
        }
        return super.hasResource(name);
    }

    @Override
    public InputStream getResourceAsStream(String name) throws IOException {
        Map inMemoryResources = IN_MEMORY_RESOURCES.get();
        if (null != inMemoryResources) {
            byte[] bytecode = inMemoryResources.get(name);
            if (null != bytecode)
                return new FastByteArrayInputStream(bytecode);
        }

        return super.getResourceAsStream(name);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy