Refactoring of config objs

This commit is contained in:
Ziver Koc 2016-07-27 02:45:52 +02:00
parent 4c4d424ff7
commit af1d6546eb
8 changed files with 256 additions and 330 deletions

View file

@ -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;

View file

@ -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<? extends WAConfiguration> configClass;
public ConfigPage(Class<? extends WAConfiguration> configClass) {
//super(WAServicePage.NAVIGATION_NAME +"/"+ serviceName +"/"+ name);
this.configClass = configClass;
}
@Override
public Templator htmlResponse(WAContext context,
HttpHeader client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) throws Exception {
try {
DBConnection db = WAContext.getDB();
List<WAConfiguration> 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<WAConfiguration>(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<WAConfiguration> getAllConfigs(){
return null;
}
}

View file

@ -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<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) {
try {
List<?> confObjs = getConfigData();
ArrayList<Configurator<WAConfigEntry>> confList = new ArrayList<>();
for(Object obj : confObjs){
confList.add(new Configurator(obj));
}
// Actions
int id = Integer.parseInt(request.get("id"));
Configurator<WAConfigEntry> 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<WAConfigEntry> findObj(List<Configurator<WAConfigEntry>> list, int id){
for (Configurator<WAConfigEntry> 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<? extends WAConfigEntry> getConfigData();
/**
* @return the class that contains the configuration data
*/
public abstract Class<? extends WAConfigEntry> getConfigClass();
}

View file

@ -70,40 +70,43 @@ public abstract class WAPage implements HttpPage{
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) throws IOException {
WAContext context = new WAContext(header);
List<Navigation> breadcrumb = context.getBreadcrumb();
try {
WAContext context = new WAContext(header);
List<Navigation> 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<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) throws IOException;
Map<String, String> request) throws Exception;
public DataNode jsonResponse(WAContext context,
HttpHeader client_info,

View file

@ -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;
}
}

View file

@ -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;
}

View file

@ -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<ApacheVirtualHostEntry> 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<? extends WAConfigEntry> 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("<VirtualHost *:80>");
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("</VirtualHost>");
out.println("<VirtualHost *:443>");
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("</VirtualHost>");
out.println("");
}
else {
out.println("<VirtualHost *:80>");
out.println(" ServerName "+ domain +":80");
out.println(" DocumentRoot "+ path);
out.println("</VirtualHost>");
out.println("");
}
}
@Override
public Class<? extends WAConfigEntry> 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("<VirtualHost *:80>");
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("</VirtualHost>");
out.println("<VirtualHost *:443>");
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("</VirtualHost>");
out.println("");
}
else {
out.println("<VirtualHost *:80>");
out.println(" ServerName "+ domain +":80");
out.println(" DocumentRoot "+ path);
out.println("</VirtualHost>");
out.println("");
}
}
}
}

View file

@ -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("<VirtualHost *:80>");
out.println(" ServerName " + domain + ":80");
out.println(" ");
out.println(" RewriteEngine On");
out.println(" RewriteRule ^/$ /" + path + " [R]");
out.println(" ProxyPreserveHost on");
out.println(" <Proxy *>");
out.println(" Order deny,allow");
out.println(" Allow from all");
out.println(" </Proxy>");
out.println(" ProxyPass / ajp://localhost:8009/");
out.println(" ProxyPassReverse / http://localhost:8080/");
out.println("</VirtualHost>");
out.println("");
}
/*
@Override
public void saveConfiguration(PrintStream out) {
out.println("<VirtualHost *:80>");
out.println(" ServerName " + domain + ":80");
out.println(" ");
out.println(" RewriteEngine On");
out.println(" RewriteRule ^/$ /" + path + " [R]");
out.println(" ProxyPreserveHost on");
out.println(" <Proxy *>");
out.println(" Order deny,allow");
out.println(" Allow from all");
out.println(" </Proxy>");
out.println(" ProxyPass / ajp://localhost:8009/");
out.println(" ProxyPassReverse / http://localhost:8080/");
out.println("</VirtualHost>");
out.println("");
}
*/
}