From 72e43ddf5a01b28d57a41bdbd4b77a0519d92912 Mon Sep 17 00:00:00 2001 From: ulrich <not disclosed> Date: Sun, 19 Mar 2017 16:27:57 +0000 Subject: [PATCH] compileAll (Entwurf) --- web/WEB-INF/web.xml | 2 web/ui/index.html | 3 src/java/de/uhilger/filecms/api/CompileService.java | 99 ++++++++++----- src/java/de/uhilger/filecms/api/FileMgr.java | 92 --------------- web/ui/ui.js | 34 +++++ src/java/de/uhilger/filecms/api/Api.java | 103 +++++++++++++++++ 6 files changed, 203 insertions(+), 130 deletions(-) diff --git a/src/java/de/uhilger/filecms/api/Api.java b/src/java/de/uhilger/filecms/api/Api.java index fc588bc..3da69f3 100644 --- a/src/java/de/uhilger/filecms/api/Api.java +++ b/src/java/de/uhilger/filecms/api/Api.java @@ -20,8 +20,13 @@ package de.uhilger.filecms.api; +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.security.Principal; +import java.util.logging.Logger; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; @@ -30,6 +35,19 @@ */ public abstract class Api implements WebKontext, RequestKontext { + private static final Logger logger = Logger.getLogger(Api.class.getName()); + + public static final String WBX_DATA_PATH = "daten/"; + public static final String PUB_DIR_PATH = "www/"; + public static final String HOME_DIR_PATH = "home/"; + public static final String PUB_DIR_NAME = "Oeffentlich"; + //public static final String HOME_DIR_NAME = "Persoenlicher Ordner"; + public static final String HOME_DIR_NAME = "Persoenlich"; + public static final String WBX_ADMIN_ROLE = "wbxAdmin"; + + public static final String WBX_BASE = "$basis"; + public static final String WBX_DATA = "$daten"; + /** Zeiger zum Servlet-Kontext dieser Anwendung */ private ServletContext ctx; @@ -37,6 +55,91 @@ private HttpServletRequest request; + /** + * Einen relativen Pfad in einen absoluten Pfad der WebBox + * aufloesen. + * + * Nur die absoluten Pfade zu PUB_DIR_NAME, HOME_DIR_NAME + * sowie WBX_BASE und WBX_DATA werden ausgegeben. Letztere + * beiden nur fuer Nutzer mit der Rolle WBX_ADMIN_ROLE. + * + * D.h., es werden nur Pfade aufgeloest, die sich innerhalb + * des Ordners der WeBox befinden. + * + * @param relPath + * @return + */ + protected File getTargetDir(String relPath) { + logger.fine(relPath); + File targetDir; + String targetPath = null; + if(relPath.startsWith(PUB_DIR_NAME)) { + targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length()); + targetDir = new File(getBase().getAbsolutePath(), targetPath); + } else if(relPath.startsWith(HOME_DIR_NAME)) { + targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length()); + targetDir = new File(getBase().getAbsolutePath(), targetPath); + } else if(getRequest().isUserInRole(WBX_ADMIN_ROLE)) { + logger.fine("in admin role"); + if(relPath.startsWith(WBX_BASE)) { + logger.fine("is base"); + targetPath = getCatalinaBase(); + targetDir = new File(targetPath, relPath.substring(WBX_BASE.length())); + } else if(relPath.startsWith(WBX_DATA)) { + targetPath = getWbxDataDir(); + logger.fine("is data, combine " + targetPath + ' ' + relPath.substring(WBX_DATA.length())); + targetDir = new File(targetPath, relPath.substring(WBX_DATA.length())); + } else { + targetDir = getDefaultDir(relPath); + } + } else { + // kann eigentlich nicht sein.. + targetDir = getDefaultDir(relPath); + } + logger.fine("returning targetDir " + targetDir.getAbsolutePath()); + //File targetDir = new File(getBase().getAbsolutePath(), targetPath); + return targetDir; + } + + protected File getDefaultDir(String relPath) { + String targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length()); + return new File(getBase().getAbsolutePath(), targetPath); + } + + protected 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; + } + + protected String getUserName() { + String userName = null; + Object p = getRequest().getUserPrincipal(); + if(p instanceof Principal) { + userName = ((Principal) p).getName(); + } + return userName; + } + + protected String getCatalinaBase() { + String path = getServletContext().getRealPath("/"); + logger.fine("getRealPath: " + path); // file-cms in webapps + File file = new File(path); + file = file.getParentFile().getParentFile(); + return file.getAbsolutePath(); + } + + protected String getWbxDataDir() { + String wbxBase = getBase().getAbsolutePath(); + File file = new File(wbxBase); + return file.getAbsolutePath(); + } /* ------------- Implementierung WebKontext ------------- */ @Override diff --git a/src/java/de/uhilger/filecms/api/CompileService.java b/src/java/de/uhilger/filecms/api/CompileService.java index 21f3464..49bc175 100644 --- a/src/java/de/uhilger/filecms/api/CompileService.java +++ b/src/java/de/uhilger/filecms/api/CompileService.java @@ -17,26 +17,17 @@ */ 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.FileFilter; 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.Level; 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; @@ -47,12 +38,70 @@ /** * */ -public class CompileService implements RequestKontext, WebKontext { +public class CompileService extends Api { private static final Logger logger = Logger.getLogger(CompileService.class.getName()); + + public List<CompilerIssue> compileAll(String relPath) { + logger.fine(relPath); + List<CompilerIssue> compilerIssues = new ArrayList(); + try { + File targetDir = getTargetDir(relPath); + ArrayList<File> files = new ArrayList(); + collectFiles(files, targetDir, new JavaFileFilter()); + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector(); + StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); + Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(files); + compiler.getTask(null, null, diagnostics, null, null, compilationUnits).call(); + fileManager.close(); + + List compileResults = diagnostics.getDiagnostics(); + 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); + } + } + } catch(Exception ex) { + logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); + } + return compilerIssues; + } - private ServletContext ctx; - private HttpServletRequest request; + private void collectFiles(ArrayList<File> files, File dir, FileFilter filter) { + File[] dirFiles = dir.listFiles(filter); + for(int i = 0; i < dirFiles.length; i++) { + if(dirFiles[i].isDirectory()) { + logger.fine("drill down to " + dirFiles[i].getAbsolutePath()); + collectFiles(files, dirFiles[i], filter); + } else { + logger.fine("add " + dirFiles[i].getAbsolutePath()); + files.add(dirFiles[i]); + } + } + } + + public class JavaFileFilter implements FileFilter { + + @Override + public boolean accept(File pathname) { + boolean doAccept = false; + if(pathname.getName().endsWith(".java") || pathname.isDirectory()) { + doAccept = true; + } + return doAccept; + } + + } /** * @@ -128,6 +177,7 @@ return compilerIssues; } + /* private File getTargetDir(String relPath) { logger.fine(relPath); String targetPath = null; @@ -162,26 +212,7 @@ } 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; - } +*/ } diff --git a/src/java/de/uhilger/filecms/api/FileMgr.java b/src/java/de/uhilger/filecms/api/FileMgr.java index 8fb1ea3..c2e2f8e 100644 --- a/src/java/de/uhilger/filecms/api/FileMgr.java +++ b/src/java/de/uhilger/filecms/api/FileMgr.java @@ -46,16 +46,6 @@ public class FileMgr extends Api { private static final Logger logger = Logger.getLogger(FileMgr.class.getName()); - public static final String WBX_DATA_PATH = "daten/"; - public static final String PUB_DIR_PATH = "www/"; - public static final String HOME_DIR_PATH = "home/"; - public static final String PUB_DIR_NAME = "Oeffentlich"; - //public static final String HOME_DIR_NAME = "Persoenlicher Ordner"; - public static final String HOME_DIR_NAME = "Persoenlich"; - public static final String WBX_ADMIN_ROLE = "wbxAdmin"; - - public static final String WBX_BASE = "$basis"; - public static final String WBX_DATA = "$daten"; public static final int OP_COPY = 1; public static final int OP_MOVE = 2; @@ -372,86 +362,4 @@ /* ---- Hilfsfunktionen ---- */ - /** - * Einen relativen Pfad in einen absoluten Pfad der WebBox - * aufloesen. - * - * Nur die absoluten Pfade zu PUB_DIR_NAME, HOME_DIR_NAME - * sowie WBX_BASE und WBX_DATA werden ausgegeben. Letztere - * beiden nur fuer Nutzer mit der Rolle WBX_ADMIN_ROLE. - * - * D.h., es werden nur Pfade aufgeloest, die sich innerhalb - * des Ordners der WeBox befinden. - * - * @param relPath - * @return - */ - private File getTargetDir(String relPath) { - logger.fine(relPath); - File targetDir; - String targetPath = null; - if(relPath.startsWith(PUB_DIR_NAME)) { - targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length()); - targetDir = new File(getBase().getAbsolutePath(), targetPath); - } else if(relPath.startsWith(HOME_DIR_NAME)) { - targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length()); - targetDir = new File(getBase().getAbsolutePath(), targetPath); - } else if(getRequest().isUserInRole(WBX_ADMIN_ROLE)) { - if(relPath.startsWith(WBX_BASE)) { - targetPath = getCatalinaBase(); - targetDir = new File(targetPath, relPath.substring(WBX_BASE.length())); - } else if(relPath.startsWith(WBX_DATA)) { - targetPath = getWbxDataDir(); - targetDir = new File(targetPath, relPath.substring(WBX_BASE.length())); - } else { - targetDir = getDefaultDir(relPath); - } - } else { - // kann eigentlich nicht sein.. - targetDir = getDefaultDir(relPath); - } - logger.fine(targetPath); - //File targetDir = new File(getBase().getAbsolutePath(), targetPath); - return targetDir; - } - - private File getDefaultDir(String relPath) { - String targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length()); - return new File(getBase().getAbsolutePath(), targetPath); - } - - 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; - } - - private String getCatalinaBase() { - String path = getServletContext().getRealPath("/"); - logger.fine("getRealPath: " + path); // file-cms in webapps - File file = new File(path); - file = file.getParentFile().getParentFile(); - return file.getAbsolutePath(); - } - - private String getWbxDataDir() { - String wbxBase = getBase().getAbsolutePath(); - File file = new File(wbxBase); - return file.getAbsolutePath(); - } } \ No newline at end of file diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml index eb03881..05d807e 100644 --- a/web/WEB-INF/web.xml +++ b/web/WEB-INF/web.xml @@ -7,7 +7,7 @@ Bleibt der Eintrag leer oder wird er ganz weggelassen, wird ein hart kodierter Pfad fuer die WebBox verwendet.</description> <param-name>datenAblage</param-name> - <param-value> </param-value> + <param-value>/home/ulli/srv/wbx_probe/daten</param-value> </context-param> <listener> <description>Der Initialiser setzt globale Variable fuer die Dateiverwaltung</description> diff --git a/web/ui/index.html b/web/ui/index.html index 6923342..a1a33ad 100644 --- a/web/ui/index.html +++ b/web/ui/index.html @@ -109,7 +109,8 @@ <a class="dropdown-item disabled" href="/file-cms/ui">Dateien verwalten</a> <div class="dropdown-divider"></div> <a id="m-test" class="dropdown-item" href="#">Test</a> - <a id="m-test-2" class="dropdown-item" href="#">Test 2</a> + <a id="m-test-2" class="dropdown-item" href="#">Compile</a> + <a id="m-test-3" class="dropdown-item" href="#">Compile all</a> <div class="dropdown-divider"></div> <a id="logout" class="dropdown-item" href="#">Abmelden</a> </div> diff --git a/web/ui/ui.js b/web/ui/ui.js index 345603d..8182c68 100644 --- a/web/ui/ui.js +++ b/web/ui/ui.js @@ -61,7 +61,8 @@ $('#m-paste').on('click', fm_menu_paste); $('#m-shrink').on('click', fm_menu_shrink); $('#m-test').on('click', fm_menu_test); - $('#m-test-2').on('click', fm_menu_test_2); + $('#m-test-2').on('click', fm_menu_compile); + $('#m-test-3').on('click', fm_menu_compile_all); $('#saveModal').on('hidden.bs.modal', function (e) { $('#modal_ok').attr('onclick','').unbind('click'); }); @@ -294,8 +295,11 @@ } } -function fm_menu_test_2() { +function fm_menu_compile() { fm_compile('0', fm_mark_compile_results_in_editor); +} +function fm_menu_compile_all() { + fm_compile_all(); } function fm_mark_compile_results_in_editor(resp) { @@ -324,6 +328,32 @@ } } +function fm_compile_all() { + var m = '?c=de.uhilger.filecms.api.CompileService&m=compileAll&p=' + pfad; + var u = '../svc' + m; + fm_get(u, "json", function(resp) { + if(resp.List[0].CompilerIssue !== undefined) { + var lno; + var eMsg; + if(resp.List[0].CompilerIssue instanceof Array) { + var issueNo = 0; + while(issueNo < resp.List[0].CompilerIssue.length) { + console.log(' +++ ---- +++ '); + console.log(resp.List[0].CompilerIssue[issueNo].kind); + console.log(resp.List[0].CompilerIssue[issueNo].lineNumber); + console.log(resp.List[0].CompilerIssue[issueNo].sourceName); + console.log(resp.List[0].CompilerIssue[issueNo].message); + issueNo++; + } + } else { + lno = resp.List[0].CompilerIssue.lineNumber; + eMsg = resp.List[0].CompilerIssue.kind + ' ' + resp.List[0].CompilerIssue.message; + console.log(lno + ' ' + eMsg); + } + } + }); +} + function fm_compile(modeStr, callback) { var liste = fm_gewaehlte_dateien(); var m = '?c=de.uhilger.filecms.api.CompileService&m=compile&p=' + pfad + '&p=' + encodeURIComponent(liste) + -- Gitblit v1.9.3