From cf5d4c650c095373ad4ce0e33e862c7e0fc9bb59 Mon Sep 17 00:00:00 2001 From: Jan Vonka Date: Mon, 17 Dec 2007 11:02:39 +0000 Subject: [PATCH] Dynamic models - validate message resource bundle exists (when reloading) git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@7680 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../messages/repoadmin-interpreter-help.txt | 2 +- .../repo/admin/RepoAdminServiceImpl.java | 98 +++++++++++++------ .../repo/i18n/MessageServiceImpl.java | 6 +- 3 files changed, 71 insertions(+), 35 deletions(-) diff --git a/config/alfresco/messages/repoadmin-interpreter-help.txt b/config/alfresco/messages/repoadmin-interpreter-help.txt index c943b2e1d0..314eee1b8f 100755 --- a/config/alfresco/messages/repoadmin-interpreter-help.txt +++ b/config/alfresco/messages/repoadmin-interpreter-help.txt @@ -98,7 +98,7 @@ ok> reload messages Reload message resource bundle from repository into runtime message service. - e.g. undeploy messages lifecycle-messages + e.g. reload messages lifecycle-messages ## ## end diff --git a/source/java/org/alfresco/repo/admin/RepoAdminServiceImpl.java b/source/java/org/alfresco/repo/admin/RepoAdminServiceImpl.java index 66abcdd3d8..c6ff34f570 100755 --- a/source/java/org/alfresco/repo/admin/RepoAdminServiceImpl.java +++ b/source/java/org/alfresco/repo/admin/RepoAdminServiceImpl.java @@ -40,6 +40,7 @@ import org.alfresco.repo.dictionary.DictionaryDAO; import org.alfresco.repo.dictionary.M2Model; import org.alfresco.repo.dictionary.RepositoryLocation; import org.alfresco.repo.i18n.MessageService; +import org.alfresco.repo.i18n.MessageServiceImpl; import org.alfresco.service.cmr.admin.RepoAdminService; import org.alfresco.service.cmr.dictionary.DictionaryException; import org.alfresco.service.cmr.dictionary.ModelDefinition; @@ -553,33 +554,26 @@ public class RepoAdminServiceImpl implements RepoAdminService // Check that all the passed values are not null ParameterCheck.mandatory("ResourceClasspath", resourceClasspath); - String bundleBaseName = null; + String bundleBaseName = resourceClasspath; // note: resource path should be in form path1/path2/path3/bundlebasename int idx = resourceClasspath.lastIndexOf("/"); - if ((idx != -1) && (idx < (resourceClasspath.length()-1))) + if (idx != -1) { - bundleBaseName = resourceClasspath.substring(idx+1); + if (idx < (resourceClasspath.length()-1)) + { + bundleBaseName = resourceClasspath.substring(idx+1); + } + else + { + bundleBaseName = null; + } } - if (bundleBaseName == null) - { - throw new AlfrescoRuntimeException("Message deployment failed - missing bundle base name (path = " + resourceClasspath + ")"); - } + checkBundleBaseName(bundleBaseName); - if (bundleBaseName.indexOf("_") != -1) - { - // currently limited due to parser in DictionaryRepositoryBootstrap - throw new AlfrescoRuntimeException("Message deployment failed - bundle base name '" + bundleBaseName + "' should not contain '_' (underscore)"); - } - - if (bundleBaseName.indexOf(".") != -1) - { - throw new AlfrescoRuntimeException("Message deployment failed - bundle base name '" + bundleBaseName + "' should not contain '.' (period)"); - } - - String pattern = "classpath*:" + resourceClasspath + "*.properties"; + String pattern = "classpath*:" + resourceClasspath + "*" + MessageServiceImpl.PROPERTIES_FILE_SUFFIX; PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); @@ -625,7 +619,7 @@ public class RepoAdminServiceImpl implements RepoAdminService } catch (Throwable e) { - throw new AlfrescoRuntimeException("Message resource bundle deployment failed ", e); + throw new AlfrescoRuntimeException("Message resource bundle deployment failed for resource classpath " + resourceClasspath, e); } return bundleBaseName; @@ -653,12 +647,12 @@ public class RepoAdminServiceImpl implements RepoAdminService if (nodeRefs.size() == 0) { - throw new AlfrescoRuntimeException("Could not find custom labels location " + repoMessagesLocation.getPath()); + throw new AlfrescoRuntimeException("Could not find messages location " + repoMessagesLocation.getPath()); } else if (nodeRefs.size() > 1) { // unexpected: should not find multiple nodes with same name - throw new AlfrescoRuntimeException("Found multiple custom labels location " + repoMessagesLocation.getPath()); + throw new AlfrescoRuntimeException("Found multiple messages location " + repoMessagesLocation.getPath()); } NodeRef customLabelsNodeRef = nodeRefs.get(0); @@ -721,8 +715,7 @@ public class RepoAdminServiceImpl implements RepoAdminService */ public void undeployMessageBundle(String bundleBaseName) { - // Check that all the passed values are not null - ParameterCheck.mandatory("bundleBaseName", bundleBaseName); + checkBundleBaseName(bundleBaseName); try { @@ -751,7 +744,7 @@ public class RepoAdminServiceImpl implements RepoAdminService } catch (Throwable t) { - throw new AlfrescoRuntimeException("Messages undeployment failed ", t); + throw new AlfrescoRuntimeException("Message resource bundle undeployment failed ", t); } } @@ -760,9 +753,8 @@ public class RepoAdminServiceImpl implements RepoAdminService * @see org.alfresco.service.cmr.admin.RepoAdminService#reloadMessageBundle(java.lang.String) */ public void reloadMessageBundle(String bundleBaseName) - { - // Check that all the passed values are not null - ParameterCheck.mandatory("bundleBaseName", bundleBaseName); + { + checkBundleBaseName(bundleBaseName); try { @@ -771,15 +763,57 @@ public class RepoAdminServiceImpl implements RepoAdminService // re-register bundle String repoBundlePath = storeRef.toString() + repoMessagesLocation.getPath() + "/cm:" + bundleBaseName; + + NodeRef rootNode = nodeService.getRootNode(storeRef); + + List nodeRefs = searchService.selectNodes(rootNode, repoMessagesLocation.getPath()+CRITERIA_ALL, null, namespaceService, false); + + boolean found = false; + for (NodeRef nodeRef : nodeRefs) + { + String customLabelName = (String)nodeService.getProperty(nodeRef, ContentModel.PROP_NAME); + + if (customLabelName.startsWith(bundleBaseName)) + { + found = true; + break; + } + } - messageService.unregisterResourceBundle(repoBundlePath); - messageService.registerResourceBundle(repoBundlePath); - - logger.info("Message resources re-loaded: " + bundleBaseName); + if (found) + { + messageService.unregisterResourceBundle(repoBundlePath); + messageService.registerResourceBundle(repoBundlePath); + + logger.info("Message resources re-loaded: " + bundleBaseName); + } + else + { + throw new AlfrescoRuntimeException("Could not find message resource bundle " + repoBundlePath); + } } catch (Throwable e) { throw new AlfrescoRuntimeException("Message resource re-load failed", e); } } + + private void checkBundleBaseName(String bundleBaseName) + { + if ((bundleBaseName == null) || (bundleBaseName.equals(""))) + { + throw new AlfrescoRuntimeException("Message deployment failed - missing bundle base name"); + } + + if (bundleBaseName.indexOf("_") != -1) + { + // currently limited due to parser in DictionaryRepositoryBootstrap + throw new AlfrescoRuntimeException("Message deployment failed - bundle base name '" + bundleBaseName + "' should not contain '_' (underscore)"); + } + + if (bundleBaseName.indexOf(".") != -1) + { + throw new AlfrescoRuntimeException("Message deployment failed - bundle base name '" + bundleBaseName + "' should not contain '.' (period)"); + } + } } diff --git a/source/java/org/alfresco/repo/i18n/MessageServiceImpl.java b/source/java/org/alfresco/repo/i18n/MessageServiceImpl.java index f2575b51f6..3f39450ccf 100644 --- a/source/java/org/alfresco/repo/i18n/MessageServiceImpl.java +++ b/source/java/org/alfresco/repo/i18n/MessageServiceImpl.java @@ -46,6 +46,8 @@ public class MessageServiceImpl implements MessageService { private static final Log logger = LogFactory.getLog(MessageServiceImpl.class); + public static final String PROPERTIES_FILE_SUFFIX = ".properties"; + /** * Lock objects */ @@ -542,12 +544,12 @@ public class MessageServiceImpl implements MessageService NodeRef rootNode = serviceRegistry.getNodeService().getRootNode(storeRef); // first attempt - with locale - List nodeRefs = searchService.selectNodes(rootNode, path+"_"+locale+".properties", null, resolver, false); + List nodeRefs = searchService.selectNodes(rootNode, path+"_"+locale+PROPERTIES_FILE_SUFFIX, null, resolver, false); if ((nodeRefs == null) || (nodeRefs.size() == 0)) { // second attempt - basename - nodeRefs = searchService.selectNodes(rootNode, path+".properties", null, resolver, false); + nodeRefs = searchService.selectNodes(rootNode, path+PROPERTIES_FILE_SUFFIX, null, resolver, false); if ((nodeRefs == null) || (nodeRefs.size() == 0)) {