3 Replies Latest reply on Jan 31, 2008 7:51 AM by lundegaard

    Facelet Templates from a Database

    tom_goring

      Hi,

      Has anyone an idea of how to load facelets from the database rather than WAR?

      I'd like to use templates in the database for rendering seam email's.

      The problem I seem to have is that if I install my own "org.jboss.seam.core.resourceLoader"
      Email uses public URL getResource(String resource) rather than
      public InputStream getResourceAsStream(String resource)

      I can't return a URL for a database document.

      Any ideas?

      Thanks

      Tom

        • 1. Re: Facelet Templates from a Database
          tom_goring

          Hi,

          Any idea on this as I'm a bit stuck.

          My requirement is that users can define the dynamic content of an email and I'd like to use the JSF tags for handling tables etc. So I want to store the facelet in the database.

          Any pointers appreciated.

          Thanks

          tom


          • 2. Re: Facelet Templates from a Database
            nickarls

            From the department of long-shorts:

            have the function return a resource-URL like "http://context/resource?id=" + resource

            and that servlet would fetch the resource from DB and spit it out.

            As I said, there are probably much more elegant ways of doing this...

            • 3. Re: Facelet Templates from a Database
              lundegaard

              I am using a similar code to access templates from another webapp, but the sample DatabaseResourceResolver shows you the basis:

              import java.io.ByteArrayInputStream;
              import java.io.IOException;
              import java.io.InputStream;
              import java.net.MalformedURLException;
              import java.net.URL;
              import java.net.URLConnection;
              import java.net.URLStreamHandler;
              
              import com.sun.facelets.impl.DefaultResourceResolver;
              
              /**
               * @author Thomas Maerz (thomas.maerz@primedo.com)
               */
              public class DatabaseResourceResolver extends DefaultResourceResolver {
              
               @Override
               public URL resolveUrl(String path) {
               URL url = super.resolveUrl(path);
               if (url != null) {
               return url;
               }
              
               try {
               return new URL("internal", null, 0, path, getHandler(path));
               } catch (MalformedURLException e) {
               }
               return null;
               }
              
               private static URLStreamHandler getHandler(final String path) {
              
               return new URLStreamHandler() {
              
               @Override
               protected URLConnection openConnection(URL url) throws IOException {
               return new URLConnection(url) {
              
               @Override
               public void connect() throws IOException {
               }
              
               @Override
               public InputStream getInputStream() throws IOException {
               // Connect to Database and return your content
               return new ByteArrayInputStream(new byte[] {});
               }
              
               };
               }
              
               };
              
               }
              
              }
              


              web.xml
               <context-param>
               <param-name>facelets.RESOURCE_RESOLVER</param-name>
               <param-value>DatabaseResourceResolver</param-value>
               </context-param>
              


              Facelets file
              <ui:composition
               xmlns:ui="http://java.sun.com/jsf/facelets"
               template="/cms/some/path/index.html">
              




              But there are some pitfalls:

              - Facelets will cache the the returned URL with the path as key
              - You have to handle reloading yourself (Override getLastModified() in URLConnection)
              - return valid XML


              Hope that helps,
              Tom