Skip to content

Commit 381a50a

Browse files
committed
last commit introduced bug w/ classloading
1 parent bcb146c commit 381a50a

8 files changed

Lines changed: 65 additions & 29 deletions

File tree

koans/app/lib/file-compiler.jar

91 Bytes
Binary file not shown.

koans/app/lib/file-monitor.jar

58 Bytes
Binary file not shown.

koans/app/lib/koans.jar

252 Bytes
Binary file not shown.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,21 @@ public Class<?> loadClass(URL url, String className){
126126
locationByClass.put(clazz, url);
127127
return clazz;
128128
}
129+
130+
public long getTimeout() {
131+
return timeout;
132+
}
133+
134+
public String getBinDir() {
135+
return binDir;
136+
}
137+
138+
public String getSourceDir() {
139+
return sourceDir;
140+
}
141+
142+
public String[] getClassPath() {
143+
return classPath;
144+
}
129145

130146
}

lib/file-monitor/src/com/sandwich/util/io/FileMonitorFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void run() {
1919
monitors.clear();
2020
}
2121
}));
22-
new Thread(new Runnable(){
22+
Thread pollingThread = new Thread(new Runnable(){
2323
public void run() {
2424
do{
2525
try {
@@ -35,7 +35,9 @@ public void run() {
3535
}
3636
}while(true);
3737
}
38-
}).start();
38+
});
39+
pollingThread.setName("FileMonitorPolling");
40+
pollingThread.start();
3941
}
4042

4143
public static FileMonitor getInstance(File monitoredFile, File dataFile) {

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

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,33 @@
99

1010
public class KoanClassLoader extends DynamicClassLoader {
1111

12-
private static DynamicClassLoader instance;
13-
private FileMonitor fileMonitor;
12+
private static KoanClassLoader instance;
13+
private final FileMonitor fileMonitor;
1414

1515
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()));
16+
this(DirectoryManager.getBinDir(),
17+
DirectoryManager.getSourceDir(),
18+
buildClassPath(),
19+
DynamicClassLoader.class.getClassLoader(),
20+
ApplicationSettings.getFileCompilationTimeoutInMs(),
21+
FileMonitorFactory.getInstance(new File(DirectoryManager.getMainDir()), new File(DirectoryManager.getDataFile())));
22+
}
23+
24+
private KoanClassLoader(String binDir, String sourceDir,
25+
String[] classPath, ClassLoader parent,
26+
long timeout, FileMonitor fileMonitor) {
27+
super(binDir, sourceDir, classPath, parent, timeout);
28+
this.fileMonitor = fileMonitor;
29+
}
30+
31+
@Override
32+
public boolean isFileModifiedSinceLastPoll(String sourcePath, long lastModified) {
33+
return fileMonitor.isFileModifiedSinceLastPoll(sourcePath, lastModified);
34+
}
35+
36+
@Override
37+
public void updateFileSavedTime(File sourceFile) {
38+
fileMonitor.updateFileSaveTime(sourceFile);
2239
}
2340

2441
private static String[] buildClassPath() {
@@ -32,25 +49,16 @@ private static String[] buildClassPath() {
3249
return classPath;
3350
}
3451

35-
public static void setInstance(DynamicClassLoader loader){
36-
instance = loader;
37-
}
38-
3952
synchronized public static DynamicClassLoader getInstance(){
4053
if(instance == null){
4154
instance = new KoanClassLoader();
4255
}
43-
return instance;
44-
}
45-
46-
@Override
47-
public boolean isFileModifiedSinceLastPoll(String sourcePath, long lastModified) {
48-
return fileMonitor.isFileModifiedSinceLastPoll(sourcePath, lastModified);
56+
return instance.clone();
4957
}
5058

5159
@Override
52-
public void updateFileSavedTime(File sourceFile) {
53-
fileMonitor.updateFileSaveTime(sourceFile);
60+
public KoanClassLoader clone(){
61+
return new KoanClassLoader(getBinDir(), getSourceDir(), getClassPath(), getClass().getClassLoader(), getTimeout(), fileMonitor);
5462
}
5563

5664
}

lib/koans-lib/src/com/sandwich/koan/runner/AppLauncher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
public class AppLauncher {
1818

1919
public static void main(final String... args) throws Throwable {
20-
new Thread(new Runnable(){
20+
new Thread(new Runnable(){
2121
public void run() {
2222
do{
2323
try {

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,17 @@ public void run(String s){
206206

207207
public void resetClassLoader() {
208208
Constructor<KoanClassLoader> constructor = null;
209-
boolean accessible = false;
209+
Field fileMonitorField = null;
210+
boolean consWasAccessible = false;
211+
boolean monitorWasAccessible = false;
210212
try {
211213
constructor = KoanClassLoader.class.getDeclaredConstructor();
212-
accessible = constructor.isAccessible();
214+
consWasAccessible = constructor.isAccessible();
213215
constructor.setAccessible(true);
214-
KoanClassLoader.setInstance(constructor.newInstance());
215-
} catch (InstantiationException e) {
216-
throw new RuntimeException(e);
216+
fileMonitorField = KoanClassLoader.class.getDeclaredField("instance");
217+
monitorWasAccessible = fileMonitorField.isAccessible();
218+
fileMonitorField.setAccessible(true);
219+
fileMonitorField.set(KoanClassLoader.class, constructor.newInstance());
217220
} catch (IllegalAccessException e) {
218221
throw new RuntimeException(e);
219222
} catch (IllegalArgumentException e) {
@@ -224,9 +227,16 @@ public void resetClassLoader() {
224227
throw new RuntimeException(e);
225228
} catch (SecurityException e) {
226229
throw new RuntimeException(e);
230+
} catch (NoSuchFieldException e) {
231+
throw new RuntimeException(e);
232+
} catch (InstantiationException e) {
233+
throw new RuntimeException(e);
227234
} finally {
228235
if(constructor != null){
229-
constructor.setAccessible(accessible);
236+
constructor.setAccessible(consWasAccessible);
237+
}
238+
if(fileMonitorField != null){
239+
fileMonitorField.setAccessible(monitorWasAccessible);
230240
}
231241
}
232242
}

0 commit comments

Comments
 (0)