/*
Dateiverwaltung - File management in your browser
Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
package de.uhilger.filecms.api;
import static de.uhilger.filecms.api.FileMgr.HOME_DIR_NAME;
import static de.uhilger.filecms.api.FileMgr.HOME_DIR_PATH;
import static de.uhilger.filecms.api.FileMgr.PUB_DIR_NAME;
import static de.uhilger.filecms.api.FileMgr.PUB_DIR_PATH;
import de.uhilger.filecms.data.CompilerIssue;
import de.uhilger.filecms.data.FileRef;
import de.uhilger.filecms.web.Initialiser;
import de.uhilger.transit.web.RequestKontext;
import de.uhilger.transit.web.WebKontext;
import java.io.File;
import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
/**
*
*/
public class CompileService implements RequestKontext, WebKontext {
private static final Logger logger = Logger.getLogger(CompileService.class.getName());
private ServletContext ctx;
private HttpServletRequest request;
public List compile(String relPath, List fileNames) throws IOException {
//Files[] files1 = ... ; // input for first compilation task
//Files[] files2 = ... ; // input for second compilation task
File targetDir = getTargetDir(relPath);
//System.out.println(targetDir.getAbsolutePath());
ArrayList files = new ArrayList();
for(int i=0; i < fileNames.size(); i++) {
Object o = fileNames.get(i);
if(o instanceof ArrayList) {
ArrayList al = (ArrayList) o;
logger.fine(al.get(0).toString());
File targetFile = new File(targetDir, al.get(0).toString());
logger.fine(targetFile.getAbsolutePath());
files.add(targetFile);
}
}
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector diagnostics = new DiagnosticCollector();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
Iterable extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(files);
/*
final Iterable options =
Arrays.asList(new String[]{"-Xlint",
"-cp", project.getClassPath(),
"-d", targetDir.getAbsolutePath()
});
compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits1).call();
*/
compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits1).call();
/*
Iterable extends JavaFileObject> compilationUnits2
= fileManager.getJavaFileObjects(files2); // use alternative method
// reuse the same file manager to allow caching of jar files
compiler.getTask(null, fileManager, null, null, null, compilationUnits2).call();
*/
fileManager.close();
List compileResults = diagnostics.getDiagnostics();
List compilerIssues = new ArrayList();
Iterator i = compileResults.iterator();
while(i.hasNext()) {
Object o = i.next();
Diagnostic extends JavaFileObject> err;
if(o instanceof Diagnostic) {
err = (Diagnostic) o;
CompilerIssue issue = new CompilerIssue();
issue.setKind(err.getKind().name());
issue.setLineNumber(err.getLineNumber());
issue.setMessage(err.getMessage(Locale.GERMANY));
issue.setSoureName(err.getSource().getName());
compilerIssues.add(issue);
}
}
return compilerIssues;
}
private File getTargetDir(String relPath) {
logger.fine(relPath);
String targetPath = null;
if(relPath.startsWith(PUB_DIR_NAME)) {
targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
} else if(relPath.startsWith(HOME_DIR_NAME)) {
targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length());
} else {
// kann eigentlich nicht sein..
}
logger.fine(targetPath);
File targetDir = new File(getBase().getAbsolutePath(), targetPath);
return targetDir;
}
private FileRef getBase() {
FileRef base = null;
Object o = getServletContext().getAttribute(Initialiser.FILE_BASE);
if(o instanceof String) {
String baseStr = (String) o;
logger.fine(baseStr);
File file = new File(baseStr);
base = new FileRef(file.getAbsolutePath(), file.isDirectory());
}
return base;
}
private String getUserName() {
String userName = null;
Object p = getRequest().getUserPrincipal();
if(p instanceof Principal) {
userName = ((Principal) p).getName();
}
return userName;
}
@Override
public HttpServletRequest getRequest() {
return request;
}
@Override
public void setRequest(HttpServletRequest r) {
this.request = r;
}
@Override
public ServletContext getServletContext() {
return ctx;
}
@Override
public void setServletContext(ServletContext servletContext) {
this.ctx = servletContext;
}
}
/*
Beispeil fuer einen dynamischen Compiler-Aufruf
'in memory'
String className = "mypackage.MyClass";
String javaCode = "package mypackage;\n" +
"public class MyClass implements Runnable {\n" +
" public void run() {\n" +
" System.out.println("\"Hello World\");\n" +
" }\n" +
"}\n";
Class aClass = CompilerUtils.CACHED_COMPILER.loadFromJava(className, javaCode);
Runnable runner = (Runnable) aClass.newInstance();
runner.run();
*/
/*
CodeMirror Breakpoint bzw. Gutter Marker
https://codemirror.net/demo/marker.html
*/