Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this the correct way of doing this

 
ashish kulkarni
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to get messages from my properties file, in my
java code, so i have written a class as below,
i have used singleton pattern so there is only one
instance of this class, and then i have a method
called getMessage which returns the message from
properties file..
I need to put some code for internatinalization
messages, any suggestions.
package com.pfizer.maps.util;
import com.pfizer.maps.common.*;
import javax.servlet.http.*;
import java.util.*;
import org.apache.struts.util.*;
import org.apache.struts.action.*;
/**
* <p>Title:MapsMessageResource </p>
* <p>Description: This class gets the message text
fromt he resource file,
* it basically uses
org.apache.struts.util.MessageResource
* class</p>
*/
public class MapsMessageResource
{
private static MapsMessageResource instance = new
MapsMessageResource();
private MapsMessageResource()
{
}
public static MapsMessageResource getInstance()
{
if (instance == null)
{
instance = new MapsMessageResource();
}
return instance;
}

public String getMessage(String key,
HttpServletRequest request, HttpSession session)
{
Locale locale =
(Locale)session.getAttribute(Action.LOCALE_KEY);
if(locale== null)
{
locale = request.getLocale();
}
return getMessageFromBundle(locale, key,
getConfig(locale));
}
private String getMessageFromBundle(Locale locale,
String key, String config)
{
MessageResources bundle =getMessageResource(config);
return bundle.getMessage(locale, key);
}
private String getConfig(Locale locale)
{
return "com.pfizer.maps.maps";
}
private MessageResources getMessageResource(String
config)
{
return MessageResources.getMessageResources(config);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic