• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

read config file properties dynamically

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
/* */ }
/* */ }
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags. You can edit your post to add them.
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic