WebInterface: список модулей (bundles)
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
|
||||
<groupId>asys</groupId>
|
||||
<artifactId>webinterface</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<version>0.2-SNAPSHOT</version>
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -12,7 +12,7 @@ public class Activator implements BundleActivator {
|
||||
|
||||
@Override
|
||||
public void start(BundleContext context) throws Exception {
|
||||
webServer = new WebServer();
|
||||
webServer = new WebServer(context);
|
||||
webServer.start(8778);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ package asys.webinterface;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import org.osgi.framework.Bundle;
|
||||
import org.osgi.framework.BundleContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
@@ -13,14 +15,26 @@ import java.nio.charset.Charset;
|
||||
|
||||
public class IndexHandler implements HttpHandler {
|
||||
private static final Charset defaultCharset = Charset.forName("UTF-8");
|
||||
private BundleContext context;
|
||||
|
||||
public IndexHandler(BundleContext context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(HttpExchange httpExchange) throws IOException {
|
||||
String plainText = "ASys Webinterface";
|
||||
httpExchange.sendResponseHeaders(200, plainText.length());
|
||||
httpExchange.setAttribute("Context-Type", "text/plain;charset=utf-8");
|
||||
StringBuilder htmlText = new StringBuilder("<html><body>");
|
||||
htmlText.append("<h1>").append("ASys Web interface").append("</h1>")
|
||||
.append("<ul>");
|
||||
for (Bundle bundle : context.getBundle(0).getBundleContext().getBundles()) {
|
||||
htmlText.append("<li>").append(bundle.getHeaders().get("Bundle-Name")).append("</li>");
|
||||
}
|
||||
htmlText.append("</ul></body></html>");
|
||||
|
||||
httpExchange.sendResponseHeaders(200, htmlText.length());
|
||||
httpExchange.setAttribute("Context-Type", "text/html;charset=utf-8");
|
||||
OutputStream responseBody = httpExchange.getResponseBody();
|
||||
responseBody.write(plainText.getBytes(defaultCharset));
|
||||
responseBody.write(htmlText.toString().getBytes(defaultCharset));
|
||||
responseBody.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,16 +5,22 @@
|
||||
package asys.webinterface;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import org.osgi.framework.BundleContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
public class WebServer {
|
||||
private HttpServer server;
|
||||
private BundleContext context;
|
||||
|
||||
public WebServer(BundleContext context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void start(int port) throws IOException {
|
||||
server = HttpServer.create(new InetSocketAddress(port), 0);
|
||||
server.createContext("/", new IndexHandler());
|
||||
server.createContext("/", new IndexHandler(context));
|
||||
server.start();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user