Skip to content

Commit 8794845

Browse files
committed
decoupling knowledge of monitor from compiler
1 parent e5cb2d8 commit 8794845

11 files changed

Lines changed: 72 additions & 33 deletions

File tree

‎koans/app/lib/file-compiler.jar‎

-684 Bytes
Binary file not shown.

‎koans/app/lib/file-monitor.jar‎

-2.66 KB
Binary file not shown.

‎koans/app/lib/koans.jar‎

-157 Bytes
Binary file not shown.

‎lib/file-compiler/.classpath‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
33
<classpathentry kind="src" path="src"/>
4-
<classpathentry combineaccessrules="false" kind="src" path="/file-monitor"/>
54
<classpathentry combineaccessrules="false" kind="src" path="/util"/>
65
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
76
<classpathentry kind="output" path="bin"/>

‎lib/file-compiler/src/com/sandwich/util/io/DynamicClassLoader.java‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,37 @@
1010
import java.util.Map;
1111
import java.util.Map.Entry;
1212

13-
public class DynamicClassLoader extends ClassLoader {
13+
public abstract class DynamicClassLoader extends ClassLoader {
1414

1515
private static Map<URL, Class<?>> classesByLocation = new HashMap<URL, Class<?>>();
1616
private static Map<Class<?>, URL> locationByClass = new HashMap<Class<?>, URL>();
1717

1818
private final long timeout;
19-
private final FileMonitor fileMonitor;
2019
private final String binDir, sourceDir;
2120
private final String[] classPath;
2221

23-
public DynamicClassLoader(String binDir, String sourceDir, String[] classPath, FileMonitor fileMonitor){
24-
this(binDir, sourceDir, classPath, fileMonitor, ClassLoader.getSystemClassLoader());
22+
public DynamicClassLoader(String binDir, String sourceDir, String[] classPath){
23+
this(binDir, sourceDir, classPath, ClassLoader.getSystemClassLoader());
2524
}
2625

27-
public DynamicClassLoader(String binDir, String sourceDir, String[] classPath,
28-
FileMonitor fileMonitor, ClassLoader parent) {
29-
this(binDir, sourceDir, classPath, fileMonitor, parent, 5000l);
26+
public DynamicClassLoader(String binDir, String sourceDir, String[] classPath, ClassLoader parent) {
27+
this(binDir, sourceDir, classPath, parent, 5000l);
3028
}
3129

3230
public DynamicClassLoader(String binDir, String sourceDir,
33-
String[] classPath, FileMonitor fileMonitor, ClassLoader parent,
31+
String[] classPath, ClassLoader parent,
3432
long timeout) {
3533
super(parent);
36-
this.fileMonitor = fileMonitor;
3734
this.binDir = binDir;
3835
this.sourceDir = sourceDir;
3936
this.classPath = classPath;
4037
this.timeout = timeout;
4138
}
4239

40+
public abstract boolean isFileModifiedSinceLastPoll(String sourcePath, long lastModified);
41+
42+
public abstract void updateFileSavedTime(File sourceFile);
43+
4344
public static void remove(URL url){
4445
String urlToString = url.toString().replace(FileCompiler.CLASS_SUFFIX, "").replace(FileCompiler.JAVA_SUFFIX, "");
4546
for(Entry<URL, Class<?>> entry : classesByLocation.entrySet()){
@@ -75,7 +76,7 @@ public Class<?> loadClass(String className, CompilationListener listener){
7576
if(classFile.exists()){
7677
String absolutePath = classFile.getAbsolutePath();
7778
boolean isAnonymous = absolutePath.contains("$");
78-
if(fileMonitor.isFileModifiedSinceLastPoll(sourceFile.getAbsolutePath(), sourceFile.lastModified())){
79+
if(isFileModifiedSinceLastPoll(sourceFile.getAbsolutePath(), sourceFile.lastModified())){
7980
if(!isAnonymous){
8081
compile(className, fileName, sourceFile, timeout, listener);
8182
}
@@ -97,7 +98,7 @@ public Class<?> loadClass(String className, CompilationListener listener){
9798
private void compile(String className, String fileName, File sourceFile, long timeout, CompilationListener listener)
9899
throws IOException {
99100
FileCompiler.compile(sourceFile, new File(binDir), listener, timeout, classPath);
100-
fileMonitor.updateFileSaveTime(sourceFile);
101+
updateFileSavedTime(sourceFile);
101102
}
102103

103104
public Class<?> loadClass(URL url, String className){
@@ -126,9 +127,4 @@ public Class<?> loadClass(URL url, String className){
126127
return clazz;
127128
}
128129

129-
@Override
130-
public DynamicClassLoader clone(){
131-
return new DynamicClassLoader(binDir, sourceDir, classPath, fileMonitor,
132-
getClass().getClassLoader(), timeout);
133-
}
134130
}

‎lib/file-monitor/.classpath‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
55
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6+
<classpathentry combineaccessrules="false" kind="src" path="/util"/>
67
<classpathentry kind="output" path="bin"/>
78
</classpath>

‎lib/koans-lib/src/com/sandwich/koan/KoanClassLoader.java‎

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,29 @@
77
import com.sandwich.util.io.FileMonitorFactory;
88
import com.sandwich.util.io.directories.DirectoryManager;
99

10-
public class KoanClassLoader extends ClassLoader {
10+
public class KoanClassLoader extends DynamicClassLoader {
1111

1212
private static DynamicClassLoader instance;
13+
private FileMonitor fileMonitor;
14+
15+
private KoanClassLoader(){
16+
super(DirectoryManager.getBinDir(),
17+
DirectoryManager.getSourceDir(),
18+
buildClassPath(),
19+
DynamicClassLoader.class.getClassLoader(),
20+
ApplicationSettings.getFileCompilationTimeoutInMs());
21+
this.fileMonitor = FileMonitorFactory.getInstance(new File(DirectoryManager.getMainDir()), new File(DirectoryManager.getDataFile()));
22+
}
1323

14-
public static DynamicClassLoader createInstance() {
15-
String binDir = DirectoryManager.getBinDir();
16-
String sourceDir = DirectoryManager.getSourceDir();
24+
private static String[] buildClassPath() {
1725
File[] jars = new File(DirectoryManager.getProjectLibraryDir()).listFiles();
1826
String[] classPath = new String[jars.length];
1927
for (int i = 0; i < jars.length; i++) {
20-
classPath[i] = jars[i].getAbsolutePath();
28+
if(jars[i].getAbsolutePath().toLowerCase().endsWith(".jar")){
29+
classPath[i] = jars[i].getAbsolutePath();
30+
}
2131
}
22-
FileMonitor fileMonitor = FileMonitorFactory.getInstance(
23-
new File(DirectoryManager.getMainDir()), new File(DirectoryManager.getDataFile()));
24-
ClassLoader classLoader = DynamicClassLoader.class.getClassLoader();
25-
long timeout = ApplicationSettings.getFileCompilationTimeoutInMs();
26-
return new DynamicClassLoader(binDir, sourceDir, classPath, fileMonitor, classLoader, timeout);
32+
return classPath;
2733
}
2834

2935
public static void setInstance(DynamicClassLoader loader){
@@ -32,9 +38,19 @@ public static void setInstance(DynamicClassLoader loader){
3238

3339
synchronized public static DynamicClassLoader getInstance(){
3440
if(instance == null){
35-
instance = createInstance();
41+
instance = new KoanClassLoader();
3642
}
37-
return instance.clone();
43+
return instance;
44+
}
45+
46+
@Override
47+
public boolean isFileModifiedSinceLastPoll(String sourcePath, long lastModified) {
48+
return fileMonitor.isFileModifiedSinceLastPoll(sourcePath, lastModified);
49+
}
50+
51+
@Override
52+
public void updateFileSavedTime(File sourceFile) {
53+
fileMonitor.updateFileSaveTime(sourceFile);
3854
}
3955

4056
}

‎lib/koans-tests/test/com/sandwich/koan/path/CommandLineTestCase.java‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.sandwich.koan.path;
22
import java.io.ByteArrayOutputStream;
33
import java.io.PrintStream;
4+
import java.lang.reflect.Constructor;
45
import java.lang.reflect.Field;
56
import java.lang.reflect.InvocationTargetException;
67
import java.lang.reflect.Method;
@@ -43,7 +44,7 @@ public abstract class CommandLineTestCase {
4344
@Before
4445
public void setUp() throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, InvocationTargetException {
4546
DirectoryManager.setDirectorySet(new UnitTestDirectories());
46-
KoanClassLoader.setInstance(KoanClassLoader.createInstance());
47+
resetClassLoader();
4748
out = System.out;
4849
err = System.err;
4950
TestUtils.setValue("behavior", new RunKoans(), ArgumentType.RUN_KOANS);
@@ -59,7 +60,7 @@ public void tearDown() throws IllegalArgumentException, IllegalAccessException,
5960
setRealPath();
6061
System.setOut(out);
6162
System.setErr(err);
62-
KoanClassLoader.setInstance(KoanClassLoader.createInstance());
63+
resetClassLoader();
6364
}
6465

6566
protected void setRealPath(){
@@ -202,4 +203,31 @@ public void run(String s){
202203
throw new KoanIncompleteException(lineText+" was expected, but not found in: "+bytesString);
203204
}
204205
}
206+
207+
public void resetClassLoader() {
208+
Constructor<KoanClassLoader> constructor = null;
209+
boolean accessible = false;
210+
try {
211+
constructor = KoanClassLoader.class.getDeclaredConstructor();
212+
accessible = constructor.isAccessible();
213+
constructor.setAccessible(true);
214+
KoanClassLoader.setInstance(constructor.newInstance());
215+
} catch (InstantiationException e) {
216+
throw new RuntimeException(e);
217+
} catch (IllegalAccessException e) {
218+
throw new RuntimeException(e);
219+
} catch (IllegalArgumentException e) {
220+
throw new RuntimeException(e);
221+
} catch (InvocationTargetException e) {
222+
throw new RuntimeException(e);
223+
} catch (NoSuchMethodException e) {
224+
throw new RuntimeException(e);
225+
} catch (SecurityException e) {
226+
throw new RuntimeException(e);
227+
} finally {
228+
if(constructor != null){
229+
constructor.setAccessible(accessible);
230+
}
231+
}
232+
}
205233
}

‎lib/koans-tests/test/com/sandwich/koan/runner/AppReadinessForDeploymentTest.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.junit.Before;
1818
import org.junit.Test;
1919

20-
import com.sandwich.koan.KoanClassLoader;
2120
import com.sandwich.koan.KoanMethod;
2221
import com.sandwich.koan.cmdline.CommandLineArgumentBuilder;
2322
import com.sandwich.koan.cmdline.CommandLineArgumentRunner;
@@ -145,7 +144,7 @@ public void run(){
145144

146145
private void doAsIfInProd(Runnable runnable) {
147146
DirectoryManager.setDirectorySet(new ProductionExecutedFromTestsDirectories());
148-
KoanClassLoader.setInstance(KoanClassLoader.createInstance());
147+
resetClassLoader();
149148
setRealPath();
150149
runnable.run();
151150
}
File renamed without changes.

0 commit comments

Comments
 (0)