You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by "Richard S. Hall" <he...@ungoverned.org> on 2008/07/27 23:05:22 UTC

Re: Error while uninstalling bundles dynamically.

Pierre,

I created a bundle out of your code below and at first was not able to 
recreate the issue by simply stopping and restarting the installer 
bundle. In that case it worked as expected.

But then I tried to restart the framework, that is when I witnessed the 
issue. The issue here is that you are trying to mess with the set of 
installed bundles while the framework is trying to restart that set of 
installed bundles. This is really not a good idea. You are essentially 
pulling the rug out from underneath the framework by uninstalling a 
bundle that it is trying to restart.

What has likely happened is the framework has created a list of bundles 
that need to be processed and during the processing of these bundles, 
the installer bundle goes and uninstall one. Eventually the framework 
processes the uninstalled bundle after processing the installer bundle 
and it results in the error you mention.

I think what your bundle needs to do here is register for a framework 
started event so it can do some post-processing of the installed bundle 
list. Following such an approach, you might have to make a special case 
when your installer is installed and started for the first time, since 
the framework started event will already be passed. Unless you always 
expect it to be installed (such as via the auto-start properties), then 
I think it might work correctly.

-> richard


Pierre De Rop wrote:
> Hi there ...
>
> When starting the Felix fwk, we use our own bundle installer which is 
> in charge of installing required bundles dynamically (using 
> BundleContext.installBundle() method).
> Our Installer installs bundles with the start level service; and each 
> bundle is installed with some dedicated start levels.
> When the installer has installed/started every required bundles, it 
> finally enters into start level 10.
> Some times, *before entering into the start level 10*, the installer 
> has to *uninstall* already installed bundles; but when entering into 
> sl 10: I get the following exception: 
> "java.lang.IllegalStateException: Cannot stop an uninstalled bundle."
>
> Here is a sample code which illustrate my problem (if any ?):
> This is a sample code for the bundle installer, which loads the 
> "MyBundle" bundle from /tmp/mybundle.jar:
> When the cache is empty, the installer will install/start 
> /tmp/mybundle.jar with start level 10.
> But if the bundle is already installed, then it will be uninstalled, 
> just before entering into sl 10 -->
>
> package installer;
>
> // Jdk
> import java.util.*;
> import java.util.jar.*;
> import java.io.*;
> import java.net.*;
> import static java.lang.System.out;
>
> // Osgi
> import org.osgi.service.component.*;
> import org.osgi.framework.*;
> import org.osgi.service.startlevel.StartLevel;
>
> public class BundleInstallerActivator implements BundleActivator {
>  public void start(BundleContext bctx) {
>    try {
>      // Get the StartLevel service.
>      ServiceReference ref = 
> bctx.getServiceReference(StartLevel.class.getName());
>      StartLevel sl = (StartLevel) bctx.getService(ref);
>
>      // if bundle /tmp/mybundle.jar (with name "MyBundle") is already 
> installed: uninstall it ->
>
>      boolean doInstall = true;
>      for (Bundle b : bctx.getBundles()) {
>        if (b.getSymbolicName().equals("MyBundle")) {
>          out.println("Uninstalling /tmp/mybundle.jar ...");
>          b.uninstall();
>          doInstall = false;
>        }
>    }
>        // else, install/start "/tmp/mybundle.jar" with start level 10 ->
>        if (doInstall) {
>      out.println("Installing /tmp/mybundle.jar with start level=10");
>      FileInputStream in = new FileInputStream("/tmp/mybundle.jar");
>      sl.setInitialBundleStartLevel(10);
>      Bundle myBundle = bctx.installBundle("MyBundle", in);
>
>      out.println("Starting /tmp/mybundle.jar ");
>      myBundle.start(); // will be started only when we enter into sl 
> 10 (see below)
>      in.close();
>    }
>
>    out.println("Entering into start level 10 ...");
>    sl.setStartLevel(10); //
>  }
>    catch (Throwable t) {
>      t.printStackTrace();
>    }
> }
>
>  public void stop(BundleContext bctx) {
>  }
> }
>
> -> When I start Felix with an empty cache, it works fine, and I get my 
> bundle "/tmp/mybundle.jar" installer and activated (once the start 
> level 10 is entered):
>
> rm -rf ~/.felix/; java -jar bin/felix.jar
> Welcome to Felix.
> =================
> Enter profile name: test
> -> Installing /tmp/mybundle.jar with start level=10
> Starting /tmp/mybundle.jar
> Entering into start level 10
> MyBundle activated
>
> -> But, when I restart Felix (without removing the cache, which 
> contains the bundle "MyBundle"), then the installer uninstall the
> /tmp/mybundle.jar, but I get the following exception, when entering 
> into sl 10:
>
> java -jar bin/felix.jar ar
> Welcome to Felix.
> =================
> Enter profile name: test
> -> Uninstalling /tmp/mybundle.jar ...
> Entering into start level 10
> ERROR: Error stopping MyBundle (java.lang.IllegalStateException: 
> Cannot stop an uninstalled bundle.)
> java.lang.IllegalStateException: Cannot stop an uninstalled bundle.
>        at org.apache.felix.framework.Felix._stopBundle(Felix.java:1979)
>        at org.apache.felix.framework.Felix.stopBundle(Felix.java:1954)
>        at 
> org.apache.felix.framework.Felix.setFrameworkStartLevel(Felix.java:1141)
>        at 
> org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:258)
>        at java.lang.Thread.run(Thread.java:595)
>
>
> Was I wrong with my bundle installer ? does any one could explain why 
> I get the IllegalState exception ?
>
> Kind Regards
> /Pierre
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Error while uninstalling bundles dynamically.

