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 { public class WAContext {
private static final Logger logger = LogUtil.getLogger(); private static final Logger logger = LogUtil.getLogger();
private static Navigation rootNav; private static Navigation rootNav;
public static DBConnection db; private static DBConnection db;
private static PluginManager pluginManager; 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,6 +70,7 @@ public abstract class WAPage implements HttpPage{
Map<String, Object> session, Map<String, Object> session,
Map<String, String> cookie, Map<String, String> cookie,
Map<String, String> request) throws IOException { Map<String, String> request) throws IOException {
try {
WAContext context = new WAContext(header); WAContext context = new WAContext(header);
List<Navigation> breadcrumb = context.getBreadcrumb(); List<Navigation> breadcrumb = context.getBreadcrumb();
@ -83,8 +84,7 @@ public abstract class WAPage implements HttpPage{
writer.write(node); writer.write(node);
writer.close(); writer.close();
} }
} } else {
else {
tmpl.clear(); tmpl.clear();
tmpl.set("title", "WebAdmin"); tmpl.set("title", "WebAdmin");
@ -105,6 +105,9 @@ public abstract class WAPage implements HttpPage{
out.print(tmpl.compile()); 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, public abstract Templator htmlResponse(WAContext context,
HttpHeader client_info, HttpHeader client_info,
Map<String, Object> session, Map<String, Object> session,
Map<String, String> cookie, Map<String, String> cookie,
Map<String, String> request) throws IOException; Map<String, String> request) throws Exception;
public DataNode jsonResponse(WAContext context, public DataNode jsonResponse(WAContext context,
HttpHeader client_info, 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,74 +1,23 @@
package wa.server.plugin.apache; package wa.server.plugin.apache;
import wa.server.WAConstants; import wa.server.WAConstants;
import wa.server.WAContext; import wa.server.plugin.WAConfiguration;
import wa.server.page.WAConfigPage;
import wa.server.plugin.WAConfigEntry;
import wa.server.util.ConfigFileUtil;
import zutil.db.bean.DBBean;
import zutil.db.bean.DBBean.DBTable; import zutil.db.bean.DBBean.DBTable;
import zutil.io.IOUtil;
import zutil.ui.Configurator; import zutil.ui.Configurator;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ApacheConfigVirtualHost extends WAConfigPage {
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<>();
public ApacheConfigVirtualHost() throws SQLException {
super(ApacheService.NAVIGATION_NAME, CONFIG_NAME, NAVIGATION_NAME);
vhosts = DBBean.load(WAContext.getDB(), ApacheVirtualHostEntry.class);
}
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;
}
@Override
public Class<? extends WAConfigEntry> getConfigClass() {
return ApacheVirtualHostEntry.class;
}
@DBTable(WAConstants.DB_TABLE_PREFIX + "_apache_vhost") @DBTable(WAConstants.DB_TABLE_PREFIX + "_apache_vhost")
public static class ApacheVirtualHostEntry extends WAConfigEntry { 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 SITE_AVAILABLE_DIR = "/apache2/site-available/";
private static final String SITE_ENABLED_DIR = "/apache2/site-enabled/";
@Configurator.Configurable("Domain") @Configurator.Configurable("Domain")
protected String domain; protected String domain;
protected transient String domain_old; protected transient String domain_old;
@ -100,18 +49,20 @@ public class ApacheConfigVirtualHost extends WAConfigPage {
public final void configure() throws IOException { @Override
public void saveConfiguration() throws IOException {
if(domain != domain_old){ // Configuration file has changed name if(domain != domain_old){ // Configuration file has changed name
WAConstants.getConfigFile(APACHE_SITE_AVAILABLE_PATH+ domain_old).delete(); WAConstants.getConfigFile(SITE_AVAILABLE_DIR+ domain_old).delete();
domain_old = domain; domain_old = domain;
} }
PrintStream out = new PrintStream( PrintStream out = new PrintStream(
WAConstants.getConfigFile(APACHE_SITE_AVAILABLE_PATH+ domain)); WAConstants.getConfigFile(SITE_AVAILABLE_DIR+ domain));
writeConfiguration(out); saveConfiguration(out);
out.close(); out.close();
} }
protected void writeConfiguration(PrintStream out){
private void saveConfiguration(PrintStream out){
if(ssl){ if(ssl){
out.println("<VirtualHost *:80>"); out.println("<VirtualHost *:80>");
out.println(" ServerName "+ domain +":80"); out.println(" ServerName "+ domain +":80");
@ -137,5 +88,10 @@ public class ApacheConfigVirtualHost extends WAConfigPage {
out.println(""); out.println("");
} }
} }
@Override
public void deleteConfiguration() throws Exception {
} }
} }

View file

@ -21,18 +21,14 @@
*/ */
package wa.server.plugin.tomcat; package wa.server.plugin.tomcat;
import wa.server.plugin.apache.ApacheConfigVirtualHost.ApacheVirtualHostEntry;
import java.io.PrintStream; import java.io.PrintStream;
public class TomcatConfigApplication { public class TomcatConfigApplication {
/*
public static class TomcatApplicationEntry extends ApacheVirtualHostEntry {
@Override @Override
public void writeConfiguration(PrintStream out) { public void saveConfiguration(PrintStream out) {
out.println("<VirtualHost *:80>"); out.println("<VirtualHost *:80>");
out.println(" ServerName " + domain + ":80"); out.println(" ServerName " + domain + ":80");
out.println(" "); out.println(" ");
@ -48,5 +44,6 @@ public class TomcatConfigApplication {
out.println("</VirtualHost>"); out.println("</VirtualHost>");
out.println(""); out.println("");
} }
*/
} }
}