Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes read config file properties dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "read config file properties dynamically" Watch "read config file properties dynamically" New topic
Author

read config file properties dynamically

javafreak javaa
Greenhorn

Joined: Mar 03, 2011
Posts: 1
resource handler class which will get the config manager object

package .utils;

import .ConfigManager;
import .ConfigManagerFactory;
import Constants;
import java.io.Serializable;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;

public class ResourceHandler implements Serializable
{
private transient ResourceBundle rb;
private Locale locale;
private String languageCode;
private String customerCode;
private static final Logger LOG = Logger.getLogger(ResourceHandler.class.getName());

public ResourceHandler(String languageCode,String customerCode)
{
this.customerCode=customerCode;
this.languageCode=languageCode;
}
public String getResourceString(String key)
{
String resStr;
ConfigManager cm = ConfigManagerFactory.createConfigManager();
String resFileName = customerCode.toLowerCase() +"."+ languageCode;
try
{
/**
* The properties file folder structure must exist inside the doamins/domain/settings
* This folder must exist in the server's classpath
* */
resStr = cm.getProperty(resFileName, key);

// LOG.debug("Resource bundle filename ->" + resFileName);
}
catch (Exception e)
{
System.out.println(" Resource Error : = > " + e);
return key;
}
return resStr;
}

public String getCurrentLanguageCode()
{
return languageCode;
}
public Locale getCurrentLocale()
{
return locale;
}

public ResourceBundle getCurrentResourceBundle()
{
return rb;
}

public String getCustomerCode()
{
return customerCode;
}

}
--------------------------------------------------------------------------
config manage factory class



/* */ package config;
/* */
/* */ import java.lang.reflect.Constructor;
/* */ import java.lang.reflect.InvocationTargetException;
/* */ import java.util.MissingResourceException;
/* */ import java.util.ResourceBundle;
/* */ import weblogic.logging.NonCatalogLogger;
/* */
/* */ public class ConfigManagerFactory
/* */ {
/* 24 */ private static NonCatalogLogger LOG = new NonCatalogLogger(ConfigManagerFactory.class.getName());
/* */ public static final String MANAGER_CLASS_SPEC = "managerClass";
/* */ public static final String DEFAULT_MANAGER_CLASS = "ConfigManagerImpl";
/* 53 */ private static ConfigManager instance = null;
/* */
/* */ public static ConfigManager createConfigManager()
/* */ {
/* 31 */ synchronized (ConfigManager.class) {
/* 32 */ if (instance == null) {
/* 33 */ instance = initializeConfigManager();
/* */ }
/* */ }
/* 36 */ return instance;
/* */ }
/* */
/* */ private static ConfigManager initializeConfigManager()
/* */ {
/* 62 */ String refName = ConfigManager.class.getName();
/* 63 */ String className = "ConfigManagerImpl";
/* */ try {
/* 65 */ ResourceBundle resourceBundle = ResourceBundle.getBundle(refName);
/* 66 */ className = resourceBundle.getString("managerClass");
/* */ } catch (MissingResourceException e) {
/* 68 */ LOG.debug(e.getMessage());
/* */ }
/* 70 */ Class clazz = null;
/* */ try {
/* 72 */ clazz = Class.forName(className);
/* */ } catch (ClassNotFoundException e) {
/* 74 */ LOG.debug(e.getMessage());
/* 75 */ return null;
/* */ }
/* 77 */ Constructor constructor = null;
/* */ try {
/* 79 */ constructor = clazz.getDeclaredConstructor(new Class[0]);
/* */ } catch (NoSuchMethodException e) {
/* 81 */ LOG.debug(e.getMessage());
/* 82 */ return null;
/* */ } catch (SecurityException e) {
/* 84 */ LOG.debug(e.getMessage());
/* 85 */ return null;
/* */ }
/* */ try {
/* 88 */ return ((ConfigManager)constructor.newInstance(null));
/* */ } catch (InstantiationException e) {
/* 90 */ LOG.debug(e.getMessage());
/* 91 */ return null;
/* */ } catch (IllegalAccessException e) {
/* 93 */ LOG.debug(e.getMessage());
/* 94 */ return null;
/* */ } catch (IllegalArgumentException e) {
/* 96 */ LOG.debug(e.getMessage());
/* 97 */ return null;
/* */ } catch (InvocationTargetException e) {
/* 99 */ LOG.debug(e.getMessage());
/* 100 */ return null;
/* */ } catch (ClassCastException e) {
/* 102 */ LOG.debug(e.getMessage()); }
/* 103 */ return null;
/* */ }
/* */ }