Posted by "Richard S. Hall" <he...@ungoverned.org>.
Yes! This is exactly what I was talking about. :-)

Granted, your real installer bundle will likely need to be a little more 
robust, but that is the idea.

-> richard

Pierre De Rop wrote:
> Hi Richard;
>
> I followed your strategy, and it sounds like working fine ! :-)
> Here is the modified sample code for the bundle installer (it is not 
> the real bundle installer, but just a sample code which illustrates 
> the issue ...)
>
> So, when I catch the "Bundle started" event ->
>
>    * If /tmp/mybundle.jar is already installed -> I uninstall it
>    * Else, I install and start it (with sl=10)
>    * and finally, I enter into start level 10
>
> ... and I don't have the mentioned exception anymore ...
>
>
>
> package installer;
>
> // Jdk
> import java.util.*;
> import java.util.jar.*;
> import java.io.*;
> import java.net.*;
> import static java.lang.System.out;
>
> // Osgi
> import org.osgi.framework.*;
> import org.osgi.service.startlevel.StartLevel;
>
> public class BundleInstallerActivator implements BundleActivator, 
> FrameworkListener {
>  StartLevel sl;
>  BundleContext bctx;
>
>  public void start(BundleContext bctx) {
>    try {
>      // Get the StartLevel service.
>      this.bctx = bctx;
>      ServiceReference ref = 
> bctx.getServiceReference(StartLevel.class.getName());
>      sl = (StartLevel) bctx.getService(ref);
>    } catch (Throwable t) {
>      t.printStackTrace();
>    }
>
>    bctx.addFrameworkListener(this);
>  }
>
>  public void frameworkEvent(FrameworkEvent event) {
>    try {
>      if (event.getType() == event.STARTED) {
>    System.out.println("FWK started: curr start level=" + 
> sl.getStartLevel());
>
>    // if bundle "MyBundle" is already installed: stop/uninstall it
>
>    boolean doInstall = true;
>    for (Bundle b : bctx.getBundles()) {
>      if (b.getSymbolicName().equals("MyBundle")) {
>        out.println("UNINSTALLING /tmp/mybundle.jar !");
>        b.uninstall();
>        doInstall = false;
>      }
>    }
>        // If bundle "MyBundle" not installed: install it with start 
> level 10 and start it.
>        if (doInstall) {
>      out.println("Installing /tmp/mybundle.jar with start level=10");
>      FileInputStream in = new FileInputStream("/tmp/mybundle.jar");
>      sl.setInitialBundleStartLevel(10);
>      Bundle myBundle = bctx.installBundle("MyBundle", in);
>
>      out.println("Starting /tmp/mybundle.jar ");
>      myBundle.start();
>      in.close();
>    }
>
>    out.println("Entering into start level 10");
>    sl.setStartLevel(10);
>      }
>    }
>      catch (Throwable t) {
>      t.printStackTrace();
>    }
>  }
>
>  public void stop(BundleContext bctx) {
>  }
> }
>
>
> Richard S. Hall wrote:
>> Pierre,
>>
>> I created a bundle out of your code below and at first was not able 
>> to recreate the issue by simply stopping and restarting the installer 
>> bundle. In that case it worked as expected.
>>
>> But then I tried to restart the framework, that is when I witnessed 
>> the issue. The issue here is that you are trying to mess with the set 
>> of installed bundles while the framework is trying to restart that 
>> set of installed bundles. This is really not a good idea. You are 
>> essentially pulling the rug out from underneath the framework by 
>> uninstalling a bundle that it is trying to restart.
>>
>> What has likely happened is the framework has created a list of 
>> bundles that need to be processed and during the processing of these 
>> bundles, the installer bundle goes and uninstall one. Eventually the 
>> framework processes the uninstalled bundle after processing the 
>> installer bundle and it results in the error you mention.
>>
>> I think what your bundle needs to do here is register for a framework 
>> started event so it can do some post-processing of the installed 
>> bundle list. Following such an approach, you might have to make a 
>> special case when your installer is installed and started for the 
>> first time, since the framework started event will already be passed. 
>> Unless you always expect it to be installed (such as via the 
>> auto-start properties), then I think it might work correctly.
>>
>> -> richard
>>
>>
>> Pierre De Rop wrote:
>>> Hi there ...
>>>
>>> When starting the Felix fwk, we use our own bundle installer which 
>>> is in charge of installing required bundles dynamically (using 
>>> BundleContext.installBundle() method).
>>> Our Installer installs bundles with the start level service; and 
>>> each bundle is installed with some dedicated start levels.
>>> When the installer has installed/started every required bundles, it 
>>> finally enters into start level 10.
>>> Some times, *before entering into the start level 10*, the installer 
>>> has to *uninstall* already installed bundles; but when entering into 
>>> sl 10: I get the following exception: 
>>> "java.lang.IllegalStateException: Cannot stop an uninstalled bundle."
>>>
>>> Here is a sample code which illustrate my problem (if any ?):
>>> This is a sample code for the bundle installer, which loads the 
>>> "MyBundle" bundle from /tmp/mybundle.jar:
>>> When the cache is empty, the installer will install/start 
>>> /tmp/mybundle.jar with start level 10.
>>> But if the bundle is already installed, then it will be uninstalled, 
>>> just before entering into sl 10 -->
>>>
>>> package installer;
>>>
>>> // Jdk
>>> import java.util.*;
>>> import java.util.jar.*;
>>> import java.io.*;
>>> import java.net.*;
>>> import static java.lang.System.out;
>>>
>>> // Osgi
>>> import org.osgi.service.component.*;
>>> import org.osgi.framework.*;
>>> import org.osgi.service.startlevel.StartLevel;
>>>
>>> public class BundleInstallerActivator implements BundleActivator {
>>>  public void start(BundleContext bctx) {
>>>    try {
>>>      // Get the StartLevel service.
>>>      ServiceReference ref = 
>>> bctx.getServiceReference(StartLevel.class.getName());
>>>      StartLevel sl = (StartLevel) bctx.getService(ref);
>>>
>>>      // if bundle /tmp/mybundle.jar (with name "MyBundle") is 
>>> already installed: uninstall it ->
>>>
>>>      boolean doInstall = true;
>>>      for (Bundle b : bctx.getBundles()) {
>>>        if (b.getSymbolicName().equals("MyBundle")) {
>>>          out.println("Uninstalling /tmp/mybundle.jar ...");
>>>          b.uninstall();
>>>          doInstall = false;
>>>        }
>>>    }
>>>        // else, install/start "/tmp/mybundle.jar" with start level 
>>> 10 ->
>>>        if (doInstall) {
>>>      out.println("Installing /tmp/mybundle.jar with start level=10");
>>>      FileInputStream in = new FileInputStream("/tmp/mybundle.jar");
>>>      sl.setInitialBundleStartLevel(10);
>>>      Bundle myBundle = bctx.installBundle("MyBundle", in);
>>>
>>>      out.println("Starting /tmp/mybundle.jar ");
>>>      myBundle.start(); // will be started only when we enter into sl 
>>> 10 (see below)
>>>      in.close();
>>>    }
>>>
>>>    out.println("Entering into start level 10 ...");
>>>    sl.setStartLevel(10); //
>>>  }
>>>    catch (Throwable t) {
>>>      t.printStackTrace();
>>>    }
>>> }
>>>
>>>  public void stop(BundleContext bctx) {
>>>  }
>>> }
>>>
>>> -> When I start Felix with an empty cache, it works fine, and I get 
>>> my bundle "/tmp/mybundle.jar" installer and activated (once the 
>>> start level 10 is entered):
>>>
>>> rm -rf ~/.felix/; java -jar bin/felix.jar
>>> Welcome to Felix.
>>> =================
>>> Enter profile name: test
>>> -> Installing /tmp/mybundle.jar with start level=10
>>> Starting /tmp/mybundle.jar
>>> Entering into start level 10
>>> MyBundle activated
>>>
>>> -> But, when I restart Felix (without removing the cache, which 
>>> contains the bundle "MyBundle"), then the installer uninstall the
>>> /tmp/mybundle.jar, but I get the following exception, when entering 
>>> into sl 10:
>>>
>>> java -jar bin/felix.jar ar
>>> Welcome to Felix.
>>> =================
>>> Enter profile name: test
>>> -> Uninstalling /tmp/mybundle.jar ...
>>> Entering into start level 10
>>> ERROR: Error stopping MyBundle (java.lang.IllegalStateException: 
>>> Cannot stop an uninstalled bundle.)
>>> java.lang.IllegalStateException: Cannot stop an uninstalled bundle.
>>>        at org.apache.felix.framework.Felix._stopBundle(Felix.java:1979)
>>>        at org.apache.felix.framework.Felix.stopBundle(Felix.java:1954)
>>>        at 
>>> org.apache.felix.framework.Felix.setFrameworkStartLevel(Felix.java:1141) 
>>>
>>>        at 
>>> org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:258)
>>>        at java.lang.Thread.run(Thread.java:595)
>>>
>>>
>>> Was I wrong with my bundle installer ? does any one could explain 
>>> why I get the IllegalState exception ?
>>>
>>> Kind Regards
>>> /Pierre
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Error while uninstalling bundles dynamically.

