From af1d6546eb981c9ebf839c8467f31b49b3955e15 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Wed, 27 Jul 2016 02:45:52 +0200 Subject: [PATCH] Refactoring of config objs --- src/wa/server/WAContext.java | 2 +- src/wa/server/page/ConfigPage.java | 109 ++++++++++ src/wa/server/page/WAConfigPage.java | 140 ------------- src/wa/server/page/WAPage.java | 68 ++++--- src/wa/server/plugin/WAConfigEntry.java | 19 -- src/wa/server/plugin/WAConfiguration.java | 19 ++ .../apache/ApacheConfigVirtualHost.java | 188 +++++++----------- .../tomcat/TomcatConfigApplication.java | 41 ++-- 8 files changed, 256 insertions(+), 330 deletions(-) create mode 100755 src/wa/server/page/ConfigPage.java delete mode 100755 src/wa/server/page/WAConfigPage.java delete mode 100755 src/wa/server/plugin/WAConfigEntry.java create mode 100755 src/wa/server/plugin/WAConfiguration.java diff --git a/src/wa/server/WAContext.java b/src/wa/server/WAContext.java index 7d42ee0..852f19f 100755 --- a/src/wa/server/WAContext.java +++ b/src/wa/server/WAContext.java @@ -45,7 +45,7 @@ import java.util.logging.Logger; public class WAContext { private static final Logger logger = LogUtil.getLogger(); private static Navigation rootNav; - public static DBConnection db; + private static DBConnection db; private static PluginManager pluginManager; diff --git a/src/wa/server/page/ConfigPage.java b/src/wa/server/page/ConfigPage.java new file mode 100755 index 0000000..2d7e3b9 --- /dev/null +++ b/src/wa/server/page/ConfigPage.java @@ -0,0 +1,109 @@ +/* + * 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 wa.server.WAContext; +import wa.server.plugin.WAConfiguration; +import zutil.db.DBConnection; +import zutil.io.file.FileUtil; +import zutil.log.LogUtil; +import zutil.net.http.HttpHeader; +import zutil.parser.Templator; +import zutil.ui.Configurator; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * A dynamic configuration page where a list of the same + * Bean type can be configured + * + * Created by Ziver on 2015-07-27. + */ +public abstract class ConfigPage extends WAPage { + private static final Logger log = LogUtil.getLogger(); + private static final String TEMPLATE = "WebContent/page/ConfigPage.tmpl"; + + + private Class configClass; + + + public ConfigPage(Class configClass) { + //super(WAServicePage.NAVIGATION_NAME +"/"+ serviceName +"/"+ name); + this.configClass = configClass; + } + + + @Override + public Templator htmlResponse(WAContext context, + HttpHeader client_info, + Map session, + Map cookie, + Map request) throws Exception { + try { + DBConnection db = WAContext.getDB(); + List objList = getAllConfigs(); + + // Actions + if (request.containsKey("action") && request.containsKey("id")) { + int id = Integer.parseInt(request.get("id")); + WAConfiguration target = getConfig(id); + if (target != null) { + switch (request.get("action")) { + case "create": + case "modify": + new Configurator(target).setValues(request).applyConfiguration(); + target.saveConfiguration(); + target.save(db); + break; + case "delete": + target.deleteConfiguration(); + target.delete(db); + break; + } + } + } + + // Prepare Output + Templator tmpl = new Templator(FileUtil.find(TEMPLATE)); + tmpl.set("params", Configurator.getConfiguration(configClass)); + tmpl.set("data", objList); + return tmpl; + } catch (IOException e) { + log.log(Level.SEVERE, null, e); + } + return null; + } + + + private WAConfiguration getConfig(int id){ + return null; + } + private List getAllConfigs(){ + return null; + } +} diff --git a/src/wa/server/page/WAConfigPage.java b/src/wa/server/page/WAConfigPage.java deleted file mode 100755 index c3392d0..0000000 --- a/src/wa/server/page/WAConfigPage.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * 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 wa.server.WAContext; -import wa.server.plugin.WAConfigEntry; -import wa.server.plugin.WAServiceStatus; -import zutil.io.file.FileUtil; -import zutil.log.LogUtil; -import zutil.net.http.HttpHeader; -import zutil.parser.Templator; -import zutil.ui.Configurator; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * A dynamic configuration page where a list of the same - * Bean type can be configured - * - * Created by Ziver on 2015-07-27. - */ -public abstract class WAConfigPage extends WAPage { - private static final Logger log = LogUtil.getLogger(); - private static final String TEMPLATE = "WebContent/page/ConfigPage.tmpl"; - - - - public WAConfigPage(String serviceName, String name, String niceName) { - super(WAServicePage.NAVIGATION_NAME +"/"+ serviceName +"/"+ name); - WAContext.getRootNav().createSubNav(WAServicePage.NAVIGATION_NAME).createSubNav(serviceName) - .createSubNav(name, niceName); - } - - - @Override - public Templator htmlResponse(WAContext context, - HttpHeader client_info, - Map session, - Map cookie, - Map request) { - try { - List confObjs = getConfigData(); - ArrayList> confList = new ArrayList<>(); - for(Object obj : confObjs){ - confList.add(new Configurator(obj)); - } - - // Actions - int id = Integer.parseInt(request.get("id")); - Configurator target = findObj(confList, id); - switch (request.get("action")){ - case "create": - target = new Configurator(createConfig()); - target.applyConfiguration(); - case "modify": - target.setValues(request).applyConfiguration(); - break; - case "delete": - deleteConfig(id); - break; - case "enable": - target.getObject().setEnabled(true); - break; - case "disable": - target.getObject().setEnabled(false); - break; - } - - // Prepare Output - Templator tmpl = new Templator(FileUtil.find(TEMPLATE)); - tmpl.set("params", Configurator.getConfiguration( - getConfigClass())); - tmpl.set("data", confList); - return tmpl; - } catch (IOException e) { - log.log(Level.SEVERE, null, e); - } - return null; - } - - - - private static Configurator findObj(List> list, int id){ - for (Configurator conf : list) { - if(conf.getObject().getId() == id) - return conf; - } - return null; - } - - - - - public abstract WAConfigEntry createConfig(); - public abstract void deleteConfig(int id); - - /** - * Configure service with current configuration data - */ - public abstract void configure() throws Exception; - - - - /** - * @return a list of configuration objects, the object - * configuration can be changed with the - * {@link zutil.ui.Configurator} class. - */ - public abstract List getConfigData(); - - /** - * @return the class that contains the configuration data - */ - public abstract Class getConfigClass(); -} diff --git a/src/wa/server/page/WAPage.java b/src/wa/server/page/WAPage.java index 2007f3b..e6e055c 100755 --- a/src/wa/server/page/WAPage.java +++ b/src/wa/server/page/WAPage.java @@ -70,40 +70,43 @@ public abstract class WAPage implements HttpPage{ Map session, Map cookie, Map request) throws IOException { - WAContext context = new WAContext(header); - List breadcrumb = context.getBreadcrumb(); + try { + WAContext context = new WAContext(header); + List breadcrumb = context.getBreadcrumb(); - if(("application/json").equals(header.getHeader("ContentType")) || - request.containsKey("json")){ - DataNode node = this.jsonResponse(context, header, session, cookie, request); - if(node != null) { - out.setHeader("Content-Type", "application/json"); - JSONWriter writer = new JSONWriter(out); - writer.write(node); - writer.close(); + if (("application/json").equals(header.getHeader("ContentType")) || + request.containsKey("json")) { + DataNode node = this.jsonResponse(context, header, session, cookie, request); + if (node != null) { + out.setHeader("Content-Type", "application/json"); + JSONWriter writer = new JSONWriter(out); + writer.write(node); + writer.close(); + } + } else { + tmpl.clear(); + + tmpl.set("title", "WebAdmin"); + tmpl.set("top-nav", context.getNavigationInstance().getSubNavs()); + tmpl.set("side-nav-show", true); + if (!breadcrumb.isEmpty()) + tmpl.set("side-nav", breadcrumb.get(0).createPagedNavInstance(header).getSubNavs()); + tmpl.set("breadcrumb", breadcrumb); + tmpl.set("alerts", context.getAlerts()); + //tmpl.set("footer", null); + + Templator content = this.htmlResponse(context, header, session, cookie, request); + if (content != null) { + if (!breadcrumb.isEmpty()) + content.set("nav", breadcrumb.get(breadcrumb.size() - 1)); + tmpl.set("content", content); + } + + out.print(tmpl.compile()); } - } - else { - tmpl.clear(); - - tmpl.set("title", "WebAdmin"); - tmpl.set("top-nav", context.getNavigationInstance().getSubNavs()); - tmpl.set("side-nav-show", true); - if(!breadcrumb.isEmpty()) - tmpl.set("side-nav", breadcrumb.get(0).createPagedNavInstance(header).getSubNavs()); - tmpl.set("breadcrumb", breadcrumb); - tmpl.set("alerts", context.getAlerts()); - //tmpl.set("footer", null); - - Templator content = this.htmlResponse(context, header, session, cookie, request); - if(content != null) { - if(!breadcrumb.isEmpty()) - content.set("nav", breadcrumb.get(breadcrumb.size() - 1)); - tmpl.set("content", content); - } - - out.print(tmpl.compile()); + } catch (Exception e) { + throw new IOException(e); } } @@ -113,11 +116,12 @@ public abstract class WAPage implements HttpPage{ } + public abstract Templator htmlResponse(WAContext context, HttpHeader client_info, Map session, Map cookie, - Map request) throws IOException; + Map request) throws Exception; public DataNode jsonResponse(WAContext context, HttpHeader client_info, diff --git a/src/wa/server/plugin/WAConfigEntry.java b/src/wa/server/plugin/WAConfigEntry.java deleted file mode 100755 index 26474d4..0000000 --- a/src/wa/server/plugin/WAConfigEntry.java +++ /dev/null @@ -1,19 +0,0 @@ -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/WAConfiguration.java b/src/wa/server/plugin/WAConfiguration.java new file mode 100755 index 0000000..e663adc --- /dev/null +++ b/src/wa/server/plugin/WAConfiguration.java @@ -0,0 +1,19 @@ +package wa.server.plugin; + +import zutil.db.bean.DBBean; + +/** + * Created by Ziver on 2016-07-27. + */ +public abstract class WAConfiguration extends DBBean { + + /** + * Configure or reconfigure service with current configuration data + */ + public abstract void saveConfiguration() throws Exception; + + /** + * Remove the configuration + */ + public abstract void deleteConfiguration() throws Exception; +} diff --git a/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java b/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java index 3118edb..fa2b16d 100755 --- a/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java +++ b/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java @@ -1,141 +1,97 @@ package wa.server.plugin.apache; import wa.server.WAConstants; -import wa.server.WAContext; -import wa.server.page.WAConfigPage; -import wa.server.plugin.WAConfigEntry; -import wa.server.util.ConfigFileUtil; -import zutil.db.bean.DBBean; +import wa.server.plugin.WAConfiguration; import zutil.db.bean.DBBean.DBTable; -import zutil.io.IOUtil; import zutil.ui.Configurator; -import java.io.FileInputStream; import java.io.IOException; import java.io.PrintStream; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -public class ApacheConfigVirtualHost extends WAConfigPage { +@DBTable(WAConstants.DB_TABLE_PREFIX + "_apache_vhost") +public class ApacheConfigVirtualHost extends WAConfiguration { private static final String CONFIG_NAME = "vhost"; private static final String NAVIGATION_NAME = "Apache Virtual Host"; - 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 static final String SITE_AVAILABLE_DIR = "/apache2/site-available/"; + private static final String SITE_ENABLED_DIR = "/apache2/site-enabled/"; - public ApacheConfigVirtualHost() throws SQLException { - super(ApacheService.NAVIGATION_NAME, CONFIG_NAME, NAVIGATION_NAME); - vhosts = DBBean.load(WAContext.getDB(), ApacheVirtualHostEntry.class); + @Configurator.Configurable("Domain") + protected String domain; + protected transient String domain_old; + @Configurator.Configurable("DocRoot") + protected String path; + @Configurator.Configurable("SSL") + protected boolean ssl; + + + + public String getDomain() { + return domain; + } + public void setDomain(String domain) { + this.domain = domain; + } + public String getPath() { + return path; + } + public void setPath(String path) { + this.path = path; + } + public boolean isSSL() { + return ssl; + } + public void setSSL(boolean ssl) { + this.ssl = ssl; } - public WAConfigEntry createConfig(){ - return new ApacheVirtualHostEntry(); - } - public void deleteConfig(int id){ - - } - - - @Override - public void configure() throws Exception { - // Update main configuration file - ConfigFileUtil.writeBetweenBoundary( - WAConstants.getConfigFile(APACHE_MAIN_CONFIG_FILE), - "#", - IOUtil.readContentAsString(new FileInputStream(STATIC_PRE_CONF), true)); - // Write Vhost configuration - for(ApacheVirtualHostEntry vhost : vhosts){ - vhost.configure(); - } - } - @Override - public List getConfigData() { - return vhosts; + public void saveConfiguration() throws IOException { + if(domain != domain_old){ // Configuration file has changed name + WAConstants.getConfigFile(SITE_AVAILABLE_DIR+ domain_old).delete(); + domain_old = domain; + } + PrintStream out = new PrintStream( + WAConstants.getConfigFile(SITE_AVAILABLE_DIR+ domain)); + + saveConfiguration(out); + out.close(); } + + private void saveConfiguration(PrintStream out){ + if(ssl){ + out.println(""); + out.println(" ServerName "+ domain +":80"); + out.println(" RewriteEngine On"); + out.println(" RewriteCond %{SERVER_PORT} !^443$"); + out.println(" RewriteRule ^(.*)$ https://server$1 [L,R]"); + out.println(""); + out.println(""); + out.println(" ServerName "+ domain +":443"); + out.println(" DocumentRoot "+ path); + out.println(); + out.println(" SSLEngine on"); + out.println(" SSLCertificateFile "+ WAConstants.getConfigFile(WAConstants.WA_SSL_CERT)); + out.println(" SSLCertificateKeyFile "+ WAConstants.getConfigFile(WAConstants.WA_SSL_KEY)); + out.println(""); + out.println(""); + } + else { + out.println(""); + out.println(" ServerName "+ domain +":80"); + out.println(" DocumentRoot "+ path); + out.println(""); + out.println(""); + } + } + @Override - public Class getConfigClass() { - return ApacheVirtualHostEntry.class; - } + public void deleteConfiguration() throws Exception { - - @DBTable(WAConstants.DB_TABLE_PREFIX + "_apache_vhost") - public static class ApacheVirtualHostEntry extends WAConfigEntry { - @Configurator.Configurable("Domain") - protected String domain; - protected transient String domain_old; - @Configurator.Configurable("DocRoot") - protected String path; - @Configurator.Configurable("SSL") - protected boolean ssl; - - - - public String getDomain() { - return domain; - } - public void setDomain(String domain) { - this.domain = domain; - } - public String getPath() { - return path; - } - public void setPath(String path) { - this.path = path; - } - public boolean isSSL() { - return ssl; - } - public void setSSL(boolean ssl) { - this.ssl = ssl; - } - - - - public final void configure() throws IOException { - if(domain != domain_old){ // Configuration file has changed name - WAConstants.getConfigFile(APACHE_SITE_AVAILABLE_PATH+ domain_old).delete(); - domain_old = domain; - } - PrintStream out = new PrintStream( - WAConstants.getConfigFile(APACHE_SITE_AVAILABLE_PATH+ domain)); - - writeConfiguration(out); - out.close(); - } - protected void writeConfiguration(PrintStream out){ - if(ssl){ - out.println(""); - out.println(" ServerName "+ domain +":80"); - out.println(" RewriteEngine On"); - out.println(" RewriteCond %{SERVER_PORT} !^443$"); - out.println(" RewriteRule ^(.*)$ https://server$1 [L,R]"); - out.println(""); - out.println(""); - out.println(" ServerName "+ domain +":443"); - out.println(" DocumentRoot "+ path); - out.println(); - out.println(" SSLEngine on"); - out.println(" SSLCertificateFile "+ WAConstants.getConfigFile(WAConstants.WA_SSL_CERT)); - out.println(" SSLCertificateKeyFile "+ WAConstants.getConfigFile(WAConstants.WA_SSL_KEY)); - out.println(""); - out.println(""); - } - else { - out.println(""); - out.println(" ServerName "+ domain +":80"); - out.println(" DocumentRoot "+ path); - out.println(""); - out.println(""); - } - } } } + diff --git a/src/wa/server/plugin/tomcat/TomcatConfigApplication.java b/src/wa/server/plugin/tomcat/TomcatConfigApplication.java index 00a9218..f6e2451 100755 --- a/src/wa/server/plugin/tomcat/TomcatConfigApplication.java +++ b/src/wa/server/plugin/tomcat/TomcatConfigApplication.java @@ -21,32 +21,29 @@ */ package wa.server.plugin.tomcat; -import wa.server.plugin.apache.ApacheConfigVirtualHost.ApacheVirtualHostEntry; - import java.io.PrintStream; public class TomcatConfigApplication { - - - public static class TomcatApplicationEntry extends ApacheVirtualHostEntry { - @Override - public void writeConfiguration(PrintStream out) { - out.println(""); - out.println(" ServerName " + domain + ":80"); - out.println(" "); - out.println(" RewriteEngine On"); - out.println(" RewriteRule ^/$ /" + path + " [R]"); - out.println(" ProxyPreserveHost on"); - out.println(" "); - out.println(" Order deny,allow"); - out.println(" Allow from all"); - out.println(" "); - out.println(" ProxyPass / ajp://localhost:8009/"); - out.println(" ProxyPassReverse / http://localhost:8080/"); - out.println(""); - out.println(""); - } +/* + @Override + public void saveConfiguration(PrintStream out) { + out.println(""); + out.println(" ServerName " + domain + ":80"); + out.println(" "); + out.println(" RewriteEngine On"); + out.println(" RewriteRule ^/$ /" + path + " [R]"); + out.println(" ProxyPreserveHost on"); + out.println(" "); + out.println(" Order deny,allow"); + out.println(" Allow from all"); + out.println(" "); + out.println(" ProxyPass / ajp://localhost:8009/"); + out.println(" ProxyPassReverse / http://localhost:8080/"); + out.println(""); + out.println(""); } + */ } +