Spring SystemPropertyInitializingBean

When using POI in any of your projects, and the application you're building is a web application, you probably have it running on a Windows machine. If not, you know all about the struggle with the "headless mode" environment setting to tell the JVM how to handle graphics rendering.

I always like to keep my applications as clean as possible to the users. The system administrator is also a user of the software (during installation at least). So I wanted the application to set the environment properties itself, In this case, I built a nice little Spring bean to handle this. The solution is so simple, that it is almost a brilliant display of what Spring can solve for you. Suddenly, all these environment setting problems turned into a simple Spring configuration problem. Here's how:

In the Spring configuration, the whole "headless mode" problem got reduced to this bean definition:
<bean id="systemproperty_initializer" 
   class="com.rolfje.SystemPropertyInitiliazingBean">
   <property name="systemProperties">
      <map>
         <!-- Set headless mode to true, 
              for POI sheet.autoSizeColumn See 
<span>              </span><span>http://poi.apache.org/hssf/quick-guide.html</span><span> 
         -->
         <entry key="java.awt.headless" value="true"/>
     </map>
   </property>
</bean></span>

The code for the SystemPropertyInitilizingBean is really a simple list iterator which walks though the map and sets everything as a system property:

/**
 * Bean for automatically initializing System 
 * properties from within a Spring context. 
 */
public class SystemPropertyInitializingBean 
       implements InitializingBean {

        /** Properties to be set */
        private Map systemProperties;

        /** Sets the system properties */
        public void afterPropertiesSet() 
               throws Exception {
                if (systemProperties == null || 
                       systemProperties.isEmpty()) {
                        // No properties to initialize
                        return;
                }

                Iterator i = systemProperties.keySet().iterator();
                while (i.hasNext()) {
                        String key = (String) i.next();
                        String value = (String) systemProperties.get(key);

                        System.setProperty(key, value);
                }
        }

        public void setSystemProperties(Map systemProperties) {
                this.systemProperties = systemProperties;
        }
}

In my opinion this is a nice and clean solution, which does not impact any of the application code, hides the setting of obscure system properties from the administrator, is simple to the developer, gives room to add comments (see Spring xml above)and is reusable. All in a few lines of code. You can even easily have it parese settings from a config file, and put that into system environment variables without changing a line of code. All in Spring XML.

Sometimes it's the small things that make you feel nice :-)