Posted by Pierre De Rop <pi...@alcatel-lucent.fr>.
Hi Richard;

I followed your strategy, and it sounds like working fine ! :-)
Here is the modified sample code for the bundle installer (it is not the 
real bundle installer, but just a sample code which illustrates the 
issue ...)

So, when I catch the "Bundle started" event ->

    * If /tmp/mybundle.jar is already installed -> I uninstall it
    * Else, I install and start it (with sl=10)
    * and finally, I enter into start level 10

... and I don't have the mentioned exception anymore ...



package installer;

// Jdk
import java.util.*;
import java.util.jar.*;
import java.io.*;
import java.net.*;
import static java.lang.System.out;

// Osgi
import org.osgi.framework.*;
import org.osgi.service.startlevel.StartLevel;

public class BundleInstallerActivator implements BundleActivator, 
FrameworkListener {
  StartLevel sl;
  BundleContext bctx;

  public void start(BundleContext bctx) {
    try {
      // Get the StartLevel service.
      this.bctx = bctx;
      ServiceReference ref = 
bctx.getServiceReference(StartLevel.class.getName());
      sl = (StartLevel) bctx.getService(ref);
    } catch (Throwable t) {
      t.printStackTrace();
    }

    bctx.addFrameworkListener(this);
  }

  public void frameworkEvent(FrameworkEvent event) {
    try {
      if (event.getType() == event.STARTED) {
    System.out.println("FWK started: curr start level=" + 
sl.getStartLevel());

    // if bundle "MyBundle" is already installed: stop/uninstall it

    boolean doInstall = true;
    for (Bundle b : bctx.getBundles()) {
      if (b.getSymbolicName().equals("MyBundle")) {
        out.println("UNINSTALLING /tmp/mybundle.jar !");
        b.uninstall();
        doInstall = false;
      }
    }
     
    // If bundle "MyBundle" not installed: install it with start level 
10 and start it.
     
    if (doInstall) {
      out.println("Installing /tmp/mybundle.jar with start level=10");
      FileInputStream in = new FileInputStream("/tmp/mybundle.jar");
      sl.setInitialBundleStartLevel(10);
      Bundle myBundle = bctx.installBundle("MyBundle", in);

      out.println("Starting /tmp/mybundle.jar ");
      myBundle.start();
      in.close();
    }

    out.println("Entering into start level 10");
    sl.setStartLevel(10);
      }
    }
   
    catch (Throwable t) {
      t.printStackTrace();
    }
  }

  public void stop(BundleContext bctx) {
  }
}


