clean i18n directory when first time loading

This commit is contained in:
nbaars 2015-01-10 10:24:51 +01:00
parent 3d6236242f
commit 36ea6ad12d
4 changed files with 22 additions and 15 deletions

View File

@ -71,7 +71,7 @@ public class Plugin {
} }
} }
public void loadFiles(List<Path> files) { public void loadFiles(List<Path> files, boolean reload) {
for (Path file : files) { for (Path file : files) {
if (fileEndsWith(file, ".html") && hasParentDirectoryWithName(file, NAME_LESSON_SOLUTION_DIRECTORY)) { if (fileEndsWith(file, ".html") && hasParentDirectoryWithName(file, NAME_LESSON_SOLUTION_DIRECTORY)) {
solutionLanguageFiles.put(file.getParent().getFileName().toString(), file.toFile()); solutionLanguageFiles.put(file.getParent().getFileName().toString(), file.toFile());
@ -83,20 +83,27 @@ public class Plugin {
lessonSourceFile = file.toFile(); lessonSourceFile = file.toFile();
} }
if (fileEndsWith(file, ".properties") && hasParentDirectoryWithName(file, NAME_LESSON_I18N_DIRECTORY)) { if (fileEndsWith(file, ".properties") && hasParentDirectoryWithName(file, NAME_LESSON_I18N_DIRECTORY)) {
try { copyProperties(reload, file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Files.copy(file, bos);
Path propertiesPath = createPropertiesDirectory();
ResourceBundleClassLoader.setPropertiesPath(propertiesPath);
Files.write(propertiesPath.resolve(file.getFileName()), bos.toByteArray(),
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} catch (IOException io) {
throw new PluginLoadingFailure("Property file detected, but unable to copy the properties", io);
}
} }
} }
} }
private void copyProperties(boolean reload, Path file) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Files.copy(file, bos);
Path propertiesPath = createPropertiesDirectory();
ResourceBundleClassLoader.setPropertiesPath(propertiesPath);
if ( reload ) {
Files.write(propertiesPath.resolve(file.getFileName()), bos.toByteArray(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} else {
Files.write(propertiesPath.resolve(file.getFileName()), bos.toByteArray(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
}
} catch (IOException io) {
throw new PluginLoadingFailure("Property file detected, but unable to copy the properties", io);
}
}
private Path createPropertiesDirectory() throws IOException { private Path createPropertiesDirectory() throws IOException {
if (Files.exists(pluginDirectory.resolve(NAME_LESSON_I18N_DIRECTORY))) { if (Files.exists(pluginDirectory.resolve(NAME_LESSON_I18N_DIRECTORY))) {
return pluginDirectory.resolve(NAME_LESSON_I18N_DIRECTORY); return pluginDirectory.resolve(NAME_LESSON_I18N_DIRECTORY);

View File

@ -21,7 +21,7 @@ public class PluginsLoader implements Runnable {
this.path = path; this.path = path;
} }
public List<Plugin> loadPlugins() { public List<Plugin> loadPlugins(final boolean reload) {
final List<Plugin> plugins = new ArrayList<Plugin>(); final List<Plugin> plugins = new ArrayList<Plugin>();
try { try {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() { Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@ -33,7 +33,7 @@ public class PluginsLoader implements Runnable {
extractor.extract(); extractor.extract();
Plugin plugin = new Plugin(extractor.getBaseDirectory()); Plugin plugin = new Plugin(extractor.getBaseDirectory());
plugin.loadClasses(extractor.getClasses()); plugin.loadClasses(extractor.getClasses());
plugin.loadFiles(extractor.getFiles()); plugin.loadFiles(extractor.getFiles(), reload);
plugins.add(plugin); plugins.add(plugin);
} catch (Plugin.PluginLoadingFailure e) { } catch (Plugin.PluginLoadingFailure e) {
logger.error("Unable to load plugin, continue reading others..."); logger.error("Unable to load plugin, continue reading others...");
@ -51,6 +51,6 @@ public class PluginsLoader implements Runnable {
@Override @Override
public void run() { public void run() {
loadPlugins(); loadPlugins(true);
} }
} }

View File

@ -316,7 +316,7 @@ public class Course {
} }
Path pluginDirectory = Paths.get(path); Path pluginDirectory = Paths.get(path);
webgoatContext.setPluginDirectory(pluginDirectory); webgoatContext.setPluginDirectory(pluginDirectory);
List<Plugin> plugins = new PluginsLoader(pluginDirectory).loadPlugins(); List<Plugin> plugins = new PluginsLoader(pluginDirectory).loadPlugins(false);
for (Plugin plugin : plugins) { for (Plugin plugin : plugins) {
try { try {
Class<AbstractLesson> c = plugin.getLesson(); Class<AbstractLesson> c = plugin.getLesson();