Converting service to pure page

This commit is contained in:
Ziver Koc 2016-07-23 00:15:54 +02:00
parent 3a0f40dab9
commit f2b4cfdec7
23 changed files with 140 additions and 219 deletions

View file

@ -24,7 +24,6 @@ package wa.server.page;
import wa.server.WAContext;
import wa.server.plugin.WAConfigEntry;
import wa.server.plugin.WAPage;
import wa.server.plugin.WAServiceConfig;
import zutil.io.file.FileUtil;
import zutil.log.LogUtil;

View file

@ -1,125 +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.WAPage;
import wa.server.plugin.WAService;
import wa.server.plugin.WAServiceConfig;
import zutil.io.file.FileUtil;
import zutil.log.LogUtil;
import zutil.net.http.HttpHeader;
import zutil.parser.Templator;
import zutil.plugin.PluginManager;
import zutil.ui.Navigation;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Created by Ziver on 2015-04-06.
*/
public class ServicePage extends WAPage {
private static final Logger log = LogUtil.getLogger();
public static final String NAVIGATION_NAME = "Services";
private static final String TMPL_FILE = "WebContent/page/ServicePage.tmpl";
private ServiceStatusPage rootStatusPage;
private ArrayList<WAService> services;
private ArrayList<ServiceStatusPage> statusPages;
private ArrayList<LogPage> logPages;
private ArrayList<ConfigPage> configPages;
public ServicePage(){
super.setPageName("service");
PluginManager pluginManager = new PluginManager();
this.services = pluginManager.toArray(WAService.class);
this.rootStatusPage = new ServiceStatusPage(pluginManager);
this.statusPages = new ArrayList<>();
this.logPages = new ArrayList<>();
this.configPages = new ArrayList<>();
Navigation nav = WAContext.getRootNav().createSubNav(this.getPageName(), NAVIGATION_NAME);
nav.setResource(this);
nav.setWeight(100);
for(WAService plugin : services) {
statusPages.add(new ServiceStatusPage(plugin.getStatus()));
logPages.add(new LogPage(plugin.getLog()));
Navigation serviceNav = nav.createSubNav(plugin.getName());
serviceNav.setResource(plugin);
for(WAServiceConfig conf : plugin.getConfigurations()){
ConfigPage page = new ConfigPage(conf);
configPages.add(page);
serviceNav.createSubNav(conf.getName())
.setResource(page);
}
}
}
@Override
public Templator htmlResponse(WAContext context,
HttpHeader client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) {
try {
Object resource = context.getBreadcrumb().get(context.getBreadcrumb().size()-1).getResource();
int index;
if((index = configPages.indexOf(resource)) >= 0){
return configPages.get(index).htmlResponse(context, client_info, session, cookie, request);
}
else if ((index = services.indexOf(resource)) >= 0) {
WAService obj = services.get(index);
ServiceStatusPage statusPage = statusPages.get(index);
LogPage logPage = logPages.get(index);
Templator tmpl = new Templator(FileUtil.find(TMPL_FILE));
if(statusPage != null)
tmpl.set("service_status",
statusPage.htmlResponse(context, client_info, session, cookie, request));
if(logPage != null)
tmpl.set("service_logs",
logPage.htmlResponse(context, client_info, session, cookie, request));
return tmpl;
}
else{ // root page
return rootStatusPage.htmlResponse(context, client_info, session, cookie, request);
}
}catch (IOException e){
log.log(Level.SEVERE, null, e);
}
return null;
}
}

View file

