Skip to content

Commit cf97631

Browse files
committed
making engine support other jvm languages
1 parent 381a50a commit cf97631

19 files changed

Lines changed: 142 additions & 95 deletions

File tree

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.sandwich.util.io;
1+
package com.sandwich.util.io.classloader;
22

33
import java.io.ByteArrayOutputStream;
44
import java.io.File;
@@ -10,6 +10,11 @@
1010
import java.util.Map;
1111
import java.util.Map.Entry;
1212

13+
import com.sandwich.util.io.filecompiler.CompilationListener;
14+
import com.sandwich.util.io.filecompiler.CompilerConfig;
15+
import com.sandwich.util.io.filecompiler.FileCompiler;
16+
import com.sandwich.util.io.filecompiler.FileCompilerAction;
17+
1318
public abstract class DynamicClassLoader extends ClassLoader {
1419

1520
private static Map<URL, Class<?>> classesByLocation = new HashMap<URL, Class<?>>();
@@ -42,7 +47,10 @@ public DynamicClassLoader(String binDir, String sourceDir,
4247
public abstract void updateFileSavedTime(File sourceFile);
4348

4449
public static void remove(URL url){
45-
String urlToString = url.toString().replace(FileCompiler.CLASS_SUFFIX, "").replace(FileCompiler.JAVA_SUFFIX, "");
50+
String urlToString = url.toString().replace(FileCompiler.CLASS_SUFFIX, "");
51+
for(String suffix : CompilerConfig.getSupportedFileSuffixes()){
52+
urlToString.replace(suffix, "");
53+
}
4654
for(Entry<URL, Class<?>> entry : classesByLocation.entrySet()){
4755
if(entry.getKey().toString().contains(urlToString)){
4856
locationByClass.remove(entry.getValue());
@@ -78,15 +86,15 @@ public Class<?> loadClass(String className, CompilationListener listener){
7886
boolean isAnonymous = absolutePath.contains("$");
7987
if(isFileModifiedSinceLastPoll(sourceFile.getAbsolutePath(), sourceFile.lastModified())){
8088
if(!isAnonymous){
81-
compile(className, fileName, sourceFile, timeout, listener);
89+
compile(fileName, sourceFile, timeout, listener);
8290
}
8391
}
8492
return loadClass(classFile.toURI().toURL(), className);
8593
}
8694
try{
8795
return super.loadClass(className);
8896
}catch(ClassNotFoundException x){
89-
compile(className, fileName, sourceFile, timeout, listener);
97+
compile(fileName, sourceFile, timeout, listener);
9098
classFile = new File(fileName);
9199
return loadClass(classFile.toURI().toURL(), className);
92100
}
@@ -95,7 +103,7 @@ public Class<?> loadClass(String className, CompilationListener listener){
95103
}
96104
}
97105

98-
private void compile(String className, String fileName, File sourceFile, long timeout, CompilationListener listener)
106+
private void compile(String fileName, File sourceFile, long timeout, CompilationListener listener)
99107
throws IOException {
100108
FileCompiler.compile(sourceFile, new File(binDir), listener, timeout, classPath);
101109
updateFileSavedTime(sourceFile);

lib/file-compiler/src/com/sandwich/util/io/CompilationFailureLogger.java renamed to lib/file-compiler/src/com/sandwich/util/io/filecompiler/CompilationFailureLogger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.sandwich.util.io;
1+
package com.sandwich.util.io.filecompiler;
22

33
import java.io.File;
44

lib/file-compiler/src/com/sandwich/util/io/CompilationListener.java renamed to lib/file-compiler/src/com/sandwich/util/io/filecompiler/CompilationListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.sandwich.util.io;
1+
package com.sandwich.util.io.filecompiler;
22

33
import java.io.File;
44

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.sandwich.util.io.filecompiler;
2+
3+
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.Collection;
7+
import java.util.List;
8+
import java.util.ResourceBundle;
9+
10+
public class CompilerConfig {
11+
12+
private static final ResourceBundle commandBySuffixRB = ResourceBundle.getBundle("com.sandwich.util.io.filecompiler.compilationcommands");
13+
14+
public static boolean isSourceFile(String fileName) {
15+
return commandBySuffixRB.containsKey(getSuffix(fileName));
16+
}
17+
18+
public static String[] getCompilationCommand(File src, String destinationPath, String classPath) {
19+
String absolutePath = src.getAbsolutePath();
20+
String command = commandBySuffixRB.getString(getSuffix(absolutePath));
21+
if(command == null){
22+
throw new RuntimeException("Do not know how to compile " + absolutePath);
23+
}
24+
List<String> splitCommand = Arrays.asList(command.split(" "));
25+
List<String> commandSegments = new ArrayList<String>();
26+
for(String segment : splitCommand){
27+
String lowerCaseSegment = segment.toLowerCase();
28+
if("${bindir}".equals(lowerCaseSegment)){
29+
commandSegments.add(destinationPath);
30+
}else if("${classpath}".equals(lowerCaseSegment)){
31+
commandSegments.add(classPath);
32+
}else if("${filename}".equals(lowerCaseSegment)){
33+
commandSegments.add(src.getAbsolutePath());
34+
}else{
35+
commandSegments.add(segment);
36+
}
37+
}
38+
return commandSegments.toArray(new String[commandSegments.size()]);
39+
}
40+
41+
public static Collection<String> getSupportedFileSuffixes() {
42+
return commandBySuffixRB.keySet();
43+
}
44+
45+
public static String getSuffix(String fileName) {
46+
if(fileName != null){
47+
int periodIndex = fileName.indexOf('.');
48+
if(periodIndex > -1){
49+
return fileName.substring(periodIndex).toLowerCase();
50+
}
51+
}
52+
return "";
53+
}
54+
55+
public static boolean isSuffixSupported(String suffix) {
56+
return getSupportedFileSuffixes().contains(suffix);
57+
}
58+
59+
}

lib/file-compiler/src/com/sandwich/util/io/FileCompiler.java renamed to lib/file-compiler/src/com/sandwich/util/io/filecompiler/FileCompiler.java

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
package com.sandwich.util.io;
1+
package com.sandwich.util.io.filecompiler;
22

33
import java.io.File;
44
import java.io.FileNotFoundException;
55
import java.io.IOException;
6+
import java.util.HashMap;
7+
import java.util.Map;
68

9+
import com.sandwich.util.io.FileUtils;
710
import com.sandwich.util.io.ui.DefaultErrorPresenter;
811
import com.sandwich.util.io.ui.ErrorPresenter;
912

1013
public class FileCompiler {
1114

15+
private static final Map<String, String> sourceFileToClassFile = new HashMap<String, String>();
16+
private static final Map<String, String> classFileToSourceFile = new HashMap<String, String>();
17+
1218
private static final String DOLLAR_SIGN = "$";
13-
public static final String JAVA_SUFFIX = ".java";
1419
public static final String CLASS_SUFFIX = ".class";
20+
public static final String JAVA_SUFFIX = ".java";
1521

1622
public static void compile(String src, String bin) throws IOException {
1723
compile(new DefaultErrorPresenter(), new File(src), new File(bin));
@@ -40,32 +46,46 @@ public static void compile(File src, File bin,
4046
}
4147
}
4248
FileUtils.forEachFile(src, bin, new FileCompilerAction(bin, listener, timeout, classpath));
49+
String srcPath = src.getAbsolutePath();
50+
String classPath = srcPath;
51+
for(String suffix : CompilerConfig.getSupportedFileSuffixes()){
52+
if(classPath.endsWith(suffix)){
53+
classPath = classPath.replace(suffix, CLASS_SUFFIX);
54+
}
55+
}
56+
sourceFileToClassFile.put(srcPath, classPath);
57+
classFileToSourceFile.put(classPath, srcPath);
4358
}
4459

4560
public static String getContentsOfJavaFile(String sourceDir, String className) {
4661
return FileUtils.readFileAsString(getSourceFileFromClass(sourceDir, className));
4762
}
4863

4964
public static File getSourceFileFromClass(String sourceDir, String className) {
50-
File sourceFile = new File(
51-
sourceDir + System.getProperty("file.separator") +
52-
classNameToJavaFileName(className));
53-
if (!sourceFile.exists()) {
54-
throw new IllegalArgumentException(new FileNotFoundException(
55-
sourceFile.getAbsolutePath() + " does not exist"));
56-
}
57-
return sourceFile;
58-
}
59-
60-
public static String classNameToJavaFileName(String className) {
61-
className = className.replace(".", System.getProperty("file.separator"));
6265
if(className.contains(DOLLAR_SIGN)){
6366
className = className.substring(0, className.indexOf(DOLLAR_SIGN));
6467
}
65-
return className + JAVA_SUFFIX;
68+
File possibleSourceFile = new File(sourceDir);
69+
File sourceFile = null;
70+
for(String folder : className.split("\\.")){
71+
possibleSourceFile = new File(possibleSourceFile, folder);
72+
}
73+
for(String suffix : CompilerConfig.getSupportedFileSuffixes()){
74+
File file = new File(possibleSourceFile.getAbsolutePath() + suffix);
75+
if(file.exists()){
76+
sourceFile = file;
77+
break;
78+
}
79+
}
80+
if (sourceFile == null || !sourceFile.exists()) {
81+
throw new IllegalArgumentException(new FileNotFoundException(
82+
sourceFile == null ? null : sourceFile.getAbsolutePath() + " does not exist"));
83+
}
84+
return sourceFile;
6685
}
6786

6887
public static File sourceToClass(String sourceDir, String binDir, File file) {
88+
//C:\Users\sandwich\Development\koans\koans\app\bin\beginner\AboutKoans.class
6989
return new File(file.getAbsolutePath()
7090
.replace(sourceDir, binDir).replace(JAVA_SUFFIX, CLASS_SUFFIX));
7191
}
@@ -81,6 +101,7 @@ public static File classToClassFile(Class<?> clazz) {
81101
}
82102

83103
public static File classToSource(String binDir, String sourceDir, String absolutePath) {
104+
//C:\Users\sandwich\Development\koans\koans\src\beginner\AboutKoans.java
84105
return new File(absolutePath
85106
.replace(binDir, sourceDir).replace(CLASS_SUFFIX, JAVA_SUFFIX));
86107
}

lib/file-compiler/src/com/sandwich/util/io/FileCompilerAction.java renamed to lib/file-compiler/src/com/sandwich/util/io/filecompiler/FileCompilerAction.java

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.sandwich.util.io;
1+
package com.sandwich.util.io.filecompiler;
22

33
import java.io.File;
44
import java.io.IOException;
@@ -9,14 +9,16 @@
99
import java.util.concurrent.TimeUnit;
1010

1111
import com.sandwich.util.ExceptionUtils;
12+
import com.sandwich.util.io.FileAction;
13+
import com.sandwich.util.io.StreamUtils;
1214

13-
class FileCompilerAction implements FileAction {
15+
public class FileCompilerAction implements FileAction {
1416

1517
private final String destinationPath;
1618
private final String[] classPaths;
1719
private CompilationListener compilationListener;
1820
private final long timeout;
19-
static final CompilationListener LOGGING_HANDLER = new CompilationFailureLogger();
21+
public static final CompilationListener LOGGING_HANDLER = new CompilationFailureLogger();
2022

2123
public FileCompilerAction(File destinationPath, CompilationListener errorHandler, String...classPaths){
2224
this(destinationPath, errorHandler, 1000, classPaths);
@@ -35,8 +37,8 @@ public FileCompilerAction(File destinationPath,
3537

3638
public void sourceToDestination(File src, File bin) throws IOException {
3739
String fileName = src.getName();
38-
if (fileName.length() > 4 && fileName.toLowerCase().endsWith(FileCompiler.JAVA_SUFFIX)) {
39-
String[] command = constructJavaCompilationCommand(src);
40+
if (CompilerConfig.isSourceFile(fileName)) {
41+
String[] command = CompilerConfig.getCompilationCommand(src, destinationPath, getClasspath());
4042
Process p = Runtime.getRuntime().exec(command);
4143
try {
4244
executeWithTimeout(src, command, p, timeout);
@@ -82,52 +84,12 @@ public Object call() {
8284
}
8385
}
8486

85-
private String[] constructJavaCompilationCommand(File src) {
86-
return copy(new String[]{"javac", "-d", destinationPath}, getClasspath(), new String[]{src.getAbsolutePath()});
87-
}
88-
89-
private String[] copy(String[]...strings) {
90-
String[] copies = new String[getTotalSize(strings)];
91-
int i = 0;
92-
for(String[] strings2 : strings){
93-
for(String string : strings2){
94-
copies[i++] = string;
95-
}
96-
}
97-
return copies;
98-
}
99-
100-
private int getTotalSize(String[][] strings) {
101-
int i = 0;
102-
for(String[] strings2 : strings){
103-
i += strings2.length;
104-
}
105-
return i;
106-
}
107-
108-
private String[] getClasspath() {
109-
String[] classpaths = new String[classPaths.length + 1];
110-
if (classPaths.length > 0) {
111-
classpaths[0] = "-classpath";
112-
}else{
113-
return new String[]{};
114-
}
115-
116-
String classpath = "";
87+
private String getClasspath() {
88+
String classPath = "";
11789
for(String jar : classPaths) {
118-
classpath += jar + java.io.File.pathSeparatorChar;
90+
classPath += jar + java.io.File.pathSeparatorChar;
11991
}
120-
return new String[] {"-classpath", classpath};
121-
}
122-
123-
public File makeDestination(File dest, String fileInDirectory) {
124-
String fileName = dest.getName();
125-
fileName = fileName.length() > 4
126-
&& fileName.toLowerCase().contains(FileCompiler.JAVA_SUFFIX) ? fileName
127-
.replace(FileCompiler.JAVA_SUFFIX, FileCompiler.CLASS_SUFFIX) : fileName;
128-
dest = new File(dest, fileInDirectory);
129-
System.out.println("file: " + dest.getAbsolutePath());
130-
return dest;
92+
return classPath;
13193
}
13294

13395
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.groovy=groovyc -d ${bindir} -classpath ${classpath} ${filename}
2+
.java=javac -d ${bindir} -classpath ${classpath} ${filename}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ synchronized void notifyListeners(){
6565
Map<String, Long> getFilesystemHashes() throws IOException {
6666
final HashMap<String,Long> fileHashes = new HashMap<String,Long>();
6767
FileUtils.forEachFile(fileSystemPath, fileSystemPath, new FileAction(){
68-
public File makeDestination(File dest, String fileInDirectory) {
69-
return new File(dest, fileInDirectory);
70-
}
7168
public void sourceToDestination(File src, File dest) throws IOException {
7269
fileHashes.put(src.getAbsolutePath(), src.lastModified());
7370
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private static boolean isEqual(String value, Object e2, boolean ignoreCase){
5050
private static ResourceBundle getConfigBundle(){
5151
if(CONFIG_BUNDLE == null){
5252
try {
53-
CONFIG_BUNDLE= new PropertyResourceBundle(new FileInputStream(
53+
CONFIG_BUNDLE = new PropertyResourceBundle(new FileInputStream(
5454
DirectoryManager.injectFileSystemSeparators(
5555
DirectoryManager.getConfigDir(), "config.properties")));
5656
} catch (Exception e) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import java.io.File;
44

5-
import com.sandwich.util.io.DynamicClassLoader;
65
import com.sandwich.util.io.FileMonitor;
76
import com.sandwich.util.io.FileMonitorFactory;
7+
import com.sandwich.util.io.classloader.DynamicClassLoader;
88
import com.sandwich.util.io.directories.DirectoryManager;
99

1010
public class KoanClassLoader extends DynamicClassLoader {

0 commit comments

Comments
 (0)