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

org.springframework.boot.loader.tools.layer.CustomLayers Maven / Gradle / Ivy

/*
 * Copyright 2012-2020 the original author or authors.
 *
 * 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
 *
 *      https://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.springframework.boot.loader.tools.layer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.springframework.boot.loader.tools.Layer;
import org.springframework.boot.loader.tools.Layers;
import org.springframework.boot.loader.tools.Library;
import org.springframework.util.Assert;

/**
 * Custom {@link Layers} implementation where layer content is selected by the user.
 *
 * @author Madhura Bhave
 * @author Phillip Webb
 * @since 2.3.0
 */
public class CustomLayers implements Layers {

	private final List layers;

	private final List> applicationSelectors;

	private final List> librarySelectors;

	public CustomLayers(List layers, List> applicationSelectors,
			List> librarySelectors) {
		Assert.notNull(layers, "Layers must not be null");
		Assert.notNull(applicationSelectors, "ApplicationSelectors must not be null");
		validateSelectorLayers(applicationSelectors, layers);
		Assert.notNull(librarySelectors, "LibrarySelectors must not be null");
		validateSelectorLayers(librarySelectors, layers);
		this.layers = new ArrayList<>(layers);
		this.applicationSelectors = new ArrayList<>(applicationSelectors);
		this.librarySelectors = new ArrayList<>(librarySelectors);
	}

	private static  void validateSelectorLayers(List> selectors, List layers) {
		for (ContentSelector selector : selectors) {
			validateSelectorLayers(selector, layers);
		}
	}

	private static void validateSelectorLayers(ContentSelector selector, List layers) {
		Layer layer = selector.getLayer();
		Assert.state(layer != null, "Missing content selector layer");
		Assert.state(layers.contains(layer),
				() -> "Content selector layer '" + selector.getLayer() + "' not found in " + layers);
	}

	@Override
	public Iterator iterator() {
		return this.layers.iterator();
	}

	@Override
	public Stream stream() {
		return this.layers.stream();
	}

	@Override
	public Layer getLayer(String resourceName) {
		return selectLayer(resourceName, this.applicationSelectors, () -> "Resource '" + resourceName + "'");
	}

	@Override
	public Layer getLayer(Library library) {
		return selectLayer(library, this.librarySelectors, () -> "Library '" + library.getName() + "'");
	}

	private  Layer selectLayer(T item, List> selectors, Supplier name) {
		for (ContentSelector selector : selectors) {
			if (selector.contains(item)) {
				return selector.getLayer();
			}
		}
		throw new IllegalStateException(name.get() + " did not match any layer");
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy