title: Deploy exploded bundle to Apache Felix using an Eclipse launch task
body: <p>I am looking for a way to (re)deploy an exploded bundle (meaning not jarred but in a folder) to a running Apache Felix OSGi container from within Eclipse, preferably using a launch task.</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/5518388/is-there-a-way-to-deploy-exploded-bundles-in-apache-felix">this</a> question, which has an answer that comes close but it depends on typing commands into a Gogo shell, which is not convenient for long-term development use. I'd like to use Eclipse's launch task mechanism for this, but if there are alternatives that are equally fast and convenient I am open to that as well.</p>
  
  <p>Now I think that if I can fire Gogo shell commands from an Eclipse launch tasks, that would be a solution, but I also can't get my head around how to do that. I presume I need the Remote Shell bundle for that right?</p>
  
  <p>I am starting to think about writing a telnet client in Java that can connect to the Remote Shell bundle and execute Gogo commands in an automated fashion. I have seen <a href="https://stackoverflow.com/questions/3480755/open-source-telnet-java-api">some</a> <a href="https://stackoverflow.com/questions/5988029/java-telnet-library/14098926#14098926">example</a> of that already which I can modify to suit my needs... However I am getting a 'reinventing the wheel' kind of feeling from that. Surely there is a better way?</p>
  
  <p><em>Some background to help you understand what I am doing:</em></p>
  
  <p>I have set up an Eclipse 'OSGiContainer' project which basically contains the Apache Felix jar and the third party bundles I want to deploy (like Gogo shell), similar to the project setup described <a href="http://felix.apache.org/site/integrating-felix-with-eclipse.html" rel="nofollow noreferrer">here</a>. Then I created a second 'MyBundle' project that contains my bundle. I want to start the OSGi container by launching the OSGiContainer project, and then just develop on my bundle and test my changes by launching the MyBundle project into the OSGiContainer that I just want to keep running the whole time during development. </p>
  
  <p>Project layout:</p>
  
  <ul>
  <li>OSGiContainer
  
  <ul>
  <li>bin (contains felix jar)</li>
  <li>bundles (third party bundles)</li>
  <li>conf (Felix' config.properties file)</li>
  </ul></li>
  <li>MyBundle
  
  <ul>
  <li>src </li>
  <li>target
  
  <ul>
  <li>classes</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>I am then able to deploy my bundle to the OSGi container by invoking these commands on the Gogo shell:</p>
  
  <pre><code>install reference:file:../MyBundle/target/classes
  start &lt;bundleId&gt;
  </code></pre>
  
  <p>To re-deploy, I invoke these commands:</p>
  
  <pre><code>stop &lt;bundleId&gt;
  uninstall &lt;bundleId&gt;
  install reference:file:../MyBundle/target/classes
  start &lt;bundleId&gt;
  </code></pre>
  
  <p>You can imagine having to invoke 4 commands on the shell each time is not that much fun... So even if you can give me a way to boil this down to less commands to type it would be a great improvement already.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I hacked around a bit and came up with the class below. It's an adaptation of the telnet example with some small changes and a main method with the necessary commands to uninstall a bundle and then re-install and start it. The path to the bundle should be given as an argument to the program and would look like:</p>
  
  <pre><code>reference:file:../MyBundle/target/classes
  </code></pre>
  
  <p>I still very much welcome answers to this question, as I don't really like this solution at all. I have however verified that this works:</p>
  
  <pre><code>import java.io.IOException;
  import java.io.InputStream;
  import java.io.PrintStream;
  import java.net.SocketException;
  import java.util.concurrent.BlockingQueue;
  import java.util.concurrent.CountDownLatch;
  import java.util.concurrent.LinkedBlockingQueue;
  
  import org.apache.commons.net.telnet.TelnetClient;
  
  public class GogoDeployer {
      static class Responder extends Thread {
          private StringBuilder builder = new StringBuilder();
          private final GogoDeployer checker;
          private CountDownLatch latch;
          private String waitFor = null;
          private boolean isKeepRunning = true;
  
          Responder(GogoDeployer checker) {
              this.checker = checker;
          }
  
          boolean foundWaitFor(String waitFor) {
              return builder.toString().contains(waitFor);
          }
  
          public synchronized String getAndClearBuffer() {
              String result = builder.toString();
              builder = new StringBuilder();
              return result;
          }
  
          @Override
          public void run() {
              while (isKeepRunning) {
                  String s;
  
                  try {
                      s = checker.messageQueue.take();
                  } catch (InterruptedException e) {
                      break;
                  }
  
                  synchronized (Responder.class) {
                      builder.append(s);
                  }
  
                  if (waitFor != null &amp;&amp; latch != null &amp;&amp; foundWaitFor(waitFor)) {
                      latch.countDown();
                  }
              }
              System.out.println("Responder stopped.");
          }
  
          public String waitFor(String waitFor) {
              synchronized (Responder.class) {
                  if (foundWaitFor(waitFor)) {
                      return getAndClearBuffer();
                  }
              }
  
              this.waitFor = waitFor;
              latch = new CountDownLatch(1);
              try {
                  latch.await();
              } catch (InterruptedException e) {
                  e.printStackTrace();
                  return null;
              }
  
              String result = null;
              synchronized (Responder.class) {
                  result = builder.toString();
                  builder = new StringBuilder();
              }
  
              return result;
          }
      }
  
      static class TelnetReader extends Thread {
          private boolean isKeepRunning = true;
          private final GogoDeployer checker;
          private final TelnetClient tc;
  
          TelnetReader(GogoDeployer checker, TelnetClient tc) {
              this.checker = checker;
              this.tc = tc;
          }
  
          @Override
          public void run() {
              InputStream instr = tc.getInputStream();
  
              try {
                  byte[] buff = new byte[1024];
                  int ret_read = 0;
  
                  do {
                      if (instr.available() &gt; 0) {
                          ret_read = instr.read(buff);
                      }
                      if (ret_read &gt; 0) {
                          checker.sendForResponse(new String(buff, 0, ret_read));
                          ret_read = 0;
                      }
                  } while (isKeepRunning &amp;&amp; (ret_read &gt;= 0));
              } catch (Exception e) {
                  System.err.println("Exception while reading socket:" + e.getMessage());
              }
  
              try {
                  tc.disconnect();
                  checker.stop();
                  System.out.println("Disconnected.");
              } catch (Exception e) {
                  System.err.println("Exception while closing telnet:" + e.getMessage());
              }
          }
      }
  
      private static final String prompt = "g!";
      private static GogoDeployer client;
  
  
      private String host;
      private BlockingQueue&lt;String&gt; messageQueue = new LinkedBlockingQueue&lt;String&gt;();
      private int port;
      private TelnetReader reader;
      private Responder responder;
      private TelnetClient tc;
  
      public GogoDeployer(String host, int port) {
          this.host = host;
          this.port = port;
      }
  
      public void stop() {
          responder.isKeepRunning = false;
          reader.isKeepRunning = false;
  
          try {
              Thread.sleep(10);
          } catch (InterruptedException e) {
          }
          responder.interrupt();
          reader.interrupt();
      }
  
      public void send(String command) {
          PrintStream ps = new PrintStream(tc.getOutputStream());
          ps.println(command);
          ps.flush();
      }
  
      public void sendForResponse(String s) {
          messageQueue.add(s);
      }
  
      public void connect() throws SocketException, IOException {
          tc = new TelnetClient();
          tc.connect(host, port);
          reader = new TelnetReader(this, tc);
          reader.start();
          responder = new Responder(this);
          responder.start();
      }
  
      public String waitFor(String s) {
          return responder.waitFor(s);
      }
  
      private static String exec(String cmd) {
          String result = "";
          System.out.println(cmd);
          client.send(cmd);
          result = client.waitFor(prompt);
          return result;
      }
  
      public static void main(String[] args) {
          try {
              String project = args[0];
              client = new GogoDeployer("localhost", 6666);
              client.connect();
              System.out.println(client.waitFor(prompt));
              System.out.println(exec("uninstall " + project));
              String result = exec("install " + project);
              System.out.println(result);
              int start = result.indexOf(":");
              int stop = result.indexOf(prompt);
              String bundleId = result.substring(start + 1, stop).trim();
              System.out.println(exec("start " + bundleId));
              client.stop();
          } catch (SocketException e) {
              System.err.println("Unable to conect to Gogo remote shell: " + e.getMessage());
          } catch (IOException e) {
              System.err.println("Unable to conect to Gogo remote shell: " + e.getMessage());
          }
      }
  }
  </code></pre>