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