Some progress on service configuration

This commit is contained in:
Ziver Koc 2015-06-03 15:08:41 +00:00
parent 29def452b6
commit ab7296198a
10 changed files with 138 additions and 84 deletions

View file

@ -24,7 +24,7 @@ package wa.server.page;
import wa.server.WAContext;
import wa.server.page.struct.WANavigation;
import wa.server.plugin.WAService;
import wa.server.plugin.WAServiceStatus;
import zutil.io.file.FileUtil;
import zutil.log.LogUtil;
import zutil.net.http.HttpHeaderParser;
@ -32,6 +32,7 @@ import zutil.parser.DataNode;
import zutil.parser.Templator;
import zutil.plugin.PluginManager;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
@ -43,17 +44,21 @@ import java.util.logging.Logger;
*/
public class ServiceStatusPage implements WAPage {
private static final Logger log = LogUtil.getLogger();
public static final String NAVIGATION_NAME = "Service Status";
private static final String TMPL_FILE = "WebContent/page/ServiceStatusPage.tmpl";
private ArrayList<WAService> plugins;
private ArrayList<WAServiceStatus> services;
public ServiceStatusPage(WAServiceStatus ss){
services = new ArrayList<>();
services.add(ss);
}
public ServiceStatusPage(PluginManager pluginManager){
this.plugins = pluginManager.toArray(WAService.class);
this.services = pluginManager.toArray(WAServiceStatus.class);
WANavigation nav = new WANavigation("Services");
for(WAService plugin : plugins)
nav.addSubNav(new WANavigation(plugin.getName()));
WANavigation.addRootNav(nav);
WANavigation.getRootNav(ServicePage.NAVIGATION_NAME).addSubNav(
new WANavigation(NAVIGATION_NAME, this));
}
@ -65,13 +70,9 @@ public class ServiceStatusPage implements WAPage {
Map<String, String> request) {
try {
WAService obj = getPlugin(context);
if (obj != null) {
Templator tmpl = new Templator(FileUtil.find(TMPL_FILE));
tmpl.set("nav", context.getBreadcrumb().get(1));
return tmpl;
}
Templator tmpl = new Templator(FileUtil.find(TMPL_FILE));
tmpl.set("services", services);
return tmpl;
}catch (IOException e){
log.log(Level.SEVERE, null, e);
}
@ -83,18 +84,23 @@ public class ServiceStatusPage implements WAPage {
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request){
return null;
}
private WAService getPlugin(WAContext context){
if(context.getBreadcrumb().size() >= 2){
int i = plugins.indexOf(context.getBreadcrumb().get(1).getResource());
if(i >= 0)
return plugins.get(i);
DataNode root = new DataNode(DataNode.DataType.Map);
if (request.containsKey("service_id") && request.containsKey("service_action")) {
int index = Integer.parseInt(request.get("ServiceId"));
if(0 > index || index > services.size()) {
root.set("error", "Invalid service_id");
}
else {
WAServiceStatus service = services.get(index);
switch (request.get("ServiceAction")) {
case "start": service.start(); break;
case "stop": service.stop(); break;
default:
root.set("error", "Unknown action");
}
}
}
return null;
return root;
}
}