From 2d1a7d647cae068a3b2973c033e8bd10d682dd02 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Mon, 16 Nov 2015 17:48:37 +0100 Subject: [PATCH] Backend implementation of config objects --- WebAdmin.iml | 2 +- resources/WebContent/page/ConfigPage.tmpl | 6 +- src/wa/server/WAAbstractPage.java | 2 +- src/wa/server/page/ConfigPage.java | 41 +++- .../page/{WALogPage.java => LogPage.java} | 177 +++++++++--------- src/wa/server/page/ServicePage.java | 14 +- src/wa/server/plugin/WAConfigEntry.java | 19 ++ src/wa/server/plugin/WALog.java | 7 + src/wa/server/plugin/WAService.java | 5 + src/wa/server/plugin/WAServiceConfig.java | 11 +- .../apache/ApacheConfigVirtualHost.java | 48 ++--- .../server/plugin/apache/ApacheService.java | 10 +- .../tomcat/TomcatConfigApplication.java | 4 +- 13 files changed, 215 insertions(+), 131 deletions(-) mode change 100644 => 100755 WebAdmin.iml mode change 100644 => 100755 resources/WebContent/page/ConfigPage.tmpl mode change 100644 => 100755 src/wa/server/WAAbstractPage.java mode change 100644 => 100755 src/wa/server/page/ConfigPage.java rename src/wa/server/page/{WALogPage.java => LogPage.java} (92%) mode change 100644 => 100755 mode change 100644 => 100755 src/wa/server/page/ServicePage.java create mode 100755 src/wa/server/plugin/WAConfigEntry.java create mode 100755 src/wa/server/plugin/WALog.java mode change 100644 => 100755 src/wa/server/plugin/WAService.java mode change 100644 => 100755 src/wa/server/plugin/WAServiceConfig.java mode change 100644 => 100755 src/wa/server/plugin/apache/ApacheConfigVirtualHost.java mode change 100644 => 100755 src/wa/server/plugin/apache/ApacheService.java mode change 100644 => 100755 src/wa/server/plugin/tomcat/TomcatConfigApplication.java diff --git a/WebAdmin.iml b/WebAdmin.iml old mode 100644 new mode 100755 index d464eb7..f51865e --- a/WebAdmin.iml +++ b/WebAdmin.iml @@ -12,7 +12,6 @@ - @@ -26,5 +25,6 @@ + \ No newline at end of file diff --git a/resources/WebContent/page/ConfigPage.tmpl b/resources/WebContent/page/ConfigPage.tmpl old mode 100644 new mode 100755 index c93dfff..1fafc26 --- a/resources/WebContent/page/ConfigPage.tmpl +++ b/resources/WebContent/page/ConfigPage.tmpl @@ -17,7 +17,7 @@ {{/.getConfiguration()}} - {{^.enabled}} @@ -26,11 +26,11 @@ {{/.enabled}} {{#.enabled}} - {{/.enabled}} - diff --git a/src/wa/server/WAAbstractPage.java b/src/wa/server/WAAbstractPage.java old mode 100644 new mode 100755 index ea67a42..692c7a1 --- a/src/wa/server/WAAbstractPage.java +++ b/src/wa/server/WAAbstractPage.java @@ -111,7 +111,7 @@ public class WAAbstractPage implements HttpPage{ if(content != null) { if(!breadcrumb.isEmpty()) content.set("nav", breadcrumb.get(breadcrumb.size() - 1)); - tmpl.set("content", content.compile()); + tmpl.set("content", content); } out.print(tmpl.compile()); diff --git a/src/wa/server/page/ConfigPage.java b/src/wa/server/page/ConfigPage.java old mode 100644 new mode 100755 index 79ba444..b53f370 --- a/src/wa/server/page/ConfigPage.java +++ b/src/wa/server/page/ConfigPage.java @@ -23,9 +23,11 @@ package wa.server.page; import wa.server.WAContext; +import wa.server.plugin.WAConfigEntry; import wa.server.plugin.WAServiceConfig; import wa.server.plugin.apache.ApacheConfigVirtualHost; import zutil.db.bean.Configurator; +import zutil.db.bean.DBBean; import zutil.io.file.FileUtil; import zutil.log.LogUtil; import zutil.net.http.HttpHeaderParser; @@ -65,14 +67,36 @@ public class ConfigPage implements WAPage{ Map request) { try { List confObjs = config.getConfigData(); - ArrayList confList = new ArrayList<>(); + ArrayList> confList = new ArrayList<>(); for(Object obj : confObjs){ confList.add(new Configurator(obj)); } - confList.add(new Configurator(new ApacheConfigVirtualHost.ApacheVirtualHostData())); - confList.add(new Configurator(new ApacheConfigVirtualHost.ApacheVirtualHostData())); - confList.add(new Configurator(new ApacheConfigVirtualHost.ApacheVirtualHostData())); + // Actions + int index = Integer.parseInt(request.get("id")); + Configurator target = findObj(confList, index); + switch (request.get("action")){ + case "create": + target = new Configurator(config.createConfig()); + case "modify": + for(Configurator.ConfigurationParam param : target.getConfiguration()){ + if(request.containsKey(param.getName())) + param.setValue(request.get(param.getName())); + } + target.applyConfiguration(); + break; + case "delete": + config.deleteConfig(index); + break; + case "enable": + target.getObject().setEnabled(true); + break; + case "disable": + target.getObject().setEnabled(false); + break; + } + + // Prepare Output Templator tmpl = new Templator(FileUtil.find(TMPL_FILE)); tmpl.set("params", Configurator.getConfiguration( config.getConfigClass())); @@ -92,4 +116,13 @@ public class ConfigPage implements WAPage{ Map request) { return null; } + + + private static Configurator findObj(List> list, int id){ + for (Configurator conf : list) { + if(conf.getObject().getId() == id) + return conf; + } + return null; + } } diff --git a/src/wa/server/page/WALogPage.java b/src/wa/server/page/LogPage.java old mode 100644 new mode 100755 similarity index 92% rename from src/wa/server/page/WALogPage.java rename to src/wa/server/page/LogPage.java index 3e7a00e..831cf43 --- a/src/wa/server/page/WALogPage.java +++ b/src/wa/server/page/LogPage.java @@ -1,85 +1,92 @@ -/* - * Copyright (c) 2015 ezivkoc - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -package wa.server.page; - -import sun.reflect.generics.reflectiveObjects.NotImplementedException; -import zutil.net.http.HttpHeaderParser; -import zutil.net.http.HttpPage; -import zutil.net.http.HttpPrintStream; -import zutil.parser.DataNode; -import zutil.parser.json.JSONWriter; -import zutil.struct.CircularBuffer; - -import java.io.IOException; -import java.util.Iterator; -import java.util.Map; -import java.util.logging.Handler; -import java.util.logging.LogRecord; - -/** - * Created by Ziver on 2015-09-22. - */ -public class WALogPage extends Handler implements HttpPage { - private CircularBuffer logBuffer; - - @Override - public void publish(LogRecord record) { - if(super.isLoggable(record)){ - logBuffer.add(record); - } - } - - - @Override - public void flush() {} - @Override - public void close() throws SecurityException { - throw new NotImplementedException(); - } - - - @Override - public void respond(HttpPrintStream out, - HttpHeaderParser client_info, - Map session, - Map cookie, - Map request) throws IOException { - - DataNode logNode = new DataNode(DataNode.DataType.List); - Iterator it = logBuffer.iterator(); - for(int i=0; i<20 && it.hasNext(); ++i){ - LogRecord record = it.next(); - DataNode node = new DataNode(DataNode.DataType.Map); - node.set("timestamp", record.getMillis()); - node.set("source", record.getLoggerName()); - node.set("msg", record.getMessage()); - logNode.add(node); - } - - DataNode root = new DataNode(DataNode.DataType.Map); - root.add(logNode); - JSONWriter writer = new JSONWriter(out); - writer.write(root); - writer.close(); - } -} +/* + * Copyright (c) 2015 ezivkoc + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package wa.server.page; + +import sun.reflect.generics.reflectiveObjects.NotImplementedException; +import wa.server.plugin.WALog; +import zutil.net.http.HttpHeaderParser; +import zutil.net.http.HttpPage; +import zutil.net.http.HttpPrintStream; +import zutil.parser.DataNode; +import zutil.parser.json.JSONWriter; +import zutil.struct.CircularBuffer; + +import java.io.IOException; +import java.util.Iterator; +import java.util.Map; +import java.util.logging.Handler; +import java.util.logging.LogRecord; + +/** + * Created by Ziver on 2015-09-22. + */ +public class LogPage extends Handler implements HttpPage { + private CircularBuffer logBuffer; + private WALog log; + + public LogPage(WALog log) { + logBuffer = new CircularBuffer<>(500); + this.log = log; + } + + @Override + public void publish(LogRecord record) { + if(super.isLoggable(record)){ + logBuffer.add(record); + } + } + + + @Override + public void flush() {} + @Override + public void close() throws SecurityException { + throw new NotImplementedException(); + } + + + @Override + public void respond(HttpPrintStream out, + HttpHeaderParser client_info, + Map session, + Map cookie, + Map request) throws IOException { + + DataNode logNode = new DataNode(DataNode.DataType.List); + Iterator it = logBuffer.iterator(); + for(int i=0; i<20 && it.hasNext(); ++i){ + LogRecord record = it.next(); + DataNode node = new DataNode(DataNode.DataType.Map); + node.set("timestamp", record.getMillis()); + node.set("source", record.getLoggerName()); + node.set("msg", record.getMessage()); + logNode.add(node); + } + + DataNode root = new DataNode(DataNode.DataType.Map); + root.add(logNode); + JSONWriter writer = new JSONWriter(out); + writer.write(root); + writer.close(); + } +} diff --git a/src/wa/server/page/ServicePage.java b/src/wa/server/page/ServicePage.java old mode 100644 new mode 100755 index 567bc28..cc23337 --- a/src/wa/server/page/ServicePage.java +++ b/src/wa/server/page/ServicePage.java @@ -50,18 +50,21 @@ public class ServicePage implements WAPage { private ServiceStatusPage rootStatusPage; private ArrayList services; private ArrayList statusPages; + private ArrayList logPages; private ArrayList configPages; public ServicePage(PluginManager pluginManager){ this.services = pluginManager.toArray(WAService.class); this.rootStatusPage = new ServiceStatusPage(pluginManager); this.statusPages = new ArrayList<>(); + this.logPages = new ArrayList<>(); this.configPages = new ArrayList<>(); WANavigation nav = WANavigation.createRootNav(NAVIGATION_NAME); nav.setResource(this); for(WAService plugin : services) { statusPages.add(new ServiceStatusPage(plugin.getStatus())); + logPages.add(new LogPage(plugin.getLog())); WANavigation serviceNav = nav.createSubNav(plugin.getName()); serviceNav.setResource(plugin); @@ -91,12 +94,15 @@ public class ServicePage implements WAPage { else if ((index = services.indexOf(resource)) >= 0) { WAService obj = services.get(index); ServiceStatusPage statusPage = statusPages.get(index); + LogPage logPage = logPages.get(index); Templator tmpl = new Templator(FileUtil.find(TMPL_FILE)); - tmpl.set("service_status", - statusPage.htmlResponse(context, client_info, session, cookie, request).compile()); - //tmpl.set("service_logs", - // statusPage.htmlResponse(context, client_info, session, cookie, request).compile()); + if(statusPage != null) + tmpl.set("service_status", + statusPage.htmlResponse(context, client_info, session, cookie, request)); + if(logPage != null) + tmpl.set("service_logs", + statusPage.htmlResponse(context, client_info, session, cookie, request)); return tmpl; } else{ // root page diff --git a/src/wa/server/plugin/WAConfigEntry.java b/src/wa/server/plugin/WAConfigEntry.java new file mode 100755 index 0000000..26474d4 --- /dev/null +++ b/src/wa/server/plugin/WAConfigEntry.java @@ -0,0 +1,19 @@ +package wa.server.plugin; + +import zutil.db.bean.DBBean; + +/** + * Created by Ziver on 2015-11-16. + */ +public abstract class WAConfigEntry extends DBBean{ + + private boolean enabled = true; + + + public void setEnabled(boolean enabled){ + this.enabled = enabled; + } + public boolean isEnabled(){ + return enabled; + } +} diff --git a/src/wa/server/plugin/WALog.java b/src/wa/server/plugin/WALog.java new file mode 100755 index 0000000..35ffc64 --- /dev/null +++ b/src/wa/server/plugin/WALog.java @@ -0,0 +1,7 @@ +package wa.server.plugin; + +/** + * Created by Ziver on 2015-11-16. + */ +public class WALog { +} diff --git a/src/wa/server/plugin/WAService.java b/src/wa/server/plugin/WAService.java old mode 100644 new mode 100755 index 73a7717..4c2b9b1 --- a/src/wa/server/plugin/WAService.java +++ b/src/wa/server/plugin/WAService.java @@ -9,6 +9,11 @@ public interface WAService { */ public WAServiceStatus getStatus(); + /** + * @return a service log object or null if it is not possible to read logs + */ + public WALog getLog(); + /** * @return a installer object that will install the service or null if the installer is not available */ diff --git a/src/wa/server/plugin/WAServiceConfig.java b/src/wa/server/plugin/WAServiceConfig.java old mode 100644 new mode 100755 index 1b63aa7..54212c4 --- a/src/wa/server/plugin/WAServiceConfig.java +++ b/src/wa/server/plugin/WAServiceConfig.java @@ -12,12 +12,15 @@ public interface WAServiceConfig { /** * Read in current configuration data */ - public void read() throws Exception; + //public void read() throws Exception; /** * Save configured data to disk or database */ - public void save() throws Exception; + //public void save() throws Exception; + + public WAConfigEntry createConfig(); + public void deleteConfig(int id); /** * Configure service with current configuration data @@ -31,10 +34,10 @@ public interface WAServiceConfig { * configuration can be changed with the * {@link zutil.db.bean.Configurator} class. */ - public List getConfigData(); + public List getConfigData(); /** * @return the class that contains the configuration data */ - public Class getConfigClass(); + public Class getConfigClass(); } diff --git a/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java b/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java old mode 100644 new mode 100755 index 9ccbed4..38c68b0 --- a/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java +++ b/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java @@ -1,6 +1,7 @@ package wa.server.plugin.apache; import wa.server.WAConstants; +import wa.server.plugin.WAConfigEntry; import wa.server.plugin.WAServiceConfig; import wa.server.util.ConfigFileUtil; import zutil.db.DBConnection; @@ -16,30 +17,30 @@ import java.util.List; public class ApacheConfigVirtualHost implements WAServiceConfig{ private static final String CONFIG_NAME = "Apache Virtual Host"; - private static final String APACHE_MAIN_CONFIG_FILE = "apache2/apache2.conf"; + private static final String APACHE_MAIN_CONFIG_FILE = "/apache2/apache2.conf"; + private static final String APACHE_SITE_AVAILABLE_PATH = "/apache2/site-available/"; private static final String STATIC_PRE_CONF = "wa/server/plugin/apache/apache_default.config"; - private List vhosts = new ArrayList<>(); + private List vhosts = new ArrayList<>(); + public ApacheConfigVirtualHost() throws Exception { + DBConnection db = WAConstants.getDB(); + vhosts = DBBean.load(db, ApacheVirtualHostEntry.class); + } + @Override public String getName() { return CONFIG_NAME; } - @Override - public void read() throws Exception { - DBConnection db = WAConstants.getDB(); - vhosts = DBBean.load(db, ApacheVirtualHostData.class); - } + public WAConfigEntry createConfig(){ + return new ApacheVirtualHostEntry(); + } + public void deleteConfig(int id){ + + } - @Override - public void save() throws Exception { - DBConnection db = WAConstants.getDB(); - for(ApacheVirtualHostData vhost : vhosts){ - vhost.save(db); - } - } @Override public void configure() throws Exception { @@ -51,23 +52,23 @@ public class ApacheConfigVirtualHost implements WAServiceConfig{ new FileInputStream(STATIC_PRE_CONF))); // Write Vhost configuration - for(ApacheVirtualHostData vhost : vhosts){ + for(ApacheVirtualHostEntry vhost : vhosts){ vhost.configure(); } } @Override - public List getConfigData() { + public List getConfigData() { return vhosts; } @Override - public Class getConfigClass() { - return ApacheVirtualHostData.class; + public Class getConfigClass() { + return ApacheVirtualHostEntry.class; } @DBTable(WAConstants.DB_TABLE_PREFIX + "_apache_vhost") - public static class ApacheVirtualHostData extends DBBean{ + public static class ApacheVirtualHostEntry extends WAConfigEntry { @Configurable("Domain") protected String domain; protected transient String domain_old; @@ -101,10 +102,11 @@ public class ApacheConfigVirtualHost implements WAServiceConfig{ public final void configure() throws IOException { if(domain != domain_old){ // Configuration file has changed name - WAConstants.getConfigFile("apache2/enabled/"+ domain_old).delete(); + WAConstants.getConfigFile(APACHE_SITE_AVAILABLE_PATH+ domain_old).delete(); + domain_old = domain; } PrintStream out = new PrintStream( - WAConstants.getConfigFile("apache2/enabled/"+ domain)); + WAConstants.getConfigFile(APACHE_SITE_AVAILABLE_PATH+ domain)); writeConfiguration(out); out.close(); @@ -129,8 +131,8 @@ public class ApacheConfigVirtualHost implements WAServiceConfig{ } else { out.println(""); - out.println(" ServerName " + domain + ":80"); - out.println(" DocumentRoot " + path); + out.println(" ServerName "+ domain +":80"); + out.println(" DocumentRoot "+ path); out.println(""); out.println(""); } diff --git a/src/wa/server/plugin/apache/ApacheService.java b/src/wa/server/plugin/apache/ApacheService.java old mode 100644 new mode 100755 index 5f3b24d..e166425 --- a/src/wa/server/plugin/apache/ApacheService.java +++ b/src/wa/server/plugin/apache/ApacheService.java @@ -22,10 +22,7 @@ package wa.server.plugin.apache; -import wa.server.plugin.WAServiceConfig; -import wa.server.plugin.WAInstaller; -import wa.server.plugin.WAService; -import wa.server.plugin.WAServiceStatus; +import wa.server.plugin.*; import zutil.log.LogUtil; import java.util.logging.Logger; @@ -53,6 +50,11 @@ public class ApacheService implements WAService { return status; } + @Override + public WALog getLog() { + return null; + } + @Override public WAInstaller getInstaller() { if(installer == null) diff --git a/src/wa/server/plugin/tomcat/TomcatConfigApplication.java b/src/wa/server/plugin/tomcat/TomcatConfigApplication.java old mode 100644 new mode 100755 index 58264f7..de1030c --- a/src/wa/server/plugin/tomcat/TomcatConfigApplication.java +++ b/src/wa/server/plugin/tomcat/TomcatConfigApplication.java @@ -23,14 +23,14 @@ package wa.server.plugin.tomcat; import java.io.PrintStream; -import wa.server.plugin.apache.ApacheConfigVirtualHost.ApacheVirtualHostData; +import wa.server.plugin.apache.ApacheConfigVirtualHost.ApacheVirtualHostEntry; public class TomcatConfigApplication { - public static class TomcatApplicationData extends ApacheVirtualHostData { + public static class TomcatApplicationEntry extends ApacheVirtualHostEntry { @Override public void writeConfiguration(PrintStream out) { out.println("");