myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gvanma...@comcast.net (Gary VanMatre)
Subject Re: [Trnidad] Is there a way to use Trinidad API to load Resources for our Custom Components?
Date Wed, 23 Jan 2008 17:28:22 GMT
>From: Samba <saasira@gmail.com> 
>Hi all,
>  I'm building a couple of custom components based on trinidad core, more like adding
some 
>enhancements...
>
> I would like to know if Trinidad expose any API  to be used by Coustom components to
load our 
>own JavaScript, CSS , and Images. 
>

You can plug in a custom script loaders into a web application that already uses trinidad.
 Trinidad uses a resource servlet.  At startup, the resource servlet scanns the class path
for META-INF/services/resources/{url-pattern}.resources.  These files define resource loaders.

Steps:
1) Create a trinidad resource loader by extending RegexResourceLoader.

<code>
/**
 * <p>This is a composite resource loader that hooks into the 
 * <code>ResourceServlet</code>.  
 * <pre>
 *   &lt;servlet&gt;
 *   &lt;servlet-name&gt;resources&lt;/servlet-name&gt;
 *   &lt;servlet-class>org.apache.myfaces.trinidad.webapp.ResourceServlet
 *    &lt;/servlet-class&gt;
 * &lt;/servlet&gt;
 * </pre><br/><br/>
 *
 * It is registered by placing a text file under
 * the "/META-INF/servlets/resources" folder.  The name of the file must
 * begin with the url folder prefix registered in the web deployment
 * descriptor (web.xml) and the suffix should be ".resources".  
 *
 * <pre>
 *<servlet-mapping>
 *  <servlet-name>resources</servlet-name>
 *  <url-pattern>/acme/*</url-pattern>
 *</servlet-mapping>
 * </pre><br/><br/>
 *
 * The folder prefix needs to correspond to the formulation of various 
 * web resources.
 * 
 *<pre> 
 *  #{facesContext.externalContext.requestContextPath}/acme/images/folder.gif}
 *</pre> 
 * 
 * 
 * 
 */
public class AcmeResourceLoader
  extends RegexResourceLoader
{
  public AcmeResourceLoader()
  {
    // any resource in "/acme/" with the following suffixes will be
    // loaded from the base folder of "META-INF".
    // The servlet pattern match "/acme/*" should exist under "META-INF".
    // For example URL : context-root/acme/images/type1.gif
    //           map to: META-INF/acme/images/type1.gif
    register("(/.*\\.(jpg|gif|png|jpeg))", 
             new ClassLoaderResourceLoader("META-INF"));
    // associate pattern match on the "/acme/all-acme.js" to deliver
    // all JS files bundled by the AcmeScriptsResourceLoader
    register("(/.*all-acme.js)", 
             new AcmeScriptsResourceLoader("/acme/all-acme.js"));
  }

  /**
   * <p>Aggregates all JavaScript files into a single stream identified
   * by a single URI.  All JavaScripts for the acme custom component library
   * will be delivered using a single script include.
   * 
   * <pre>
   *  &lt;script type="text/javascript" 
   *   scr="#{facesContext.externalContext.requestContextPath}/acme/all-acme.js"
   *   /&gt;
   * </pre>
   * <br/>
   *  -- or --
   *  <pre>
   *   &lt;trh:script source="/acme/all-acme.js"/&gt;
   *  </pre>
   * </p>
   */
  public static class AcmeScriptsResourceLoader
    extends AggregatingResourceLoader
  {
    /**
     * <p>The URI used to identify all of the JavaScripts that will be 
     * included using a single URI.</p>
     * 
     * @param scriptsURI the URI relative to "/acme/*"
     */
    public AcmeScriptsResourceLoader(String scriptsURI)
    {
      // pass the base folder and list of script files.  
      // the script files have to be within the classpath
      super(scriptsURI, _LIBRARIES, new ClassLoaderResourceLoader());
      this.setSeparator(AcmeScriptsResourceLoader._NEWLINE_SEPARATOR);
    }
    /**
     * <p>List of all JavaScript files to include.  The Peer JS
     * file should always be registered last for a component
     * grouping if the peeer is registered with the component
     * in the peer JS file.</p>
     */
    static private final String[] _LIBRARIES =
    { "oracle/adfdemo/acme/js/component/AcmeTagPane.js", 
      "oracle/adfdemo/acme/js/event/AcmeTagSelectEvent.js", 
      "oracle/adfdemo/acme/js/component/AcmeTagPanePeer.js" };
    /**
     * <p>The separator to use in between streams.</p>
     */
    static private final String _NEWLINE_SEPARATOR = "\n";
  }

}  
</code>

2)  Create a simple registration file (META-INF/services/resources/acme.resources) in the
jar or web project containing the fully qualifed path to the resource loader.

<code>
oracle.adfdemo.acme.faces.resource.AcmeResourceLoader
</code>

3) Add the resource servlet mappings to your web deployment descriptor (web.xml).

<code>
<servlet-mapping>
  <servlet-name>resources</servlet-name>
   <url-pattern>/acme/*</url-pattern>
</servlet-mapping>
</code>

4) Include the script in the jsp page.

<code>
     <trh:head>
         <trh:script source="/acme/all-acme.js"/>
      </trh:head>
</code>


>Any help in this regard is highly appreciated,
>Thanking you all,
>Samba

Gary
Mime
View raw message