Configuring static resources for use within the application in JBoss AS 7




In previous versions of JBoss, you could link an external directory into the classpath and load static resources like images/CSS/JS into the application using the WEB-INF/context.xml

<Context allowLinking="true" cookies="true" crossContext="true" override="true"> 
<Resources allowLinking="true" homeDir="/mydomain/staticfiles" /> 
</Context>

In Weblogic servers, there is the virtual-directory-mapping to achieve this.

In Jboss 7 this no longer works, and you need to bundle the static files within the EAR or War. Note: If you need to serve these over the browser like a public web server as static files then you need a servlet method like this http://balusc.blogspot.com/2007/04/imageservlet.html

But if you want to load a static property file or other content into your webapp, the solution provided by Jboss is what this post is about. You can define a “module” within JBOSS_HOME and store the files there or extend that to any location on the filesystem.



The base reference for this solution is on the Jboss forums How to put an external file in the classpath




Please read that for the theory behind this. Actual steps to be used to store and load any static files or properties or config etc:



1. Create a new module for your configuration or static files - at this location



jboss-eap/modules/com/mycompany/main/module.xml


<?xml version="1.0" encoding="UTF-8"?> 
<module xmlns="urn:jboss:module:1.1" name="com.mycompany"> <resources> 
<resource-root path="."/> 
</resources> 
</module> 

2. Add your properties files or images to the module at this location



jboss-eap/

  modules/

    com/

      mycompany/

        main/

          module.xml

          settings.properties

          logo.jpg





3. Add your module to your application classpath in a jboss-deployment-structure.xml file

<?xml version="1.0" encoding="UTF-8"?> 
<jboss-deployment-structure> <deployment> <dependencies> 
<module name="com.mycompany" />
</dependencies> 
</deployment> 
</jboss-deployment-structure>

This file must be placed either in the META-INF directory of your EAR file or the WEB-INF directory of your WAR file. See Class Loading in AS7 for more information.

OR add your module to your application classpath using a MANIFEST.MF entry in the META-INF of the EAR



Manifest-Version: 1.0

Dependencies: com.mycompany





5. Load a properties file from the classloader in your application



URL imgUrl = this.getClass().getClassLoader().getResource("logo.jpg");





As you’ve noticed this sits within the JBOSS_HOME/modules directory. You can further change the location of this by editing the standalone.bat or standalone.sh files as follows



:RESTART

"%JAVA%" %JAVA_OPTS% ^

"-Dorg.jboss.boot.log.file=%JBOSS_LOG_DIR%\boot.log" ^

"-Dlogging.configuration=file:%JBOSS_CONFIG_DIR%/logging.properties" ^

-jar "%JBOSS_HOME%\jboss-modules.jar" ^

-mp "%JBOSS_MODULEPATH%;d:\mycustommodules" ^

-jaxpmodule "javax.xml.jaxp-provider" ^

org.jboss.as.standalone ^

-Djboss.home.dir="%JBOSS_HOME%" ^

%*

And the corresponding structure on Windows platform looks like this

mycustommodules/

  com/

    mycompany/

      main/

        module.xml

        settings.properties

        logo.jpg



In this way your static directory does not need to be within the JBOSS folder at all.

Comments

  1. This is great, just a question:

    How do I get the image from a XHTML JSF page?

    Regards
    Diego

    ReplyDelete

Post a Comment

Popular Posts