SlideShare a Scribd company logo
1 of 4
Eclipse as we know is a great implementation of plugin-architecture.

We decouple different components of a system into bundles.
plugin-architecture is very simplistic in nature, highly extensible and modular at the
cost of a tricky class-loading policy.
Actually Eclipse is a story of several class-loaders.
It iniataites a chain of class loaders to load the plug-ins lazily as specified in
component-specific manifest.mf files.

If we understand its class-loading policy and learn some tricks, then we can make
different third-party jars talk to each other and avoid infamous 'ClassNotFound
Exception'.
The immediate parent of a plug-in class loader is the Eclipse Boot Class Loader
(providing access to boot.jar). The parent is the standard java system class loader
which terminates the chain. The Application Class-loader which loads classes from
the system's CLASSPATH is not part of the chain.

That's why while loading product components, Eclipse does not look into Classpath
in contrast to any other java-based client applications.
On start-up, the eclipse platform builds a shadow of all the plugins by reading all the
manifest files into a plug-in registry.

Whenever a plugin com.xyz.myProduct is started by Eclipse Application Class,
the class-loader for com.xyz.myProduct takes-off.
If com.myProduct.xyz tries to access any class from another plugin
com.myProduct.abc,
Eclipse invokes the Plug-in Class-loader coresponding to com.myProduct.abc.
That's the essence of Eclipse OSGi.

Q1. How to use third-party applications while building and running my
product ?
Its of no use to specify third-party jars in classpath.
While building the application com.myProduct, we should bundle all the required jars
in a single plugin (com.myProduct.library) and expose the apis of the bundled log4j,
jpox, xstream etc.

Q2. How a third-party jar (log4j.jar) can see the classes from
com.myProduct.xyz.jar during runtime ?
Lets assume com.abc.myProduct plugin logs messages using log4j.
So during runtime, log4j needs to see the classes from com.myProduct.abc plugin
This can be achieved by registering log4j with Eclipse-Buddy Policy and specifying
com.abc.myProduct as a buddy of log4j.
# log4j Manifest.MF
Bundle-Name: org.apache.log4j
Bundle-Version: 1.2.13
...
Eclipse-BuddyPolicy: registered

# myplugin Manifest.MF
Bundle-Name: com.abc.myProduct
Bundle-Version: 1.0.0
Requires-Bundle: org.apache.log4j,...
Eclipse-RegisterBuddy: org.apache.log4j
If anyone registers with log4j as its buddy, and log4j needs to find a class
com.abc.myProduct.MyComponent then it will check all the buddies before throwing
"ClassNotFound" Exception

Q3. How can users integrate third-party jars with myProduct plugins on-the-
fly ?
Lets see how users can actually hack eclipse configuration files to integrate required
jars on-the-fly.
say user has installed a plugin com.magicScript which allows him to program using
any script including jruby. But the product plugin doesn't ship the jruby jars i.e. does
not adopt any of the above mechanisms.

So user have to add JRUBY_MOME in eclipse.ini. Now when eclipse will start up it
will set jruby home in the path.
Lets assume the com.magicScript plugin already depends on a plugin
com.magicScript.library containing a lib folder.

Next the jruby.jar needs to be placed inside the lib folder and the location libjruby.jar
needs to be specified in the manifest.mf of com.magicScript.library.

Finally, starting eclipse with -clean -Intialization option will automatically first set
Jruby path and then invoking magicScript perspective will trigger the classLoader for
the bundle com.magicScript.library which in turn will load jruby classes from the jar
(as specified in manifest.mf).

Thus user will be able to code/compile/run jruby.
This same trick can be used if we want the user to dynamically specify a DB driver
jar and connect to a db using a plugin already installed in his environment.
Say com.myProduct.dbExplorer plugin will load classes from the jar to be specified
by users during runtime.

(4) So far we depend only on Eclipse Bundle-ClassLoader and we configure eclipse
in such a way (either development-time / runtime) that all required jars will be loaded
by the classloader of the bundle in which jars are packed.

But what if we need to access a class which can not be loaded by the bundle-
classloader ?
We need to set certain jars in custom classloader so the classes bundled in the jar can
be loaded on demand !

