ARTEMIS-4081: some small cleanups and fixups
This commit is contained in:
parent
97e0a3d7f2
commit
304033673c
|
@ -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<String, String> 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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("******************************************************************************************************************************************************************************");
|
||||
}
|
||||
|
|
|
@ -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<String> lines = Files.lines(cmd.toPath());
|
||||
Iterator<String> iterator = lines.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
String line = iterator.next();
|
||||
if (line.trim().startsWith(pattern)) {
|
||||
return line;
|
||||
try (Stream<String> lines = Files.lines(cmd.toPath())) {
|
||||
Iterator<String> 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<String, String> 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<String> originalConsumer, Function<String, String> 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<String> originalLines = Files.lines(targetFile.toPath());
|
||||
originalLines.forEach(line -> {
|
||||
if (originalConsumer != null) {
|
||||
originalConsumer.accept(line);
|
||||
if (originalConsumer != null) {
|
||||
try (Stream<String> 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<String> 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");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -195,16 +195,10 @@
|
|||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>false</filtering>
|
||||
<includes>
|
||||
<include>**/*</include>
|
||||
</includes>
|
||||
</resource>
|
||||
<resource>
|
||||
<directory>src/main/filtered-resources</directory>
|
||||
<filtering>true</filtering>
|
||||
<includes>
|
||||
<include>**/*</include>
|
||||
</includes>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
|
|
|
@ -119,24 +119,27 @@ public class CompareUpgradeTest {
|
|||
}
|
||||
|
||||
File file = new File(fileName);
|
||||
Stream<String> 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<String> 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());
|
||||
|
||||
|
|
Loading…
Reference in New Issue