From fb8b531bfd98e22287bc31505f383d11a88c99b1 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Mon, 16 Mar 2020 17:45:56 -0700 Subject: [PATCH] Polish --- .../boot/maven/CustomLayersProvider.java | 136 ++++++++---------- 1 file changed, 60 insertions(+), 76 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/CustomLayersProvider.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/CustomLayersProvider.java index 44c8ffe893..5e7042cc3b 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/CustomLayersProvider.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/CustomLayersProvider.java @@ -18,6 +18,7 @@ package org.springframework.boot.maven; import java.util.ArrayList; import java.util.List; +import java.util.function.Predicate; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -27,6 +28,7 @@ import org.w3c.dom.NodeList; import org.springframework.boot.loader.tools.Layer; import org.springframework.boot.loader.tools.layer.CustomLayers; import org.springframework.boot.loader.tools.layer.application.FilteredResourceStrategy; +import org.springframework.boot.loader.tools.layer.application.LocationFilter; import org.springframework.boot.loader.tools.layer.application.ResourceFilter; import org.springframework.boot.loader.tools.layer.application.ResourceStrategy; import org.springframework.boot.loader.tools.layer.library.CoordinateFilter; @@ -51,91 +53,77 @@ public class CustomLayersProvider { for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element) { - Element ele = (Element) node; - String nodeName = ele.getNodeName(); - if ("layers".equals(nodeName)) { - layers.addAll(getLayers(ele)); - } - if ("libraries".equals(nodeName)) { - libraryStrategies.addAll(getLibraryStrategies(ele.getChildNodes())); - } - if ("classes".equals(nodeName)) { - resourceStrategies.addAll(getResourceStrategies(ele.getChildNodes())); - } + processNode(layers, libraryStrategies, resourceStrategies, (Element) node); } } return new CustomLayers(layers, resourceStrategies, libraryStrategies); } - private List getLibraryStrategies(NodeList nodes) { - List strategy = new ArrayList<>(); - for (int i = 0; i < nodes.getLength(); i++) { - Node item = nodes.item(i); - if (item instanceof Element) { - Element element = (Element) item; - String layer = element.getAttribute("layer"); - if ("layer-content".equals(element.getTagName())) { - List filters = new ArrayList<>(); - NodeList filterList = item.getChildNodes(); - if (filterList.getLength() == 0) { - throw new IllegalArgumentException("Filters for layer-content must not be empty."); - } - for (int k = 0; k < filterList.getLength(); k++) { - Node filter = filterList.item(k); - if (filter instanceof Element) { - List includeList = getPatterns((Element) filter, "include"); - List excludeList = getPatterns((Element) filter, "exclude"); - addLibraryFilter(filters, filter, includeList, excludeList); - } - } - strategy.add(new FilteredLibraryStrategy(layer, filters)); - } - } + private void processNode(List layers, List libraryStrategies, + List resourceStrategies, Element node) { + String nodeName = node.getNodeName(); + if ("layers".equals(nodeName)) { + layers.addAll(getLayers(node)); + } + NodeList contents = node.getChildNodes(); + if ("libraries".equals(nodeName)) { + libraryStrategies.addAll(getStrategies(contents, + (StrategyFactory) FilteredLibraryStrategy::new, + CoordinateFilter::new, "coordinates"::equals)); + } + if ("classes".equals(nodeName)) { + resourceStrategies.addAll(getStrategies(contents, + (StrategyFactory) FilteredResourceStrategy::new, + LocationFilter::new, "locations"::equals)); } - return strategy; } - private void addLibraryFilter(List filters, Node filter, List includeList, - List excludeList) { - if ("coordinates".equals(filter.getNodeName())) { - filters.add(new CoordinateFilter(includeList, excludeList)); + private List getLayers(Element element) { + List layers = new ArrayList<>(); + NodeList nodes = element.getChildNodes(); + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + if (node instanceof Element) { + Element ele = (Element) node; + String nodeName = ele.getNodeName(); + if ("layer".equals(nodeName)) { + layers.add(new Layer(ele.getTextContent())); + } + } } + return layers; } - private List getResourceStrategies(NodeList strategies) { - List strategy = new ArrayList<>(); - for (int i = 0; i < strategies.getLength(); i++) { - Node item = strategies.item(i); - List filters = new ArrayList<>(); + private List getStrategies(NodeList nodes, StrategyFactory strategyFactory, + FilterFactory filterFactory, Predicate filterPredicate) { + List contents = new ArrayList<>(); + for (int i = 0; i < nodes.getLength(); i++) { + Node item = nodes.item(i); if (item instanceof Element) { Element element = (Element) item; - String layer = element.getAttribute("layer"); if ("layer-content".equals(element.getTagName())) { NodeList filterList = item.getChildNodes(); if (filterList.getLength() == 0) { throw new IllegalArgumentException("Filters for layer-content must not be empty."); } - for (int k = 0; k < filterList.getLength(); k++) { - Node filter = filterList.item(k); - if (filter instanceof Element) { - List includeList = getPatterns((Element) filter, "include"); - List excludeList = getPatterns((Element) filter, "exclude"); - addFilter(filters, filter, includeList, excludeList); + List filters = new ArrayList<>(); + for (int j = 0; j < filterList.getLength(); j++) { + Node filterNode = filterList.item(j); + if (filterNode instanceof Element) { + List includeList = getPatterns((Element) filterNode, "include"); + List excludeList = getPatterns((Element) filterNode, "exclude"); + if (filterPredicate.test(filterNode.getNodeName())) { + E filter = filterFactory.getFilter(includeList, excludeList); + filters.add(filter); + } } } - strategy.add(new FilteredResourceStrategy(layer, filters)); + String layer = element.getAttribute("layer"); + contents.add(strategyFactory.getStrategy(layer, filters)); } } } - return strategy; - } - - private void addFilter(List filters, Node filter, List includeList, - List excludeList) { - if ("locations".equals(filter.getNodeName())) { - filters.add( - new org.springframework.boot.loader.tools.layer.application.LocationFilter(includeList, excludeList)); - } + return contents; } private List getPatterns(Element element, String key) { @@ -150,20 +138,16 @@ public class CustomLayersProvider { return values; } - private List getLayers(Element element) { - List layers = new ArrayList<>(); - NodeList nl = element.getChildNodes(); - for (int i = 0; i < nl.getLength(); i++) { - Node node = nl.item(i); - if (node instanceof Element) { - Element ele = (Element) node; - String nodeName = ele.getNodeName(); - if ("layer".equals(nodeName)) { - layers.add(new Layer(ele.getTextContent())); - } - } - } - return layers; + interface StrategyFactory { + + T getStrategy(String layer, List filters); + + } + + interface FilterFactory { + + E getFilter(List includes, List excludes); + } }