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, tryLockTimeout, "getting loaded resource bundles, messages and base names in 'MessageServiceImpl.getLocaleProperties()'");
        try
        {
            tenantLoadedResourceBundles = getLoadedResourceBundles(tenantDomain, true);
            loadedBundles = tenantLoadedResourceBundles.get(locale);
            tenantCachedMessages = getMessages(tenantDomain, true);
            props = tenantCachedMessages.get(locale);
            tenantResourceBundleBaseNames = getResourceBundleBaseNames(tenantDomain, false, false);
            loadedBundleCount = tenantResourceBundleBaseNames.size();
        }
        finally
        {
            readLock.unlock();
        }
        if (loadedBundles == null)
        {
            LockHelper.tryLock(writeLock, tryLockTimeout, "adding resource bundle for locale in 'MessageServiceImpl.getLocaleProperties()'");
            try
            {
                loadedBundles = new HashSet();
                tenantLoadedResourceBundles.put(locale, loadedBundles);
                putLoadedResourceBundles(tenantDomain, tenantLoadedResourceBundles);
                init = true;
            }
            finally
            {
                writeLock.unlock();
            }
        }
        if (props == null)
        {
            LockHelper.tryLock(writeLock, tryLockTimeout,
                    "adding resource bundle properties into the cache (because properties are not cached) in 'MessageServiceImpl.getLocaleProperties()'");
            try
            {
                props = new HashMap();
                tenantCachedMessages.put(locale, props);
                putMessages(tenantDomain, tenantCachedMessages);
                init = true;
            }
            finally
            {
                writeLock.unlock();
            }
        }
        if ((loadedBundles.size() != loadedBundleCount) || (init == true))
        {
            LockHelper.tryLock(writeLock, tryLockTimeout,
                    "searching resource bundle and adding new resource bundle for locale if the bundle is not found in 'MessageServiceImpl.getLocaleProperties()'");
            try
            {
                // get registered resource bundles               
                Set resBundleBaseNames = getResourceBundleBaseNames(tenantDomain, true, false);
                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, tryLockTimeout, "getting resource bundle base names in 'MessageServiceImpl.getRegisteredBundles()'");
        try
        {
            return getResourceBundleBaseNames(getTenantDomain(), false, false);
        }
        finally
        {
            readLock.unlock();
        }
    }  
    
    private Set getResourceBundleBaseNames(String tenantDomain, boolean haveWriteLock, boolean forWrite)
    {
        // Assume a read lock is present
        Set resourceBundleBaseNames = resourceBundleBaseNamesCache.get(tenantDomain);
        if (resourceBundleBaseNames != null)
        {
            return getOrCopyResourceBundleBaseNames(resourceBundleBaseNames, forWrite);
        }
        if (!haveWriteLock)
        {
            // They are not there. Upgrade to the write lock.
            readLock.unlock();
            LockHelper.tryLock(writeLock, tryLockTimeout, "getting cached resource bundle base names by tenant domain in 'MessageServiceImpl.getRegisteredBundles()'");
        }
        try
        {
            resourceBundleBaseNames = resourceBundleBaseNamesCache.get(tenantDomain);
            if (resourceBundleBaseNames != null)
            {
                return getOrCopyResourceBundleBaseNames(resourceBundleBaseNames, forWrite);
            }
            reset(tenantDomain); // reset caches - may have been invalidated (e.g. in a cluster)
            resourceBundleBaseNames = resourceBundleBaseNamesCache.get(tenantDomain);
        }
        finally
        {
            if (!haveWriteLock)
            {
                writeLock.unlock();
                LockHelper.tryLock(readLock, tryLockTimeout, "upgrading to read lock in MessageServiceImpl.getResourceBundleBaseNames()");
            }
        }
        
        if (resourceBundleBaseNames == null)
        {     
            // unexpected
            throw new AlfrescoRuntimeException("Failed to re-initialise resourceBundleBaseNamesCache " + tenantDomain);
        }
        // Done
        return getOrCopyResourceBundleBaseNames(resourceBundleBaseNames, forWrite);
    }  
    
    private Set getOrCopyResourceBundleBaseNames(Set inbound, boolean createNew)
    {
        return createNew ? new HashSet(inbound) : inbound;
    }
    
    private void putResourceBundleBaseNames(String tenantDomain, Set resourceBundleBaseNames)
    {
        resourceBundleBaseNames = Collections.unmodifiableSet(new HashSet(resourceBundleBaseNames));
        resourceBundleBaseNamesCache.put(tenantDomain, resourceBundleBaseNames);
    } 
    
    private void removeResourceBundleBaseNames(String tenantDomain)
    {
        if (resourceBundleBaseNamesCache.get(tenantDomain) != null)
        {
            resourceBundleBaseNamesCache.remove(tenantDomain);
        }
    } 
    
    private Map> getLoadedResourceBundles(String tenantDomain, boolean forWrite)
    {
        // Assume a read lock is present
        Map> loadedResourceBundles = loadedResourceBundlesCache.get(tenantDomain);
        if (loadedResourceBundles != null)
        {
            return getOrCopyResourceBundles(loadedResourceBundles, forWrite);
        }
        
        // Not present.  Upgrade to write lock.
        readLock.unlock();
        LockHelper.tryLock(writeLock, tryLockTimeout, "getting cached resource bundle by tenant domain in 'MessageServiceImpl.getLoadedResourceBundles()'");
        try
        {
            loadedResourceBundles = loadedResourceBundlesCache.get(tenantDomain);
            if (loadedResourceBundles != null)
            {
                return getOrCopyResourceBundles(loadedResourceBundles, forWrite);
            }
            reset(tenantDomain); // reset caches - may have been invalidated (e.g. in a cluster)
            loadedResourceBundles = loadedResourceBundlesCache.get(tenantDomain);
        }
        finally
        {
            writeLock.unlock();
            LockHelper.tryLock(readLock, tryLockTimeout, "upgrading to read lock in MessageServiceImpl.getLoadedResourceBundles()");
        }
        
        if (loadedResourceBundles == null)
        {     
            // unexpected
            throw new AlfrescoRuntimeException("Failed to re-initialise loadedResourceBundlesCache " + tenantDomain);
        }
        // Done
        return getOrCopyResourceBundles(loadedResourceBundles, forWrite);
    }
    
    private Map> getOrCopyResourceBundles(Map> inbound, boolean createNew)
    {
       return createNew ? new HashMap>(inbound) : inbound;
    }
    
    private void putLoadedResourceBundles(String tenantDomain, Map> loadedResourceBundles)
    {
        loadedResourceBundles = Collections.unmodifiableMap(new HashMap>(loadedResourceBundles));
        loadedResourceBundlesCache.put(tenantDomain, loadedResourceBundles);
    } 
    
    private void removeLoadedResourceBundles(String tenantDomain)
    {
        if (loadedResourceBundlesCache.get(tenantDomain) != null)
        {
            loadedResourceBundlesCache.remove(tenantDomain);
        }
    } 
    
    private void clearLoadedResourceBundles(String tenantDomain)
    {
        if (loadedResourceBundlesCache.get(tenantDomain) != null)
        {
            putLoadedResourceBundles(tenantDomain, new HashMap>());
        }
    } 
    
    private Map> getMessages(String tenantDomain, boolean forWrite)
    {
        // Assume a read lock
        Map> messages = messagesCache.get(tenantDomain);
        if (messages != null)
        {
            return getOrCopyMessages(messages, forWrite);
        }
        
        // Need to create it.  Upgrade to write lock.
        readLock.unlock();
        LockHelper.tryLock(writeLock, tryLockTimeout, "getting messages by tenant domain from the cache in 'MessageServiceImpl.getMessages()'");
        try
        {
            messages = messagesCache.get(tenantDomain);
            if (messages != null)
            {
                return getOrCopyMessages(messages, forWrite);
            }
            reset(tenantDomain); // reset caches - may have been invalidated (e.g. in a cluster)
            messages = messagesCache.get(tenantDomain);
        }
        finally
        {
            writeLock.unlock();
            LockHelper.tryLock(readLock, tryLockTimeout, "upgrading to read lock in MessageServiceImpl.getMessages()");
        }
        
        if (messages == null)
        {     
            // unexpected
            throw new AlfrescoRuntimeException("Failed to re-initialise messagesCache " + tenantDomain);
        }
        // Done
        return getOrCopyMessages(messages, forWrite);
    }  
    
    private Map> getOrCopyMessages(Map> inbound, boolean createNew)
    {
       return createNew ? new HashMap>(inbound) : inbound;
    }
    
    private void putMessages(String tenantDomain, Map> messages)
    {
        messages = Collections.unmodifiableMap(new HashMap>(messages)); 
        messagesCache.put(tenantDomain, messages);
    } 
    
    private void removeMessages(String tenantDomain)
    {
        if (messagesCache.get(tenantDomain) != null)
        {
            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