A typical scenario is com.xyz.MyProduct.library contains scriptDebugger.jar (some
3rdparty jar) whose api need to be invoked during runtime and the api class will
access some class of jruby.jar (may be specified by user during runtime) which can't
be packed inside the product.
//The follwoing piece-of-code should be part of the
com.xyz.myProduct.ScriptManager to load classes from jruby jar that user will
specify during runtime.
// gather the names and loacation of jars as provided by user through preference page
after product is deployed.
String[] jarslist = DynamicJarLoader.getThirdPartyJars();
URL[] jarURLs = new URL[jarslist.length];
JarFileLoader newLoader = new JarFileLoader(jarURLs);
for (int i = 0; i < jarslist.length; i++) {
newLoader.addFile (jarslist[i]);
}
class JarFileLoader extends URLClassLoader
{
public JarFileLoader (URL[] urls)
{
super (urls);
}

public void addFile (String path) throws MalformedURLException
{
String urlPath = "jar:file://" + path + "!/";
addURL (new URL (urlPath));
}
}

Now swap bundleclassloader with your classloader !

ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
try {
current.setContextClassLoader( newLoader);
// Either Load reqd. classes and work with them
newLoader.loadClass ("org.jruby.JRubyClient");
newLoader.loadClass ("org.hsqldb.jdbcDriver");
newLoader.loadClass("oracle.jdbc.driver.OracleDriver");
// Or invoke some other api (of scriptRunner.jar which will load JRuby classes to
compile/run jruby script)
}catch(Exception exception) {
exception.printStackTrace();
}finally { // Restore Eclipse Bundle Class-loader
current.setContextClassLoader(oldLoader);
}
further reading : http://www.eclipsezone.com/articles/eclipse-vms/

More Related Content

What's hot

Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz SAurabh PRajapati
 
212 kuliah 01 pengenalan pemrograman berorientasi objek (java)
212 kuliah 01   pengenalan pemrograman berorientasi objek (java)212 kuliah 01   pengenalan pemrograman berorientasi objek (java)
212 kuliah 01 pengenalan pemrograman berorientasi objek (java)yuan99
 
JVM, JRE and Javac are the main part for the java program
 JVM, JRE and Javac are the main part for the java program JVM, JRE and Javac are the main part for the java program
JVM, JRE and Javac are the main part for the java programsiyaram ray
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to javajayc8586
 
Integrating tomcat with apache
Integrating tomcat with apacheIntegrating tomcat with apache
Integrating tomcat with apachegovindraj8787
 
Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...Mr. Akaash
 
Introduction to java
Introduction to java Introduction to java
Introduction to java Java Lover
 

What's hot (20)

Introduction to java technology
Introduction to java technologyIntroduction to java technology
Introduction to java technology
 
JAVA Program Examples
JAVA Program ExamplesJAVA Program Examples
JAVA Program Examples
 
Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz
 
History of java'
History of java'History of java'
History of java'
 
212 kuliah 01 pengenalan pemrograman berorientasi objek (java)
212 kuliah 01   pengenalan pemrograman berorientasi objek (java)212 kuliah 01   pengenalan pemrograman berorientasi objek (java)
212 kuliah 01 pengenalan pemrograman berorientasi objek (java)
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Java architecture
Java architectureJava architecture
Java architecture
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Learn Java Part 1
Learn Java Part 1Learn Java Part 1
Learn Java Part 1
 
2. hello java
2. hello java2. hello java
2. hello java
 
JVM, JRE and Javac are the main part for the java program
 JVM, JRE and Javac are the main part for the java program JVM, JRE and Javac are the main part for the java program
JVM, JRE and Javac are the main part for the java program
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Integrating tomcat with apache
Integrating tomcat with apacheIntegrating tomcat with apache
Integrating tomcat with apache
 
Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...
 
What is-java
What is-javaWhat is-java
What is-java
 
Introduction to java
Introduction to java Introduction to java
Introduction to java
 
Mpl 1
Mpl 1Mpl 1
Mpl 1
 
Java unit 1
Java unit 1Java unit 1
Java unit 1
 
Java lab lecture 1
Java  lab  lecture 1Java  lab  lecture 1
Java lab lecture 1
 
Java programming course for beginners
Java programming course for beginnersJava programming course for beginners
Java programming course for beginners
 