Richard S. Hall wrote:
> Pierre,
>
> I created a bundle out of your code below and at first was not able to 
> recreate the issue by simply stopping and restarting the installer 
> bundle. In that case it worked as expected.
>
> But then I tried to restart the framework, that is when I witnessed 
> the issue. The issue here is that you are trying to mess with the set 
> of installed bundles while the framework is trying to restart that set 
> of installed bundles. This is really not a good idea. You are 
> essentially pulling the rug out from underneath the framework by 
> uninstalling a bundle that it is trying to restart.
>
> What has likely happened is the framework has created a list of 
> bundles that need to be processed and during the processing of these 
> bundles, the installer bundle goes and uninstall one. Eventually the 
> framework processes the uninstalled bundle after processing the 
> installer bundle and it results in the error you mention.
>
> I think what your bundle needs to do here is register for a framework 
> started event so it can do some post-processing of the installed 
> bundle list. Following such an approach, you might have to make a 
> special case when your installer is installed and started for the 
> first time, since the framework started event will already be passed. 
> Unless you always expect it to be installed (such as via the 
> auto-start properties), then I think it might work correctly.
>
> -> richard
>
>
> Pierre De Rop wrote:
>> Hi there ...
>>
>> When starting the Felix fwk, we use our own bundle installer which is 
>> in charge of installing required bundles dynamically (using 
>> BundleContext.installBundle() method).
>> Our Installer installs bundles with the start level service; and each 
>> bundle is installed with some dedicated start levels.
>> When the installer has installed/started every required bundles, it 
>> finally enters into start level 10.
>> Some times, *before entering into the start level 10*, the installer 
>> has to *uninstall* already installed bundles; but when entering into 
>> sl 10: I get the following exception: 
>> "java.lang.IllegalStateException: Cannot stop an uninstalled bundle."
>>
>> Here is a sample code which illustrate my problem (if any ?):
>> This is a sample code for the bundle installer, which loads the 
>> "MyBundle" bundle from /tmp/mybundle.jar:
>> When the cache is empty, the installer will install/start 
>> /tmp/mybundle.jar with start level 10.
>> But if the bundle is already installed, then it will be uninstalled, 
>> just before entering into sl 10 -->
>>
>> package installer;
>>
>> // Jdk
>> import java.util.*;
>> import java.util.jar.*;
>> import java.io.*;
>> import java.net.*;
>> import static java.lang.System.out;
>>
>> // Osgi
>> import org.osgi.service.component.*;
>> import org.osgi.framework.*;
>> import org.osgi.service.startlevel.StartLevel;
>>
>> public class BundleInstallerActivator implements BundleActivator {
>>  public void start(BundleContext bctx) {
>>    try {
>>      // Get the StartLevel service.
>>      ServiceReference ref = 
>> bctx.getServiceReference(StartLevel.class.getName());
>>      StartLevel sl = (StartLevel) bctx.getService(ref);
>>
>>      // if bundle /tmp/mybundle.jar (with name "MyBundle") is already 
>> installed: uninstall it ->
>>
>>      boolean doInstall = true;
>>      for (Bundle b : bctx.getBundles()) {
>>        if (b.getSymbolicName().equals("MyBundle")) {
>>          out.println("Uninstalling /tmp/mybundle.jar ...");
>>          b.uninstall();
>>          doInstall = false;
>>        }
>>    }
>>        // else, install/start "/tmp/mybundle.jar" with start level 10 ->
>>        if (doInstall) {
>>      out.println("Installing /tmp/mybundle.jar with start level=10");
>>      FileInputStream in = new FileInputStream("/tmp/mybundle.jar");
>>      sl.setInitialBundleStartLevel(10);
>>      Bundle myBundle = bctx.installBundle("MyBundle", in);
>>
>>      out.println("Starting /tmp/mybundle.jar ");
>>      myBundle.start(); // will be started only when we enter into sl 
>> 10 (see below)
>>      in.close();
>>    }
>>
>>    out.println("Entering into start level 10 ...");
>>    sl.setStartLevel(10); //
>>  }
>>    catch (Throwable t) {
>>      t.printStackTrace();
>>    }
>> }
>>
>>  public void stop(BundleContext bctx) {
>>  }
>> }
>>
>> -> When I start Felix with an empty cache, it works fine, and I get 
>> my bundle "/tmp/mybundle.jar" installer and activated (once the start 
>> level 10 is entered):
>>
>> rm -rf ~/.felix/; java -jar bin/felix.jar
>> Welcome to Felix.
>> =================
>> Enter profile name: test
>> -> Installing /tmp/mybundle.jar with start level=10
>> Starting /tmp/mybundle.jar
>> Entering into start level 10
>> MyBundle activated
>>
>> -> But, when I restart Felix (without removing the cache, which 
>> contains the bundle "MyBundle"), then the installer uninstall the
>> /tmp/mybundle.jar, but I get the following exception, when entering 
>> into sl 10:
>>
>> java -jar bin/felix.jar ar
>> Welcome to Felix.
>> =================
>> Enter profile name: test
>> -> Uninstalling /tmp/mybundle.jar ...
>> Entering into start level 10
>> ERROR: Error stopping MyBundle (java.lang.IllegalStateException: 
>> Cannot stop an uninstalled bundle.)
>> java.lang.IllegalStateException: Cannot stop an uninstalled bundle.
>>        at org.apache.felix.framework.Felix._stopBundle(Felix.java:1979)
>>        at org.apache.felix.framework.Felix.stopBundle(Felix.java:1954)
>>        at 
>> org.apache.felix.framework.Felix.setFrameworkStartLevel(Felix.java:1141)
>>        at 
>> org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:258)
>>        at java.lang.Thread.run(Thread.java:595)
>>
>>
>> Was I wrong with my bundle installer ? does any one could explain why 
>> I get the IllegalState exception ?
>>
>> Kind Regards
>> /Pierre
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>


