Chapter 36. Portlet Dynamic Module

As explained previously, portlet dependencies (including JavaScript files) can be declared in the gatein-resources.xml file, which forces declaration of portlet dependencies at packaging time when the corresponding war file is created.
The JSR286 specification provides a mechanism for modifying portal headers, which can also be used to load dependency JavaScript files. Although this mechanism is portable, it has the following drawbacks:
  • A script can be loaded multiple times, specially if two portlets in two different war files load the same scripts since the only way to identify a script is by its URL.
  • Scripts have to be loaded by the head section of the portal, impacting front-end performance.
The portal combines both approaches by allowing to create dynamic dependencies of a portlet at runtime, during the render phase of the portlet:

public void render(RenderRequest req, RenderResponse resp) throws PortletException, IOException {
  resp.setProperty("org.gatein.javascript.dependency", "base");
  resp.addProperty("org.gatein.javascript.dependency", "common");
}
The code above is equivalent to the following declaration in the gatein-resources.xml file:

<portlet>
  <name>MyPortlet</name>
  <module>
  <depends>
    <module>base</module>
  </depends>
  <depends>
    <module>common</module>
  </depends>
  </module>
</portlet>