Accessing and using .jsf files from the database

What is the best way to enable my webapplication to use JSF files stored in the database? I’d like to be able to dynamically (during runtime) create new JSF pages which will be made available without having to redeploy the application.

So in other words: I would like to store the bigger part of my JSF pages in the database and would like JSF to use the database as a datasource for getting JSF files.

I’ve thought long about a solution and found some possible ways. However, I haven’t been able to implement either of them.

  1. Whenever a new page has to be added/removed: manipulate the files in the classpath (e.g. remove or add a file to the .war file)
  2. Extending the classpath of the webapplication so it will be able to get files from an at runtime defined location (i.e. /tmp or directly using a database connection)
  3. Provide JSF with a way to find resources another way ( this doesn’t seem possible? )

My environment:

  • Java SE 6
  • Jetty as servlet container
  • Mojarra as jsf implementation

Now, my question:

Is it possible for someone to let JSF find pages at a location other than the default classpath, preferably the database?

Any response is greatly appreciated!

Answer

1: Whenever a new page has to be added/removed: manipulate the files in the classpath (e.g. remove or add a file to the .war file)

This is definitely possible if the WAR is expanded. I am not sure about Jetty, but it works for me with Mojarra 2.x on Tomcat 7 and Glassfish 3. Just writing the file to the expanded WAR folder the usual Java IO way suffices.

File file = new File(servletContext.getRealPath("/foo.xhtml"));

if (!file.exists()) {
    OutputStream output = new FileOutputStream(file);

    try {
        output.write(bytes); // Can be bytes from DB.
    } finally {
        output.close();
    }
}

This needs to be executed before the FacesServlet kicks in. A Filter is a perfect place. See also this related answer:


2: Extending the classpath of the webapplication so it will be able to get files from an at runtime defined location (i.e. /tmp or directly using a database connection)

You can package Facelets files in a JAR file and put it in the classpath and provide a Facelets ResourceResolver which serves the files from the JAR on when no match is found in WAR. You can find complete code examples in the following answers:


3: Provide JSF with a way to find resources another way ( this doesn’t seem possible? )

You’ve plenty of play room in the custom ResourceResolver.

Leave a Reply

Your email address will not be published. Required fields are marked *