All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kapil.framework.reader.ResourceBundleMessageReader Maven / Gradle / Ivy

Go to download

This is a set of utilities and classes that I have found useful over the years. In my career spanning over a decade, I have time and again written the same code or some part of the code over and over again. I never found the time to collate the details in a reusable library. This project will be a collection of such files. The work that I have been doing is more than 5 years old, however the project has been conceived in 2011.

There is a newer version: 1.2.1
Show newest version
/*******************************************************************************
 * Copyright 2011 @ Kapil Viren Ahuja
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.kapil.framework.reader;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import com.kapil.framework.exception.impl.UnImplementedException;
import com.kapil.framework.logger.ILogger;
import com.kapil.framework.logger.LogFactory;


/**
 * 

* This implementation of the {@link IMessageReader} locates the messages from a * resource bundle located in the application classpath. This implementation, has the capability * to read messages from multiple data sources. * * In addition, this implementation, provides the capabilities to read locale based messages. *

* * To use this implementation: *
 * // Initialize the message reader for the resourceBundle, reading the messages from a bundle names "userMessages"
 * IMessageReader messageReader = MessageReaderFactory.getInstance().getReader(resourceBundles, "userMessages");
 * 
 * // Get a message based on a key for the current locale.
 * String message = messageReader.getMessage("someKey");
 * // Get a message based on a key for the specific locale.
 * String message = messageReader.getMessage("someKey", Locale.UK_en);
 * 
*/ public final class ResourceBundleMessageReader extends MessageReader { /** The logger. */ private static final ILogger logger = LogFactory.getInstance().getLogger(ResourceBundleMessageReader.class); /** * Only Constructor needs a list of data sources to instantiate the reader. * * @param dataSources a {@link ArrayList} containing data sources that need to be searched for keys * */ public ResourceBundleMessageReader(List dataSources) { super(dataSources); } /** * Gets the message from the resource bundle. * * @param key {@link String} containing key which needs to be read from the bundle. * * @return {@link String} containing the message for the key. * */ public String getMessage(final String key) { return getMessage(key, Locale.getDefault()); } /** * Gets the message from the resource bundle for a specific locale. * * @param key {@link String} containing key which needs to be read from the bundle. * @param locale {@link Locale} for which the message needs to read from. * * @return {@link String} containing the message for the key. If not key is found in any of the data sources, * we return a blank String. * */ public String getMessage(final String key, final Locale locale) { if (super.getDataSources() == null) { throw new RuntimeException("No datasources found for the Message Reader."); } String message = ""; int size = super.getDataSources().size(); for (int index = 0; index < size; ++index) { try { logger.debug("Reading key [" + key + "] from datasource [" + super.getDataSources().get(index) + "]"); message = ResourceBundle.getBundle(super.getDataSources().get(index), locale).getString(key); } catch (MissingResourceException mrex) { // We do not want to throw an exception in case no key was found so that the search can continue for other data sources. // This exception has been ignored purposefully. logger.info("No key was found for ## in the reader.", new String[] { key }); } } return message; } public boolean getBooleanProperty(String name) { throw new UnImplementedException(UnImplementedException.NOT_IMPLEMENTED, new String[]{"getBooleanProperty"}); } public int getIntegerProperty(String name) { throw new UnImplementedException(UnImplementedException.NOT_IMPLEMENTED, new String[]{"getIntegerProperty"}); } public List getList(String name) { throw new UnImplementedException(UnImplementedException.NOT_IMPLEMENTED, new String[]{"getList"}); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy