提交 7b305d83 编写于 作者: F fanjiefeng

更新编译器

上级 d801df46
......@@ -114,13 +114,13 @@
<option value="lib">lib</option>
</select>
<br>
<span>编译器路径</span>
<input type="text" id="compilerPath" value="" />
<select name="compilerPath" id="compilerPathSelect"
onchange="selected('compilerPath',this)">
<span>日志开关</span>
<input type="text" id="logSwitch" value="" />
<select name="logSwitch" id="logSwitchSelect"
onchange="selected('logSwitch',this)">
<option value="" selected="selected"></option>
<option value="bin/javac">自带</option>
<option value="javac">系统</option>
<option value="true"></option>
<option value="false"></option>
</select>
<br>
<input type="button" value="更改" onclick="updateServerConfig()" />
......
......@@ -30,11 +30,11 @@ function showServerConfig(text) {
var classPathSelect = document.getElementById("classPathSelect");
classPathSelect.options[0].value = serverConfig.classPath;
classPathSelect.selectIndex = 0;
var compilerPath = document.getElementById("compilerPath");
compilerPath.value = serverConfig.compilerPath;
var compilerPathSelect = document.getElementById("compilerPathSelect");
compilerPathSelect.options[0].value = serverConfig.compilerPath;
compilerPathSelect.selectIndex = 0;
var logSwitchInput = document.getElementById("logSwitch");
logSwitchInput.value = serverConfig.logSwitch;
var logSwitchSelect = document.getElementById("logSwitchSelect");
logSwitchSelect.options[0].value = serverConfig.logSwitch;
logSwitchSelect.selectIndex = 0;
}
function showWebProjects(text) {
......@@ -69,7 +69,7 @@ function updateServerConfig() {
json.receiveTimeOut = document.getElementById("receiveTimeOut").value;
json.port = document.getElementById("port").value;
json.classPath = document.getElementById("classPath").value;
json.compilerPath = document.getElementById("compilerPath").value;
json.logSwitch = document.getElementById("logSwitch").value;
send("POST", "/@ServerConfig", true, JSON.stringify(json), showServerConfig, null);
}
......
......@@ -36,13 +36,13 @@ public class ServerConfig extends HttpServlet {
save.setMember("receiveTimeOut", recv.getMember("receiveTimeOut"));
save.setMember("port", recv.getMember("port"));
save.setMember("classPath", recv.getMember("classPath"));
save.setMember("compilerPath", recv.getMember("compilerPath"));
save.setMember("logSwitch", recv.getMember("logSwitch"));
if(StringTools.writeAsPath(configPath, save.toString(true))) {
hs.setUrlCharset(save.getMember("urlCharset").getValueString());
hs.setCharset(save.getMember("charset").getValueString());
hs.setReceiveTimeOut(Integer.parseInt(save.getMember("receiveTimeOut").getValueString()));
HttpServer.classPath=save.getMember("classPath").getValueString();
HttpServer.compilerPath=save.getMember("compilerPath").getValueString();
HttpServer.logSwitch=Boolean.parseBoolean(save.getMember("logSwitch").getValueString());
resp.sendData(save.toString(), "text/txt");
try {
Thread.sleep(2000);
......
/*
* 文件: CompilerList.json
* 作用: 编译优先级列表
*/
[
{
"name": "本地编译器",
"className": "pub.fjf.tools.LocalCompiler",
"compilerName": "LocalCompiler",
"content": "bin/pub/fjf/tools"
},
{
"name": "系统编译器",
"className": "pub.fjf.tools.SystemCompiler",
"compilerName": "SystemCompiler",
"content": "bin/pub/fjf/tools"
},
{
"name": "外部编译器",
"className": "pub.fjf.tools.ExternalCompiler",
"compilerName": "ExternalCompiler",
"content": "bin/pub/fjf/tools"
}
]
\ No newline at end of file
......@@ -3,7 +3,6 @@
* 作用: 配置Http服务器参数
*/
{
"compilerPath": "javac",
"webProjects": [
{
"removable": false,
......@@ -26,5 +25,6 @@
"urlCharset": "default",
"receiveTimeOut": "24000",
"port": "80",
"classPath": "bin"
"classPath": "bin",
"logSwitch": "false"
}
\ No newline at end of file
无法预览此类型文件
......@@ -31,7 +31,7 @@ public class HttpServer {
private ThreadPoolExecutor executors=null;
private ArrayList<WebProject> webProjects=new ArrayList<WebProject>();//项目列表
public static String classPath="bin";//服务器类路径
public static String compilerPath="javac";//编译器路径
public static boolean logSwitch=true;
/**
* 创建Http服务器
* @param configPath 配置文件路径
......@@ -73,7 +73,7 @@ public class HttpServer {
this.setReceiveTimeOut(Integer.parseInt(json.getJsonVar("this.receiveTimeOut").getValueString()));
this.port=Integer.parseInt(json.getJsonVar("this.port").getValueString());
HttpServer.classPath=json.getJsonVar("this.classPath").getValueString();
HttpServer.compilerPath=json.getJsonVar("this.compilerPath").getValueString();
HttpServer.logSwitch=Boolean.parseBoolean(json.getJsonVar("this.logSwitch").getValueString());
ArrayList<Json> wpList=json.getJsonVar("this.webProjects").getElements();
for(int i=0;i<wpList.size();i++) {
Json wpJson=wpList.get(i);
......
package pub.fjf.tools;
import java.io.IOException;
import java.io.InputStream;
public class ExternalCompiler implements JerryRatCompiler {
private String errorInform;
private String compilerPath;
public ExternalCompiler(){
this("javac");
}
public ExternalCompiler(String compilerPath){
this.compilerPath=compilerPath;
}
@Override
public boolean compiler(String classPath, String javaPath) {
// TODO Auto-generated method stub
boolean result=false;
this.errorInform="";
StringBuffer cmd=new StringBuffer();
cmd.append(compilerPath+" ");
if(classPath.length()>0) {
cmd.append("-cp ");
cmd.append(classPath);
cmd.append(" ");
}
cmd.append(javaPath);
for(int i=0;i<cmd.length();i++) {
if(cmd.charAt(i)=='/') {
cmd.setCharAt(i, '\\');
}
}
Process p=null;
try {
p=Runtime.getRuntime().exec(cmd.toString());
try {
p.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream is=p.getErrorStream();
try {
if(is.available()==0) {
result=true;
}else {
byte[] bytes=new byte[is.available()];
is.read(bytes);
this.errorInform=new String(bytes);
System.err.println(this.errorInform);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
package pub.fjf.tools;
public interface JerryRatCompiler {
/**
* 编译器接口
* @param classPath 类链接目录
* @param javaPath 源码文件路径
* @return 是否编译成功
*/
public boolean compiler(String classPath, String javaPath);
}
package pub.fjf.tools;
import javax.tools.JavaCompiler;
import com.sun.tools.javac.api.JavacTool;
public class LocalCompiler implements JerryRatCompiler {
JavaCompiler javac=null;
public LocalCompiler() throws Exception {
try {
this.javac=JavacTool.create();
}catch(NoClassDefFoundError e) {
throw new Exception();
}
}
@Override
public boolean compiler(String classPath, String javaPath) {
// TODO Auto-generated method stub
boolean result=false;
String[] aguments= {"-cp",classPath,javaPath};
if(javac.run(null, null, null, aguments)==0) {
result=true;
}
return result;
}
}
package pub.fjf.tools;
import javax.tools.JavaCompiler;
import com.sun.tools.javac.api.JavacTool;
//编译器
public class PathCompiler {
private static JerryRatCompiler compiler;
private String rootPath;
private String classPath;
static {
loadCompile("config/CompilerList.json");
if(compiler==null) {
System.out.println("无编译器可用!");
}
}
public static void loadCompile(String configPath) {
Json configJson=new Json(StringTools.readAsPath(configPath));
if(configJson.getTypeCode()==Json.TYPE_ARRAY) {
for(int i=0;i<configJson.getElementCount();i++) {
PathClassLoader pathClassLoader=new PathClassLoader();
Class<?> C=null;
Json compilerJson=configJson.getElement(i);
String name=compilerJson.getMember("name").getValueString();
String className=compilerJson.getMember("className").getValueString();
String compilerName=compilerJson.getMember("compilerName").getValueString();
String content=compilerJson.getMember("content").getValueString();
try {
C=Class.forName(className, true, pathClassLoader);
} catch (ClassNotFoundException e) {}
if(C==null) {
String classPath=content;
if(classPath!=null && classPath.length()>0) {
classPath+="/";
}
classPath+=compilerName+".class";
C=pathClassLoader.readClass(classPath);
}
if(C!=null) {
try {
compiler=(JerryRatCompiler)(C.newInstance());
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("创建 "+name+" 失败!");
}
}else {
System.out.println("加载 "+name+" 失败!");
}
if(compiler!=null) {
System.out.println("创建 "+name+" 成功!");
break;
}
}
}
}
public PathCompiler() {
this("");
}
......@@ -32,21 +74,14 @@ public class PathCompiler {
}
public boolean compile(String javaPath) {
boolean result=false;
StringBuffer path=new StringBuffer();
if(this.rootPath!=null && this.rootPath.length()>0) {
path.append(this.rootPath);
path.append("/");
}
path.append(javaPath);
for(int i=0;i<path.length();i++) {
if(path.charAt(i)=='/') {
path.setCharAt(i, '\\');
if(compiler!=null) {
StringBuffer path=new StringBuffer();
if(this.rootPath!=null && this.rootPath.length()>0) {
path.append(this.rootPath);
path.append("/");
}
}
JavaCompiler javac=JavacTool.create();
String[] aguments= {"-cp",this.classPath,path.toString()};
if(javac.run(null, null, null, aguments)==0) {
result=true;
path.append(javaPath);
result=compiler.compiler(classPath, path.toString());
}
return result;
}
......
package pub.fjf.tools;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class SystemCompiler implements JerryRatCompiler {
JavaCompiler javac=null;
public SystemCompiler() throws Exception {
this.javac=ToolProvider.getSystemJavaCompiler();
if(javac==null) {
throw new Exception();
}
}
@Override
public boolean compiler(String classPath, String javaPath) {
// TODO Auto-generated method stub
boolean result=false;
String[] aguments= {"-cp",classPath,javaPath};
if(javac.run(null, null, null, aguments)==0) {
result=true;
}
return result;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册