Viewers also liked

Wondeland Of Modelling
Wondeland Of ModellingWondeland Of Modelling
Wondeland Of ModellingKaniska Mandal
 
Fluency phrases set 1revised
Fluency phrases set 1revisedFluency phrases set 1revised
Fluency phrases set 1revisedeasorrell
 
Standing O Capabilities
Standing O CapabilitiesStanding O Capabilities
Standing O Capabilitiesdorourke88
 
Standing O Capabilities
Standing O CapabilitiesStanding O Capabilities
Standing O Capabilitiesdorourke88
 
Standing O Capabilities
Standing O CapabilitiesStanding O Capabilities
Standing O Capabilitiesdorourke88
 
Missing number presentation
Missing number presentationMissing number presentation
Missing number presentationeasorrell
 
Standing O Capabilities
Standing O CapabilitiesStanding O Capabilities
Standing O Capabilitiesdorourke88
 
Fluency phrases set 1revised
Fluency phrases set 1revisedFluency phrases set 1revised
Fluency phrases set 1revisedeasorrell
 
Correa leiva andrea
Correa leiva andreaCorrea leiva andrea
Correa leiva andreadebora
 
Machine Learning Comparative Analysis - Part 1
Machine Learning Comparative Analysis - Part 1Machine Learning Comparative Analysis - Part 1
Machine Learning Comparative Analysis - Part 1Kaniska Mandal
 
Tab Energy Hits a Nerve with Target Audience
Tab Energy Hits a Nerve with Target AudienceTab Energy Hits a Nerve with Target Audience
Tab Energy Hits a Nerve with Target Audiencekellymorris
 
Elementos Esenciales de un Buena Propuesta por Margaret Krome, Michael Fields...
Elementos Esenciales de un Buena Propuesta por Margaret Krome, Michael Fields...Elementos Esenciales de un Buena Propuesta por Margaret Krome, Michael Fields...
Elementos Esenciales de un Buena Propuesta por Margaret Krome, Michael Fields...F2C 2009 Conference
 
Euthanasia Isabel Mª Guerrero
Euthanasia Isabel Mª GuerreroEuthanasia Isabel Mª Guerrero
Euthanasia Isabel Mª Guerreroguest317ebc
 
Electric Heating Solutions - Nexthermal Smart Heat Management
Electric Heating Solutions - Nexthermal Smart Heat ManagementElectric Heating Solutions - Nexthermal Smart Heat Management
Electric Heating Solutions - Nexthermal Smart Heat ManagementDeven Singh
 
The shore club at park shore naples florida
The shore club at park shore naples floridaThe shore club at park shore naples florida
The shore club at park shore naples floridaClick Here
 
1ª Historia Maletin Google para el Emprendedor
1ª Historia Maletin Google para el Emprendedor1ª Historia Maletin Google para el Emprendedor
1ª Historia Maletin Google para el EmprendedorGoogle Emprendedores
 

Viewers also liked (20)

Designing Better API
Designing Better APIDesigning Better API
Designing Better API
 
Wondeland Of Modelling
Wondeland Of ModellingWondeland Of Modelling
Wondeland Of Modelling
 
Fluency phrases set 1revised
Fluency phrases set 1revisedFluency phrases set 1revised
Fluency phrases set 1revised
 
Standing O Capabilities
Standing O CapabilitiesStanding O Capabilities
Standing O Capabilities
 
Standing O Capabilities
Standing O CapabilitiesStanding O Capabilities
Standing O Capabilities
 
Standing O Capabilities
Standing O CapabilitiesStanding O Capabilities
Standing O Capabilities
 
Missing number presentation
Missing number presentationMissing number presentation
Missing number presentation
 
ART K
ART KART K
ART K
 
Standing O Capabilities
Standing O CapabilitiesStanding O Capabilities
Standing O Capabilities
 
Fluency phrases set 1revised
Fluency phrases set 1revisedFluency phrases set 1revised
Fluency phrases set 1revised
 
Correa leiva andrea
Correa leiva andreaCorrea leiva andrea
Correa leiva andrea
 
Machine Learning Comparative Analysis - Part 1
Machine Learning Comparative Analysis - Part 1Machine Learning Comparative Analysis - Part 1
Machine Learning Comparative Analysis - Part 1
 
