Minor refactoring for tool tasks

- Don't scan for translations on initialization in TranslationPageGenerator in order to speed up startup time of ToolsRunner
- Extract checking for null / empty array of File#listFiles into a separate method
- Move single method of RuntimeUtils into Utils class
This commit is contained in:
ljacqu 2016-10-30 10:43:59 +01:00
parent 195e409efd
commit 46af922fba
10 changed files with 45 additions and 58 deletions

View File

@ -5,9 +5,6 @@ import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.pool.HikariPool.PoolInitializationException;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.Columns;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.DataSourceType;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.security.crypts.XFBCRYPT;
@ -15,8 +12,8 @@ import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.RuntimeUtils;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils;
import java.sql.Blob;
import java.sql.Connection;
@ -100,8 +97,8 @@ public class MySQL implements DataSource {
this.phpBbGroup = settings.getProperty(HooksSettings.PHPBB_ACTIVATED_GROUP_ID);
this.wordpressPrefix = settings.getProperty(HooksSettings.WORDPRESS_TABLE_PREFIX);
this.poolSize = settings.getProperty(DatabaseSettings.MYSQL_POOL_SIZE);
if(poolSize == -1) {
poolSize = RuntimeUtils.getCoreCount();
if (poolSize == -1) {
poolSize = Utils.getCoreCount();
}
}

View File

@ -1,20 +0,0 @@
package fr.xephi.authme.util;
/**
* Runtime utilities.
*/
public class RuntimeUtils {
// Utility class
private RuntimeUtils() {
}
/**
* Return the available core count of the JVM.
*
* @return the core count
*/
public static int getCoreCount() {
return Runtime.getRuntime().availableProcessors();
}
}

View File

@ -48,4 +48,13 @@ public final class Utils {
return false;
}
}
/**
* Return the available core count of the JVM.
*
* @return the core count
*/
public static int getCoreCount() {
return Runtime.getRuntime().availableProcessors();
}
}

View File

@ -13,6 +13,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import static tools.utils.FileIoUtils.listFilesOrThrow;
import static org.junit.Assert.fail;
/**
@ -28,11 +29,7 @@ public class YamlTextFileCheckerTest {
@BeforeClass
public static void loadMessagesFiles() {
File folder = TestHelper.getJarFile(MESSAGES_FOLDER);
File[] files = folder.listFiles();
if (files == null || files.length == 0) {
throw new IllegalStateException("Could not read folder '" + folder.getName() + "'");
}
messageFiles = Arrays.asList(files);
messageFiles = Arrays.asList(listFilesOrThrow(folder));
}
@Test
@ -82,17 +79,13 @@ public class YamlTextFileCheckerTest {
* @param errors collection of errors to add to if the verification fails
*/
private void checkFile(File file, String mandatoryKey, List<String> errors) {
String error = null;
try {
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
if (StringUtils.isEmpty(configuration.getString(mandatoryKey))) {
error = "Message for '" + mandatoryKey + "' is empty";
errors.add("Message for '" + mandatoryKey + "' is empty");
}
} catch (Exception e) {
error = "Could not load file: " + StringUtils.formatException(e);
}
if (!StringUtils.isEmpty(error)) {
errors.add(file.getName() + ": " + error);
errors.add("Could not load file: " + StringUtils.formatException(e));
}
}
}

View File

@ -34,8 +34,6 @@ public class TranslationPageGenerator implements AutoToolTask {
private static final int[] COLOR_1 = {12, 9, 0};
private static final int[] COLOR_2 = { 6, 15, 6};
private final TranslationsGatherer gatherer = new TranslationsGatherer();
@Override
public String getTaskName() {
return "updateTranslations";
@ -43,6 +41,7 @@ public class TranslationPageGenerator implements AutoToolTask {
@Override
public void executeDefault() {
TranslationsGatherer gatherer = new TranslationsGatherer();
NestedTagValue translationValuesHolder = new NestedTagValue();
for (TranslationInfo translation : gatherer.getTranslationInfo()) {

View File

@ -11,6 +11,8 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static tools.utils.FileIoUtils.listFilesOrThrow;
/**
* Gathers all available translations of AuthMe.
*/
@ -31,10 +33,7 @@ public class TranslationsGatherer {
}
private void gatherTranslations() {
File[] files = new File(MESSAGES_FOLDER).listFiles();
if (files == null) {
throw new IllegalStateException("Cannot read files of '" + MESSAGES_FOLDER + "'");
}
File[] files = listFilesOrThrow(new File(MESSAGES_FOLDER));
for (File file : files) {
String code = getLanguageCode(file.getName());
if (code != null) {

View File

@ -10,6 +10,8 @@ import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static tools.utils.FileIoUtils.listFilesOrThrow;
/**
* Verifies the help translations for validity and completeness.
*/
@ -57,10 +59,7 @@ public class VerifyHelpTranslations implements ToolTask {
}
private static List<File> getHelpTranslations() {
File[] files = new File(FOLDER).listFiles();
if (files == null) {
throw new IllegalStateException("Could not get files from '" + FOLDER + "'");
}
File[] files = listFilesOrThrow(new File(FOLDER));
List<File> helpFiles = Arrays.stream(files)
.filter(file -> HELP_MESSAGE_PATTERN.matcher(file.getName()).matches())
.collect(Collectors.toList());

View File

@ -17,6 +17,8 @@ import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static tools.utils.FileIoUtils.listFilesOrThrow;
/**
* Task to verify the keys in the messages files.
*/
@ -124,12 +126,7 @@ public final class VerifyMessagesTask implements ToolTask {
}
private static List<File> getMessagesFiles() {
File folder = new File(MESSAGES_FOLDER);
File[] files = folder.listFiles();
if (files == null) {
throw new IllegalStateException("Could not read files from folder '" + folder.getName() + "'");
}
File[] files = listFilesOrThrow(new File(MESSAGES_FOLDER));
List<File> messageFiles = new ArrayList<>();
for (File file : files) {
if (MESSAGE_FILE_PATTERN.matcher(file.getName()).matches()) {

View File

@ -8,6 +8,8 @@ import tools.utils.ToolsConstants;
import java.io.File;
import java.util.Scanner;
import static tools.utils.FileIoUtils.listFilesOrThrow;
/**
* Task which exports all messages to a local folder.
*/
@ -22,11 +24,7 @@ public class WriteAllExportsTask extends ExportMessagesTask {
@Override
public void execute(Scanner scanner) {
File[] messageFiles = new File(MESSAGES_FOLDER).listFiles();
if (messageFiles == null || messageFiles.length == 0) {
throw new IllegalStateException("Could not read messages folder");
}
final File[] messageFiles = listFilesOrThrow(new File(MESSAGES_FOLDER));
final FileConfiguration defaultMessages = loadDefaultMessages();
for (File file : messageFiles) {
String code = file.getName().substring("messages_".length(), file.getName().length() - ".yml".length());

View File

@ -1,5 +1,6 @@
package tools.utils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@ -62,4 +63,19 @@ public final class FileIoUtils {
}
}
/**
* Returns a folder's files or throws an exception if the folder could not be read or if it is empty.
*
* @param folder the folder to read
* @return the files in the folder
*/
public static File[] listFilesOrThrow(File folder) {
File[] files = folder.listFiles();
if (files == null) {
throw new IllegalStateException("Could not read folder '" + folder + "'");
} else if (files.length == 0) {
throw new IllegalStateException("Folder '" + folder + "' is empty");
}
return files;
}
}