diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOCache.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOCache.java index 1c3135e5ac..4e903b0112 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOCache.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOCache.java @@ -18,9 +18,7 @@ */ package org.alfresco.module.org_alfresco_module_rm.caveat.dao; -import java.io.Serializable; -import java.util.Map; - +import com.google.common.collect.ImmutableMap; import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; /** @@ -29,18 +27,18 @@ import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; * @author Tom Page * @since 2.4.a */ -public class CaveatDAOCache & Serializable> implements CaveatDAOInterface +public class CaveatDAOCache implements CaveatDAOInterface { /** The wrapped caveat DAO. */ - private CaveatDAOInterface caveatDAO; + private CaveatDAOInterface caveatDAO; /** A cache of the system caveat groups. */ - private SMAP caveatGroups; + private ImmutableMap caveatGroups; /** * {@inheritDoc} The first call to this method will be cached and returned for every successive call. */ @Override - public SMAP getCaveatGroups() + public ImmutableMap getCaveatGroups() { if (caveatGroups == null) { @@ -54,7 +52,7 @@ public class CaveatDAOCache & Serializable * * @param caveatDAO The caveat DAO to be wrapped. */ - public void setCaveatDAO(CaveatDAOInterface caveatDAO) + public void setCaveatDAO(CaveatDAOInterface caveatDAO) { this.caveatDAO = caveatDAO; } diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java index 8d6fa67634..8e9a8aa43e 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java @@ -21,14 +21,13 @@ package org.alfresco.module.org_alfresco_module_rm.caveat.dao; import java.io.IOException; import java.io.InputStream; -import java.io.Serializable; import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMap.Builder; import org.alfresco.module.org_alfresco_module_rm.caveat.CaveatException.MalformedConfiguration; import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroupType; @@ -47,7 +46,7 @@ import org.slf4j.LoggerFactory; * @author Tom Page * @since 2.4.a */ -public class CaveatDAOFromJSON & Serializable> implements CaveatDAOInterface +public class CaveatDAOFromJSON implements CaveatDAOInterface { /** JSON key for the group id. */ private static final String GROUP_ID_JSON_KEY = "id"; @@ -81,10 +80,9 @@ public class CaveatDAOFromJSON & Serializa * @throws MalformedConfiguration If the configuration file cannot be interpreted. */ @Override - public SMAP getCaveatGroups() + public ImmutableMap getCaveatGroups() { - @SuppressWarnings("unchecked") - SMAP result = (SMAP) new HashMap(); + Builder builder = ImmutableMap.builder(); try (final InputStream in = this.getClass().getResourceAsStream(configLocation)) { if (in != null) @@ -97,9 +95,7 @@ public class CaveatDAOFromJSON & Serializa final JSONObject nextObj = jsonArray.getJSONObject(i); CaveatGroup caveatGroup = createGroup(nextObj); String caveatGroupId = caveatGroup.getId(); - if (result.containsKey(caveatGroupId)) { throw new MalformedConfiguration( - "Configuration contains two caveat groups with id " + caveatGroupId); } - result.put(caveatGroupId, caveatGroup); + builder.put(caveatGroupId, caveatGroup); } } else @@ -111,7 +107,17 @@ public class CaveatDAOFromJSON & Serializa { throw new MalformedConfiguration("Could not read caveat configuration: " + configLocation, e); } - return result; + + ImmutableMap map; + try + { + map = builder.build(); + } + catch (IllegalArgumentException e) + { + throw new MalformedConfiguration("Configuration contains two caveat groups with the same id.", e); + } + return map; } /** diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONBootstrap.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONBootstrap.java index 3d98844a5a..71a266bd97 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONBootstrap.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONBootstrap.java @@ -23,6 +23,7 @@ import static org.alfresco.module.org_alfresco_module_rm.caveat.CaveatConstants. import java.io.Serializable; import java.util.Map; +import com.google.common.collect.ImmutableMap; import org.alfresco.module.org_alfresco_module_rm.caveat.CaveatException.MalformedConfiguration; import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil; @@ -41,7 +42,7 @@ import org.springframework.extensions.surf.util.AbstractLifecycleBean; * @author Tom Page * @since 2.4.a */ -public class CaveatDAOFromJSONBootstrap & Serializable> extends AbstractLifecycleBean +public class CaveatDAOFromJSONBootstrap extends AbstractLifecycleBean { /** Logging utility for the class. */ private static final Logger LOGGER = LoggerFactory.getLogger(CaveatDAOFromJSONBootstrap.class); @@ -49,14 +50,14 @@ public class CaveatDAOFromJSONBootstrap & private final AuthenticationUtil authenticationUtil; private final TransactionService transactionService; private AttributeService attributeService; - private CaveatDAOInterface caveatDAO; + private CaveatDAOInterface caveatDAO; private boolean isInitialised = false; public CaveatDAOFromJSONBootstrap(AuthenticationUtil authUtil, TransactionService txService, AttributeService attributeService, - CaveatDAOInterface caveatDAO) + CaveatDAOInterface caveatDAO) { this.authenticationUtil = authUtil; this.transactionService = txService; @@ -65,7 +66,7 @@ public class CaveatDAOFromJSONBootstrap & } /** Set the object from which configuration options will be read. */ - public void setClassificationServiceDAO(CaveatDAOInterface caveatDAO) { this.caveatDAO = caveatDAO; } + public void setClassificationServiceDAO(CaveatDAOInterface caveatDAO) { this.caveatDAO = caveatDAO; } public void setAttributeService(AttributeService attributeService) { this.attributeService = attributeService; } public boolean isInitialised() @@ -99,15 +100,15 @@ public class CaveatDAOFromJSONBootstrap & * * @return the persisted caveat groups if they have been persisted, else {@code null}. */ - private SMAP getPersistedCaveatGroups(final Serializable[] key) + private ImmutableMap getPersistedCaveatGroups(final Serializable[] key) { - return authenticationUtil.runAsSystem(new RunAsWork() + return authenticationUtil.runAsSystem(new RunAsWork>() { @Override @SuppressWarnings("unchecked") - public SMAP doWork() throws Exception + public ImmutableMap doWork() throws Exception { - return (SMAP) attributeService.getAttribute(key); + return (ImmutableMap) attributeService.getAttribute(key); } }); } @@ -133,8 +134,8 @@ public class CaveatDAOFromJSONBootstrap & */ protected void initialiseConfiguredCaveatGroups(Serializable[] key) { - final SMAP persistedGroups = getPersistedCaveatGroups(key); - final SMAP classpathGroups = caveatDAO.getCaveatGroups(); + final ImmutableMap persistedGroups = getPersistedCaveatGroups(key); + final ImmutableMap classpathGroups = caveatDAO.getCaveatGroups(); // Note! We cannot log the entities or even the size of these lists for security reasons. LOGGER.debug("Persisted CaveatGroup: {}", loggableStatusOf(persistedGroups)); diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOInterface.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOInterface.java index 5001f3b05b..6f48422949 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOInterface.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOInterface.java @@ -19,21 +19,20 @@ package org.alfresco.module.org_alfresco_module_rm.caveat.dao; -import java.io.Serializable; -import java.util.Map; - +import com.google.common.collect.ImmutableMap; import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; + /** * An object responsible for providing access to the configured caveat groups and marks. * * @author Tom Page * @since 2.4.a */ -public interface CaveatDAOInterface & Serializable> +public interface CaveatDAOInterface { /** * Gets a map of all the available caveat groups keyed by id. */ - SMAP getCaveatGroups(); + ImmutableMap getCaveatGroups(); } diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONUnitTest.java index c6d4139b74..f6f6e70fc4 100644 --- a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONUnitTest.java +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONUnitTest.java @@ -37,8 +37,7 @@ import org.junit.Test; public class CaveatDAOFromJSONUnitTest { /** The class under test. */ - @SuppressWarnings("rawtypes") - CaveatDAOFromJSON caveatDAOFromJSON = new CaveatDAOFromJSON(); + CaveatDAOFromJSON caveatDAOFromJSON = new CaveatDAOFromJSON(); /** Test that loading the default caveat configuration file doesn't throw any exceptions. */ @Test