aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/e1/pdj/GenericCompiler.java
blob: 31278b79490bf7bff88050eca8713f0d519e123e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.e1.pdj;

import java.io.*;

import com.cycling74.max.MaxSystem;

abstract class GenericCompiler {
	File javaFile;
	String cp[];
	String compilerName;
	
	static String rtJar;
	
	GenericCompiler(String name) { 
		compilerName = name + ": ";
	}
	
	String getConfigurationClassPath() {
		return PDJClassLoader.getConfigurationClassPath();
	}
	
	File resolvJavaFile() {
		int base = PDJClassLoader.fclasses.toString().length() + 1;
		String className = javaFile.toString().substring(base);
		return new File(className);
	}
	
	private int doexec(String arg) throws Exception {
		Process p = Runtime.getRuntime().exec(arg, null, PDJClassLoader.fclasses);
		
		BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
		BufferedReader stderr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
		String buff;
		boolean cont = true;
		while(cont) {
			cont = false;
			
			buff = stdout.readLine();
			if ( buff != null ) {
				MaxSystem.post(compilerName + buff);
				cont = true;
			} else {
				cont = false; 
			}
		
			buff = stderr.readLine();
			if ( buff != null ) {
				MaxSystem.post(compilerName + buff);
				cont = true;
			} else {
				cont = cont & false;
			}
		}
		
		p.waitFor();
		
		return p.exitValue();
	}
	
	int exec(String arg) throws PDJClassLoaderException {
		MaxSystem.post("pdj: trying to compile class: " + resolvJavaFile().toString());
		
		try {
			return doexec(arg);
		} catch (Exception e) {
			throw new PDJClassLoaderException(e);
		}
	}
	
	abstract void compileClass() throws PDJClassLoaderException;
}