/*
 * Copyright 2021 The Apache Software Foundation.
 *
 * 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.tomcat.vestige.launcher;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

/**
 * @author Gael Lalire
 */
public class DelegateClassLoader extends ClassLoader {

    private ClassLoader delegate;

    private boolean parentFirst;

    public DelegateClassLoader(ClassLoader parent, ClassLoader delegate, boolean parentFirst) {
        super(parent);
        this.delegate = delegate;
        this.parentFirst = parentFirst;
    }

    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        return loadClass(name, false);
    }

    @Override
    protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
        Class<?> loadClass;
        if (parentFirst) {
            try {
                loadClass = super.loadClass(name, resolve);
            } catch (ClassNotFoundException e) {
                loadClass = delegate.loadClass(name);
                if (resolve) {
                    resolveClass(loadClass);
                }
            }
        } else {
            try {
                loadClass = delegate.loadClass(name);
                if (resolve) {
                    resolveClass(loadClass);
                }
            } catch (ClassNotFoundException e) {
                loadClass = super.loadClass(name, resolve);
            }
        }
        return loadClass;
    }

    @Override
    public URL getResource(String name) {
        URL resource;
        if (parentFirst) {
            resource = super.getResource(name);
            if (resource == null) {
                resource = delegate.getResource(name);
            }
        } else {
            resource = delegate.getResource(name);
            if (resource == null) {
                resource = super.getResource(name);
            }
        }
        return resource;
    }

    @Override
    public Enumeration<URL> getResources(String name) throws IOException {
        Enumeration<URL> resources = super.getResources(name);
        Enumeration<URL> delegateResources = delegate.getResources(name);
        return new Enumeration<URL>() {

            @Override
            public boolean hasMoreElements() {
                return resources.hasMoreElements() || delegateResources.hasMoreElements();
            }

            @Override
            public URL nextElement() {
                if (parentFirst) {
                    if (resources.hasMoreElements()) {
                        return resources.nextElement();
                    }
                    return delegateResources.nextElement();
                } else {
                    if (delegateResources.hasMoreElements()) {
                        return delegateResources.nextElement();
                    }
                    return resources.nextElement();
                }
            }
        };
    }

    @Override
    public String toString() {
        return getParent().toString();
    }

}
