servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache ServiceMix > Classloaders
Date Thu, 04 Mar 2010 19:55:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=SM&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/SM/Classloaders">Classloaders</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~gertvanthienen">Gert
Vanthienen</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h1><a name="Classloaders-Classloaders"></a>Classloaders</h1>

<p>There are several classloaders involved in ServiceMix:</p>
<ul>
	<li>the container class loader</li>
	<li>shared library class loader: the parent is the container class loader</li>
	<li>component class loader: parents are container class loader + any referenced SL
class loader</li>
	<li>service unit class loader: parent is the component class loader</li>
</ul>


<p>The container class loader contains: JRE + <tt>/conf</tt> + <tt>/lib/&#42;.jar</tt>
+ <tt>/lib/optional/&#42;.jar</tt>. ServiceMix 3.1, uses <a href="http://classworlds.codehaus.org/"
rel="nofollow">Classworlds</a> to configure a classloader finding path. You can add
your own path into the <tt>conf/servicemix.conf</tt> file.</p>


<p>The components class loaders are defined in the JBI spec and contain the jars referenced
in the JBI descriptor (<tt>META-INF/jbi.xml</tt>).  These class loaders can use
a parent-first (default) or self-first delegation. When a class is loaded, the class loader
will first ask its parent(s) and work back down the classloader hierarchy, or  will first
load the class from its referenced jars.</p>

<p>The service unit class loader is not specified in the JBI spec, because the SU content
is specific to each component.  In ServiceMix, all XBean based SUs may define their own classloader
using the &lt;classpath /&gt; location (though since ServiceMix 3.1 this is no longer
necessary, see the <a href="#Classloaders-NewinServiceMix3.1">last section below</a>).</p>

<p>Let's say you deploy a POJO to the servicemix-lwcontainer.  When building the configuration,
for XBean to find the class, it will ask the SU classloader to do so.  So the component may
be inside this classloader, or one of its parent (servicemix-lwcontainer, servicemix-shared
and the container classloader).  However, the component and SL classloaders are not easily
modified (you need to repackage the artifact and redeploy it), so you can put this class in
the SU or the container.</p>

<p>If you put this class in the container, you will need to restart the container after
having added the jar in the classpath, which is not what we want.  So usually, we put it in
the SU.</p>

<p>The other benefit of using classloaders is that you can have isolated components.
 You could deploy two components (or SU) which use different version of the same library without
any problems.  This is not possible if you put all the dependencies in the container classpath.</p>

<h2><a name="Classloaders-Selffirstdelegation"></a>Self-first delegation</h2>

<p>The common delegation mechanism for classloaders is to delegate to the parent first
when loading a class.  Thus, all classes defined in the container classloader are shared.
 But when a class references another class (using an import statement in the Java code for
example), the referenced classes will be loaded by the same classloader. To avoid such problems,
you can use a self-first delegation where classes are loaded from the classloader, and if
not found, it will ask its parent.</p>

<h3><a name="Classloaders-SharedlibrariesandSMXcomponents"></a>Shared libraries
and SMX components</h3>
<p>To enable self-first delegation for shared libraries and components change the service's
POM.XML to include</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;build&gt;</span>
  <span class="code-tag">&lt;plugins&gt;</span>
    <span class="code-tag">&lt;plugin&gt;</span>
      <span class="code-tag">&lt;groupId&gt;</span>org.apache.servicemix.tooling<span
class="code-tag">&lt;/groupId&gt;</span>
      <span class="code-tag">&lt;artifactId&gt;</span>jbi-maven-plugin<span
class="code-tag">&lt;/artifactId&gt;</span>
      <span class="code-tag">&lt;configuration&gt;</span>
        <span class="code-tag">&lt;classLoaderDelegation&gt;</span>self-first<span
class="code-tag">&lt;/classLoaderDelegation&gt;</span>
      <span class="code-tag">&lt;/configuration&gt;</span>
    <span class="code-tag">&lt;/plugin&gt;</span>
  <span class="code-tag">&lt;/plugins&gt;</span>
<span class="code-tag">&lt;/build&gt;</span>
</pre>
</div></div>

<h3><a name="Classloaders-SUClassloaders"></a>SU Classloaders</h3>

<p>XBean based service units can define their own classloader.  This can be done by
adding the following tag in the main XBean configuration file for the SU (<tt>xbean.xml</tt>
or <tt>servicemix.xml</tt> for the <tt>servicemix-lwcontainer</tt>
component):</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;classpath [inverse=<span
class="code-quote">"true"</span>]&gt;</span>
  [<span class="code-tag">&lt;nonOverridable&gt;</span>xxx<span class="code-tag">&lt;/nonOverridable&gt;</span>]*
  [<span class="code-tag">&lt;hidden&gt;</span>xxx<span class="code-tag">&lt;/hidden&gt;</span>]*
  [<span class="code-tag">&lt;location&gt;</span>xxx<span class="code-tag">&lt;/location&gt;</span>]*
<span class="code-tag">&lt;/classpath&gt;</span>
</pre>
</div></div>
<p>The <tt>inverse</tt> attribute can be set to <tt>true</tt>
to use self-first delegation mode.</p>

<p>The <tt>nonOverridable</tt> tag can be used to specify that classes with
a name starting with the specified string can not be overridden by this classloader (the classloader
will always use the parent classes).</p>

