Map result = null;
// Spec says empty local returns raw headers.
if (locale.length() == 0)
{
result = new StringMap(adapt(BundleRevisionImpl.class).getHeaders());
}
// If we have no result, try to get it from the cached headers.
if (result == null)
{
synchronized (m_cachedHeaders)
{
// If the bundle is uninstalled, then we should always return
// the uninstalled headers, which are the default locale as per
// the spec.
if (m_uninstalledHeaders != null)
{
result = m_uninstalledHeaders;
}
// If the bundle has been updated, clear the cached headers.
else if (getLastModified() > m_cachedHeadersTimestamp)
{
m_cachedHeaders.clear();
}
// Otherwise, returned the cached headers if they exist.
else
{
// Check if headers for this locale have already been resolved
if (m_cachedHeaders.containsKey(locale))
{
result = (Map) m_cachedHeaders.get(locale);
}
}
}
}
// If the requested locale is not cached, then try to create it.
if (result == null)
{
// Get a modifiable copy of the raw headers.
Map headers = new StringMap(adapt(BundleRevisionImpl.class).getHeaders());
// Assume for now that this will be the result.
result = headers;
// Check to see if we actually need to localize anything
boolean localize = false;
for (Iterator it = headers.values().iterator(); !localize && it.hasNext(); )
{
if (((String) it.next()).startsWith("%"))
{
localize = true;
}
}
if (!localize)
{
// If localization is not needed, just cache the headers and return
// them as-is. Not sure if this is useful
updateHeaderCache(locale, headers);
}
else
{
// Do localization here and return the localized headers
String basename = (String) headers.get(Constants.BUNDLE_LOCALIZATION);
if (basename == null)
{
basename = Constants.BUNDLE_LOCALIZATION_DEFAULT_BASENAME;
}
// Create ordered list of revisions to search for localization
// property resources.
List<BundleRevision> revisionList = createLocalizationRevisionList(
adapt(BundleRevisionImpl.class));
// Create ordered list of files to load properties from
List<String> resourceList = createLocalizationResourceList(basename, locale);
// Create a merged props file with all available props for this locale
boolean found = false;
Properties mergedProperties = new Properties();
for (BundleRevision br : revisionList)
{
for (String res : resourceList)
{
URL temp = ((BundleRevisionImpl) br).getEntry(res + ".properties");
if (temp != null)
{
found = true;
try
{
mergedProperties.load(
temp.openConnection().getInputStream());
}
catch (IOException ex)
{
// File doesn't exist, just continue loop
}
}
}
}
// If the specified locale was not found, then the spec says we should
// return the default localization.
if (!found && !locale.equals(Locale.getDefault().toString()))
{
result = getCurrentLocalizedHeader(Locale.getDefault().toString());
}
// Otherwise, perform the localization based on the discovered
// properties and cache the result.
else
{
// Resolve all localized header entries
for (Iterator it = headers.entrySet().iterator(); it.hasNext(); )
{
Map.Entry entry = (Map.Entry) it.next();
String value = (String) entry.getValue();
if (value.startsWith("%"))
{