enumKeys = resourcebundle.getKeys();
                                while (enumKeys.hasMoreElements() == true)
                                {
                                    String key = enumKeys.nextElement();
                                    props.remove(key);
                                }
                            }
                
                            loadedBundles.remove(resBundlePath);
                        }
                    }
                }
            }
            
            // unregister resource bundle
            if (resourceBundleBaseNamesForAllLocales != null)
            {
                resourceBundleBaseNamesForAllLocales.remove(resBundlePath);
                logger.info("Unregistered message bundle '" + resBundlePath + "'");
            }
                     
            clearLoadedResourceBundles(tenantDomain); // force re-load of message cache
        }
        finally
        {
            writeLock.unlock();
        }
    }
    
    /**
     * Get the messages for a locale.
     * 
     * Will use cache where available otherwise will load into cache from bundles.
     *
     * @param locale    the locale
     * @return          message map
     */
    private Map getLocaleProperties(Locale locale)
    {
        Set loadedBundles = null;
        Map props = null;
        int loadedBundleCount = 0;
             
        String tenantDomain = getTenantDomain();
        boolean init = false;
        
        Map> tenantLoadedResourceBundles = null;
        Map> tenantCachedMessages = null;
        Set tenantResourceBundleBaseNames = null;
        LockHelper.tryLock(readLock, 100);
        try
        {
            tenantLoadedResourceBundles = getLoadedResourceBundles(tenantDomain);
            loadedBundles = tenantLoadedResourceBundles.get(locale);
            tenantCachedMessages = getMessages(tenantDomain);
            props = tenantCachedMessages.get(locale);
            tenantResourceBundleBaseNames = getResourceBundleBaseNames(tenantDomain);
            loadedBundleCount = tenantResourceBundleBaseNames.size();
        }
        finally
        {
            readLock.unlock();
        }
        if (loadedBundles == null)
        {
            LockHelper.tryLock(writeLock, 100);
            try
            {
                loadedBundles = new HashSet();
                tenantLoadedResourceBundles.put(locale, loadedBundles);
                init = true;
            }
            finally
            {
                writeLock.unlock();
            }
        }
        if (props == null)
        {
            LockHelper.tryLock(writeLock, 100);
            try
            {
                props = new HashMap();
                tenantCachedMessages.put(locale, props);
                init = true;
            }
            finally
            {
                writeLock.unlock();
            }
        }
        if ((loadedBundles.size() != loadedBundleCount) || (init == true))
        {
            LockHelper.tryLock(writeLock, 100);
            try
            {
                // get registered resource bundles               
                Set resBundleBaseNames = getResourceBundleBaseNames(tenantDomain);
                int count = 0;
                
                // load resource bundles for given locale (by tenant, if applicable)
                for (String resBundlePath : resBundleBaseNames)
                {
                    if (loadedBundles.contains(resBundlePath) == false)
                    {
                        ResourceBundle resourcebundle = null;
                        int idx1 = resBundlePath.indexOf(StoreRef.URI_FILLER);
                       
                        if (idx1 != -1)
                        {
                            // load from repository
                            int idx2 = resBundlePath.indexOf("/", idx1+3);
                            String store = resBundlePath.substring(0, idx2);
                            String path = resBundlePath.substring(idx2);
                            StoreRef storeRef = tenantService.getName(new StoreRef(store));
                            
                            try
                            {
                                resourcebundle = getRepoResourceBundle(storeRef, path, locale);
                            }
                            catch (IOException ioe)
                            {
                                throw new AlfrescoRuntimeException("Failed to read message resource bundle from repository " + resBundlePath + " : " + ioe);
                            }
                        }
                        else
                        {
                            // load from classpath
                            resourcebundle = ResourceBundle.getBundle(resBundlePath, locale);
                        }
                        if (resourcebundle != null)
                        {
                            Enumeration enumKeys = resourcebundle.getKeys();
                            while (enumKeys.hasMoreElements() == true)
                            {
                                String key = enumKeys.nextElement();
                                props.put(key, resourcebundle.getString(key));
                            }
    
                            loadedBundles.add(resBundlePath);
                            count++;
                        }
                    }
                }
                
                logger.info("Message bundles (x " + count + ") loaded for locale " + locale);
            }
            finally
            {
                writeLock.unlock();
            }
        }
        return props;
    }
    
    public ResourceBundle getRepoResourceBundle(
            final StoreRef storeRef,
            final String path,
            final Locale locale) throws IOException
    {   
        // TODO - need to replace basic strategy with a more complete
        // search & instantiation strategy similar to ResourceBundle.getBundle()
        // Consider search query with basename* and then apply strategy ...
        
        // Avoid permission exceptions
        RunAsWork getBundleWork = new RunAsWork()
        {
            @Override
            public ResourceBundle doWork() throws Exception
            {
                NodeRef rootNode = nodeService.getRootNode(storeRef);
                // first attempt - with locale        
                NodeRef nodeRef = getNode(rootNode, path+"_"+locale+PROPERTIES_FILE_SUFFIX);
                
                if (nodeRef == null)
                {
                    // second attempt - basename 
                    nodeRef = getNode(rootNode, path+PROPERTIES_FILE_SUFFIX);
                }
                
                if (nodeRef == null)
                {
                    logger.debug("Could not find message resource bundle " + storeRef + "/" + path);
                    return null;
                }
                
                ContentReader cr = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
                ResourceBundle resBundle = new MessagePropertyResourceBundle(
                        new InputStreamReader(cr.getContentInputStream(), cr.getEncoding()));
                return resBundle;
            }
        };
        return AuthenticationUtil.runAs(getBundleWork, AuthenticationUtil.getSystemUserName());
    }
    
    public void onEnableTenant()
    {
        // NOOP - refer to DictionaryRepositoryBootstrap
    }
    
    public void onDisableTenant()
    {
        destroy(); // will be called in context of tenant
    }
    public void init()
    { 
        // initialise empty message service       
        String tenantDomain = getTenantDomain();
        
        putResourceBundleBaseNames(tenantDomain, new HashSet());
        putLoadedResourceBundles(tenantDomain, new HashMap>());
        putMessages(tenantDomain, new HashMap>());
        
        logger.info("Empty message service initialised");
    }
    
    public void destroy()
    {
        // used by reset and also as callback when destroying tenant(s) during shutdown
        String tenantDomain = getTenantDomain();
        
        removeLoadedResourceBundles(tenantDomain);
        removeMessages(tenantDomain);
        removeResourceBundleBaseNames(tenantDomain);
        
        logger.info("Messages cache destroyed (all locales)");
    }
    
    public Set getRegisteredBundles()
    {
        LockHelper.tryLock(readLock, 100);
        try
        {
            return getResourceBundleBaseNames(getTenantDomain());
        }
        finally
        {
            readLock.unlock();
        }
    }  
    
    private Set getResourceBundleBaseNames(String tenantDomain)
    {
        // Assume a read lock is present
        Set resourceBundleBaseNames = resourceBundleBaseNamesCache.get(tenantDomain);
        if (resourceBundleBaseNames != null)
        {
            return resourceBundleBaseNames;
        }
        
        // They are not there.  Upgrade to the write lock.
        readLock.unlock();
        LockHelper.tryLock(writeLock, 100);
        try
        {
            resourceBundleBaseNames = resourceBundleBaseNamesCache.get(tenantDomain);
            if (resourceBundleBaseNames != null)
            {
                return resourceBundleBaseNames;
            }
            reset(tenantDomain); // reset caches - may have been invalidated (e.g. in a cluster)
            resourceBundleBaseNames = resourceBundleBaseNamesCache.get(tenantDomain);
        }
        finally
        {
            writeLock.unlock();
            LockHelper.tryLock(readLock, 100);
        }
        
        if (resourceBundleBaseNames == null)
        {     
            // unexpected
            throw new AlfrescoRuntimeException("Failed to re-initialise resourceBundleBaseNamesCache " + tenantDomain);
        }
        // Done
        return resourceBundleBaseNames;
    }  
    
    private void putResourceBundleBaseNames(String tenantDomain, Set resourceBundleBaseNames)
    {
        resourceBundleBaseNamesCache.put(tenantDomain, resourceBundleBaseNames);
    } 
    
    private void removeResourceBundleBaseNames(String tenantDomain)
    {
        if (resourceBundleBaseNamesCache.get(tenantDomain) != null)
        {
            resourceBundleBaseNamesCache.get(tenantDomain).clear();
            resourceBundleBaseNamesCache.remove(tenantDomain);
        }
    } 
    
    private Map> getLoadedResourceBundles(String tenantDomain)
    {
        // Assume a read lock is present
        Map> loadedResourceBundles = loadedResourceBundlesCache.get(tenantDomain);
        if (loadedResourceBundles != null)
        {
            return loadedResourceBundles;
        }
        
        // Not present.  Upgrade to write lock.
        readLock.unlock();
        LockHelper.tryLock(writeLock, 100);
        try
        {
            loadedResourceBundles = loadedResourceBundlesCache.get(tenantDomain);
            if (loadedResourceBundles != null)
            {
                return loadedResourceBundles;
            }
            reset(tenantDomain); // reset caches - may have been invalidated (e.g. in a cluster)
            loadedResourceBundles = loadedResourceBundlesCache.get(tenantDomain);
        }
        finally
        {
            writeLock.unlock();
            LockHelper.tryLock(readLock, 100);
        }
        
        if (loadedResourceBundles == null)
        {     
            // unexpected
            throw new AlfrescoRuntimeException("Failed to re-initialise loadedResourceBundlesCache " + tenantDomain);
        }
        // Done
        return loadedResourceBundles;
    }  
    
    private void putLoadedResourceBundles(String tenantDomain, Map> loadedResourceBundles)
    {
        loadedResourceBundlesCache.put(tenantDomain, loadedResourceBundles);
    } 
    
    private void removeLoadedResourceBundles(String tenantDomain)
    {
        if (loadedResourceBundlesCache.get(tenantDomain) != null)
        {
            loadedResourceBundlesCache.get(tenantDomain).clear();
            loadedResourceBundlesCache.remove(tenantDomain);
        }
    } 
    
    private void clearLoadedResourceBundles(String tenantDomain)
    {
        if (loadedResourceBundlesCache.get(tenantDomain) != null)
        {
            loadedResourceBundlesCache.get(tenantDomain).clear();
        }
    } 
    
    private Map> getMessages(String tenantDomain)
    {
        // Assume a read lock
        Map> messages = messagesCache.get(tenantDomain);
        if (messages != null)
        {
            return messages;
        }
        
        // Need to create it.  Upgrade to write lock.
        readLock.unlock();
        LockHelper.tryLock(writeLock, 100);
        try
        {
            messages = messagesCache.get(tenantDomain);
            if (messages != null)
            {
                return messages;
            }
            reset(tenantDomain); // reset caches - may have been invalidated (e.g. in a cluster)
            messages = messagesCache.get(tenantDomain);
        }
        finally
        {
            writeLock.unlock();
            LockHelper.tryLock(readLock, 100);
        }
        
        if (messages == null)
        {     
            // unexpected
            throw new AlfrescoRuntimeException("Failed to re-initialise messagesCache " + tenantDomain);
        }
        // Done
        return messages;
    }  
    
    private void putMessages(String tenantDomain, Map> messages)
    {
        messagesCache.put(tenantDomain, messages);
    } 
    
    private void removeMessages(String tenantDomain)
    {
        if (messagesCache.get(tenantDomain) != null)
        {
            messagesCache.get(tenantDomain).clear();
            messagesCache.remove(tenantDomain);
        }
    } 
    
    // local helper - returns tenant domain (or empty string if default non-tenant)
    private String getTenantDomain()
    {
        return tenantService.getCurrentUserDomain();
    }
    
    public void register(MessageDeployer messageDeployer)
    {
        if (! messageDeployers.contains(messageDeployer))
        {
            messageDeployers.add(messageDeployer);
        }
    }
    
    /**
     * Resets the message service
     */      
    public void reset()
    {
        reset(getTenantDomain());
    }
    
    private void reset(String tenantDomain)
    {
        if (logger.isDebugEnabled()) 
        {
            logger.debug("Resetting messages ...");
        }
        
        TenantUtil.runAsSystemTenant(new TenantRunAsWork