<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
Wildcard support in Dynamic-imports
</h3>
<span style="margin-bottom: 10px;">
created by <a href="http://community.jboss.org/people/alesj">Ales Justin</a> in <i>JBoss Microcontainer Development</i> - <a href="http://community.jboss.org/message/543069#543069">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>I've implemented an initial version of wildcard support.</p><p>e.g. Dynamic-import: com.acme.foo.*</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>When creating lazy delegate in Module (which we do for all dynamic requirements),</p><p>we differentiate between wildcards and plain dynamic import.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">         <font color="navy"><b>if</b></font> (pr.isWildcard())
         <font color="navy">{</font>
            ClassLoaderPolicyFactory factory = <font color="navy"><b>new</b></font> WildcardClassLoaderPolicyFactory(domain, item);
            <font color="navy"><b>return</b></font> <font color="navy"><b>new</b></font> WildcardDelegateLoader(factory, filter);
         <font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Our wildcard policy tracks potential matching modules via ModuleRegistry notion from ClassLoading instance.</p><p>Hence wildcard factory registers the policy as a listener.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">   <font color="navy"><b>public</b></font> ClassLoaderPolicy createClassLoaderPolicy()
   <font color="navy">{</font>
      WildcardClassLoaderPolicy policy = <font color="navy"><b>new</b></font> WildcardClassLoaderPolicy(domain, requirement, module);
      ClassLoading classLoading = domain.getClassLoading();
      classLoading.addModuleRegistry(policy); <font color="darkgreen">// so we know when to reset on module change</font>
      <font color="navy"><b>return</b></font> policy;
   <font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>What our wildcard policy does is actually delegation to other existing matching modules.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">   <font color="navy"><b>public</b></font> URL getResource(String path)
   <font color="navy">{</font>
      Module cached = resourceCache.get(path);
      <font color="navy"><b>if</b></font> (cached != <font color="navy"><b>null</b></font>)
         <font color="navy"><b>return</b></font> cached.getResource(path);
 
      ClassFilter filter = requirement.toClassFilter();
      <font color="navy"><b>if</b></font> (filter.matchesResourcePath(path))
      <font color="navy">{</font>
         <font color="navy"><b>for</b></font> (Module m : modules)
         <font color="navy">{</font>
            URL url = m.getResource(path);
            <font color="navy"><b>if</b></font> (url != <font color="navy"><b>null</b></font>)
            <font color="navy">{</font>
               resourceCache.put(path, m);
               used.add(m);
               <font color="navy"><b>return</b></font> url;
            <font color="navy">}</font>
         <font color="navy">}</font>
      <font color="navy">}</font>
      <font color="navy"><b>return</b></font> <font color="navy"><b>null</b></font>;
   <font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>"modules" list gets populated via tracking, where we only include potential matching modules.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">   <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> addModule(Module module)
   <font color="navy">{</font>
      Domain md = getDomain(module);
      <font color="navy"><b>if</b></font> (md != <font color="navy"><b>null</b></font> && module.canResolve(requirement))
      <font color="navy">{</font>
         <font color="navy"><b>boolean</b></font> isAncestor = (domain != md); <font color="darkgreen">// not the same domain, so it must be ancestor</font>
         <font color="navy"><b>synchronized</b></font> (<font color="navy"><b>this</b></font>)
         <font color="navy">{</font>
            <font color="navy"><b>if</b></font> (isAncestor)
            <font color="navy">{</font>
               <font color="navy"><b>if</b></font> (domain.isParentFirst())
               <font color="navy">{</font>
                  modules.add(0, module);
                  parentsBefore++;
               <font color="navy">}</font>
               <font color="navy"><b>else</b></font>
                  modules.add(module);
            <font color="navy">}</font>
            <font color="navy"><b>else</b></font>
               modules.add(parentsBefore, module);
         <font color="navy">}</font>
 
         reset();
      <font color="navy">}</font>
   <font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Same on removal, where we also try to bounce our module if we see that some module we used went away.</p><p>We also remove ourselves from module listening when undeploying.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">   <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> removeModule(Module module)
   <font color="navy">{</font>
      <font color="navy"><b>synchronized</b></font> (<font color="navy"><b>this</b></font>)
      <font color="navy">{</font>
         <font color="navy"><b>if</b></font> (modules.remove(module))
         <font color="navy">{</font>
            Domain md = getDomain(module);
            <font color="navy"><b>boolean</b></font> isAncestor = (domain != md);
            <font color="navy"><b>if</b></font> (isAncestor && domain.isParentFirst())
               parentsBefore--;
 
            reset();
         <font color="navy">}</font>
      <font color="navy">}</font>
 
      <font color="navy"><b>boolean</b></font> sameModule = this.module == module;
 
      <font color="darkgreen">// Unregister this policy as module listener</font>
      <font color="navy"><b>if</b></font> (sameModule)
      <font color="navy">{</font>
         ClassLoading classLoading = domain.getClassLoading();
         classLoading.removeModuleRegistry(<font color="navy"><b>this</b></font>);
         this.module = <font color="navy"><b>null</b></font>;
      <font color="navy">}</font>
 
      <font color="darkgreen">// It's not us (we're already uninstalling) and we used this, let's bounce.</font>
      <font color="navy"><b>if</b></font> (used.remove(module) && sameModule == <font color="navy"><b>false</b></font>)
      <font color="navy">{</font>
         LifeCycle lifeCycle = this.module.getLifeCycle();
         <font color="navy"><b>if</b></font> (lifeCycle != <font color="navy"><b>null</b></font> && module.isCascadeShutdown() == <font color="navy"><b>false</b></font>)
         <font color="navy">{</font>
            <font color="navy"><b>try</b></font>
            <font color="navy">{</font>
               lifeCycle.bounce();
            <font color="navy">}</font>
            <font color="navy"><b>catch</b></font> (Exception e)
            <font color="navy">{</font>
               <font color="navy"><b>throw</b></font> <font color="navy"><b>new</b></font> IllegalArgumentException(<font color="red">"Error bouncing module: "</font> + this.module);
            <font color="navy">}</font>
         <font color="navy">}</font>
      <font color="navy">}</font>
   <font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Then comes the tricky part. :-)</p><p>Since the policy is mostly just used to find the matching resource, where its underlying classloader should do the real loading.</p><p>But in our case where we delegate all of the things, this needed some hacking into existing code.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>To find the right ClassLoader I hacked BaseDelegateLoader's getBaseClassLoader method to protected (don't see why it should really be pckg protected), and then overriden it in WildcardDelegateLoader.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">   <font color="navy"><b>protected</b></font> BaseClassLoader getBaseClassLoader(String message, String context)
   <font color="navy">{</font>
      ClassLoaderPolicy policy = getPolicy();
      <font color="navy"><b>if</b></font> (policy <font color="navy"><b>instanceof</b></font> WildcardClassLoaderPolicy == <font color="navy"><b>false</b></font>)
         <font color="navy"><b>throw</b></font> <font color="navy"><b>new</b></font> IllegalArgumentException(<font color="red">"Can only handle wildcard policy: "</font> + policy);
 
      WildcardClassLoaderPolicy wclp = (WildcardClassLoaderPolicy) policy;
      <font color="navy"><b>return</b></font> wclp.getBaseClassLoader(context);
   <font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Which then delegates to the policy with additional context parameter -- which is really the path of the requested resource.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">   BaseClassLoader getBaseClassLoader(String context)
   <font color="navy">{</font>
      Module m = findModule(context);
      <font color="navy"><b>if</b></font> (m != <font color="navy"><b>null</b></font>)
      <font color="navy">{</font>
         ClassLoader cl = ClassLoading.getClassLoaderForModule(m);
         <font color="navy"><b>if</b></font> (cl <font color="navy"><b>instanceof</b></font> BaseClassLoader)
            <font color="navy"><b>return</b></font> BaseClassLoader.class.cast(cl);
      <font color="navy">}</font>
      <font color="navy"><b>return</b></font> <font color="navy"><b>null</b></font>;
   <font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>But the changes don't stop there, as the ClassLoadingManager/Task also asume there is a single matching classloader.</p><p>So, I introduced a new protected method on BaseClassLoaderPolicy.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">   <font color="darkgreen">/**
    * Get the classloader based on classloading task.
    *
    * Since ClassLoadingTask ctor is package protected
    * this method cannot be easily abused, since the only
    * code that can instantiate ClassLoadingTask is our ClassLoaderManager.
    *
    * @param task the classloading task info
    * @return the classloader
    */</font>
   <font color="navy"><b>protected</b></font> <font color="navy"><b>synchronized</b></font> BaseClassLoader getClassLoader(ClassLoadingTask task)
   <font color="navy">{</font>
      <font color="navy"><b>return</b></font> getClassLoader();
   <font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Which is then overriden in wildcard policy.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">   <font color="navy"><b>protected</b></font> BaseClassLoader getClassLoader(ClassLoadingTask task)
   <font color="navy">{</font>
      <font color="navy"><b>if</b></font> (task == <font color="navy"><b>null</b></font>)
         <font color="navy"><b>throw</b></font> <font color="navy"><b>new</b></font> IllegalArgumentException(<font color="red">"Null task"</font>);
 
      String path = ClassLoaderUtils.classNameToPath(task.getClassName());
      <font color="navy"><b>return</b></font> getBaseClassLoader(path);
   <font color="navy">}</font>
</code></pre></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/543069#543069">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in JBoss Microcontainer Development at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2115">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>