<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>TechJava&#187; TechJava &#8211; Articles tagged by JAR</title>
	<atom:link href="http://www.techjava.de/topics/tag/jar/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techjava.de</link>
	<description>Journal on Java Technology</description>
	<lastBuildDate>Thu, 17 Jun 2010 10:41:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The PluginLoader &#8211; Applying Java Class loading Concepts</title>
		<link>http://www.techjava.de/topics/2008/02/plugin-class-loader/</link>
		<comments>http://www.techjava.de/topics/2008/02/plugin-class-loader/#comments</comments>
		<pubDate>Mon, 25 Feb 2008 08:29:16 +0000</pubDate>
		<dc:creator>joker</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[technology]]></category>
		<category><![CDATA[class loader]]></category>
		<category><![CDATA[class loading]]></category>
		<category><![CDATA[component]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[JAR]]></category>
		<category><![CDATA[JVM]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://www.techjava.de/topics/2008/02/plugin-class-loader/</guid>
		<description><![CDATA[Introduction
Many frameworks support plugins for increasing flexibility. They need to be loaded during runtime making it possible to change the supported features used in application without recompiling the framework and application themselves. Loading a plug-in means loading java classes, which is done by a class loader. We want to load the plug-ins from jar files, [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>Many frameworks support plugins for increasing flexibility. They need to be loaded during runtime making it possible to change the supported features used in application without recompiling the framework and application themselves. Loading a plug-in means loading java classes, which is done by a class loader. We want to load the plug-ins from jar files, without providing a name for every JAR. Unfortunately, the java standard API does not contain 
<a  href="http://www.techjava.de/topics/tag/class-loading/">a class loader</a> for loading classes from a list of jar files in a directory. We need to provide it ourselves. In addition, the standard class loading mechanism is designed with following idea in mind: in runtime the application accesses the known classname, that is searched for in class path and loaded if found. In our case, we want to force the framework load it extensions, without setting up the class path before applications starts, which is a little different use case.</p>
<h3>Simple JAR class loader</h3>
<p>The first step is loading classes from jar files. For this purpose we create a class named <strong>JarFileClassLoader</strong> which should be able to load a class from a jar file. It inherits from <strong>ClassLoader</strong>, the Java default implementation for a class loader. Loading a class from a jar file means scanning each jar file entries, decide if it is a class then load it. Because we want to be flexible about the criterion we use to select our classes, we introduce an interface <strong>ICriterion</strong>, which decides for a given class if the criterion is fulfilled or not. Using this, the JarFileClassLoader can now scan a single jar file and load every class fulfilling the injected criterion. Loading a class means scanning the caches of the parent class loaders, calling the <strong>loadClass(String)</strong> method of the parents if not found and scanning the jar file afterwards if the class was still not found. This beahviour is described in a previous 
<a  href="http://www.techjava.de/topics/tag/class-loading/">post</a>, introducing the basics of Java Class Loading. </p>
<p>JarFileClassLoader is capable of loading a class if it does not reference other classes. These references have to be resolved during class loading time. Let consider the example of classes A and B where A references B. If A is loaded by a class loader CL, CL is asked for loading B also. This is done by calling the <strong>loadClass(String)</strong> method of CL. If B is contained in the jar file CL loads classes from, CL will find it and return it to the JVM. If B is not contained in CL’s jar file, CL’s parents are asked to load it. Thus the class will be loaded if it is contained in the classpath where one of the java class loaders will find it. If the class is contained in another jar file in the list, but not in the class path it will not be found and the loading will end with a <strong>ClassNotFoundException</strong>.<br />
<img src="http://www.techjava.de/wp-content/uploads/PluginClassLoader/pluginclassloader.png" alt="Plugin Class Loader" style="float:right; margin:5px" /></p>
<h3>Plugin class loader</h3>
<p>To tackle this problem we introduce another class, the <strong>PluginLoader</strong>. It contains a collection of <strong>JarFileClassLoaders</strong> and inherits from ClassLoader, thus is a ClassLoader itself. The <strong>PluginLoader</strong> acts as a parent for all <strong>JarFileClassLoaders</strong> and is therefore asked every time a class has to be loaded. It will then iterate over all <strong>JarFileClassLoaders</strong> calling the <strong>loadClass(String)</strong> method on all of them to find the class it looks for. This way, classes contained in sibling class loaders are found also without violating the rule that a class loader can only see classes of itself or its parents. </p>
<h3>Putting everything together</h3>
<p>Now the structure of our plug-in loader component becomes clear. What we additionally need to do, is get rid of all the stack overflows we produce with the above classes. The problem here is that any <strong>JarFileClassLoader</strong> asks its parent, the <strong>PluginLoader</strong>, to load a class. This will ask all child <strong>JarFileClassLoaders</strong> to load the class including the CL which asked the <strong>PluginLoader</strong> before. The result is an infinite call stack loop &#8211; a stack overflow. Therefore we relax the concept of the java class loading. We introduce a method <strong>loadClassSimple(String)</strong> on the <strong>JarFileClassLoaders</strong> which looks just in its very own jar file. If it does not find the wanted class it just throws an Exception without asking a parent or cache-lookups. This method is called by the <strong>PluginLoader</strong> only, therefore the rest of the class loading is not affected.<br />
<img src="http://www.techjava.de/wp-content/uploads/PluginClassLoader/pluginclassloader_instance.png" alt="Plugin Class Loader at runtime" style="float:right; margin:5px" /><br />
Last but not least we consider some performance issues. If a class is searched in other jar files, those have to be scanned every time again resulting in poor performance. Therefore we introduce a cache in the <strong>JarFileClassLoader</strong> containing every class that was loaded before. Every request is served now by trying to find the class in the cache first. </p>
<h3>Limitations</h3>
<p>Due to the fact, that <strong>PluginLoader</strong> asks its child <strong>JarClassLoaders</strong> to load a class in particuliar order, and the first class found will be taken, the order in which <strong>JarClassLoaders</strong> are asked matters. This issue can cause problems if e.G. different versions of the same library are deployed on location scanned by the <strong>PluginLoader</strong> and should be handled with care.</p>
<h3>References</h3>
<ul>
<li>
<div>[2007,book] 
<a  href="#2007_KRUEGER" class="toggle">bibtex</a>  
<a  href='http://www.javabuch.de/} publisher =	{Addison-Wesley} isbn =	{3-8273-2373-8' title='Go to document' onclick="javascript:pageTracker._trackPageview('/external/www.javabuch.de/} publisher =_{Addison-Wesley} isbn =_{3-8273-2373-8');" ><img src='http://www.techjava.de/wp-content/plugins/bib2html/external.png' width='10' height='10' alt='Go to document' /></a></div>
<div>G. Krueger and T. Stark, <em>Handbuch der Java-Programmierung</em>, , 2007.</div>
<div class="bibtex" id="2007_KRUEGER">
         <code>@book{2007_KRUEGER, <br />
 &nbsp;&nbsp;author =	{Guido Krueger and Thomas Stark}, <br />
 &nbsp; title =	{Handbuch der Java-Programmierung}, <br />
 &nbsp; month =	Nov, year =	{2007}, <br />
 &nbsp; url = {http://www.javabuch.de/} publisher =	{Addison-Wesley} isbn =	{3-8273-2373-8}<br />
}</code>
    </div>
</li>
<li>
<div>[,techreport] 
<a  href="#" class="toggle">bibtex</a>  
<a  href='{http://www.developer.com/java/other/article.php/2248831' title='Go to document' onclick="javascript:pageTracker._trackPageview('/external/{http//www.developer.com/java/other/article.php/2248831');" ><img src='http://www.techjava.de/wp-content/plugins/bib2html/external.png' width='10' height='10' alt='Go to document' /></a></div>
<div>&quot;Java Class Loading: The Basics,&quot;.</div>
<div class="bibtex" id="">
         <code>@techreport{CL_BASICS author =	{Brandon E. Taylor}, <br />
 &nbsp; title =	{Java Class Loading: The Basics}, <br />
 &nbsp; url = {http://www.developer.com/java/other/article.php/2248831}</code>
    </div>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.techjava.de/topics/2008/02/plugin-class-loader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
