Java Interview Questions2 OSGI

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

1.

OSGI

1. Benefits of using OSGI


Reduced Complexity,Reuse, Real World – Easy Deployment, Dynamic Updates,Adaptive ,
Transparency ,Versioning , Simple , Small ,fast, Secure ,runs everywhere, Widely Used .

2. How to load classes in OSGI, Creation of executable extension etc.


When a class name is given, class loader first locates the class and then reads a class file of that name from
the native file system. Therefore this loading process is platform dependent.
By default java.lang.ClassLoader is registered as a class loader that is capable of loading classes in parallel.
But the subclasses needs to register as parallel or not at the time of instantiation.
Classes can also be loaded from network, constructed on runtime and loaded. ClassLoader class has
a method name defineClass which takes input as byte array and loads a class.
Class Loader Parent
All class loaders except bootstrap class loader has a parent class loader. This parent is not as in parent-child
relationship of inheritance. Every class loader instance is associated with a parent class loader.
When a class loader is entrusted with the responsibility of loading a class, as a first step it delegates
this work to the associated parent class loader. Then this parent class loader gets the instruction and
sequentially it delegates the call to its parent class loader. In this chain of hierarchy the bootstrap class
loader is at the top.
When a class loader instance is created, using its constructor the parent classloader can be associated with
it.
Class Loader Rule 1
A class is loaded only once into the JVM.
In this rule, what is “a class”? Uniqueness of a class is identified along with the ClassLoader instance that
loaded this class into the JVM. A class is always identified using its fully qualified name (package.classname).
So when a class is loaded into JVM, you have an entry as (package, classname, classloader). Therefore the
same class can be loaded twice by two different ClassLoader instances.

3. Explain classloader in java ?


Java ClassLoader loads a java class file into java virtual machine. It is as simple as that.
Each OSGi classloader instance (i.e OSGi bundle) has its own private mapping table that tells it which
classloader is responsible for providing classes from a particular package to the current bundle. This is
usually populated when the bundle is first loaded(“resolved”) although “dynamic imports” lock things up via
the “bundle registry” as needed.

When a jarfile for a bundle is loaded , OSGi’s Framework class fisrt creates a bundle object to represent it,
and that in turn creates a classloader which is a child of an “OSGi environment classloader”.
4. Types (Hierarchy) of Java Class Loaders:
Java class loaders can be broadly classified into below categories:
Bootstrap Class Loader
Bootstrap class loader loads java’s core classes like java.lang, java.util etc. These are classes that are part of
java runtime environment. Bootstrap class loader is native implementation and so they may differ across
different JVMs.
Extensions Class Loader
JAVA_HOME/jre/lib/ext contains jar packages that are extensions of standard core java classes. Extensions
class loader loads classes from this ext folder. Using the system environment propery java.ext.dirs you can
add ‘ext’ folders and jar files to be loaded using extensions class loader.
System Class Loader
Java classes that are available in the java classpath are loaded using System class loader.

5. What is OSGI container.


OSGi is a component framework for Java.  
OSGi is a framework for Java in which units of resources called bundles can be installed. Bundles can export
services or run processes, and have their dependencies managed, such that a bundle can be expected to
have its requirements managed by the container. Each bundle can also have its own internal classpath, so
that it can serve as an independent unit, should that be desireable. All of this is standardized such that any
valid OSGi bundle can theoretically be installed in any valid OSGi container.

6. How do we configure, load and run an OSGI bundle.


To run Equinox OSGi on its own either download the org.eclipse.osgi JAR from the download site or look in
your Eclipse install for a file like <install location>/eclipse/plugins/org.eclipse.osgi_3.2.0_xxx.jar. Once you
have the Equinox framework JAR, use the following command line:
java -jar org.eclipse.osgi_3.2.0.jar -console
Once this is running you will see an osgi> prompt. This is the OSGi console waiting for you to type
commands. Type '?' at the prompt for command help. The most interesting commands for getting started
are:
install <bundle URL> - Installs the bundle from the given URL
start <bundle # or bundle name> - Starts the bundle with the given numeric or symbolic id
stop <bundle # or bundle name> - Stops the bundle with the given numeric or symbolic id
ss - Reports a summary status of all installed bundles
diag <bundle # or bundle name> - Reports any resolution problems for the bundle with the given numeric or
symbolic id
If you don't have Eclipse and just want OSGi, download the JAR from the Equinox download site.
Configurations and all that...

The Equinox OSGi implementation is extremely configurable. One of the most common configuration
scenarios is to have the framework automatically install and run a set of bundles when it is started. You do
this every time you run Eclipse the IDE. Here's how it works and how you can use it in your situation.
Say you have bundles B1.jar and B2.jar that you want to have installed and started when Equinox is run. As
with other OSGi implementations, you can run the framework (see above) and install and start B1 and B2
using the console. This is a manual process but you need only do it the first time you run Equinox (OSGi
frameworks remember the installed and started bundles from run to run). To do something completely
automated, create a configuration that lists B1 and B2 as bundles to be installed and started. Set up
something like
somedir/
configuration/
config.ini
org.eclipse.osgi_3.2.0.jar
B1.jar
B2.jar
Where config.ini is a Java properties file that contains the following line. Note that the osgi.bundles property
is quite powerful. See the doc for details.
osgi.bundles=B1.jar@start, B2.jar@start
eclipse.ignoreApp=true
When Equinox is started using the command line above, B1 and B2 are installed and started. If you want to
specify a different configuration, just add -configuration <location> to the command line. Notice that since
you are just running your bundles, we added a line to tell Equinox to skip trying to start an Eclipse
application. This setting is not critical but without it a scary but otherwise inoccuous message appears in
your log.

7. Difference b/w OSGI bundle and service.


Bundle manage the lifecycle
A bundle can have only one activator (needing a BundleContext), and can have as many active components
as you want.That means you may end up trying to fit in one activator several loosely-related concerns into a
single class.
A service is any object that is registered in the OSGi Service Registry and can be looked up using its
interface name(s). The only prerequisite is that a service should implement some interface... any interface.
For example I could register a runnable object under the java.lang.Runnable interface, and clients could look
it up using that interface name.

You might also like