Dateiverwaltung für die WebBox
ulrich
2017-03-18 1f550a4fce54040aa41f6351b0ba1fe1c404652d
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;
e3043f 24 import de.uhilger.filecms.data.CompilerIssue;
0ac262 25 import de.uhilger.filecms.data.FileRef;
U 26 import de.uhilger.filecms.web.Initialiser;
27 import de.uhilger.transit.web.RequestKontext;
28 import de.uhilger.transit.web.WebKontext;
29 import java.io.File;
30 import java.io.IOException;
31 import java.security.Principal;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Locale;
37 import java.util.logging.Logger;
38 import javax.servlet.ServletContext;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.tools.Diagnostic;
41 import javax.tools.DiagnosticCollector;
42 import javax.tools.JavaCompiler;
43 import javax.tools.JavaFileObject;
44 import javax.tools.StandardJavaFileManager;
45 import javax.tools.ToolProvider;
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   
fd0b4c 57   /**
U 58    * 
59    * @param relPath
60    * @param fileNames
61    * @param mode 0 = test, 1 = build
62    * @return
63    * @throws IOException 
64    */
65   public List<CompilerIssue> compile(String relPath, List fileNames, String mode) throws IOException {
0ac262 66     //Files[] files1 = ... ; // input for first compilation task
U 67     //Files[] files2 = ... ; // input for second compilation task
68     
69     File targetDir = getTargetDir(relPath);
70     //System.out.println(targetDir.getAbsolutePath());
71     ArrayList<File> files = new ArrayList();
72     
73     for(int i=0; i < fileNames.size(); i++) {
74       Object o = fileNames.get(i);
75       if(o instanceof ArrayList) {
76         ArrayList al = (ArrayList) o;
77         logger.fine(al.get(0).toString());
78         File targetFile = new File(targetDir, al.get(0).toString());
79         logger.fine(targetFile.getAbsolutePath());
80         files.add(targetFile);
81       }
82     }
83
84     JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
85     DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector();
86     StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
87
88     Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(files);
89     
fd0b4c 90     
U 91     
92     if( mode.equals("1")) {
93       final Iterable<String> options = Arrays.asList(new String[]{"-Xlint",
94               /*"-cp", project.getClassPath(),*/
0ac262 95               "-d", targetDir.getAbsolutePath()
U 96               });
fd0b4c 97       compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits1).call();
U 98     } else {
99       compiler.getTask(null, null, diagnostics, null, null, compilationUnits1).call();
100     }     
0ac262 101     
fd0b4c 102     //compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits1).call();
0ac262 103
U 104     /*
105     Iterable<? extends JavaFileObject> compilationUnits2
106             = fileManager.getJavaFileObjects(files2); // use alternative method
107     // reuse the same file manager to allow caching of jar files
108     compiler.getTask(null, fileManager, null, null, null, compilationUnits2).call();
109     */
110     fileManager.close();
111
e3043f 112     List compileResults = diagnostics.getDiagnostics();
U 113     List<CompilerIssue> compilerIssues = new ArrayList();
114     Iterator i = compileResults.iterator();
115     while(i.hasNext()) {
116       Object o = i.next();
117       Diagnostic<? extends JavaFileObject> err;
118       if(o instanceof Diagnostic) {
119         err = (Diagnostic) o;
120         CompilerIssue issue = new CompilerIssue();
121         issue.setKind(err.getKind().name());
122         issue.setLineNumber(err.getLineNumber());
123         issue.setMessage(err.getMessage(Locale.GERMANY));
124         issue.setSoureName(err.getSource().getName());
125         compilerIssues.add(issue);
0ac262 126       }
e3043f 127     }
U 128     return compilerIssues;
0ac262 129   }
e3043f 130   
0ac262 131   private File getTargetDir(String relPath) {
U 132     logger.fine(relPath);
133     String targetPath = null;
134     if(relPath.startsWith(PUB_DIR_NAME)) {
135       targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
136     } else if(relPath.startsWith(HOME_DIR_NAME)) {
137       targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length());
138     } else {
139       // kann eigentlich nicht sein..
140     }
141     logger.fine(targetPath);
142     File targetDir = new File(getBase().getAbsolutePath(), targetPath);
143     return targetDir;
144   }
145   
146   private FileRef getBase() {
147     FileRef base = null;
148     Object o = getServletContext().getAttribute(Initialiser.FILE_BASE);
149     if(o instanceof String) {
150       String baseStr = (String) o;
151       logger.fine(baseStr);
152       File file = new File(baseStr);
153       base = new FileRef(file.getAbsolutePath(), file.isDirectory());
154     }
155     return base;
156   }
157   private String getUserName() {
158     String userName = null;
159     Object p = getRequest().getUserPrincipal();
160     if(p instanceof Principal) {
161       userName = ((Principal) p).getName();
162     }
163     return userName;
164   }    
165
166   @Override
167   public HttpServletRequest getRequest() {
168     return request;
169   }
170
171   @Override
172   public void setRequest(HttpServletRequest r) {
173     this.request = r;
174   }
175
176   @Override
177   public ServletContext getServletContext() {
178     return ctx;
179   }
180
181   @Override
182   public void setServletContext(ServletContext servletContext) {
183     this.ctx = servletContext;
184   }
185 }
186
187
188 /*
189
190  Beispeil fuer einen dynamischen Compiler-Aufruf
191  'in memory'
192
193  String className = "mypackage.MyClass";
194  String javaCode = "package mypackage;\n" +
195                   "public class MyClass implements Runnable {\n" +
196                   "    public void run() {\n" +
197                   "        System.out.println("\"Hello World\");\n" +
198                   "    }\n" +
199                   "}\n";
200  Class aClass = CompilerUtils.CACHED_COMPILER.loadFromJava(className, javaCode);
201  Runnable runner = (Runnable) aClass.newInstance();
202  runner.run();
203
d920b7 204 */
U 205
206 /*
207   CodeMirror Breakpoint bzw. Gutter Marker
208
209   https://codemirror.net/demo/marker.html
0ac262 210 */