* If resource not found and configuration dictates that exception should be thrown
*/
public String getString(final String key, final Component component, final IModel model,
final String defaultValue) throws MissingResourceException
{
final IResourceSettings resourceSettings = Application.get().getResourceSettings();
boolean addedToPage = false;
if (component != null)
{
if ((component instanceof Page) || null != component.findParent(Page.class))
{
addedToPage = true;
}
if (!addedToPage)
{
logger.warn(
"Tried to retrieve a localized string for a component that has not yet been added to the page. "
+ "This can sometimes lead to an invalid or no localized resource returned. "
+ "Make sure you are not calling Component#getString() inside your Component's constructor. "
+ "Offending component: {}", component);
}
}
String cacheKey = null;
String string = null;
// If this component is not yet added to page we do not want to check
// cache as we can generate an invalid cache key
if ((cache != null) && addedToPage)
{
cacheKey = getCacheKey(key, component);
}
// Value not found are cached as well (value = null)
if ((cacheKey != null) && cache.containsKey(cacheKey))
{
string = getFromCache(cacheKey);
}
else
{
// Iterate over all registered string resource loaders until the
// property has been found
Iterator iter = resourceSettings.getStringResourceLoaders().iterator();
while (iter.hasNext())
{
IStringResourceLoader loader = (IStringResourceLoader)iter.next();
string = loader.loadStringResource(component, key);
if (string != null)
{
break;
}
}
// Cache the result incl null if not found
if (cacheKey != null)
{
putIntoCache(cacheKey, string);
}
}
if ((string == null) && (defaultValue != null))
{
// Resource not found, so handle missing resources based on
// application configuration and try the default value
if (resourceSettings.getUseDefaultOnMissingResource())
{
string = defaultValue;
}
}
// If a property value has been found, or a default value was given,
// than replace the placeholder and we are done
if (string != null)
{
return substitutePropertyExpressions(component, string, model);
}
if (resourceSettings.getThrowExceptionOnMissingResource())
{
AppendingStringBuffer message = new AppendingStringBuffer("Unable to find resource: " +
key);
if (component != null)
{