The Artima Developer Community
Sponsored Link

Java Buzz Forum
ResourceBundle's vs. Loading Properties

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Russell Beattie

Posts: 727
Nickname: rbeattie
Registered: Aug, 2003

Russell Beattie is a Mobile Internet Developer
ResourceBundle's vs. Loading Properties Posted: May 29, 2004 9:09 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Russell Beattie.
Original Post: ResourceBundle's vs. Loading Properties
Feed Title: Russell Beattie Notebook
Feed URL: http://www.russellbeattie.com/notebook/rss.jsp?q=java,code,mobile
Feed Description: My online notebook with thoughts, comments, links and more.
Latest Java Buzz Posts
Latest Java Buzz Posts by Russell Beattie
Latest Posts From Russell Beattie Notebook

Advertisement
I have an Initialization Servlet I've used for quite a while now, it loads up when the web-app starts and grabs a .properties file with the arbitrary options I need to store for the application - everything from media directories to web service passwords.

Here's what it looks like:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class InitializationServlet extends HttpServlet {
  
	private final static String SITE_CONFIG="config";

	public void init(ServletConfig servletConfig) throws ServletException {
		
		super.init(servletConfig);
	
		String configPath = servletConfig.getInitParameter(SITE_CONFIG);
		if("".equals(configPath) || configPath == null){
			throw new ServletException("Config param not found");
		}
		
		Properties configurationProperties = new Properties();
		InputStream is = servletConfig.getServletContext().getResourceAsStream(configPath);
		try {
			configurationProperties.load(is);
				
			Iterator keyIterator = configurationProperties.keySet().iterator();
			while (keyIterator.hasNext()) {
				String key = (String) keyIterator.next();
				String propertyValue = configurationProperties.getProperty(key);
				servletConfig.getServletContext().log(key + " - " + propertyValue);
				System.out.println(key + " - " + propertyValue);
			}			
			
			servletConfig.getServletContext().setAttribute("config", config);
				
			is.close();
		
		} catch (Exception e) {
			throw new ServletException("Error loading config.", e);
		}	

   }
   
}

But then I noticed on Erik's Linkblog a link to this post about ResourceBundles in Servlets by Eddy in Mauritius. In it he gives this bit of example code:

ResourceBundle rb = ResourceBundle.getBundle("messages", Locale.ENGLISH, getClass().getClassLoader());

Umm. Wow. That's a hell of a lot cleaner, no? And I don't need a dedicated Servlet to pull in those values. A ResourceBundle isn't a Properties class by any stretch, but for what I need it's pretty much perfect. I've played with this before but could never get it working (the ClassLoader() got me), so this is an amazing discovery.

The question is, which is the best thing to do? The ResourceBundle properties file needs to be in the CLASSPATH, which may be a GoodThing (TM) as you could dynamically swap out properties just by changing an environment variable, but is a potential source of bugs as versions pop up in .jar files you weren't expecting. The other thing is that you can't save values to a ResourceBundle - it's read only. Though I can promise you, I've never even thought about doing that in a webapp.

Or maybe there's another option? Personally the whole ResourceBundle vs Properties vs. PropertyResourceBundle vs JDK 1.4's java.util.prefs package just boggles me. One line for pulling in default property values via the ResourceBundle is mighty tempting though, I have to say, regardless of whether it's the "right" thing to do or not.

-Russ

P.S. JavaDocs.org rules! It's quickly become as frequently used while I program as Google. That's how I grabbed the urls to the classes above. Seriously, the guy who launched that site deserves a medal and a free pass to JavaOne for life.

Read: ResourceBundle's vs. Loading Properties

Topic: [May 20, 2004 17:51 PDT] 5 Links Previous Topic   Next Topic Topic: Bill Gates, Blogs and RSS

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use