Lesson source file, lesson plan and lesson solution are loaded from the plugin.

This commit is contained in:
nbaars 2015-01-07 13:45:27 +01:00
parent da7feccffb
commit 744630ca4c
5 changed files with 23 additions and 54 deletions

View File

@ -385,7 +385,7 @@ public abstract class AbstractLesson extends Screen implements Comparable<Object
}
src.append(readFromFile(new BufferedReader(new FileReader(s.getWebResource(filename))), false));
src.append(readFromFile(new BufferedReader(new FileReader(filename)), false));
} catch (Exception e) {
// s.setMessage( "Could not find lesson plan for " +
@ -467,8 +467,7 @@ public abstract class AbstractLesson extends Screen implements Comparable<Object
try {
// System.out.println("Loading source file: " +
// getSourceFileName());
src = convertMetacharsJavaCode(readFromFile(new BufferedReader(new FileReader(s
.getWebResource(getSourceFileName()))), true));
src = convertMetacharsJavaCode(readFromFile(new BufferedReader(new FileReader(getSourceFileName())), true));
// TODO: For styled line numbers and better memory efficiency,
// use a custom FilterReader
@ -504,7 +503,7 @@ public abstract class AbstractLesson extends Screen implements Comparable<Object
try {
logger.debug("Loading source file: " + getSourceFileName());
src = readFromFile(new BufferedReader(new FileReader(s.getWebResource(getSourceFileName()))), false);
src = readFromFile(new BufferedReader(new FileReader(getSourceFileName())), false);
} catch (FileNotFoundException e) {
s.setMessage("Could not find source file");
@ -522,7 +521,7 @@ public abstract class AbstractLesson extends Screen implements Comparable<Object
try {
// System.out.println("Solution: " + getLessonSolutionFileName());
src = readFromFile(new BufferedReader(new FileReader(s.getWebResource(getLessonSolutionFileName()))), false);
src = readFromFile(new BufferedReader(new FileReader(getLessonSolutionFileName())), false);
} catch (Exception e) {
s.setMessage("Could not find the solution file");
src = ("Could not find the solution file or solution file does not exist.<br/>"

View File

@ -19,6 +19,7 @@ public class Plugin {
private final Path pluginDirectory;
private final Map<String, File> solutionLanguageFiles;
private final Map<String, File> lessonPlansLanguageFiles;
private final File lessonSourceFile;
public static class PluginLoadingFailure extends RuntimeException {
@ -34,6 +35,7 @@ public class Plugin {
private final List<String> loadedClasses = new ArrayList<String>();
private final Map<String, File> solutionLanguageFiles = new HashMap<>();
private final Map<String, File> lessonPlansLanguageFiles = new HashMap<>();
private File javaSource;
public Builder loadClasses(Map<String, byte[]> classes) {
for (Map.Entry<String, byte[]> clazz : classes.entrySet() ) {
@ -46,8 +48,7 @@ public class Plugin {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
PluginClassLoader pluginClassLoader = new PluginClassLoader(contextClassLoader, classFile);
try {
//TODO the plugin part is extra because the packaging is not correct in WEB-173
String realClassName = name.replace("/lesson_plans/", "").replace("/plugin", "").replaceAll("/", ".").replaceAll(".class", "");
String realClassName = name.replaceFirst("/", "").replaceAll("/", ".").replaceAll(".class", "");
Class clazz = pluginClassLoader.loadClass(realClassName);
if (AbstractLesson.class.isAssignableFrom(clazz)) {
this.lesson = clazz;
@ -69,29 +70,33 @@ public class Plugin {
throw new PluginLoadingFailure(String.format("Lesson class not found, following classes were detected in the plugin: %s",
StringUtils.collectionToCommaDelimitedString(loadedClasses)));
}
return new Plugin(this.lesson, pluginDirectory, lessonPlansLanguageFiles, solutionLanguageFiles);
return new Plugin(this.lesson, pluginDirectory, lessonPlansLanguageFiles, solutionLanguageFiles, javaSource);
}
public void loadFiles(List<Path> files) {
for (Path file : files) {
if (file.getFileName().endsWith(".html") && file.getParent().getParent().getFileName()
if (file.getFileName().toString().endsWith(".html") && file.getParent().getParent().getFileName().toString()
.endsWith("lessonSolutions")) {
solutionLanguageFiles.put(file.getParent().getFileName().toString(), file.toFile());
}
if (file.getFileName().endsWith(".html") && file.getParent().getParent().getFileName()
if (file.getFileName().toString().endsWith(".html") && file.getParent().getParent().getFileName().toString()
.endsWith("lessonPlans")) {
lessonPlansLanguageFiles.put(file.getParent().getFileName().toString(), file.toFile());
}
if ( file.getFileName().toString().endsWith(".java")) {
javaSource = file.toFile();
}
}
}
}
public Plugin(Class<AbstractLesson> lesson, Path pluginDirectory, Map<String, File> lessonPlansLanguageFiles,
Map<String, File> solutionLanguageFiles) {
Map<String, File> solutionLanguageFiles, File lessonSourceFile) {
this.lesson = lesson;
this.pluginDirectory = pluginDirectory;
this.lessonPlansLanguageFiles = lessonPlansLanguageFiles;
this.solutionLanguageFiles = solutionLanguageFiles;
this.lessonSourceFile = lessonSourceFile;
}
public Class<AbstractLesson> getLesson() {
@ -102,6 +107,8 @@ public class Plugin {
return this.solutionLanguageFiles;
}
public File getLessonSource() { return lessonSourceFile; }
public Map<String, File> getLessonPlans() {
return this.lessonPlansLanguageFiles;
}

View File

@ -17,7 +17,7 @@ public class PluginBackgroundLoader implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
String pluginPath = event.getServletContext().getRealPath("plugin_lessons");
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new PluginsLoader(Paths.get(pluginPath)), 0, 5, TimeUnit.SECONDS);
scheduler.scheduleAtFixedRate(new PluginsLoader(Paths.get(pluginPath)), 0, 5, TimeUnit.MINUTES);
}
@Override

View File

@ -330,51 +330,14 @@ public class Course {
for(Map.Entry<String, File> lessonPlan : plugin.getLessonPlans().entrySet()) {
lesson.setLessonPlanFileName(lessonPlan.getKey(), lessonPlan.getValue().toString());
}
lesson.setLessonSolutionFileName(plugin.getLessonPlans().get("en").toString());
lesson.setSourceFileName(plugin.getLessonSource().toString());
} catch (Exception e) {
logger.error("Error in loadLessons: ", e);
}
}
}
/**
* For each lesson, set the source file and lesson file
*/
private void loadResourcesFromPlugin() {
for (AbstractLesson lesson : lessons) {
logger.info("Loading resources for lesson -> " + lesson.getName());
String className = lesson.getClass().getName();
String classFile = getSourceFile(className);
logger.info("Lesson classname: " + className);
logger.info("Lesson java file: " + classFile);
for (String absoluteFile : files) {
String fileName = getFileName(absoluteFile);
//logger.debug("Course: looking at file: " + absoluteFile);
if (absoluteFile.endsWith(classFile)) {
logger.info("Set source file for " + classFile);
lesson.setSourceFileName(absoluteFile);
}
if (absoluteFile.startsWith("/lesson_plans") && absoluteFile.endsWith(".html") && className
.endsWith(fileName)) {
logger.info(
"setting lesson plan file " + absoluteFile + " for lesson " + lesson.getClass().getName());
logger.info("fileName: " + fileName + " == className: " + className);
String language = getLanguageFromFileName("/lesson_plans", absoluteFile);
lesson.setLessonPlanFileName(language, absoluteFile);
}
if (absoluteFile.startsWith("/lesson_solutions") && absoluteFile.endsWith(".html") && className
.endsWith(fileName)) {
logger.info(
"setting lesson solution file " + absoluteFile + " for lesson " + lesson.getClass().getName());
logger.info("fileName: " + fileName + " == className: " + className);
lesson.setLessonSolutionFileName(absoluteFile);
}
}
}
}
/**
* Instantiate all the lesson objects into a cache
*
@ -465,9 +428,9 @@ public class Course {
logger.info("Loading courses: " + path);
this.webgoatContext = webgoatContext;
loadLessionFromPlugin(context);
loadFiles(context, path);
loadLessons(path);
loadResources();
//loadFiles(context, path);
//loadLessons(path);
//loadResources();
}
}