You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ryan Wynn <bi...@gmail.com> on 2006/02/02 23:54:36 UTC

[shale] remoting - how do I use ClassResourceProcessor

I am beginning to try to understand shale remoting.  How do I
configure shale to serve a resource from a jar file using the
ClassResourceProcessor?

Thanks,
Ryan

Re: [shale] remoting - how do I use ClassResourceProcessor

Posted by Craig McClanahan <cr...@apache.org>.
On 2/2/06, Ryan Wynn <bi...@gmail.com> wrote:
>
> I am beginning to try to understand shale remoting.  How do I
> configure shale to serve a resource from a jar file using the
> ClassResourceProcessor?
>
> Thanks,
> Ryan
>

I will be posting for-real samples as soon as I can, but the basics go like
this.

First, assume you have a component that needs to download a script (but once
only) that is stored (within a JAR file) at "/foo/bar/script.js".  This is
known, in the terminology of Shale Remoting, as a resource identifier.

Now, in the renderer for your component, first define a helper bean that
will do the grunt work for you:

    XhtmlHelper helper = new XhtmlHelper();

Later on, in one of your encode methods, you would execute the following
logic to create a link to the script, something like this:


  public void encodeBegin(FacesContext context, UIComponent component)
throws IOException {
    ...
    ResponseWriter writer = context.getResponseWriter();
    helper.linkJavascript(context, component,
      writer, Mechanism.CLASS_RESOURCE, "/foo/bar/script.js");
    ...
  }

Shale Remoting will ensure that a <script> element is rendered for this
resource (only if it hasn't been requested already on this page), with the
actual URL being sensitive to the mappings you have established for both
FacesServlet and for class resources.

For non-component use, you just have to know what mapping approach you are
using for ClassResourceProcessor and FacesServlet, and generate an
appropriate URL.  For example, if you know that FacesServlet is mapped to
*.faces and ClassResourceProcessor is mapped to /static/* (the default),
then you would emit something like this:

   <script type="text/javascript"
url="/myapp/static/foo/bar/script.js.faces"/>

and the browser will automatically make the appropriate request for you
(where "/myapp" is the context path).  This URL first gets mapped to
FacesServlet (because of the ".faces" extension that gets stripped off),
then to the ClassResourceProcessor (because of the "/static" prefix that
gets stripped off), and then serves resource "/foo/bar/script.js" from the
classpath resources (i.e. from unpacked files in /WEB-INF/classes, or from
inside a jar file in /WEB-INF/lib).

Note that XhtmlHelper has a mapResourceId() method that can calculate the
correct URL for you, given a specified mechanism and resource id, that takes
into account all the mappings.  You'd call it something like this:

    String url = helper.mapResourceId(FacesContext.getCurrentInstance(),
      Mechanism.CLASS_RESOURCE, "/foo/bar/script.js";

As you can imagine from the fact that a FacesContext instance is required,
this *must* be done from within a Faces request -- but that will be normal
for a JSF-based application.

Craig