|
| 1 | +// Copyright 2019, Oracle Corporation and/or its affiliates. All rights reserved. |
| 2 | +// Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl. |
| 3 | + |
| 4 | +package com.oracle.weblogic.imagetool.util; |
| 5 | + |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.text.MessageFormat; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.regex.Pattern; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +import com.oracle.weblogic.imagetool.logging.LoggingFacade; |
| 19 | +import com.oracle.weblogic.imagetool.logging.LoggingFactory; |
| 20 | + |
| 21 | +public class AdditionalBuildCommands { |
| 22 | + private static final LoggingFacade logger = LoggingFactory.getLogger(AdditionalBuildCommands.class); |
| 23 | + |
| 24 | + public static final String BEFORE_JDK = "before-jdk-install"; |
| 25 | + public static final String AFTER_JDK = "after-jdk-install"; |
| 26 | + public static final String BEFORE_FMW = "before-fmw-install"; |
| 27 | + public static final String AFTER_FMW = "after-fmw-install"; |
| 28 | + public static final String BEFORE_WDT = "before-wdt-command"; |
| 29 | + public static final String AFTER_WDT = "after-wdt-command"; |
| 30 | + public static final String FINAL_BLD = "final-build-commands"; |
| 31 | + |
| 32 | + private List<NamedPattern> sections; |
| 33 | + private Map<String, List<String>> contents; |
| 34 | + |
| 35 | + private AdditionalBuildCommands() { |
| 36 | + sections = new ArrayList<>(); |
| 37 | + sections.add(getPattern(BEFORE_JDK)); |
| 38 | + sections.add(getPattern(AFTER_JDK)); |
| 39 | + sections.add(getPattern(BEFORE_FMW)); |
| 40 | + sections.add(getPattern(AFTER_FMW)); |
| 41 | + sections.add(getPattern(BEFORE_WDT)); |
| 42 | + sections.add(getPattern(AFTER_WDT)); |
| 43 | + sections.add(getPattern(FINAL_BLD)); |
| 44 | + } |
| 45 | + |
| 46 | + private static NamedPattern getPattern(String key) { |
| 47 | + return new NamedPattern(key, getSectionHeaderString(key)); |
| 48 | + } |
| 49 | + |
| 50 | + private static String getSectionHeaderString(String key) { |
| 51 | + return MessageFormat.format("\\s*\\[\\s*{0}\\s*\\]\\s*", key); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Once a file is loaded, size returns the number of sections found in the file. |
| 56 | + * @return the count of sections found in the file. |
| 57 | + */ |
| 58 | + public int size() { |
| 59 | + return contents.size(); |
| 60 | + } |
| 61 | + |
| 62 | + public Map<String,List<String>> getContents() { |
| 63 | + return contents; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Once a file is loaded, getSection should return the contents of a single section, by name. |
| 68 | + * @param name the name of the section to return |
| 69 | + * @return the contents of the section requested or null if the section was not found. |
| 70 | + */ |
| 71 | + public List<String> getSection(String name) { |
| 72 | + return contents.get(name); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Load the contents of the additional build commands file into this object. |
| 77 | + * Existing data in this instance are replaced. |
| 78 | + * @param file Path to the additional build commands file that should be loaded. |
| 79 | + * @throws IOException if an issue occurs locating or opening the file provided |
| 80 | + */ |
| 81 | + public static AdditionalBuildCommands load(Path file) throws IOException { |
| 82 | + logger.entering(file); |
| 83 | + AdditionalBuildCommands result = new AdditionalBuildCommands(); |
| 84 | + result.contents = new HashMap<>(); |
| 85 | + |
| 86 | + try (BufferedReader reader = Files.newBufferedReader(file)) { |
| 87 | + List<String> buffer = new ArrayList<>(); |
| 88 | + String currentSection = null; |
| 89 | + String line; |
| 90 | + while ((line = reader.readLine()) != null) { |
| 91 | + logger.finest(line); |
| 92 | + String sectionStart = result.checkForSectionHeader(line); |
| 93 | + //skip any lines that come before the first section header |
| 94 | + if (currentSection != null && sectionStart == null) { |
| 95 | + //collect lines inside current section |
| 96 | + buffer.add(line); |
| 97 | + } else if (currentSection != null) { |
| 98 | + //while in a section, found next section start (save last section, and start new section) |
| 99 | + logger.fine("IMG-0015", buffer.size(), currentSection); |
| 100 | + result.contents.put(currentSection, buffer); |
| 101 | + buffer = new ArrayList<>(); |
| 102 | + currentSection = sectionStart; |
| 103 | + } else if (sectionStart != null) { |
| 104 | + //current section was null, but found new section start |
| 105 | + currentSection = sectionStart; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (currentSection != null && buffer.size() > 0) { |
| 110 | + //finished reading file, store the remaining lines that were read for the section |
| 111 | + logger.fine("IMG-0015", buffer.size(), currentSection); |
| 112 | + result.contents.put(currentSection, buffer); |
| 113 | + } |
| 114 | + } catch (IOException ioe) { |
| 115 | + logger.severe("IMG-0013", file.getFileName()); |
| 116 | + throw ioe; |
| 117 | + } |
| 118 | + |
| 119 | + logger.exiting(); |
| 120 | + return result; |
| 121 | + } |
| 122 | + |
| 123 | + private String checkForSectionHeader(String line) { |
| 124 | + //Pattern: looks like something in square brackets, ignoring whitespace |
| 125 | + Pattern sectionLike = Pattern.compile(getSectionHeaderString(".*")); |
| 126 | + if (!sectionLike.matcher(line).matches()) { |
| 127 | + //line does not look like a header "[something]" |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + for (NamedPattern section : sections) { |
| 132 | + if (section.matches(line)) { |
| 133 | + return section.getName(); |
| 134 | + } |
| 135 | + } |
| 136 | + throw new IllegalArgumentException(Utils.getMessage("IMG-0014", line, validSectionNames())); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Returns a list of valid section names for the additional build commands file. |
| 141 | + * @return comma separated list of section names |
| 142 | + */ |
| 143 | + public String validSectionNames() { |
| 144 | + return sections.stream() |
| 145 | + .map(NamedPattern::getName) |
| 146 | + .collect(Collectors.joining(", ")); |
| 147 | + } |
| 148 | +} |
0 commit comments