-------------------------------------------------------------

Manage Imple clss


/* */ package com. ..utils.config;
/* */
/* */ import java.util.Locale;
/* */ import java.util.MissingResourceException;
/* */ import java.util.ResourceBundle;
/* */ import weblogic.logging.NonCatalogLogger;
/* */
/* */ public class ConfigManagerImpl
/* */ implements ConfigManager
/* */ {
/* 22 */ private static NonCatalogLogger LOG = new NonCatalogLogger(ConfigManagerImpl.class.getName());
/* */ public static final String DEFAULT_RESOURCE = "com. .. .ApplicationConfiguration";
/* */
/* */ public String getProperty(String property)
/* */ throws MissingResourceException
/* */ {
/* 39 */ return getProperty("com. .. .ApplicationConfiguration", property);
/* */ }
/* */
/* */ public String getProperty(Locale locale, String property)
/* */ throws MissingResourceException
/* */ {
/* 53 */ return getProperty("com. .. .ApplicationConfiguration", locale, property);
/* */ }
/* */
/* */ public String getProperty(String resource, String property)
/* */ throws MissingResourceException
/* */ {
/* 67 */ ResourceBundle resourceBundle = ResourceBundle.getBundle(resource);
/* 68 */ return resourceBundle.getString(property);
/* */ }
/* */
/* */ public String getProperty(String resource, Locale locale, String property)
/* */ throws MissingResourceException
/* */ {
/* 83 */ ResourceBundle resourceBundle = ResourceBundle.getBundle(resource, locale);
/* 84 */ return resourceBundle.getString(property);
/* */ }
/* */
/* */ public String getCustomerProperty(String customer, String property)
/* */ throws MissingResourceException
/* */ {
/* 100 */ return getCustomerProperty("com. .. .ApplicationConfiguration", customer, property);
/* */ }
/* */
/* */ public String getCustomerProperty(Locale locale, String customer, String property)
/* */ throws MissingResourceException
/* */ {
/* 117 */ return getCustomerProperty("com. .. .ApplicationConfiguration", locale, customer, property);
/* */ }
/* */
/* */ public String getCustomerProperty(String resource, String customer, String property)
/* */ throws MissingResourceException
/* */ {
/* 134 */ ResourceBundle resourceBundle = ResourceBundle.getBundle(resource);
/* */ try {
/* 136 */ return resourceBundle.getString(customer + '.' + property);
/* */ } catch (MissingResourceException e) {
/* 138 */ LOG.debug("Unable to get property" + customer + "." + property + "from " + resourceBundle.toString()); }
/* 139 */ return resourceBundle.getString(property);
/* */ }
/* */
/* */ public String getCustomerProperty(String resource, Locale locale, String customer, String property)
/* */ throws MissingResourceException
/* */ {
/* 158 */ ResourceBundle resourceBundle = ResourceBundle.getBundle(resource, locale);
/* */ try {
/* 160 */ return resourceBundle.getString(customer + '.' + property);
/* */ } catch (MissingResourceException e) {
/* 162 */ LOG.debug("Unable to get property" + customer + "." + property + "from " + resourceBundle.toString()); }
/* 163 */ return resourceBundle.getString(property);
/* */ }
/* */ }
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Please UseCodeTags. You can edit your post to add them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read config file properties dynamically
 
Similar Threads
How to retrieve Internationalized messages from .java code base?
Unable to read properties file using resouce bundle
solve my probelm in creatting thread
advice on persistence
XML reading issues