Re: Error while uninstalling bundles dynamically.

Posted by "Richard S. Hall" <he...@ungoverned.org>.
No, I am not talking about the extender pattern. Stated another way, you 
should not be uninstalling bundles in your activator while the framework 
is starting up. The framework sends a FrameworkEvent.STARTED event when 
it is done starting up, then you are free to do whatever you want. So, 
just have your installer bundle register to get this event.

-> richard

Pierre De Rop wrote:
> OK, I think I understand and you are probably talking about the 
> "extender pattern". I will try to modify my bundle installer in order 
> to act in the same way you are suggesting ...
>
> Thank you very much for your quick response !
>
> /Pierre
>
>
>
> Richard S. Hall wrote:
>> Pierre,
>>
>> I created a bundle out of your code below and at first was not able 
>> to recreate the issue by simply stopping and restarting the installer 
>> bundle. In that case it worked as expected.
>>
>> But then I tried to restart the framework, that is when I witnessed 
>> the issue. The issue here is that you are trying to mess with the set 
>> of installed bundles while the framework is trying to restart that 
>> set of installed bundles. This is really not a good idea. You are 
>> essentially pulling the rug out from underneath the framework by 
>> uninstalling a bundle that it is trying to restart.
>>
>> What has likely happened is the framework has created a list of 
>> bundles that need to be processed and during the processing of these 
>> bundles, the installer bundle goes and uninstall one. Eventually the 
>> framework processes the uninstalled bundle after processing the 
>> installer bundle and it results in the error you mention.
>>
>> I think what your bundle needs to do here is register for a framework 
>> started event so it can do some post-processing of the installed 
>> bundle list. Following such an approach, you might have to make a 
>> special case when your installer is installed and started for the 
>> first time, since the framework started event will already be passed. 
>> Unless you always expect it to be installed (such as via the 
>> auto-start properties), then I think it might work correctly.
>>
>> -> richard
>>
>>
>> Pierre De Rop wrote:
>>> Hi there ...
>>>
>>> When starting the Felix fwk, we use our own bundle installer which 
>>> is in charge of installing required bundles dynamically (using 
>>> BundleContext.installBundle() method).
>>> Our Installer installs bundles with the start level service; and 
>>> each bundle is installed with some dedicated start levels.
>>> When the installer has installed/started every required bundles, it 
>>> finally enters into start level 10.
>>> Some times, *before entering into the start level 10*, the installer 
>>> has to *uninstall* already installed bundles; but when entering into 
>>> sl 10: I get the following exception: 
>>> "java.lang.IllegalStateException: Cannot stop an uninstalled bundle."
>>>
>>> Here is a sample code which illustrate my problem (if any ?):
>>> This is a sample code for the bundle installer, which loads the 
>>> "MyBundle" bundle from /tmp/mybundle.jar:
>>> When the cache is empty, the installer will install/start 
>>> /tmp/mybundle.jar with start level 10.
>>> But if the bundle is already installed, then it will be uninstalled, 
>>> just before entering into sl 10 -->
>>>
>>> package installer;
>>>
>>> // Jdk
>>> import java.util.*;
>>> import java.util.jar.*;
>>> import java.io.*;
>>> import java.net.*;
>>> import static java.lang.System.out;
>>>
>>> // Osgi
>>> import org.osgi.service.component.*;
>>> import org.osgi.framework.*;
>>> import org.osgi.service.startlevel.StartLevel;
>>>
>>> public class BundleInstallerActivator implements BundleActivator {
>>>  public void start(BundleContext bctx) {
>>>    try {
>>>      // Get the StartLevel service.
>>>      ServiceReference ref = 
>>> bctx.getServiceReference(StartLevel.class.getName());
>>>      StartLevel sl = (StartLevel) bctx.getService(ref);
>>>
>>>      // if bundle /tmp/mybundle.jar (with name "MyBundle") is 
>>> already installed: uninstall it ->
>>>
>>>      boolean doInstall = true;
>>>      for (Bundle b : bctx.getBundles()) {
>>>        if (b.getSymbolicName().equals("MyBundle")) {
>>>          out.println("Uninstalling /tmp/mybundle.jar ...");
>>>          b.uninstall();
>>>          doInstall = false;
>>>        }
>>>    }
>>>        // else, install/start "/tmp/mybundle.jar" with start level 
>>> 10 ->
>>>        if (doInstall) {
>>>      out.println("Installing /tmp/mybundle.jar with start level=10");
>>>      FileInputStream in = new FileInputStream("/tmp/mybundle.jar");
>>>      sl.setInitialBundleStartLevel(10);
>>>      Bundle myBundle = bctx.installBundle("MyBundle", in);
>>>
>>>      out.println("Starting /tmp/mybundle.jar ");
>>>      myBundle.start(); // will be started only when we enter into sl 
>>> 10 (see below)
>>>      in.close();
>>>    }
>>>
>>>    out.println("Entering into start level 10 ...");
>>>    sl.setStartLevel(10); //
>>>  }
>>>    catch (Throwable t) {
>>>      t.printStackTrace();
>>>    }
>>> }
>>>
>>>  public void stop(BundleContext bctx) {
>>>  }
>>> }
>>>
>>> -> When I start Felix with an empty cache, it works fine, and I get 
>>> my bundle "/tmp/mybundle.jar" installer and activated (once the 
>>> start level 10 is entered):
>>>
>>> rm -rf ~/.felix/; java -jar bin/felix.jar
>>> Welcome to Felix.
>>> =================
>>> Enter profile name: test
>>> -> Installing /tmp/mybundle.jar with start level=10
>>> Starting /tmp/mybundle.jar
>>> Entering into start level 10
>>> MyBundle activated
>>>
>>> -> But, when I restart Felix (without removing the cache, which 
>>> contains the bundle "MyBundle"), then the installer uninstall the
>>> /tmp/mybundle.jar, but I get the following exception, when entering 
>>> into sl 10:
>>>
>>> java -jar bin/felix.jar ar
>>> Welcome to Felix.
>>> =================
>>> Enter profile name: test
>>> -> Uninstalling /tmp/mybundle.jar ...
>>> Entering into start level 10
>>> ERROR: Error stopping MyBundle (java.lang.IllegalStateException: 
>>> Cannot stop an uninstalled bundle.)
>>> java.lang.IllegalStateException: Cannot stop an uninstalled bundle.
>>>        at org.apache.felix.framework.Felix._stopBundle(Felix.java:1979)
>>>        at org.apache.felix.framework.Felix.stopBundle(Felix.java:1954)
>>>        at 
>>> org.apache.felix.framework.Felix.setFrameworkStartLevel(Felix.java:1141) 
>>>
>>>        at 
>>> org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:258)
>>>        at java.lang.Thread.run(Thread.java:595)
>>>
>>>
>>> Was I wrong with my bundle installer ? does any one could explain 
>>> why I get the IllegalState exception ?
>>>
>>> Kind Regards
>>> /Pierre
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Error while uninstalling bundles dynamically.