Tab Energy Hits a Nerve with Target Audience
Tab Energy Hits a Nerve with Target AudienceTab Energy Hits a Nerve with Target Audience
Tab Energy Hits a Nerve with Target Audience
 
Elementos Esenciales de un Buena Propuesta por Margaret Krome, Michael Fields...
Elementos Esenciales de un Buena Propuesta por Margaret Krome, Michael Fields...Elementos Esenciales de un Buena Propuesta por Margaret Krome, Michael Fields...
Elementos Esenciales de un Buena Propuesta por Margaret Krome, Michael Fields...
 
Euthanasia Isabel Mª Guerrero
Euthanasia Isabel Mª GuerreroEuthanasia Isabel Mª Guerrero
Euthanasia Isabel Mª Guerrero
 
Electric Heating Solutions - Nexthermal Smart Heat Management
Electric Heating Solutions - Nexthermal Smart Heat ManagementElectric Heating Solutions - Nexthermal Smart Heat Management
Electric Heating Solutions - Nexthermal Smart Heat Management
 
The shore club at park shore naples florida
The shore club at park shore naples floridaThe shore club at park shore naples florida
The shore club at park shore naples florida
 
Supertech Apex Tower
Supertech Apex TowerSupertech Apex Tower
Supertech Apex Tower
 
Supertech supernova
Supertech supernovaSupertech supernova
Supertech supernova
 
1ª Historia Maletin Google para el Emprendedor
1ª Historia Maletin Google para el Emprendedor1ª Historia Maletin Google para el Emprendedor
1ª Historia Maletin Google para el Emprendedor
 

Similar to Making Applications Work Together In Eclipse

Introduction to OSGi
Introduction to OSGiIntroduction to OSGi
Introduction to OSGipradeepfn
 
How to run java program without IDE
How to run java program without IDEHow to run java program without IDE
How to run java program without IDEShweta Oza
 
Java Interview Questions Answers Guide
Java Interview Questions Answers GuideJava Interview Questions Answers Guide
Java Interview Questions Answers GuideDaisyWatson5
 
WebLogic's ClassLoaders, Filtering ClassLoader and ClassLoader Analysis Tool
WebLogic's ClassLoaders, Filtering ClassLoader and ClassLoader Analysis ToolWebLogic's ClassLoaders, Filtering ClassLoader and ClassLoader Analysis Tool
WebLogic's ClassLoaders, Filtering ClassLoader and ClassLoader Analysis ToolJeffrey West
 
WebLogic Filtering ClassLoader and ClassLoader Analysis Tool Demo
WebLogic Filtering ClassLoader and ClassLoader Analysis Tool DemoWebLogic Filtering ClassLoader and ClassLoader Analysis Tool Demo
WebLogic Filtering ClassLoader and ClassLoader Analysis Tool DemoJeffrey West
 
Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010Rich Helton
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdfvenud11
 
API workshop: Deep dive into Java
API workshop: Deep dive into JavaAPI workshop: Deep dive into Java
API workshop: Deep dive into JavaTom Johnson
 
Bt0074 oops with java2
Bt0074 oops with java2Bt0074 oops with java2
Bt0074 oops with java2Techglyphs
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questionsGradeup
 

Similar to Making Applications Work Together In Eclipse (20)

Introduction to OSGi
Introduction to OSGiIntroduction to OSGi
Introduction to OSGi
 
How to run java program without IDE
How to run java program without IDEHow to run java program without IDE
How to run java program without IDE
 
Composer namespacing
Composer namespacingComposer namespacing
Composer namespacing
 
Java Interview Questions Answers Guide
Java Interview Questions Answers GuideJava Interview Questions Answers Guide
Java Interview Questions Answers Guide
 
WebLogic's ClassLoaders, Filtering ClassLoader and ClassLoader Analysis Tool
WebLogic's ClassLoaders, Filtering ClassLoader and ClassLoader Analysis ToolWebLogic's ClassLoaders, Filtering ClassLoader and ClassLoader Analysis Tool
WebLogic's ClassLoaders, Filtering ClassLoader and ClassLoader Analysis Tool
 
