Merge remote-tracking branch 'origin/dev-osgi' into dev-osgi
# Conflicts: # webinterface/build.gradle # webinterface/src/main/java/asys/webinterface/IndexHandler.java # webinterface/src/main/java/asys/webinterface/WebinterfaceImpl.java
This commit is contained in:
@@ -16,9 +16,13 @@ import org.slf4j.LoggerFactory;
|
|||||||
public class Activator implements BundleActivator, ServiceListener {
|
public class Activator implements BundleActivator, ServiceListener {
|
||||||
private final Logger logger = LoggerFactory.getLogger(Activator.class);
|
private final Logger logger = LoggerFactory.getLogger(Activator.class);
|
||||||
private ServiceTracker<?, Webinterface> serviceTracker;
|
private ServiceTracker<?, Webinterface> serviceTracker;
|
||||||
|
private MCSM_WebModule module;
|
||||||
|
private Webinterface webinterface;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(BundleContext context) throws Exception {
|
public void start(BundleContext context) throws Exception {
|
||||||
|
module = new MCSM_WebModule();
|
||||||
|
|
||||||
logger.debug("Register service listener");
|
logger.debug("Register service listener");
|
||||||
context.addServiceListener(this);
|
context.addServiceListener(this);
|
||||||
|
|
||||||
@@ -28,6 +32,9 @@ public class Activator implements BundleActivator, ServiceListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop(BundleContext context) throws Exception {
|
public void stop(BundleContext context) throws Exception {
|
||||||
|
if (webinterface != null) {
|
||||||
|
webinterface.removeModule(module.getName());
|
||||||
|
}
|
||||||
serviceTracker.close();
|
serviceTracker.close();
|
||||||
context.removeServiceListener(this);
|
context.removeServiceListener(this);
|
||||||
}
|
}
|
||||||
@@ -40,11 +47,9 @@ public class Activator implements BundleActivator, ServiceListener {
|
|||||||
if (classStr.equals(Webinterface.class.getCanonicalName())) {
|
if (classStr.equals(Webinterface.class.getCanonicalName())) {
|
||||||
try {
|
try {
|
||||||
serviceTracker.open();
|
serviceTracker.open();
|
||||||
Webinterface webinterface = serviceTracker.waitForService(5000);
|
webinterface = serviceTracker.waitForService(5000);
|
||||||
if (webinterface != null) {
|
if (webinterface != null) {
|
||||||
webinterface.addMainMenuItem("mcsmanager",
|
webinterface.addModule(module);
|
||||||
"Серверы", "/modules/mcsmanager");
|
|
||||||
return;
|
|
||||||
} else {
|
} else {
|
||||||
logger.debug("service not found =(");
|
logger.debug("service not found =(");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2017-03-15
|
||||||
|
*/
|
||||||
|
package asys.mcsmanager;
|
||||||
|
|
||||||
|
import asys.webinterface.api.HttpReqResp;
|
||||||
|
import asys.webinterface.api.WebModule;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class MCSM_WebModule implements WebModule {
|
||||||
|
private final String MODULE_NAME = "mcsmanager";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return MODULE_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getStylesheetsLinks() {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getJavaScriptLinks() {
|
||||||
|
return Collections.singletonList("/module/"+MODULE_NAME+"/module.js");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, String> getMainMenuItems() {
|
||||||
|
return new HashMap<String, String>(){{
|
||||||
|
this.put("Серверы", "/module/"+MODULE_NAME);
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(HttpReqResp httpReqResp) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
group = 'asys'
|
group = 'asys'
|
||||||
version = '0.14-SNAPSHOT'
|
version = '0.15-SNAPSHOT'
|
||||||
|
|
||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
*/
|
*/
|
||||||
package asys.webinterface;
|
package asys.webinterface;
|
||||||
|
|
||||||
|
import asys.webinterface.api.HttpReqResp;
|
||||||
|
import asys.webinterface.api.WebModule;
|
||||||
import asys.webinterface.api.Webinterface;
|
import asys.webinterface.api.Webinterface;
|
||||||
import org.osgi.framework.BundleActivator;
|
import org.osgi.framework.BundleActivator;
|
||||||
import org.osgi.framework.BundleContext;
|
import org.osgi.framework.BundleContext;
|
||||||
@@ -11,6 +13,8 @@ import org.osgi.framework.ServiceRegistration;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
public class Activator implements BundleActivator {
|
public class Activator implements BundleActivator {
|
||||||
private final Logger logger = LoggerFactory.getLogger(Activator.class);
|
private final Logger logger = LoggerFactory.getLogger(Activator.class);
|
||||||
private ServiceRegistration<?> serviceMainMenu;
|
private ServiceRegistration<?> serviceMainMenu;
|
||||||
@@ -19,8 +23,7 @@ public class Activator implements BundleActivator {
|
|||||||
@Override
|
@Override
|
||||||
public void start(BundleContext context) throws Exception {
|
public void start(BundleContext context) throws Exception {
|
||||||
WebinterfaceImpl webinterface = new WebinterfaceImpl();
|
WebinterfaceImpl webinterface = new WebinterfaceImpl();
|
||||||
webinterface.addMainMenuItem("modules", "Модули", "#");
|
webinterface.addModule(createSelfModule());
|
||||||
webinterface.addJavascript("modules", "/static/module.js");
|
|
||||||
|
|
||||||
logger.trace("Register service: {}", Webinterface.class);
|
logger.trace("Register service: {}", Webinterface.class);
|
||||||
serviceMainMenu = context.registerService(Webinterface.class, webinterface, null);
|
serviceMainMenu = context.registerService(Webinterface.class, webinterface, null);
|
||||||
@@ -29,6 +32,36 @@ public class Activator implements BundleActivator {
|
|||||||
webServer.start(8778);
|
webServer.start(8778);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private WebModule createSelfModule() {
|
||||||
|
return new WebModule() {
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "modules";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getStylesheetsLinks() {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getJavaScriptLinks() {
|
||||||
|
return Collections.singletonList("/static/module.js");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, String> getMainMenuItems() {
|
||||||
|
return new HashMap<String, String>(){{
|
||||||
|
this.put("Модули", "/modules");
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(HttpReqResp httpReqResp) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop(BundleContext context) throws Exception {
|
public void stop(BundleContext context) throws Exception {
|
||||||
webServer.stop();
|
webServer.stop();
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2017-03-15
|
||||||
|
*/
|
||||||
|
package asys.webinterface;
|
||||||
|
|
||||||
|
import asys.webinterface.api.HttpReqResp;
|
||||||
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
|
|
||||||
|
public class HttpReqRespImpl implements HttpReqResp {
|
||||||
|
private HttpExchange httpExchange;
|
||||||
|
|
||||||
|
public HttpReqRespImpl(HttpExchange httpExchange) {
|
||||||
|
this.httpExchange = httpExchange;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] getRequestURIParts() {
|
||||||
|
String uriStr = httpExchange.getRequestURI().toString().replaceAll("/{2,}", "/");
|
||||||
|
if (uriStr.startsWith("/")) {
|
||||||
|
uriStr = uriStr.replaceFirst("/","");
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] result = uriStr.split("/");
|
||||||
|
if (result.length == 1 && result[0].isEmpty()) {
|
||||||
|
return new String[0];
|
||||||
|
} else {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,24 +4,28 @@
|
|||||||
*/
|
*/
|
||||||
package asys.webinterface;
|
package asys.webinterface;
|
||||||
|
|
||||||
|
import asys.webinterface.api.HttpReqResp;
|
||||||
|
import asys.webinterface.api.WebModule;
|
||||||
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
import com.sun.net.httpserver.HttpHandler;
|
import com.sun.net.httpserver.HttpHandler;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
public class IndexHandler implements HttpHandler {
|
public class IndexHandler implements HttpHandler {
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(IndexHandler.class);
|
||||||
static final Charset defaultCharset = Charset.forName("UTF-8");
|
static final Charset defaultCharset = Charset.forName("UTF-8");
|
||||||
private String htmlTemplate;
|
private String htmlTemplate;
|
||||||
|
private WebinterfaceImpl webinterface;
|
||||||
|
|
||||||
public IndexHandler(WebinterfaceImpl webinterface) {
|
public IndexHandler(WebinterfaceImpl webinterface) {
|
||||||
try {
|
try {
|
||||||
InputStream inputStream = getClass().getResourceAsStream("/index.html");
|
InputStream inputStream = getClass().getResourceAsStream("/index.html");
|
||||||
WebinterfaceImpl.Module module = webinterface.getModule("modules");
|
htmlTemplate = IOUtils.toString(inputStream, "UTF-8");
|
||||||
htmlTemplate = IOUtils.toString(inputStream, "UTF-8")
|
this.webinterface = webinterface;
|
||||||
.replace("<{moduleScript}>", module.javascriptToTags())
|
|
||||||
.replace("<{moduleStylesheet}>", module.stylesheetToTags());
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("Error load htmlTemplate", e);
|
throw new RuntimeException("Error load htmlTemplate", e);
|
||||||
}
|
}
|
||||||
@@ -29,10 +33,32 @@ public class IndexHandler implements HttpHandler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(HttpExchange httpExchange) throws IOException {
|
public void handle(HttpExchange httpExchange) throws IOException {
|
||||||
|
HttpReqResp httpReqResp = new HttpReqRespImpl(httpExchange);
|
||||||
|
String[] requestURIParts = httpReqResp.getRequestURIParts();
|
||||||
|
if (requestURIParts.length > 0 && requestURIParts[0].equals("module")) {
|
||||||
|
logger.debug("Module request: {}", requestURIParts[1]);
|
||||||
|
WebModule module = webinterface.getModule(requestURIParts[1]);
|
||||||
|
if (module != null) {
|
||||||
|
sendHtmlTemplate(prepareHtmlTemplate(module), httpExchange);
|
||||||
|
} else {
|
||||||
|
logger.warn("404 - module \"{}\" not found", requestURIParts[1]);
|
||||||
|
}
|
||||||
|
} else if (requestURIParts.length == 0 || requestURIParts[0].equals("modules")) {
|
||||||
|
logger.debug("use default module");
|
||||||
|
sendHtmlTemplate(prepareHtmlTemplate(webinterface.getModule("modules")), httpExchange);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] prepareHtmlTemplate(WebModule module) {
|
||||||
|
return htmlTemplate.replace("<{moduleScript}>", webinterface.javascriptToTags(module))
|
||||||
|
.replace("<{moduleStylesheet}>", webinterface.stylesheetToTags(module)).getBytes(defaultCharset);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendHtmlTemplate(byte[] htmlTemplateBytes, HttpExchange httpExchange) throws IOException {
|
||||||
httpExchange.getResponseHeaders().add("Context-Type","text/html;charset=utf-8");
|
httpExchange.getResponseHeaders().add("Context-Type","text/html;charset=utf-8");
|
||||||
httpExchange.sendResponseHeaders(200, htmlTemplate.length());
|
httpExchange.sendResponseHeaders(200, htmlTemplateBytes.length);
|
||||||
OutputStream responseBody = httpExchange.getResponseBody();
|
OutputStream responseBody = httpExchange.getResponseBody();
|
||||||
responseBody.write(htmlTemplate.getBytes(defaultCharset));
|
responseBody.write(htmlTemplateBytes);
|
||||||
responseBody.close();
|
responseBody.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,115 +4,37 @@
|
|||||||
*/
|
*/
|
||||||
package asys.webinterface;
|
package asys.webinterface;
|
||||||
|
|
||||||
|
import asys.webinterface.api.WebModule;
|
||||||
import asys.webinterface.api.Webinterface;
|
import asys.webinterface.api.Webinterface;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class WebinterfaceImpl implements Webinterface {
|
public class WebinterfaceImpl implements Webinterface {
|
||||||
private Map<String, Module> modules = new HashMap<>();
|
private Map<String, WebModule> webModules = new HashMap<>();
|
||||||
private Map<String, List<MainMenuItem>> menuItems = new HashMap<>();
|
|
||||||
|
|
||||||
public class MainMenuItem {
|
@Override
|
||||||
public String title, link;
|
public void addModule(WebModule webModule) {
|
||||||
|
webModules.put(webModule.getName(), webModule);
|
||||||
public MainMenuItem(String title, String link) {
|
|
||||||
this.title = title;
|
|
||||||
this.link = link;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Module {
|
|
||||||
List<String> javascripts = new ArrayList<>();
|
|
||||||
List<String> stylesheets = new ArrayList<>();
|
|
||||||
|
|
||||||
public String javascriptToTags() {
|
|
||||||
StringJoiner sj = new StringJoiner("\n");
|
|
||||||
|
|
||||||
for (String link : javascripts) {
|
|
||||||
sj.add(String.format("<script src=\"%s\"></script>", link));
|
|
||||||
}
|
|
||||||
|
|
||||||
return sj.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String stylesheetToTags() {
|
|
||||||
StringJoiner sj = new StringJoiner("\n");
|
|
||||||
|
|
||||||
for (String link : stylesheets) {
|
|
||||||
sj.add(String.format("<link rel=\"stylesheet\" href=\"%s\">", link));
|
|
||||||
}
|
|
||||||
|
|
||||||
return sj.toString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addJavascript(String moduleName, String link) {
|
public void removeModule(String moduleName) {
|
||||||
Module module;
|
webModules.remove(moduleName);
|
||||||
if (modules.containsKey(moduleName)) {
|
|
||||||
module = modules.get(moduleName);
|
|
||||||
} else {
|
|
||||||
module = new Module();
|
|
||||||
modules.put(moduleName, module);
|
|
||||||
}
|
|
||||||
|
|
||||||
module.javascripts.add(link);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeJavascript(String moduleName) {
|
public WebModule getModule(String moduleName) {
|
||||||
if (modules.containsKey(moduleName)) {
|
return webModules.get(moduleName);
|
||||||
Module module = modules.get(moduleName);
|
|
||||||
module.javascripts.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addStylesheet(String moduleName, String link) {
|
|
||||||
Module module;
|
|
||||||
if (modules.containsKey(moduleName)) {
|
|
||||||
module = modules.get(moduleName);
|
|
||||||
} else {
|
|
||||||
module = new Module();
|
|
||||||
modules.put(moduleName, module);
|
|
||||||
}
|
|
||||||
|
|
||||||
module.stylesheets.add(link);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeStylesheet(String moduleName) {
|
|
||||||
if (modules.containsKey(moduleName)) {
|
|
||||||
Module module = modules.get(moduleName);
|
|
||||||
module.stylesheets.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addMainMenuItem(String moduleName, String title, String link) {
|
|
||||||
List<MainMenuItem> list;
|
|
||||||
if (menuItems.containsKey(moduleName)) {
|
|
||||||
list = menuItems.get(moduleName);
|
|
||||||
} else {
|
|
||||||
list = new ArrayList<>();
|
|
||||||
menuItems.put(moduleName, list);
|
|
||||||
}
|
|
||||||
list.add(new MainMenuItem(title, link));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeMainMenuItems(String moduleName) {
|
|
||||||
menuItems.remove(moduleName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String mainMenuItemsToReactJS() {
|
public String mainMenuItemsToReactJS() {
|
||||||
StringJoiner sj = new StringJoiner(",");
|
StringJoiner sj = new StringJoiner(",");
|
||||||
|
|
||||||
for(List<MainMenuItem> listItems : menuItems.values()) {
|
for (WebModule webModule : webModules.values()) {
|
||||||
for (MainMenuItem item : listItems) {
|
for(Map.Entry<String, String> entry : webModule.getMainMenuItems().entrySet()) {
|
||||||
sj.add(String.format(
|
sj.add(String.format(
|
||||||
"ce(MainMenuItem, {title: '%s', href: '%s'})",
|
"ce(MainMenuItem, {title: '%s', href: '%s'})",
|
||||||
item.title.replace("'","\\'"), item.link.replace("'","\\'")
|
entry.getKey().replace("'","\\'"), entry.getValue().replace("'","\\'")
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -120,7 +42,23 @@ public class WebinterfaceImpl implements Webinterface {
|
|||||||
return sj.toString();
|
return sj.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Module getModule(String moduleName) {
|
public String javascriptToTags(WebModule webModule) {
|
||||||
return this.modules.get(moduleName);
|
StringJoiner sj = new StringJoiner("\n");
|
||||||
|
|
||||||
|
for (String link : webModule.getJavaScriptLinks()) {
|
||||||
|
sj.add(String.format("<script src=\"%s\"></script>", link));
|
||||||
|
}
|
||||||
|
|
||||||
|
return sj.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String stylesheetToTags(WebModule webModule) {
|
||||||
|
StringJoiner sj = new StringJoiner("\n");
|
||||||
|
|
||||||
|
for (String link : webModule.getJavaScriptLinks()) {
|
||||||
|
sj.add(String.format("<link rel=\"stylesheet\" href=\"%s\">", link));
|
||||||
|
}
|
||||||
|
|
||||||
|
return sj.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2017-03-15
|
||||||
|
*/
|
||||||
|
package asys.webinterface.api;
|
||||||
|
|
||||||
|
public interface HttpReqResp {
|
||||||
|
String[] getRequestURIParts();
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2017-03-15
|
||||||
|
*/
|
||||||
|
package asys.webinterface.api;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public interface WebModule {
|
||||||
|
String getName();
|
||||||
|
List<String> getStylesheetsLinks();
|
||||||
|
List<String> getJavaScriptLinks();
|
||||||
|
Map<String, String> getMainMenuItems();
|
||||||
|
void handle(HttpReqResp httpReqResp);
|
||||||
|
}
|
||||||
@@ -5,12 +5,7 @@
|
|||||||
package asys.webinterface.api;
|
package asys.webinterface.api;
|
||||||
|
|
||||||
public interface Webinterface {
|
public interface Webinterface {
|
||||||
void addJavascript(String moduleName, String link);
|
void addModule(WebModule webModule);
|
||||||
void removeJavascript(String moduleName);
|
void removeModule(String moduleName);
|
||||||
|
WebModule getModule(String moduleName);
|
||||||
void addStylesheet(String moduleName, String link);
|
|
||||||
void removeStylesheet(String moduleName);
|
|
||||||
|
|
||||||
void addMainMenuItem(String moduleName, String title, String link);
|
|
||||||
void removeMainMenuItems(String moduleName);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user