Posted by Pierre De Rop <pi...@alcatel-lucent.fr>.
OK, I think I understand and you are probably talking about the 
"extender pattern". I will try to modify my bundle installer in order to 
act in the same way you are suggesting ...

Thank you very much for your quick response !

/Pierre



Richard S. Hall wrote:
> Pierre,
>
> I created a bundle out of your code below and at first was not able to 
> recreate the issue by simply stopping and restarting the installer 
> bundle. In that case it worked as expected.
>
> But then I tried to restart the framework, that is when I witnessed 
> the issue. The issue here is that you are trying to mess with the set 
> of installed bundles while the framework is trying to restart that set 
> of installed bundles. This is really not a good idea. You are 
> essentially pulling the rug out from underneath the framework by 
> uninstalling a bundle that it is trying to restart.
>
> What has likely happened is the framework has created a list of 
> bundles that need to be processed and during the processing of these 
> bundles, the installer bundle goes and uninstall one. Eventually the 
> framework processes the uninstalled bundle after processing the 
> installer bundle and it results in the error you mention.
>
> I think what your bundle needs to do here is register for a framework 
> started event so it can do some post-processing of the installed 
> bundle list. Following such an approach, you might have to make a 
> special case when your installer is installed and started for the 
> first time, since the framework started event will already be passed. 
> Unless you always expect it to be installed (such as via the 
> auto-start properties), then I think it might work correctly.
>
> -> richard
>
>
> Pierre De Rop wrote:
>> Hi there ...
>>
>> When starting the Felix fwk, we use our own bundle installer which is 
>> in charge of installing required bundles dynamically (using 
>> BundleContext.installBundle() method).
>> Our Installer installs bundles with the start level service; and each 
>> bundle is installed with some dedicated start levels.
>> When the installer has installed/started every required bundles, it 
>> finally enters into start level 10.
>> Some times, *before entering into the start level 10*, the installer 
>> has to *uninstall* already installed bundles; but when entering into 
>> sl 10: I get the following exception: 
>> "java.lang.IllegalStateException: Cannot stop an uninstalled bundle."
>>
>> Here is a sample code which illustrate my problem (if any ?):
>> This is a sample code for the bundle installer, which loads the 
>> "MyBundle" bundle from /tmp/mybundle.jar:
>> When the cache is empty, the installer will install/start 
>> /tmp/mybundle.jar with start level 10.
>> But if the bundle is already installed, then it will be uninstalled, 
>> just before entering into sl 10 -->
>>
>> package installer;
>>
>> // Jdk
>> import java.util.*;
>> import java.util.jar.*;
>> import java.io.*;
>> import java.net.*;
>> import static java.lang.System.out;
>>
>> // Osgi
>> import org.osgi.service.component.*;
>> import org.osgi.framework.*;
>> import org.osgi.service.startlevel.StartLevel;
>>
>> public class BundleInstallerActivator implements BundleActivator {
>>  public void start(BundleContext bctx) {
>>    try {
>>      // Get the StartLevel service.
>>      ServiceReference ref = 
>> bctx.getServiceReference(StartLevel.class.getName());
>>      StartLevel sl = (StartLevel) bctx.getService(ref);
>>
>>      // if bundle /tmp/mybundle.jar (with name "MyBundle") is already 
>> installed: uninstall it ->
>>
>>      boolean doInstall = true;
>>      for (Bundle b : bctx.getBundles()) {
>>        if (b.getSymbolicName().equals("MyBundle")) {
>>          out.println("Uninstalling /tmp/mybundle.jar ...");
>>          b.uninstall();
>>          doInstall = false;
>>        }
>>    }
>>        // else, install/start "/tmp/mybundle.jar" with start level 10 ->
>>        if (doInstall) {
>>      out.println("Installing /tmp/mybundle.jar with start level=10");
>>      FileInputStream in = new FileInputStream("/tmp/mybundle.jar");
>>      sl.setInitialBundleStartLevel(10);
>>      Bundle myBundle = bctx.installBundle("MyBundle", in);
>>
>>      out.println("Starting /tmp/mybundle.jar ");
>>      myBundle.start(); // will be started only when we enter into sl 
>> 10 (see below)
>>      in.close();
>>    }
>>
>>    out.println("Entering into start level 10 ...");
>>    sl.setStartLevel(10); //
>>  }
>>    catch (Throwable t) {
>>      t.printStackTrace();
>>    }
>> }
>>
>>  public void stop(BundleContext bctx) {
>>  }
>> }
>>
>> -> When I start Felix with an empty cache, it works fine, and I get 
>> my bundle "/tmp/mybundle.jar" installer and activated (once the start 
>> level 10 is entered):
>>
>> rm -rf ~/.felix/; java -jar bin/felix.jar
>> Welcome to Felix.
>> =================
>> Enter profile name: test
>> -> Installing /tmp/mybundle.jar with start level=10
>> Starting /tmp/mybundle.jar
>> Entering into start level 10
>> MyBundle activated
>>
>> -> But, when I restart Felix (without removing the cache, which 
>> contains the bundle "MyBundle"), then the installer uninstall the
>> /tmp/mybundle.jar, but I get the following exception, when entering 
>> into sl 10:
>>
>> java -jar bin/felix.jar ar
>> Welcome to Felix.
>> =================
>> Enter profile name: test
>> -> Uninstalling /tmp/mybundle.jar ...
>> Entering into start level 10
>> ERROR: Error stopping MyBundle (java.lang.IllegalStateException: 
>> Cannot stop an uninstalled bundle.)
>> java.lang.IllegalStateException: Cannot stop an uninstalled bundle.
>>        at org.apache.felix.framework.Felix._stopBundle(Felix.java:1979)
>>        at org.apache.felix.framework.Felix.stopBundle(Felix.java:1954)
>>        at 
>> org.apache.felix.framework.Felix.setFrameworkStartLevel(Felix.java:1141)
>>        at 
>> org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:258)
>>        at java.lang.Thread.run(Thread.java:595)
>>
>>
>> Was I wrong with my bundle installer ? does any one could explain why 
>> I get the IllegalState exception ?
>>
>> Kind Regards
>> /Pierre
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org