Announcement Announcement Module
Collapse
No announcement yet.
properties file writing and loading precedences issue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • properties file writing and loading precedences issue

    hi all
    I'm writing here because I can't find anything helping me with my code troubles.

    I want to implement something very similar to an installation process but in a web application.

    I have a spring web application which is using some configuration parameters.
    I want my web application to:
    • have a default start-up settings
    • show a web form to edit these settings (e.g. by an administrator)
    • the previous changes must be persisted (i.e. the next time the web-application is started it must load the custom settings)
    • possibly I don't want to use a database

    for now I am addressing the problem of creating a properties file at the startup (if it doesn't exist yet) and make it use by my web application.

    I thought to use a properties file in the following way:

    A bean called SettingsConfigurer is responsible for creating the properties file in the classpath, while spring PropertyPlaceholderConfigurer bean will use it to configure the other application beans.

    Since PropertyPlaceholderConfigurer implements BeanFactoryPostProcessor I need my class SettingsConfigurer to implement it too to execute before the PropertyPlaceholderConfigurer. To get my job done I set SettingsConfigurer order attribute to 0 and PropertyPlaceholderConfigurer to 1.

    But the things aren't working the way I expect since the PropertyPlaceholderConfigurer executes always before my SettingsConfigurer bean. :-(

    here's the code:

    SettingsConfigurer
    Code:
    public class SettingsConfigurator implements BeanFactoryPostProcessor, Ordered{
    
    	private ResourceLoader resourceLoader;
    	private String propertiesPath;
    	private Properties settings;
    
    [...getters and setters here]
    
    	@Override
    	public int getOrder() {
    		// TODO Auto-generated method stub
    		return 0;
    	}
    
    @Override
    	public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
    			throws BeansException {
    		// TODO Auto-generated method stub
    		if(resourceLoader == null)
    			resourceLoader = new DefaultResourceLoader();
    		if(settings == null)
    			settings = new Properties();
    		
    		Resource file = resourceLoader.getResource("file:" + this.getPropertiesPath());
    		if (!file.exists())
    		{
    			try
    			{
    			FileWriter fstream = new FileWriter(this.getPropertiesPath(), false);
    			String comments = "Default configuration";
    			
    			this.getSettings().setProperty("nodeRole", "Slave");
    			this.getSettings().setProperty("masterLocation", "http://localhost:8080/GRSSynchronizer");
    			this.getSettings().setProperty("infoTab", "false");
    			this.getSettings().setProperty("partnerTab", "false");
    			this.getSettings().setProperty("resolverTab", "false");
    			this.getSettings().setProperty("resolverXMLresponseTab", "false");
    			this.getSettings().setProperty("cronExpression", "0/10 * * * * ?");
    			this.getSettings().store(fstream, comments);
    			fstream.close();
    			}
    			catch (Exception e)
    			{
    				e.printStackTrace();
    			}
    		}
    	}
    }

    myPropertyPlaceholderConfigurer

    Code:
    public class myPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer implements Ordered{
    	
    	@Override
    	public int getOrder()
    	{
    		return 10;
    	}
    }

    dispatcher-servlet.xml

    Code:
    	<bean class="com.jglore.utils.SettingsConfigurator">
    		<constructor-arg name="propertiesPath" value="${myWebApp.root}WEB-INF/settings.properties" />
    		<constructor-arg name="settings" ref="properties" />
    	</bean>
    	
    	<bean id="properties"
    		class="java.util.Properties" />
    
    	<bean class="com.jglore.utils.myPropertyPlaceholderConfigurer">
    		<property name="location" value="file:${myWebApp.root}WEB-INF/settings.properties"/>
        </bean>
    any suggestion?

  • #2
    Hmm. There's a few ways to do this, but I have to say the complexity is nontrivial.

    One basic question: is there a reason you're not using JMX? Seems to me that by doing so, not only do your admins gain a common interface you don't have to write, but everything's already done for you. This is kind of the whole POINT of JMX, after all...

    (Persistence can be handled by using the various "persist" attributes on the ManagedResource and ManagedOperation/ManagedAttribute annotations).

    Just a thought
    - Don

    Comment


    • #3
      hi dbrinker thank you very much for your help.

      the reason I didn't think to JMX is quite simple... I don't know it :-)
      I am very new to java and spring and I have to learn a very lot of things... thank you for the suggestion. If I can get my job done I'll post the adopted solution.

      However, every new comment is welcome :-)

      Comment

      Working...
      X