WebLogic Filtering ClassLoader and ClassLoader Analysis Tool Demo
WebLogic Filtering ClassLoader and ClassLoader Analysis Tool DemoWebLogic Filtering ClassLoader and ClassLoader Analysis Tool Demo
WebLogic Filtering ClassLoader and ClassLoader Analysis Tool Demo
 
Java features
Java featuresJava features
Java features
 
Class loaders
Class loadersClass loaders
Class loaders
 
Dynamic Proxy by Java
Dynamic Proxy by JavaDynamic Proxy by Java
Dynamic Proxy by Java
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdf
 
API workshop: Deep dive into Java
API workshop: Deep dive into JavaAPI workshop: Deep dive into Java
API workshop: Deep dive into Java
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Jvm internal detail
Jvm internal detailJvm internal detail
Jvm internal detail
 
OSGI Modularity
OSGI ModularityOSGI Modularity
OSGI Modularity
 
Bt0074 oops with java2
Bt0074 oops with java2Bt0074 oops with java2
Bt0074 oops with java2
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questions
 
Java Class Loading
Java Class LoadingJava Class Loading
Java Class Loading
 
Java basics
Java basicsJava basics
Java basics
 

More from Kaniska Mandal

Machine learning advanced applications
Machine learning advanced applicationsMachine learning advanced applications
Machine learning advanced applicationsKaniska Mandal
 
MS CS - Selecting Machine Learning Algorithm
MS CS - Selecting Machine Learning AlgorithmMS CS - Selecting Machine Learning Algorithm
MS CS - Selecting Machine Learning AlgorithmKaniska Mandal
 
Core concepts and Key technologies - Big Data Analytics
Core concepts and Key technologies - Big Data AnalyticsCore concepts and Key technologies - Big Data Analytics
Core concepts and Key technologies - Big Data AnalyticsKaniska Mandal
 
Debugging over tcp and http
Debugging over tcp and httpDebugging over tcp and http
Debugging over tcp and httpKaniska Mandal
 
Concurrency Learning From Jdk Source
Concurrency Learning From Jdk SourceConcurrency Learning From Jdk Source
Concurrency Learning From Jdk SourceKaniska Mandal
 
The Road To Openness.Odt
The Road To Openness.OdtThe Road To Openness.Odt
The Road To Openness.OdtKaniska Mandal
 
Perils Of Url Class Loader
Perils Of Url Class LoaderPerils Of Url Class Loader
Perils Of Url Class LoaderKaniska Mandal
 
E4 Eclipse Super Force
E4 Eclipse Super ForceE4 Eclipse Super Force
E4 Eclipse Super ForceKaniska Mandal
 
Create a Customized GMF DnD Framework
Create a Customized GMF DnD FrameworkCreate a Customized GMF DnD Framework
Create a Customized GMF DnD FrameworkKaniska Mandal
 
Creating A Language Editor Using Dltk
Creating A Language Editor Using DltkCreating A Language Editor Using Dltk
Creating A Language Editor Using DltkKaniska Mandal
 
Advanced Hibernate Notes
Advanced Hibernate NotesAdvanced Hibernate Notes
Advanced Hibernate NotesKaniska Mandal
 
Converting Db Schema Into Uml Classes
Converting Db Schema Into Uml ClassesConverting Db Schema Into Uml Classes
Converting Db Schema Into Uml ClassesKaniska Mandal
 
Graphical Model Transformation Framework
Graphical Model Transformation FrameworkGraphical Model Transformation Framework
Graphical Model Transformation FrameworkKaniska Mandal
 
Protocol For Streaming Media
Protocol For Streaming MediaProtocol For Streaming Media
Protocol For Streaming MediaKaniska Mandal
 
Rest With Json Vs Soap With Xml
Rest With Json Vs Soap With XmlRest With Json Vs Soap With Xml
Rest With Json Vs Soap With XmlKaniska Mandal
 
Riding The Semantic Wave
Riding The Semantic WaveRiding The Semantic Wave
Riding The Semantic WaveKaniska Mandal
 

More from Kaniska Mandal (20)

Machine learning advanced applications
Machine learning advanced applicationsMachine learning advanced applications
Machine learning advanced applications
 
