Skip to content
Browse files

Remote OSGi container support

Expose the test bundle via HTTP using a simple HTTP server running
on a server selected port.
This allows for remote OSGi container running on a remote machine.
  • Loading branch information...
1 parent 328e657 commit 98d9f8a14eec10293f25a6d7053226bb135621cb @pkendall64 pkendall64 committed Feb 24, 2012
View
9 ...e/src/main/java/org/jboss/arquillian/container/osgi/remote/RemoteDeployableContainer.java
@@ -143,9 +143,16 @@ private VirtualFile toVirtualFile(Archive<?> archive) throws IOException {
private BundleHandle installBundle(VirtualFile virtualFile) throws BundleException, IOException {
BundleInfo info = BundleInfo.createBundleInfo(virtualFile);
- String streamURL = info.getRoot().getStreamURL().toExternalForm();
+
+ SimpleHTTPServer simpleHTTPServer = new SimpleHTTPServer(virtualFile);
+ simpleHTTPServer.start();
+
+ String streamURL = simpleHTTPServer.getUrl() + "/" + virtualFile.getName();
+
FrameworkMBean frameworkMBean = jmxSupport.getFrameworkMBean();
long bundleId = frameworkMBean.installBundleFromURL(info.getLocation(), streamURL);
+
+ simpleHTTPServer.shutdown();
return new BundleHandle(bundleId, info.getSymbolicName());
}
View
42 ...ner-remote/src/main/java/org/jboss/arquillian/container/osgi/remote/SimpleHTTPServer.java
@@ -0,0 +1,42 @@
+package org.jboss.arquillian.container.osgi.remote;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.UnknownHostException;
+
+import org.jboss.osgi.vfs.VirtualFile;
+
+public class SimpleHTTPServer extends Thread {
+ private final VirtualFile virtualFile;
+ private ServerSocket socket;
+
+ public SimpleHTTPServer(VirtualFile virtualFile) throws IOException {
+ this.virtualFile = virtualFile;
+ this.socket = new ServerSocket(0);
+ }
+
+ @Override
+ public void run() {
+ try {
+ while (true) {
+ SimpleHTTPServerThread thread = new SimpleHTTPServerThread(virtualFile, socket.accept());
+ thread.start();
+ }
+ } catch (Exception e) {
+ }
+ }
+
+ public void shutdown() {
+ try {
+ socket.close();
+ } catch (IOException e) {
+ }
+ }
+
+ public String getUrl() throws UnknownHostException {
+ String canonicalHostName = InetAddress.getLocalHost().getCanonicalHostName();
+ int port = this.socket.getLocalPort();
+ return "http://" + canonicalHostName + ":" + port;
+ }
+}
View
57 ...mote/src/main/java/org/jboss/arquillian/container/osgi/remote/SimpleHTTPServerThread.java
@@ -0,0 +1,57 @@
+package org.jboss.arquillian.container.osgi.remote;
+
+import java.io.BufferedReader;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.Socket;
+import java.util.StringTokenizer;
+
+import org.jboss.osgi.vfs.VFSUtils;
+import org.jboss.osgi.vfs.VirtualFile;
+
+public class SimpleHTTPServerThread extends Thread {
+ private final VirtualFile virtualFile;
+ private final Socket socket;
+
+ public SimpleHTTPServerThread(VirtualFile virtualFile, Socket socket) {
+ this.virtualFile = virtualFile;
+ this.socket = socket;
+ }
+
+ public void run() {
+ try {
+ BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ DataOutputStream output = new DataOutputStream(socket.getOutputStream());
+ try {
+ while (true) {
+ String line = input.readLine();
+ if (line.length() < 1)
+ break;
+ if (line.startsWith("GET")) {
+ StringTokenizer t = new StringTokenizer(line, " ");
+ t.nextToken();
+ String p = t.nextToken();
+ if (p.substring(1).equals(virtualFile.getName())) {
+ output.writeBytes("HTTP/1.0 200 OK\n\n");
+ VFSUtils.copyStream(virtualFile.openStream(), output);
+ } else {
+ output.writeBytes("HTTP/1.0 404 Not Found\n\n\n");
+ }
+ }
+ }
+ } catch (Exception e) {
+ output.writeBytes("HTTP/1.0 500 Server Error\n\n\n");
+ e.printStackTrace();
+ } finally {
+ output.close();
+ }
+ } catch (Exception e) {
+ } finally {
+ try {
+ socket.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+}

0 comments on commit 98d9f8a

Please sign in to comment.
Something went wrong with that request. Please try again.