@ -23,14 +23,12 @@
package wa.server.page;
import wa.server.WAContext;
import wa.server.plugin.WAPage;
import wa.server.plugin.WAServiceStatus;
import zutil.io.file.FileUtil;
import zutil.log.LogUtil;
import zutil.net.http.HttpHeader;
import zutil.parser.DataNode;
import zutil.parser.Templator;
import zutil.plugin.PluginManager;
import zutil.ui.Navigation;
import java.io.IOException;
@ -54,12 +52,11 @@ public class ServiceStatusPage extends WAPage {
services = new ArrayList<>();
services.add(ss);
}
public ServiceStatusPage(PluginManager pluginManager){
this.services = pluginManager.toArray(WAServiceStatus.class);
public ServiceStatusPage(){
this.services = WAContext.getPluginManager().toArray(WAServiceStatus.class);
Navigation nav = WAContext.getRootNav().createSubNav(ServicePage.NAVIGATION_NAME)
.createSubNav(NAVIGATION_NAME);
nav.setResource(this);
Navigation nav = WAContext.getRootNav().createSubNav(WAServicePage.NAVIGATION_NAME)
.createSubNav(NAVIGATION_NAME).setWeight(-100);
}

128
src/wa/server/page/WAPage.java Executable file
View file

@ -0,0 +1,128 @@
/*
* Copyright (c) 2015 Ziver
*
* 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 zutil.io.file.FileUtil;
import zutil.log.LogUtil;
import zutil.net.http.HttpHeader;
import zutil.net.http.HttpPage;
import zutil.net.http.HttpPrintStream;
import zutil.parser.DataNode;
import zutil.parser.Templator;
import zutil.parser.json.JSONWriter;
import zutil.ui.Navigation;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Created by Ziver on 2015-04-25.
*/
public abstract class WAPage implements HttpPage{
private static final Logger log = LogUtil.getLogger();
private static final String TMPL_FILE = "WebContent/page/Index.tmpl";
private String pageName;
private Templator tmpl;
public WAPage() {
try {
tmpl = new Templator(FileUtil.find(TMPL_FILE));
} catch(IOException e){
log.log(Level.SEVERE, null, e);
tmpl = new Templator(e.getMessage());
}
}
@Override
public final void respond(HttpPrintStream out,
HttpHeader header,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) throws IOException {
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();
}
}
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());
}
}
public void setPageName(String pageName){
this.pageName = pageName;
}
public String getPageName(){
return pageName;
}
public abstract Templator htmlResponse(WAContext context,
HttpHeader client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) throws IOException;
public DataNode jsonResponse(WAContext context,
HttpHeader client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) {
return null;
}
}

View file

@ -0,0 +1,100 @@
/*
* 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.WAInstaller;
import wa.server.plugin.WALog;
import wa.server.plugin.WAServiceConfig;
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.Navigation;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Created by Ziver on 2015-04-06.
*/
public abstract class WAServicePage extends WAPage {
private static final Logger log = LogUtil.getLogger();
public static final String NAVIGATION_NAME = "Services";
private static final String TMPL_FILE = "WebContent/page/ServicePage.tmpl";
private ServiceStatusPage statusPage;
private LogPage logPage;
public WAServicePage(String pageName, String serviceName){
super.setPageName("service/"+pageName);
Navigation rootNav = WAContext.getRootNav().createSubNav(NAVIGATION_NAME).setWeight(100);
Navigation serviceNav = rootNav.createSubNav(getPageName(), serviceName);
statusPage = new ServiceStatusPage(getStatus());
logPage = new LogPage(getLog());
}
@Override
public Templator htmlResponse(WAContext context,
HttpHeader client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) throws IOException {
Templator tmpl = new Templator(FileUtil.find(TMPL_FILE));
if(statusPage != null)
tmpl.set("service_status",
statusPage.htmlResponse(context, client_info, session, cookie, request));
if(logPage != null)
tmpl.set("service_logs",
logPage.htmlResponse(context, client_info, session, cookie, request));
return tmpl;
}
/**
* @return a service status object or null if it is not possible to check status
*/
public abstract WAServiceStatus getStatus();
/**
* @return a service log object or null if it is not possible to read logs
*/
public abstract WALog getLog();
/**
* @return a installer object that will install the service or null if the installer is not available
*/
public abstract WAInstaller getInstaller();
}

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2015 Ziver
*
* 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;
/**
* Created by Ziver on 2015-04-06.
*/
public abstract class WAStatusPage extends WAPage {
public static final String NAVIGATION_NAME = "Status";
public WAStatusPage(String subPageName, String niceName){
super.setPageName("status/"+subPageName);
WAContext.getRootNav().createSubNav(NAVIGATION_NAME)
.setWeight(0).createSubNav(this.getPageName(), niceName);
}
}