MS CS - Selecting Machine Learning Algorithm
MS CS - Selecting Machine Learning AlgorithmMS CS - Selecting Machine Learning Algorithm
MS CS - Selecting Machine Learning Algorithm
 
Core concepts and Key technologies - Big Data Analytics
Core concepts and Key technologies - Big Data AnalyticsCore concepts and Key technologies - Big Data Analytics
Core concepts and Key technologies - Big Data Analytics
 
Debugging over tcp and http
Debugging over tcp and httpDebugging over tcp and http
Debugging over tcp and http
 
Concurrency Learning From Jdk Source
Concurrency Learning From Jdk SourceConcurrency Learning From Jdk Source
Concurrency Learning From Jdk Source
 
The Road To Openness.Odt
The Road To Openness.OdtThe Road To Openness.Odt
The Road To Openness.Odt
 
Perils Of Url Class Loader
Perils Of Url Class LoaderPerils Of Url Class Loader
Perils Of Url Class Loader
 
Eclipse Tricks
Eclipse TricksEclipse Tricks
Eclipse Tricks
 
E4 Eclipse Super Force
E4 Eclipse Super ForceE4 Eclipse Super Force
E4 Eclipse Super Force
 
Create a Customized GMF DnD Framework
Create a Customized GMF DnD FrameworkCreate a Customized GMF DnD Framework
Create a Customized GMF DnD Framework
 
Creating A Language Editor Using Dltk
Creating A Language Editor Using DltkCreating A Language Editor Using Dltk
Creating A Language Editor Using Dltk
 
Advanced Hibernate Notes
Advanced Hibernate NotesAdvanced Hibernate Notes
Advanced Hibernate Notes
 
Best Of Jdk 7
Best Of Jdk 7Best Of Jdk 7
Best Of Jdk 7
 
Converting Db Schema Into Uml Classes
Converting Db Schema Into Uml ClassesConverting Db Schema Into Uml Classes
Converting Db Schema Into Uml Classes
 
EMF Tips n Tricks
EMF Tips n TricksEMF Tips n Tricks
EMF Tips n Tricks
 
Graphical Model Transformation Framework
Graphical Model Transformation FrameworkGraphical Model Transformation Framework
Graphical Model Transformation Framework
 
Mashup Magic
Mashup MagicMashup Magic
Mashup Magic
 
Protocol For Streaming Media
Protocol For Streaming MediaProtocol For Streaming Media
Protocol For Streaming Media
 
Rest With Json Vs Soap With Xml
Rest With Json Vs Soap With XmlRest With Json Vs Soap With Xml
Rest With Json Vs Soap With Xml
 
Riding The Semantic Wave
Riding The Semantic WaveRiding The Semantic Wave
Riding The Semantic Wave
 

