<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#ffffff" text="#000000">
    Esteban,<br>
    You can assume that a resource that was obtained from the classpath
    exists in your filesystem, for instance it can be a file inside a
    jar or war that are not exploded. In other words you can't always
    convert an URL to <a class="moz-txt-link-rfc2396E" href="file://">"file://"</a>.<br>
    <br>
    -- <br>
    Bauna<br>
    <br>
    On 04/15/2011 08:52 AM, Esteban Aliverti wrote:
    <blockquote
      cite="mid:BANLkTi=v=M4NX3yh8K6TTJ27SqEKgzOH0g@mail.gmail.com"
      type="cite">Hi Guys,
      <div><br>
      </div>
      <div>I want to discuss a problem I have found when using the
        combination of knowledge agent + classpathResources.</div>
      <div>I will try to describe what am I doing first to give you some
        context.&nbsp;</div>
      <div>I'm deploying drools-camel-server in a Tomcat 7 container.
        Inside the WEB-INF/classes directory I have some DRL files that
        I want to use.</div>
      <div>My knowledge-services.xml file declares the following kagent:</div>
      <div><br>
      </div>
      <div>&lt;drools:kagent id="kagent1" kbase="kbase1"
        new-instance="false"&gt;</div>
      <div><span class="Apple-tab-span" style="white-space: pre;"> </span>&lt;drools:resources&gt;&nbsp;<br
          clear="all">
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;drools:resource type="DRL" source="<b>classpath</b>:simple.drl"/&gt;</div>
      <meta http-equiv="content-type" content="text/html;
        charset=ISO-8859-1">
      <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;...&nbsp;</div>
      <div>&nbsp; &nbsp; &lt;/drools:resources&gt;</div>
      <div>&lt;/drools:kagent&gt;</div>
      <div><br>
      </div>
      <div>When spring parses this configuration file it creates a
        KnowledgeAgent instance with a ChangeSet containing all the
        listed resources.</div>
      <div>The next step is to start ResourceChangeNotifierService
        and&nbsp;ResourceChangeScannerService.&nbsp;</div>
      <div>So far so good.</div>
      <div><br>
      </div>
      <div>The problem:</div>
      <div>The problem I'm having is not directly related to drools, but
        I think it is quite easy to provide a solution for the people
        that is in my same situation.</div>
      <div><br>
      </div>
      <div>ClassPathResource is the class that represents a resource
        defined as "<b>classpath:"</b></div>
      <div><br>
      </div>
      <div>This class has 2 important methods:</div>
      <div><br>
      </div>
      <div>
        <meta http-equiv="content-type" content="text/html;
          charset=ISO-8859-1">
        <div>
          public long getLastModified(){</div>
        <div>&nbsp; return this.classLoader.getResource( this.path
          ).openConnection().getLastModified();</div>
        <div>}</div>
      </div>
      <div><br>
      </div>
      <div>public InputStream getInputStream(){</div>
      <div>&nbsp; return&nbsp;this.classLoader.getResourceAsStream( this.path );</div>
      <div>}</div>
      <div><br>
      </div>
      <div><br>
      </div>
      <div>The first method is used by&nbsp;ResourceChangeScannerService to
        check whether the resource has changed or not. It works fine.
        When the resource in the filesystem changes, the scanner detects
        the change without any problem.</div>
      <div>The scanner ends up notifying the kagent about the change,
        and the kagent passes the Resource to an instance of
        KnowledgeBuilder.&nbsp;</div>
      <div>An here is when things fail.</div>
      <div>The kbuilder uses the second method of&nbsp;ClassPathResource
        (getInputStream()) to get the content of the resource. In the
        case of Tomcat (and probably some other environments), it seems
        that the classloader (Tomcat's classloader) is using a cache. So
        the InputStream returned doesn't reflect the current state of
        the resource.</div>
      <div>Long story short: the agent is notified about a change in the
        resource, but the change is never applied to the kbase because
        the kbuilder is unable to get it :P</div>
      <div><br>
      </div>
      <div>Solutions:</div>
      <div>The first solution is not to use classpath resources :). You
        can use just url resources like <a class="moz-txt-link-freetext" href="http://">http://</a> or <a class="moz-txt-link-freetext" href="file:/">file:/</a>. But honestly,
        when you have your rules inside your webapp, it is much
        more&nbsp;comfortable&nbsp;and even&nbsp;manageable&nbsp;to avoid the use of real
        paths.</div>
      <div><br>
      </div>
      <div>What I was thinking about (I already have a working
        prototype) is to create a new Resource type for these cases.
        This resource type will let you define your resources present in
        your classpath as usually but it will translate them to URL
        Resource internally.</div>
      <div>So, in the example above:&nbsp;</div>
      <meta http-equiv="content-type" content="text/html;
        charset=ISO-8859-1">
      <meta http-equiv="content-type" content="text/html;
        charset=ISO-8859-1">
      <meta http-equiv="content-type" content="text/html;
        charset=ISO-8859-1">
      <div>
        <br>
      </div>
      <div>
        <meta http-equiv="content-type" content="text/html;
          charset=ISO-8859-1">
        &lt;drools:resource type="DRL" source="<b>URLClasspath</b>:simple.drl"/&gt;</div>
      <div><br>
      </div>
      <div>is going to be translated (internally and in a transparent
        way) to something like:
        <a class="moz-txt-link-freetext" href="file:/usr/local/apache-tomcat-7/webapps/MyWebapp/WEB-INF/simple.drl">file:/usr/local/apache-tomcat-7/webapps/MyWebapp/WEB-INF/simple.drl</a>.</div>
      <div><br>
      </div>
      <div>Opinions?&nbsp;</div>
      <div><br>
        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br>
        <br>
        Esteban Aliverti<br>
        - Developer @ <a moz-do-not-send="true"
          href="http://www.plugtree.com" target="_blank">http://www.plugtree.com
        </a><br>
        - Blog @ <a moz-do-not-send="true"
          href="http://ilesteban.wordpress.com" target="_blank">http://ilesteban.wordpress.com</a><br>
      </div>
      <pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
rules-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:rules-dev@lists.jboss.org">rules-dev@lists.jboss.org</a>
<a class="moz-txt-link-freetext" href="https://lists.jboss.org/mailman/listinfo/rules-dev">https://lists.jboss.org/mailman/listinfo/rules-dev</a>
</pre>
    </blockquote>
  </body>
</html>