Skip to main content

OSGi: Starters Guide for Install, Update and Refresh Bundles

In OSGi, with a Bundle you could Install, Update or Refresh. There is a subtle difference in all these three. Just enough for a beginner to entangle the mind. To help, This post gives an a practical insight. I recorded the commands and thier out puts.

To Begin with, Install puts a bundle in the framework runtime. It also records the file location (on FileSystem or URL). It doesnot start or resolve dependents on it.

sarath@sar-akshaya:~/zbin/equinox$ ls
bundles  equinox.jar  org.eclipse.osgi_3.4.0.v20080605-1900.jar
sarath@sar-akshaya:~/zbin/equinox$ ls bundles/
FirstBundle-1.0.0.jar   SecondBundle-1.0.0.jar  
sarath@sar-akshaya:~/zbin/equinox$ java -jar equinox.jar -console

osgi> ss

Framework is launched.

id State       Bundle
0 ACTIVE      org.eclipse.osgi_3.4.0.v20080605-1900

osgi>  install file:bundles/FirstBundle-1.0.0.jar
Bundle id is 1

//Try starting 
osgi> start 1
org.osgi.framework.BundleException: The bundle could not be resolved. Reason: Missing Constraint: Import-Package: com.so.samples.osgi.second; version="0.0.0"
 at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:305)
 at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:265)
 at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:257)
 at org.eclipse.osgi.framework.internal.core.FrameworkCommandProvider._start(FrameworkCommandProvider.java:257)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.eclipse.osgi.framework.internal.core.FrameworkCommandInterpreter.execute(FrameworkCommandInterpreter.java:150)
 at org.eclipse.osgi.framework.internal.core.FrameworkConsole.docommand(FrameworkConsole.java:302)
 at org.eclipse.osgi.framework.internal.core.FrameworkConsole.console(FrameworkConsole.java:287)
 at org.eclipse.osgi.framework.internal.core.FrameworkConsole.run(FrameworkConsole.java:223)
 at java.lang.Thread.run(Thread.java:619)

osgi> diag 1
file:bundles/FirstBundle-1.0.0.jar [1]
  Direct constraints which are unresolved:
    Missing imported package com.so.samples.osgi.second_0.0.0.

osgi> install file:bundles/SecondBundle-1.0.0.jar
Bundle id is 2

osgi> ss

Framework is launched.

id State       Bundle
0 ACTIVE      org.eclipse.osgi_3.4.0.v20080605-1900
1 INSTALLED   com.so.samples.osgi.FirstBundle_1.0.0
2 INSTALLED   com.so.samples.osgi.SecondBundle_1.0.0

//Try starting Again
osgi> start 1
org.osgi.framework.BundleException: The bundle could not be resolved. Reason: Missing Constraint: Import-Package: com.so.samples.osgi.second; version="0.0.0"
 at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:305)
 at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:265)
 at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:257)
 at org.eclipse.osgi.framework.internal.core.FrameworkCommandProvider._start(FrameworkCommandProvider.java:257)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.eclipse.osgi.framework.internal.core.FrameworkCommandInterpreter.execute(FrameworkCommandInterpreter.java:150)
 at org.eclipse.osgi.framework.internal.core.FrameworkConsole.docommand(FrameworkConsole.java:302)
 at org.eclipse.osgi.framework.internal.core.FrameworkConsole.console(FrameworkConsole.java:287)
 at org.eclipse.osgi.framework.internal.core.FrameworkConsole.run(FrameworkConsole.java:223)
 at java.lang.Thread.run(Thread.java:619)

osgi> diag 1
file:bundles/FirstBundle-1.0.0.jar [1]
  Direct constraints which are unresolved:
    Missing imported package com.so.samples.osgi.second_0.0.0.

Refresh resolves dependencies. If run on a bundle with an active dependent, it refreshes the dependents too. If a bundle has an activator, It stops and restarts.

osgi> refresh 1

osgi> ss   

Framework is launched.

