Расширение API от веб интерфейса
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
group = 'asys'
|
||||
version = '0.1-SNAPSHOT'
|
||||
version = '0.2-SNAPSHOT'
|
||||
|
||||
apply plugin: 'osgi'
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
package asys.mcsmanager;
|
||||
|
||||
import asys.webinterface.api.MainMenu;
|
||||
import asys.webinterface.api.Webinterface;
|
||||
import org.osgi.framework.BundleActivator;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.ServiceEvent;
|
||||
@@ -15,15 +15,15 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Activator implements BundleActivator, ServiceListener {
|
||||
private final Logger logger = LoggerFactory.getLogger(Activator.class);
|
||||
private ServiceTracker<?, MainMenu> serviceTracker;
|
||||
private ServiceTracker<?, Webinterface> serviceTracker;
|
||||
|
||||
@Override
|
||||
public void start(BundleContext context) throws Exception {
|
||||
logger.debug("Register service listener");
|
||||
context.addServiceListener(this);
|
||||
|
||||
logger.debug("Get service: {}", MainMenu.class);
|
||||
serviceTracker = new ServiceTracker<>(context, MainMenu.class, null);
|
||||
logger.debug("Get service: {}", Webinterface.class);
|
||||
serviceTracker = new ServiceTracker<>(context, Webinterface.class, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -37,12 +37,13 @@ public class Activator implements BundleActivator, ServiceListener {
|
||||
if (event.getType() == ServiceEvent.REGISTERED) {
|
||||
String[] objectClass = (String[]) event.getServiceReference().getProperty("objectClass");
|
||||
for (String classStr : objectClass) {
|
||||
if (classStr.equals("asys.webinterface.api.MainMenu")) {
|
||||
if (classStr.equals(Webinterface.class.getCanonicalName())) {
|
||||
try {
|
||||
serviceTracker.open();
|
||||
MainMenu mainMenu = serviceTracker.waitForService(5000);
|
||||
if (mainMenu != null) {
|
||||
mainMenu.addItem("Серверы", "/modules/mcsmanager");
|
||||
Webinterface webinterface = serviceTracker.waitForService(5000);
|
||||
if (webinterface != null) {
|
||||
webinterface.addMainMenuItem("mcsmanager",
|
||||
"Серверы", "/modules/mcsmanager");
|
||||
return;
|
||||
} else {
|
||||
logger.debug("service not found =(");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
group = 'asys'
|
||||
version = '0.12-SNAPSHOT'
|
||||
version = '0.13-SNAPSHOT'
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
package asys.webinterface;
|
||||
|
||||
import asys.webinterface.api.MainMenu;
|
||||
import asys.webinterface.api.Webinterface;
|
||||
import org.osgi.framework.BundleActivator;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.ServiceRegistration;
|
||||
@@ -18,13 +18,14 @@ public class Activator implements BundleActivator {
|
||||
|
||||
@Override
|
||||
public void start(BundleContext context) throws Exception {
|
||||
MainMenuImpl mainMenu = new MainMenuImpl();
|
||||
mainMenu.addItem("Модули", "#");
|
||||
WebinterfaceImpl webinterface = new WebinterfaceImpl();
|
||||
webinterface.addMainMenuItem("modules", "Модули", "#");
|
||||
webinterface.addJavascript("modules", "/static/module.js");
|
||||
|
||||
logger.trace("Register service: {}", MainMenu.class);
|
||||
serviceMainMenu = context.registerService(MainMenu.class, mainMenu, null);
|
||||
logger.trace("Register service: {}", Webinterface.class);
|
||||
serviceMainMenu = context.registerService(Webinterface.class, webinterface, null);
|
||||
|
||||
webServer = new WebServer(context, mainMenu);
|
||||
webServer = new WebServer(context, webinterface);
|
||||
webServer.start(8778);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,10 +18,10 @@ public class AppJsHandler implements HttpHandler {
|
||||
private String doneAppJs;
|
||||
private int size;
|
||||
|
||||
public AppJsHandler(MainMenuImpl mainMenu) {
|
||||
public AppJsHandler(WebinterfaceImpl webinterface) {
|
||||
try {
|
||||
String appJs = IOUtils.toString(getClass().getResourceAsStream("/app.js"), "UTF-8");
|
||||
doneAppJs = appJs.replace("<{mainMenuItems}>", mainMenu.toReacJS());
|
||||
doneAppJs = appJs.replace("<{mainMenuItems}>", webinterface.mainMenuItemsToReactJS());
|
||||
size = doneAppJs.getBytes(IndexHandler.defaultCharset).length;
|
||||
} catch (IOException e) {
|
||||
logger.error("app.js", e);
|
||||
|
||||
@@ -15,11 +15,12 @@ public class IndexHandler implements HttpHandler {
|
||||
static final Charset defaultCharset = Charset.forName("UTF-8");
|
||||
private String htmlTemplate;
|
||||
|
||||
public IndexHandler() {
|
||||
public IndexHandler(WebinterfaceImpl webinterface) {
|
||||
try {
|
||||
InputStream inputStream = getClass().getResourceAsStream("/index.html");
|
||||
htmlTemplate = IOUtils.toString(inputStream, "UTF-8")
|
||||
.replace("<{moduleScript}>", "<script src=\"/static/module.js\"></script>");
|
||||
.replace("<{moduleScript}>", webinterface.javascriptToTags())
|
||||
.replace("<{moduleStylesheet}>", webinterface.stylesheetToTags());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Error load htmlTemplate", e);
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-03-11
|
||||
*/
|
||||
package asys.webinterface;
|
||||
|
||||
import asys.webinterface.api.MainMenu;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
public class MainMenuImpl implements MainMenu {
|
||||
private Map<String, String> items = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void addItem(String title, String href) {
|
||||
items.put(title, href);
|
||||
}
|
||||
|
||||
public String toReacJS() {
|
||||
StringJoiner sj = new StringJoiner(",");
|
||||
for(Map.Entry<String, String> entry : items.entrySet()) {
|
||||
sj.add(String.format(
|
||||
"ce(MainMenuItem, {title: '%s', href: '%s'})",
|
||||
entry.getKey().replace("'","\\'"), entry.getValue().replace("'","\\'")
|
||||
));
|
||||
}
|
||||
return sj.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MainMenuImpl{" +
|
||||
"items=" + items +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -13,17 +13,17 @@ import java.net.InetSocketAddress;
|
||||
public class WebServer {
|
||||
private HttpServer server;
|
||||
private BundleContext context;
|
||||
private MainMenuImpl mainMenu;
|
||||
private WebinterfaceImpl webinterface;
|
||||
|
||||
public WebServer(BundleContext context, MainMenuImpl mainMenu) {
|
||||
public WebServer(BundleContext context, WebinterfaceImpl webinterface) {
|
||||
this.context = context;
|
||||
this.mainMenu = mainMenu;
|
||||
this.webinterface = webinterface;
|
||||
}
|
||||
|
||||
public void start(int port) throws IOException {
|
||||
server = HttpServer.create(new InetSocketAddress(port), 0);
|
||||
server.createContext("/", new IndexHandler());
|
||||
server.createContext("/app.js", new AppJsHandler(mainMenu));
|
||||
server.createContext("/", new IndexHandler(webinterface));
|
||||
server.createContext("/app.js", new AppJsHandler(webinterface));
|
||||
server.createContext("/static", new StaticHandler());
|
||||
server.createContext("/ajax", new AjaxHandler(context));
|
||||
server.start();
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-03-12
|
||||
*/
|
||||
package asys.webinterface;
|
||||
|
||||
import asys.webinterface.api.Webinterface;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class WebinterfaceImpl implements Webinterface {
|
||||
// moduleName, links
|
||||
private Map<String, List<String>> javascripts = new HashMap<>();
|
||||
private Map<String, List<String>> stylesheets = new HashMap<>();
|
||||
private Map<String, List<MainMenuItem>> menuItems = new HashMap<>();
|
||||
|
||||
public class MainMenuItem {
|
||||
public String title, link;
|
||||
public MainMenuItem(String title, String link) {
|
||||
this.title = title;
|
||||
this.link = link;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addJavascript(String moduleName, String link) {
|
||||
List<String> list;
|
||||
if (javascripts.containsKey(moduleName)) {
|
||||
list = javascripts.get(moduleName);
|
||||
} else {
|
||||
list = new ArrayList<>();
|
||||
javascripts.put(moduleName, list);
|
||||
}
|
||||
list.add(link);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeJavascript(String moduleName) {
|
||||
javascripts.remove(moduleName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addStylesheet(String moduleName, String link) {
|
||||
List<String> list;
|
||||
if (stylesheets.containsKey(moduleName)) {
|
||||
list = stylesheets.get(moduleName);
|
||||
} else {
|
||||
list = new ArrayList<>();
|
||||
stylesheets.put(moduleName, list);
|
||||
}
|
||||
list.add(link);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeStylesheet(String moduleName) {
|
||||
stylesheets.remove(moduleName);
|
||||
}
|
||||
|
||||
@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() {
|
||||
StringJoiner sj = new StringJoiner(",");
|
||||
|
||||
for(List<MainMenuItem> listItems : menuItems.values()) {
|
||||
for (MainMenuItem item : listItems) {
|
||||
sj.add(String.format(
|
||||
"ce(MainMenuItem, {title: '%s', href: '%s'})",
|
||||
item.title.replace("'","\\'"), item.link.replace("'","\\'")
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
return sj.toString();
|
||||
}
|
||||
|
||||
public String javascriptToTags() {
|
||||
StringJoiner sj = new StringJoiner("\n");
|
||||
|
||||
for (List<String> items : javascripts.values()) {
|
||||
for (String link : items) {
|
||||
sj.add(String.format("<script src=\"%s\"></script>", link));
|
||||
}
|
||||
}
|
||||
|
||||
return sj.toString();
|
||||
}
|
||||
|
||||
public String stylesheetToTags() {
|
||||
StringJoiner sj = new StringJoiner("\n");
|
||||
|
||||
for (List<String> items : javascripts.values()) {
|
||||
for (String link : items) {
|
||||
sj.add(String.format("<link rel=\"stylesheet\" href=\"%s\">", link));
|
||||
}
|
||||
}
|
||||
|
||||
return sj.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-03-11
|
||||
*/
|
||||
package asys.webinterface.api;
|
||||
|
||||
public interface MainMenu {
|
||||
void addItem(String title, String href);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-03-12
|
||||
*/
|
||||
package asys.webinterface.api;
|
||||
|
||||
public interface Webinterface {
|
||||
void addJavascript(String moduleName, String link);
|
||||
void removeJavascript(String moduleName);
|
||||
|
||||
void addStylesheet(String moduleName, String link);
|
||||
void removeStylesheet(String moduleName);
|
||||
|
||||
void addMainMenuItem(String moduleName, String title, String link);
|
||||
void removeMainMenuItems(String moduleName);
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
background: url('/static/background.png') fixed;
|
||||
}
|
||||
</style>
|
||||
<{moduleStylesheet}>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js"></script>
|
||||
|
||||
Reference in New Issue
Block a user