<p>The <tt>hidden</tt> tag is used to hide specific classes (whose name
start with the specified string) defined by the parent classloader.</p>

<p>The <tt>location</tt> tag can be used to add jars or directories relative
to the SU root to the classloader.</p>

<h3><a name="Classloaders-NewinServiceMix3.1"></a>New in ServiceMix 3.1
</h3>

<p>Starting with ServiceMix 3.1, if no classpath is defined, or if no locations are
specified, the default ones will be automatically added in the SU classloader:</p>
<ul>
	<li><tt>/</tt></li>
	<li><tt>lib/*.jar</tt></li>
	<li><tt>lib/*.zip</tt></li>
</ul>


<p><em><b>For ServiceMix 3.0.x versions, you need to define the classpath
manually, as the default does not include any locations.</b></em></p>

<h3><a name="Classloaders-NewinServiceMix3.2"></a>New in ServiceMix 3.2</h3>

<p>You can now reference Shared Libraries and Components classloaders using the following
syntax:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;classpath [inverse=<span
class="code-quote">"true"</span>]&gt;</span>
  [<span class="code-tag">&lt;nonOverridable&gt;</span>xxx<span class="code-tag">&lt;/nonOverridable&gt;</span>]*
  [<span class="code-tag">&lt;hidden&gt;</span>xxx<span class="code-tag">&lt;/hidden&gt;</span>]*
  [<span class="code-tag">&lt;location&gt;</span>xxx<span class="code-tag">&lt;/location&gt;</span>]*
  [<span class="code-tag">&lt;library&gt;</span>xxx<span class="code-tag">&lt;/library&gt;</span>]*
  [<span class="code-tag">&lt;component&gt;</span>xxx<span class="code-tag">&lt;/component&gt;</span>]*
<span class="code-tag">&lt;/classpath&gt;</span>
</pre>
</div></div>

<p>For example to reference the "cxf" shared library and the "servicemix-wsn2005" component
in addition to the jars inside the SU, you could write:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;classpath&gt;</span>
  <span class="code-tag">&lt;library&gt;</span>cxf<span class="code-tag">&lt;/library&gt;</span>
  <span class="code-tag">&lt;component&gt;</span>servicemix-wsn2005<span
class="code-tag">&lt;/component&gt;</span>
<span class="code-tag">&lt;/classpath&gt;</span>
</pre>
</div></div>

<h3><a name="Classloaders-NewInServiceMix3.3.1"></a>New In ServiceMix 3.3.1</h3>

<p>You can now reference external archives using system properties. For example:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;classpath&gt;</span>
  <span class="code-tag">&lt;location&gt;</span>${servicemix.home}/lib/ext/my.jar<span
class="code-tag">&lt;/location&gt;</span>
<span class="code-tag">&lt;/classpath&gt;</span>
</pre>
</div></div>

<p>You can explore and use regex inside externale archives (zip, jar, war, ear). For
example:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;classpath&gt;</span>
  <span class="code-tag">&lt;location&gt;</span>jar:file:/path/to/my.ear!/my.jar<span
class="code-tag">&lt;/location&gt;</span>
  <span class="code-tag">&lt;location&gt;</span>jar:file:/path/to/my.ear!/other*.jar<span
class="code-tag">&lt;/location&gt;</span>
<span class="code-tag">&lt;/classpath&gt;</span>
</pre>
</div></div>

<h3><a name="Classloaders-NewinServiceMix4.2.0"></a>New in ServiceMix 4.2.0</h3>

<p>To allow you to access classes from other OSGi bundles in your SU code as well, the
<b><tt>library</tt></b> element has now been enhanced to allow referencing
bundles when running in ServiceMix 4.2.0 or above.  The URI syntax is <b><tt>osgi:&lt;bundle
symbolic name&gt;</tt></b> or <b><tt>osgi:&lt;bundle symbolic
name&gt;/&lt;version&gt;</tt></b> to reference a specific bundle version.</p>

<p>Example: the following snippet explicitly adds the <tt>org.springframework.bean</tt>
bundle to the SU classloader.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;classpath&gt;</span>
  <span class="code-tag">&lt;library&gt;</span>osgi:org.springframework.bean<span
class="code-tag">&lt;/library&gt;</span>
<span class="code-tag">&lt;/classpath&gt;</span>
</pre>
</div></div>

<h3><a name="Classloaders-NewinServiceMix4.2.0"></a>New in ServiceMix 4.2.0</h3>

<p>To allow you to access classes from other OSGi bundles in your SU code as well, the
<b><tt>library</tt></b> element has now been enhanced to allow referencing
bundles when running in ServiceMix 4.2.0 or above.  The URI syntax is <b><tt>osgi:&lt;bundle
symbolic name&gt;</tt></b> or <b><tt>osgi:&lt;bundle symbolic
name&gt;/&lt;version&gt;</tt></b> to reference a specific bundle version.</p>

<p>Example: the following snippet explicitly adds the <tt>org.springframework.bean</tt>
bundle to the SU classloader.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;classpath&gt;</span>
  <span class="code-tag">&lt;library&gt;</span>osgi:org.springframework.bean<span
class="code-tag">&lt;/library&gt;</span>
<span class="code-tag">&lt;/classpath&gt;</span>
</pre>
</div></div>
     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/SM/Classloaders">View Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=39140&revisedVersion=14&originalVersion=13">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/SM/Classloaders?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message