Backend implementation of config objects

This commit is contained in:
Ziver Koc 2015-11-16 17:48:37 +01:00
parent 224372f0ad
commit 2d1a7d647c
13 changed files with 215 additions and 131 deletions

2
WebAdmin.iml Normal file → Executable file
View file

@ -12,7 +12,6 @@
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="ZUtil" exported="" />
<orderEntry type="module-library" exported=""> <orderEntry type="module-library" exported="">
<library name="lib"> <library name="lib">
<CLASSES> <CLASSES>
@ -26,5 +25,6 @@
<jarDirectory url="file://$MODULE_DIR$/lib" recursive="false" /> <jarDirectory url="file://$MODULE_DIR$/lib" recursive="false" />
</library> </library>
</orderEntry> </orderEntry>
<orderEntry type="module" module-name="Zutil" exported="" />
</component> </component>
</module> </module>

6
resources/WebContent/page/ConfigPage.tmpl Normal file → Executable file
View file

@ -17,7 +17,7 @@
</td> </td>
{{/.getConfiguration()}} {{/.getConfiguration()}}
<td class="text-right"> <td class="text-right">
<button type="button" class="btn btn-primary btn-xs" title="Edit"> <button type="button" class="btn btn-default btn-xs" title="Edit">
<span class="glyphicon glyphicon-wrench" aria-hidden="true"></span>&nbsp; <span class="glyphicon glyphicon-wrench" aria-hidden="true"></span>&nbsp;
</button> </button>
{{^.enabled}} {{^.enabled}}
@ -26,11 +26,11 @@
</button> </button>
{{/.enabled}} {{/.enabled}}
{{#.enabled}} {{#.enabled}}
<button type="button" class="btn btn-success btn-xs" title="Activate"> <button type="button" class="btn btn-default btn-xs" title="Activate">
<span class="glyphicon glyphicon-ok-circle" aria-hidden="true"></span>&nbsp; <span class="glyphicon glyphicon-ok-circle" aria-hidden="true"></span>&nbsp;
</button> </button>
{{/.enabled}} {{/.enabled}}
<button type="button" class="btn btn-danger btn-xs" title="Remove"> <button type="button" class="btn btn-default btn-xs" title="Remove">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>&nbsp; <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>&nbsp;
</button> </button>
</td> </td>

2
src/wa/server/WAAbstractPage.java Normal file → Executable file
View file

@ -111,7 +111,7 @@ public class WAAbstractPage implements HttpPage{
if(content != null) { if(content != null) {
if(!breadcrumb.isEmpty()) if(!breadcrumb.isEmpty())
content.set("nav", breadcrumb.get(breadcrumb.size() - 1)); content.set("nav", breadcrumb.get(breadcrumb.size() - 1));
tmpl.set("content", content.compile()); tmpl.set("content", content);
} }
out.print(tmpl.compile()); out.print(tmpl.compile());

41
src/wa/server/page/ConfigPage.java Normal file → Executable file
View file

@ -23,9 +23,11 @@
package wa.server.page; package wa.server.page;
import wa.server.WAContext; import wa.server.WAContext;
import wa.server.plugin.WAConfigEntry;
import wa.server.plugin.WAServiceConfig; import wa.server.plugin.WAServiceConfig;
import wa.server.plugin.apache.ApacheConfigVirtualHost; import wa.server.plugin.apache.ApacheConfigVirtualHost;
import zutil.db.bean.Configurator; import zutil.db.bean.Configurator;
import zutil.db.bean.DBBean;
import zutil.io.file.FileUtil; import zutil.io.file.FileUtil;
import zutil.log.LogUtil; import zutil.log.LogUtil;
import zutil.net.http.HttpHeaderParser; import zutil.net.http.HttpHeaderParser;
@ -65,14 +67,36 @@ public class ConfigPage implements WAPage{
Map<String, String> request) { Map<String, String> request) {
try { try {
List<?> confObjs = config.getConfigData(); List<?> confObjs = config.getConfigData();
ArrayList<Configurator> confList = new ArrayList<>(); ArrayList<Configurator<WAConfigEntry>> confList = new ArrayList<>();
for(Object obj : confObjs){ for(Object obj : confObjs){
confList.add(new Configurator(obj)); 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<WAConfigEntry> 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)); Templator tmpl = new Templator(FileUtil.find(TMPL_FILE));
tmpl.set("params", Configurator.getConfiguration( tmpl.set("params", Configurator.getConfiguration(
config.getConfigClass())); config.getConfigClass()));
@ -92,4 +116,13 @@ public class ConfigPage implements WAPage{
Map<String, String> request) { Map<String, String> request) {
return null; 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;
}
} }

View file

@ -1,85 +1,92 @@
/* /*
* Copyright (c) 2015 ezivkoc * Copyright (c) 2015 ezivkoc
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * 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 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package wa.server.page; package wa.server.page;
import sun.reflect.generics.reflectiveObjects.NotImplementedException; import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import zutil.net.http.HttpHeaderParser; import wa.server.plugin.WALog;
import zutil.net.http.HttpPage; import zutil.net.http.HttpHeaderParser;
import zutil.net.http.HttpPrintStream; import zutil.net.http.HttpPage;
import zutil.parser.DataNode; import zutil.net.http.HttpPrintStream;
import zutil.parser.json.JSONWriter; import zutil.parser.DataNode;
import zutil.struct.CircularBuffer; import zutil.parser.json.JSONWriter;
import zutil.struct.CircularBuffer;
import java.io.IOException;
import java.util.Iterator; import java.io.IOException;
import java.util.Map; import java.util.Iterator;
import java.util.logging.Handler; import java.util.Map;
import java.util.logging.LogRecord; import java.util.logging.Handler;
import java.util.logging.LogRecord;
/**
* Created by Ziver on 2015-09-22. /**
*/ * Created by Ziver on 2015-09-22.
public class WALogPage extends Handler implements HttpPage { */
private CircularBuffer<LogRecord> logBuffer; public class LogPage extends Handler implements HttpPage {
private CircularBuffer<LogRecord> logBuffer;
@Override private WALog log;
public void publish(LogRecord record) {
if(super.isLoggable(record)){ public LogPage(WALog log) {
logBuffer.add(record); logBuffer = new CircularBuffer<>(500);
} this.log = log;
} }
@Override
@Override public void publish(LogRecord record) {
public void flush() {} if(super.isLoggable(record)){
@Override logBuffer.add(record);
public void close() throws SecurityException { }
throw new NotImplementedException(); }
}
@Override
@Override public void flush() {}
public void respond(HttpPrintStream out, @Override
HttpHeaderParser client_info, public void close() throws SecurityException {
Map<String, Object> session, throw new NotImplementedException();
Map<String, String> cookie, }
Map<String, String> request) throws IOException {
DataNode logNode = new DataNode(DataNode.DataType.List); @Override
Iterator<LogRecord> it = logBuffer.iterator(); public void respond(HttpPrintStream out,
for(int i=0; i<20 && it.hasNext(); ++i){ HttpHeaderParser client_info,
LogRecord record = it.next(); Map<String, Object> session,
DataNode node = new DataNode(DataNode.DataType.Map); Map<String, String> cookie,
node.set("timestamp", record.getMillis()); Map<String, String> request) throws IOException {
node.set("source", record.getLoggerName());
node.set("msg", record.getMessage()); DataNode logNode = new DataNode(DataNode.DataType.List);
logNode.add(node); Iterator<LogRecord> it = logBuffer.iterator();
} for(int i=0; i<20 && it.hasNext(); ++i){
LogRecord record = it.next();
DataNode root = new DataNode(DataNode.DataType.Map); DataNode node = new DataNode(DataNode.DataType.Map);
root.add(logNode); node.set("timestamp", record.getMillis());
JSONWriter writer = new JSONWriter(out); node.set("source", record.getLoggerName());
writer.write(root); node.set("msg", record.getMessage());
writer.close(); logNode.add(node);
} }
}
DataNode root = new DataNode(DataNode.DataType.Map);
root.add(logNode);
JSONWriter writer = new JSONWriter(out);
writer.write(root);
writer.close();
}
}

14
src/wa/server/page/ServicePage.java Normal file → Executable file
View file

@ -50,18 +50,21 @@ public class ServicePage implements WAPage {
private ServiceStatusPage rootStatusPage; private ServiceStatusPage rootStatusPage;
private ArrayList<WAService> services; private ArrayList<WAService> services;
private ArrayList<ServiceStatusPage> statusPages; private ArrayList<ServiceStatusPage> statusPages;
private ArrayList<LogPage> logPages;
private ArrayList<ConfigPage> configPages; private ArrayList<ConfigPage> configPages;
public ServicePage(PluginManager pluginManager){ public ServicePage(PluginManager pluginManager){
this.services = pluginManager.toArray(WAService.class); this.services = pluginManager.toArray(WAService.class);
this.rootStatusPage = new ServiceStatusPage(pluginManager); this.rootStatusPage = new ServiceStatusPage(pluginManager);
this.statusPages = new ArrayList<>(); this.statusPages = new ArrayList<>();
this.logPages = new ArrayList<>();
this.configPages = new ArrayList<>(); this.configPages = new ArrayList<>();
WANavigation nav = WANavigation.createRootNav(NAVIGATION_NAME); WANavigation nav = WANavigation.createRootNav(NAVIGATION_NAME);
nav.setResource(this); nav.setResource(this);
for(WAService plugin : services) { for(WAService plugin : services) {
statusPages.add(new ServiceStatusPage(plugin.getStatus())); statusPages.add(new ServiceStatusPage(plugin.getStatus()));
logPages.add(new LogPage(plugin.getLog()));
WANavigation serviceNav = nav.createSubNav(plugin.getName()); WANavigation serviceNav = nav.createSubNav(plugin.getName());
serviceNav.setResource(plugin); serviceNav.setResource(plugin);
@ -91,12 +94,15 @@ public class ServicePage implements WAPage {
else if ((index = services.indexOf(resource)) >= 0) { else if ((index = services.indexOf(resource)) >= 0) {
WAService obj = services.get(index); WAService obj = services.get(index);
ServiceStatusPage statusPage = statusPages.get(index); ServiceStatusPage statusPage = statusPages.get(index);
LogPage logPage = logPages.get(index);
Templator tmpl = new Templator(FileUtil.find(TMPL_FILE)); Templator tmpl = new Templator(FileUtil.find(TMPL_FILE));
tmpl.set("service_status", if(statusPage != null)
statusPage.htmlResponse(context, client_info, session, cookie, request).compile()); tmpl.set("service_status",
//tmpl.set("service_logs", statusPage.htmlResponse(context, client_info, session, cookie, request));
// statusPage.htmlResponse(context, client_info, session, cookie, request).compile()); if(logPage != null)
tmpl.set("service_logs",
statusPage.htmlResponse(context, client_info, session, cookie, request));
return tmpl; return tmpl;
} }
else{ // root page else{ // root page

View file

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

View file

@ -0,0 +1,7 @@
package wa.server.plugin;
/**
* Created by Ziver on 2015-11-16.
*/
public class WALog {
}

5
src/wa/server/plugin/WAService.java Normal file → Executable file
View file

@ -9,6 +9,11 @@ public interface WAService {
*/ */
public WAServiceStatus getStatus(); 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 * @return a installer object that will install the service or null if the installer is not available
*/ */

11
src/wa/server/plugin/WAServiceConfig.java Normal file → Executable file
View file

@ -12,12 +12,15 @@ public interface WAServiceConfig {
/** /**
* Read in current configuration data * Read in current configuration data
*/ */
public void read() throws Exception; //public void read() throws Exception;
/** /**
* Save configured data to disk or database * 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 * Configure service with current configuration data
@ -31,10 +34,10 @@ public interface WAServiceConfig {
* configuration can be changed with the * configuration can be changed with the
* {@link zutil.db.bean.Configurator} class. * {@link zutil.db.bean.Configurator} class.
*/ */
public List<?> getConfigData(); public List<? extends WAConfigEntry> getConfigData();
/** /**
* @return the class that contains the configuration data * @return the class that contains the configuration data
*/ */
public Class<?> getConfigClass(); public Class<? extends WAConfigEntry> getConfigClass();
} }

View file

@ -1,6 +1,7 @@
package wa.server.plugin.apache; package wa.server.plugin.apache;
import wa.server.WAConstants; import wa.server.WAConstants;
import wa.server.plugin.WAConfigEntry;
import wa.server.plugin.WAServiceConfig; import wa.server.plugin.WAServiceConfig;
import wa.server.util.ConfigFileUtil; import wa.server.util.ConfigFileUtil;
import zutil.db.DBConnection; import zutil.db.DBConnection;
@ -16,30 +17,30 @@ import java.util.List;
public class ApacheConfigVirtualHost implements WAServiceConfig{ public class ApacheConfigVirtualHost implements WAServiceConfig{
private static final String CONFIG_NAME = "Apache Virtual Host"; 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 static final String STATIC_PRE_CONF = "wa/server/plugin/apache/apache_default.config";
private List<ApacheVirtualHostData> vhosts = new ArrayList<>(); private List<ApacheVirtualHostEntry> vhosts = new ArrayList<>();
public ApacheConfigVirtualHost() throws Exception {
DBConnection db = WAConstants.getDB();
vhosts = DBBean.load(db, ApacheVirtualHostEntry.class);
}
@Override @Override
public String getName() { public String getName() {
return CONFIG_NAME; return CONFIG_NAME;
} }
@Override public WAConfigEntry createConfig(){
public void read() throws Exception { return new ApacheVirtualHostEntry();
DBConnection db = WAConstants.getDB(); }
vhosts = DBBean.load(db, ApacheVirtualHostData.class); public void deleteConfig(int id){
}
}
@Override
public void save() throws Exception {
DBConnection db = WAConstants.getDB();
for(ApacheVirtualHostData vhost : vhosts){
vhost.save(db);
}
}
@Override @Override
public void configure() throws Exception { public void configure() throws Exception {
@ -51,23 +52,23 @@ public class ApacheConfigVirtualHost implements WAServiceConfig{
new FileInputStream(STATIC_PRE_CONF))); new FileInputStream(STATIC_PRE_CONF)));
// Write Vhost configuration // Write Vhost configuration
for(ApacheVirtualHostData vhost : vhosts){ for(ApacheVirtualHostEntry vhost : vhosts){
vhost.configure(); vhost.configure();
} }
} }
@Override @Override
public List<?> getConfigData() { public List<? extends WAConfigEntry> getConfigData() {
return vhosts; return vhosts;
} }
@Override @Override
public Class<?> getConfigClass() { public Class<? extends WAConfigEntry> getConfigClass() {
return ApacheVirtualHostData.class; return ApacheVirtualHostEntry.class;
} }
@DBTable(WAConstants.DB_TABLE_PREFIX + "_apache_vhost") @DBTable(WAConstants.DB_TABLE_PREFIX + "_apache_vhost")
public static class ApacheVirtualHostData extends DBBean{ public static class ApacheVirtualHostEntry extends WAConfigEntry {
@Configurable("Domain") @Configurable("Domain")
protected String domain; protected String domain;
protected transient String domain_old; protected transient String domain_old;
@ -101,10 +102,11 @@ public class ApacheConfigVirtualHost implements WAServiceConfig{
public final void configure() throws IOException { public final void configure() throws IOException {
if(domain != domain_old){ // Configuration file has changed name 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( PrintStream out = new PrintStream(
WAConstants.getConfigFile("apache2/enabled/"+ domain)); WAConstants.getConfigFile(APACHE_SITE_AVAILABLE_PATH+ domain));
writeConfiguration(out); writeConfiguration(out);
out.close(); out.close();
@ -129,8 +131,8 @@ public class ApacheConfigVirtualHost implements WAServiceConfig{
} }
else { else {
out.println("<VirtualHost *:80>"); out.println("<VirtualHost *:80>");
out.println(" ServerName " + domain + ":80"); out.println(" ServerName "+ domain +":80");
out.println(" DocumentRoot " + path); out.println(" DocumentRoot "+ path);
out.println("</VirtualHost>"); out.println("</VirtualHost>");
out.println(""); out.println("");
} }

10
src/wa/server/plugin/apache/ApacheService.java Normal file → Executable file
View file

@ -22,10 +22,7 @@
package wa.server.plugin.apache; package wa.server.plugin.apache;
import wa.server.plugin.WAServiceConfig; import wa.server.plugin.*;
import wa.server.plugin.WAInstaller;
import wa.server.plugin.WAService;
import wa.server.plugin.WAServiceStatus;
import zutil.log.LogUtil; import zutil.log.LogUtil;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -53,6 +50,11 @@ public class ApacheService implements WAService {
return status; return status;
} }
@Override
public WALog getLog() {
return null;
}
@Override @Override
public WAInstaller getInstaller() { public WAInstaller getInstaller() {
if(installer == null) if(installer == null)

View file

@ -23,14 +23,14 @@ package wa.server.plugin.tomcat;
import java.io.PrintStream; import java.io.PrintStream;
import wa.server.plugin.apache.ApacheConfigVirtualHost.ApacheVirtualHostData; import wa.server.plugin.apache.ApacheConfigVirtualHost.ApacheVirtualHostEntry;
public class TomcatConfigApplication { public class TomcatConfigApplication {
public static class TomcatApplicationData extends ApacheVirtualHostData { public static class TomcatApplicationEntry extends ApacheVirtualHostEntry {
@Override @Override
public void writeConfiguration(PrintStream out) { public void writeConfiguration(PrintStream out) {
out.println("<VirtualHost *:80>"); out.println("<VirtualHost *:80>");