commit | author | age
|
0ac262
|
1 |
/*
|
U |
2 |
Dateiverwaltung - File management in your browser
|
|
3 |
Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
|
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify
|
|
6 |
it under the terms of the GNU Affero General Public License as
|
|
7 |
published by the Free Software Foundation, either version 3 of the
|
|
8 |
License, or (at your option) any later version.
|
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful,
|
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
GNU Affero General Public License for more details.
|
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License
|
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17 |
*/
|
|
18 |
package de.uhilger.filecms.api;
|
|
19 |
|
|
20 |
import static de.uhilger.filecms.api.FileMgr.HOME_DIR_NAME;
|
|
21 |
import static de.uhilger.filecms.api.FileMgr.HOME_DIR_PATH;
|
|
22 |
import static de.uhilger.filecms.api.FileMgr.PUB_DIR_NAME;
|
|
23 |
import static de.uhilger.filecms.api.FileMgr.PUB_DIR_PATH;
|
|
24 |
import de.uhilger.filecms.data.FileRef;
|
|
25 |
import de.uhilger.filecms.web.Initialiser;
|
|
26 |
import de.uhilger.transit.web.RequestKontext;
|
|
27 |
import de.uhilger.transit.web.WebKontext;
|
|
28 |
import java.io.File;
|
|
29 |
import java.io.IOException;
|
|
30 |
import java.security.Principal;
|
|
31 |
import java.util.ArrayList;
|
|
32 |
import java.util.Arrays;
|
|
33 |
import java.util.Iterator;
|
|
34 |
import java.util.List;
|
|
35 |
import java.util.Locale;
|
|
36 |
import java.util.logging.Logger;
|
|
37 |
import javax.servlet.ServletContext;
|
|
38 |
import javax.servlet.http.HttpServletRequest;
|
|
39 |
import javax.tools.Diagnostic;
|
|
40 |
import javax.tools.DiagnosticCollector;
|
|
41 |
import javax.tools.JavaCompiler;
|
|
42 |
import javax.tools.JavaFileObject;
|
|
43 |
import javax.tools.StandardJavaFileManager;
|
|
44 |
import javax.tools.ToolProvider;
|
|
45 |
import org.apache.commons.io.FileUtils;
|
|
46 |
|
|
47 |
/**
|
|
48 |
*
|
|
49 |
*/
|
|
50 |
public class CompileService implements RequestKontext, WebKontext {
|
|
51 |
|
|
52 |
private static final Logger logger = Logger.getLogger(CompileService.class.getName());
|
|
53 |
|
|
54 |
private ServletContext ctx;
|
|
55 |
private HttpServletRequest request;
|
|
56 |
|
|
57 |
public String compile(String relPath, List fileNames) throws IOException {
|
|
58 |
//Files[] files1 = ... ; // input for first compilation task
|
|
59 |
//Files[] files2 = ... ; // input for second compilation task
|
|
60 |
|
|
61 |
File targetDir = getTargetDir(relPath);
|
|
62 |
//System.out.println(targetDir.getAbsolutePath());
|
|
63 |
ArrayList<File> files = new ArrayList();
|
|
64 |
|
|
65 |
for(int i=0; i < fileNames.size(); i++) {
|
|
66 |
Object o = fileNames.get(i);
|
|
67 |
if(o instanceof ArrayList) {
|
|
68 |
ArrayList al = (ArrayList) o;
|
|
69 |
logger.fine(al.get(0).toString());
|
|
70 |
File targetFile = new File(targetDir, al.get(0).toString());
|
|
71 |
logger.fine(targetFile.getAbsolutePath());
|
|
72 |
files.add(targetFile);
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
77 |
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector();
|
|
78 |
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
|
|
79 |
|
|
80 |
Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(files);
|
|
81 |
|
|
82 |
final Iterable<String> options =
|
|
83 |
Arrays.asList(new String[]{"-Xlint",
|
|
84 |
/*"-cp", project.getClassPath(),*/
|
|
85 |
"-d", targetDir.getAbsolutePath()
|
|
86 |
});
|
|
87 |
|
|
88 |
compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits1).call();
|
|
89 |
|
|
90 |
/*
|
|
91 |
Iterable<? extends JavaFileObject> compilationUnits2
|
|
92 |
= fileManager.getJavaFileObjects(files2); // use alternative method
|
|
93 |
// reuse the same file manager to allow caching of jar files
|
|
94 |
compiler.getTask(null, fileManager, null, null, null, compilationUnits2).call();
|
|
95 |
*/
|
|
96 |
fileManager.close();
|
|
97 |
|
|
98 |
StringBuilder msg = new StringBuilder();
|
|
99 |
msg.append("Result of compile to Java bytecode (empty means no error):");
|
|
100 |
for (Diagnostic<? extends JavaFileObject> err : diagnostics.getDiagnostics()) {
|
|
101 |
msg.append('\n');
|
|
102 |
msg.append(err.getKind());
|
|
103 |
msg.append(": ");
|
|
104 |
if (err.getSource() != null) {
|
|
105 |
msg.append(err.getSource().getName());
|
|
106 |
}
|
|
107 |
msg.append(':');
|
|
108 |
msg.append(err.getLineNumber());
|
|
109 |
msg.append(": ");
|
|
110 |
msg.append(err.getMessage(Locale.GERMANY));
|
|
111 |
}
|
|
112 |
|
|
113 |
return msg.toString();
|
|
114 |
}
|
|
115 |
private File getTargetDir(String relPath) {
|
|
116 |
logger.fine(relPath);
|
|
117 |
String targetPath = null;
|
|
118 |
if(relPath.startsWith(PUB_DIR_NAME)) {
|
|
119 |
targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
|
|
120 |
} else if(relPath.startsWith(HOME_DIR_NAME)) {
|
|
121 |
targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length());
|
|
122 |
} else {
|
|
123 |
// kann eigentlich nicht sein..
|
|
124 |
}
|
|
125 |
logger.fine(targetPath);
|
|
126 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath);
|
|
127 |
return targetDir;
|
|
128 |
}
|
|
129 |
|
|
130 |
private FileRef getBase() {
|
|
131 |
FileRef base = null;
|
|
132 |
Object o = getServletContext().getAttribute(Initialiser.FILE_BASE);
|
|
133 |
if(o instanceof String) {
|
|
134 |
String baseStr = (String) o;
|
|
135 |
logger.fine(baseStr);
|
|
136 |
File file = new File(baseStr);
|
|
137 |
base = new FileRef(file.getAbsolutePath(), file.isDirectory());
|
|
138 |
}
|
|
139 |
return base;
|
|
140 |
}
|
|
141 |
private String getUserName() {
|
|
142 |
String userName = null;
|
|
143 |
Object p = getRequest().getUserPrincipal();
|
|
144 |
if(p instanceof Principal) {
|
|
145 |
userName = ((Principal) p).getName();
|
|
146 |
}
|
|
147 |
return userName;
|
|
148 |
}
|
|
149 |
|
|
150 |
@Override
|
|
151 |
public HttpServletRequest getRequest() {
|
|
152 |
return request;
|
|
153 |
}
|
|
154 |
|
|
155 |
@Override
|
|
156 |
public void setRequest(HttpServletRequest r) {
|
|
157 |
this.request = r;
|
|
158 |
}
|
|
159 |
|
|
160 |
@Override
|
|
161 |
public ServletContext getServletContext() {
|
|
162 |
return ctx;
|
|
163 |
}
|
|
164 |
|
|
165 |
@Override
|
|
166 |
public void setServletContext(ServletContext servletContext) {
|
|
167 |
this.ctx = servletContext;
|
|
168 |
}
|
|
169 |
}
|
|
170 |
|
|
171 |
|
|
172 |
/*
|
|
173 |
|
|
174 |
Beispeil fuer einen dynamischen Compiler-Aufruf
|
|
175 |
'in memory'
|
|
176 |
|
|
177 |
String className = "mypackage.MyClass";
|
|
178 |
String javaCode = "package mypackage;\n" +
|
|
179 |
"public class MyClass implements Runnable {\n" +
|
|
180 |
" public void run() {\n" +
|
|
181 |
" System.out.println("\"Hello World\");\n" +
|
|
182 |
" }\n" +
|
|
183 |
"}\n";
|
|
184 |
Class aClass = CompilerUtils.CACHED_COMPILER.loadFromJava(className, javaCode);
|
|
185 |
Runnable runner = (Runnable) aClass.newInstance();
|
|
186 |
runner.run();
|
|
187 |
|
d920b7
|
188 |
*/
|
U |
189 |
|
|
190 |
/*
|
|
191 |
CodeMirror Breakpoint bzw. Gutter Marker
|
|
192 |
|
|
193 |
https://codemirror.net/demo/marker.html
|
0ac262
|
194 |
*/ |