diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/InstallAbstract.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/InstallAbstract.java index 8b3ed5bd01..ae6f0823fb 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/InstallAbstract.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/InstallAbstract.java @@ -107,7 +107,6 @@ public class InstallAbstract extends InputAbstract { return content.replaceAll(Pattern.quote(key), Matcher.quoteReplacement(value)); } - protected void copy(InputStream is, OutputStream os) throws IOException { byte[] buffer = new byte[1024 * 4]; int c = is.read(buffer); @@ -117,7 +116,6 @@ public class InstallAbstract extends InputAbstract { } } - protected void write(String source, File target, HashMap filters, @@ -165,12 +163,7 @@ public class InstallAbstract extends InputAbstract { } } - /** - * This method is made public for the testsuite - */ - public InputStream openStream(String source) { + protected InputStream openStream(String source) { return this.getClass().getResourceAsStream(source); } - - } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Run.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Run.java index 6c2f7e50a5..5e3dc31889 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Run.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Run.java @@ -229,15 +229,13 @@ public class Run extends LockAbstract { } - public static final String OLD_LOG_NAME = "logging.properties"; - public static void verifyOlderLogging(File etc) throws Exception { File newLogging = new File(etc, Create.ETC_LOG4J2_PROPERTIES); - File oldLogging = new File(etc, OLD_LOG_NAME); + File oldLogging = new File(etc, Upgrade.OLD_LOGGING_PROPERTIES); if (oldLogging.exists() && !newLogging.exists()) { System.out.println("******************************************************************************************************************************************************************************"); - System.out.println("Your system has the older logging file " + OLD_LOG_NAME + ", but not the new " + Create.ETC_LOG4J2_PROPERTIES); + System.out.println("Your system has the older logging file " + Upgrade.OLD_LOGGING_PROPERTIES + ", but not the new " + Create.ETC_LOG4J2_PROPERTIES); System.out.println("It appears you did not complete the migration on this artemis instance properly. Please check all the settings or run the './artemis upgrade' command from the new artemis home"); System.out.println("******************************************************************************************************************************************************************************"); } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Upgrade.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Upgrade.java index d0e04e9047..a268ea863b 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Upgrade.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Upgrade.java @@ -34,6 +34,9 @@ import io.airlift.airline.Command; @Command(name = "upgrade", description = "Update an artemis instance to the current artemis.home, keeping all the data and broker.xml. Warning: backup your instance before using this command and compare the files.") public class Upgrade extends InstallAbstract { + + protected static final String OLD_LOGGING_PROPERTIES = "logging.properties"; + /** * Checks that the directory provided either exists and is writable or doesn't exist but can be created. */ @@ -141,14 +144,17 @@ public class Upgrade extends InstallAbstract { } private String getLine(File cmd, String pattern) throws IOException { - Stream lines = Files.lines(cmd.toPath()); - Iterator iterator = lines.iterator(); - while (iterator.hasNext()) { - String line = iterator.next(); - if (line.trim().startsWith(pattern)) { - return line; + try (Stream lines = Files.lines(cmd.toPath())) { + Iterator iterator = lines.iterator(); + while (iterator.hasNext()) { + String line = iterator.next(); + + if (line.trim().startsWith(pattern)) { + return line; + } } } + return null; } @@ -156,33 +162,35 @@ public class Upgrade extends InstallAbstract { HashMap replaceMatrix = new HashMap<>(); doUpgrade(tmpFile, targetFile, bkp, - line -> { + oldLine -> { for (String prefix : keepingPrefixes) { - if (line.trim().startsWith(prefix)) { - replaceMatrix.put(prefix, line); + if (oldLine.trim().startsWith(prefix)) { + replaceMatrix.put(prefix, oldLine); } } - }, line -> { - for (String prefix : keepingPrefixes) { - if (line.trim().startsWith(prefix)) { - String original = replaceMatrix.get(prefix); - return original; + }, + newLine -> { + for (String prefix : keepingPrefixes) { + if (newLine.trim().startsWith(prefix)) { + String originalLine = replaceMatrix.get(prefix); + return originalLine; + } } - } - return line; - }); + return newLine; + }); } private void doUpgrade(File tmpFile, File targetFile, File bkp, Consumer originalConsumer, Function targetFunction) throws Exception { Files.copy(targetFile.toPath(), bkp.toPath(), StandardCopyOption.REPLACE_EXISTING); // we first scan the original lines on the originalConsumer, giving a chance to the caller to fill out the original matrix - Stream originalLines = Files.lines(targetFile.toPath()); - originalLines.forEach(line -> { - if (originalConsumer != null) { - originalConsumer.accept(line); + if (originalConsumer != null) { + try (Stream originalLines = Files.lines(targetFile.toPath())) { + originalLines.forEach(line -> { + originalConsumer.accept(line); + }); } - }); + } // now we open the new file from the tmp, and we will give a chance for the targetFunction to replace lines from a matrix try (Stream lines = Files.lines(tmpFile.toPath()); @@ -200,23 +208,26 @@ public class Upgrade extends InstallAbstract { } private void upgradeLogging(ActionContext context, File bkpFolder, File etc) throws Exception { - File oldLogging = new File(etc, "logging.properties"); + File oldLogging = new File(etc, OLD_LOGGING_PROPERTIES); if (oldLogging.exists()) { - context.out.println("Moving " + oldLogging + " under " + bkpFolder); - File oldLoggingCopy = new File(bkpFolder, "logging.properties"); - context.out.println(oldLogging.toPath() + " copy as " + oldLoggingCopy.toPath()); + File oldLoggingCopy = new File(bkpFolder, OLD_LOGGING_PROPERTIES); + context.out.println("Copying " + oldLogging.toPath() + " to " + oldLoggingCopy.toPath()); + Files.copy(oldLogging.toPath(), bkpFolder.toPath(), StandardCopyOption.REPLACE_EXISTING); - oldLogging.delete(); + + context.out.println("Removing " + oldLogging.toPath()); + if (!oldLogging.delete()) { + context.out.println(oldLogging.toPath() + " could not be removed!"); + } + File newLogging = new File(etc, Create.ETC_LOG4J2_PROPERTIES); - - if (!newLogging.exists()) { context.out.println("Creating " + newLogging); - InputStream inputStream = getClass().getResourceAsStream("etc/" + Create.ETC_LOG4J2_PROPERTIES); - OutputStream outputStream = new FileOutputStream(newLogging); - outputStream.write(inputStream.readAllBytes()); - inputStream.close(); + try (InputStream inputStream = openStream("etc/" + Create.ETC_LOG4J2_PROPERTIES); + OutputStream outputStream = new FileOutputStream(newLogging);) { + copy(inputStream, outputStream); + } } } } @@ -232,6 +243,4 @@ public class Upgrade extends InstallAbstract { } throw new RuntimeException("Too many backup folders in place already. Please remove some of the old-config-bkp.* folders"); } - - } diff --git a/tests/smoke-tests/pom.xml b/tests/smoke-tests/pom.xml index 1607bc60c7..f59550d43a 100644 --- a/tests/smoke-tests/pom.xml +++ b/tests/smoke-tests/pom.xml @@ -195,16 +195,10 @@ src/main/resources false - - **/* - src/main/filtered-resources true - - **/* - diff --git a/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/upgradeTest/CompareUpgradeTest.java b/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/upgradeTest/CompareUpgradeTest.java index 80d00dbd44..5f43ca2b1a 100644 --- a/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/upgradeTest/CompareUpgradeTest.java +++ b/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/upgradeTest/CompareUpgradeTest.java @@ -119,24 +119,27 @@ public class CompareUpgradeTest { } File file = new File(fileName); - Stream lines = Files.lines(file.toPath()); - lines.forEach(line -> { - String trimmedLine = line.trim(); - expectedValues.forEach((key, value) -> { - if (trimmedLine.startsWith(key)) { - String actualValue = trimmedLine.substring(key.length()); - logger.debug("match = {}", line); - matchingValues.put(key, actualValue); + try (Stream lines = Files.lines(file.toPath())) { + lines.forEach(line -> { + String trimmedLine = line.trim(); + expectedValues.forEach((key, value) -> { + if (trimmedLine.startsWith(key)) { + String actualValue = trimmedLine.substring(key.length()); + logger.debug("match = {}", line); + matchingValues.put(key, actualValue); - if (value == null) { - logger.debug("no expected value was defined for {}, we will just fill out the matchingValues for further evaluation", key); - } else { - logger.debug("prefix={}, expecting={}, actualValue={}", key, value, actualValue); - Assert.assertEquals(key + " did not match", value, actualValue); + if (value == null) { + logger.debug("no expected value was defined for {}, we will just fill out the matchingValues for further evaluation", key); + } else { + if (logger.isDebugEnabled()) { + logger.debug("prefix={}, expecting={}, actualValue={}", key, value, actualValue); + } + Assert.assertEquals(key + " did not match", value, actualValue); + } } - } + }); }); - }); + } Assert.assertEquals("Some elements were not found in the output of " + fileName, matchingValues.size(), expectedValues.size());