Making Applications Work Together In Eclipse

  • 1. Eclipse as we know is a great implementation of plugin-architecture. We decouple different components of a system into bundles. plugin-architecture is very simplistic in nature, highly extensible and modular at the cost of a tricky class-loading policy. Actually Eclipse is a story of several class-loaders. It iniataites a chain of class loaders to load the plug-ins lazily as specified in component-specific manifest.mf files. If we understand its class-loading policy and learn some tricks, then we can make different third-party jars talk to each other and avoid infamous 'ClassNotFound Exception'. The immediate parent of a plug-in class loader is the Eclipse Boot Class Loader (providing access to boot.jar). The parent is the standard java system class loader which terminates the chain. The Application Class-loader which loads classes from the system's CLASSPATH is not part of the chain. That's why while loading product components, Eclipse does not look into Classpath in contrast to any other java-based client applications. On start-up, the eclipse platform builds a shadow of all the plugins by reading all the manifest files into a plug-in registry. Whenever a plugin com.xyz.myProduct is started by Eclipse Application Class, the class-loader for com.xyz.myProduct takes-off. If com.myProduct.xyz tries to access any class from another plugin com.myProduct.abc, Eclipse invokes the Plug-in Class-loader coresponding to com.myProduct.abc. That's the essence of Eclipse OSGi. Q1. How to use third-party applications while building and running my product ? Its of no use to specify third-party jars in classpath. While building the application com.myProduct, we should bundle all the required jars in a single plugin (com.myProduct.library) and expose the apis of the bundled log4j, jpox, xstream etc. Q2. How a third-party jar (log4j.jar) can see the classes from com.myProduct.xyz.jar during runtime ? Lets assume com.abc.myProduct plugin logs messages using log4j. So during runtime, log4j needs to see the classes from com.myProduct.abc plugin This can be achieved by registering log4j with Eclipse-Buddy Policy and specifying com.abc.myProduct as a buddy of log4j. # log4j Manifest.MF Bundle-Name: org.apache.log4j Bundle-Version: 1.2.13
  • 2. ... Eclipse-BuddyPolicy: registered # myplugin Manifest.MF Bundle-Name: com.abc.myProduct Bundle-Version: 1.0.0 Requires-Bundle: org.apache.log4j,... Eclipse-RegisterBuddy: org.apache.log4j If anyone registers with log4j as its buddy, and log4j needs to find a class com.abc.myProduct.MyComponent then it will check all the buddies before throwing "ClassNotFound" Exception Q3. How can users integrate third-party jars with myProduct plugins on-the- fly ? Lets see how users can actually hack eclipse configuration files to integrate required jars on-the-fly. say user has installed a plugin com.magicScript which allows him to program using any script including jruby. But the product plugin doesn't ship the jruby jars i.e. does not adopt any of the above mechanisms. So user have to add JRUBY_MOME in eclipse.ini. Now when eclipse will start up it will set jruby home in the path. Lets assume the com.magicScript plugin already depends on a plugin com.magicScript.library containing a lib folder. Next the jruby.jar needs to be placed inside the lib folder and the location libjruby.jar needs to be specified in the manifest.mf of com.magicScript.library. Finally, starting eclipse with -clean -Intialization option will automatically first set Jruby path and then invoking magicScript perspective will trigger the classLoader for the bundle com.magicScript.library which in turn will load jruby classes from the jar (as specified in manifest.mf). Thus user will be able to code/compile/run jruby. This same trick can be used if we want the user to dynamically specify a DB driver jar and connect to a db using a plugin already installed in his environment. Say com.myProduct.dbExplorer plugin will load classes from the jar to be specified by users during runtime. (4) So far we depend only on Eclipse Bundle-ClassLoader and we configure eclipse in such a way (either development-time / runtime) that all required jars will be loaded by the classloader of the bundle in which jars are packed. But what if we need to access a class which can not be loaded by the bundle- classloader ?
  • 3. We need to set certain jars in custom classloader so the classes bundled in the jar can be loaded on demand ! A typical scenario is com.xyz.MyProduct.library contains scriptDebugger.jar (some 3rdparty jar) whose api need to be invoked during runtime and the api class will access some class of jruby.jar (may be specified by user during runtime) which can't be packed inside the product. //The follwoing piece-of-code should be part of the com.xyz.myProduct.ScriptManager to load classes from jruby jar that user will specify during runtime. // gather the names and loacation of jars as provided by user through preference page after product is deployed. String[] jarslist = DynamicJarLoader.getThirdPartyJars(); URL[] jarURLs = new URL[jarslist.length]; JarFileLoader newLoader = new JarFileLoader(jarURLs); for (int i = 0; i < jarslist.length; i++) { newLoader.addFile (jarslist[i]); } class JarFileLoader extends URLClassLoader { public JarFileLoader (URL[] urls) { super (urls); } public void addFile (String path) throws MalformedURLException { String urlPath = "jar:file://" + path + "!/"; addURL (new URL (urlPath)); } } Now swap bundleclassloader with your classloader ! ClassLoader currentLoader = Thread.currentThread().getContextClassLoader(); try { current.setContextClassLoader( newLoader); // Either Load reqd. classes and work with them newLoader.loadClass ("org.jruby.JRubyClient"); newLoader.loadClass ("org.hsqldb.jdbcDriver"); newLoader.loadClass("oracle.jdbc.driver.OracleDriver"); // Or invoke some other api (of scriptRunner.jar which will load JRuby classes to compile/run jruby script) }catch(Exception exception) {
  • 4. exception.printStackTrace(); }finally { // Restore Eclipse Bundle Class-loader current.setContextClassLoader(oldLoader); } further reading : http://www.eclipsezone.com/articles/eclipse-vms/