id State       Bundle
0 ACTIVE      org.eclipse.osgi_3.4.0.v20080605-1900
1 RESOLVED    com.so.samples.osgi.FirstBundle_1.0.0
2 RESOLVED    com.so.samples.osgi.SecondBundle_1.0.0

osgi> start 1
Started Bundle:From Second 1.0.0

osgi> shutdown
Stopped Bundle:From Second 1.0.0

osgi> close

sarath@sar-akshaya:~/zbin/equinox$ java -jar equinox.jar -console

osgi> Started Bundle:From Second 1.0.0

osgi> ss

Framework is launched.

id State       Bundle
0 ACTIVE      org.eclipse.osgi_3.4.0.v20080605-1900
1 ACTIVE      com.so.samples.osgi.FirstBundle_1.0.0
2 RESOLVED    com.so.samples.osgi.SecondBundle_1.0.0

osgi> packages 1
No exported packages

osgi> packages 2
com.so.samples.osgi.second; version="0.0.0"
  file:bundles/FirstBundle-1.0.0.jar [1] imports

//make change 1.0.1
osgi> install file:bundles/SecondBundle-1.0.0.jar        
Bundle id is 2

osgi> stop 1
Stopped Bundle:From Second 1.0

osgi> start 1
Started Bundle:From Second 1.0

osgi> refresh 2
Stopped Bundle:From Second 1.0
Started Bundle:From Second 1.0

osgi> stop 1
Stopped Bundle:From Second 1.0

Update just RE-puts the bundle into framework. This is slightly different from install, that you can ONLY update a bundle with same [Bundle-SymbolicName:Bundle-Version] combination. Meaning-- You cannot Install a same jar (same uri or different) or an updated jar(new code) with the same combination. You only have to UPDATE it in the framework. Update can be done by the BundleId in the runtime. To update with a Different jar (uri is different)

osgi> update 2 

osgi> refresh 2

osgi> Stopped Bundle:From Second 1.0
Started Bundle:From Second - 1.0.0 update

osgi> update 1
Stopped Bundle:From Second - 1.0.0 update
Started Bundle:From Second - 1.0.0 update

osgi>update 1 file:bundles/newfirst.jar //with same symname:version combo
Stopped Bundle:From Second - 1.0.0 update 
Started Bundle:From Second - 1.0.0 update 
Hope this helps.. Read more on Osgi

Popular posts from this blog

Appcache manifest file issues/caveats

Application cache (appcache) is a powerful feature in HTML5. However, it does come with baggage. Many (see links below) advocated ferociously against it due to tricky issues it comes with. For someone who is just testing waters, these issues may throw them off grid. Knowing them before hand helps reduce some unpredictable effects.

Being a Vegetarian

I am a Proud Vegetarian. I don't eat Meat or Eggs. People say its hard here in US to be one. I beg to differ. The mere fact that I am hail and healthy these 4 years is a definitive proof. Apart from being bullied and trash talked by The Meat-Eaters, There is really nothing that makes this choice of mine any more than a debatable issue at a lunch or dinner. Other things aside, I am writing this blog having watched a PETA Video. Before you click on the play button, I ask you - If you are a vegetarian : Dont watch it. If you are not : Dare to watch it till the end. If you think going veg is just a fashion, think again . Even if you just want to do it for Fashion . Do it. Go Vegetarian. And Feel better asking the waiter for a Vegetarian Entrée in your next lunch.

classpath*: making your Modular Spring Resources

Spring gives multiple options to load XML resources for building contexts. the reference documentation does explain this feature quite well. However, I am taking my shot at explaining the different practical scenarios ( by order of growing modularisation) For Example, A simplest Spring based web Context Loader can be configured with resources like this <context-param> <param-name>contextConfigLocation</param-name> <param-value>applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> You just need to put applicationContext.xml in WEB-INF/ folder of your webapp. However, Typically an application is n-tiered. You can also have multiple files setup and in relative paths. like <param-value> context-files/applicationContext.xml context-files/dao.xml context-files/service.xml </param-value>