RM-2604 Replace SMAP generics with ImmutableMap<String, CaveatGroup>.

Sorry to anyone who was hoping to create other implementations of the SMAP
interface.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/DEV/caveatmarkdatatype@114320 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tom Page
2015-10-14 09:34:21 +00:00
parent 9fbb7924ce
commit e0d4480a59
5 changed files with 39 additions and 36 deletions

View File

@@ -18,9 +18,7 @@
*/ */
package org.alfresco.module.org_alfresco_module_rm.caveat.dao; package org.alfresco.module.org_alfresco_module_rm.caveat.dao;
import java.io.Serializable; import com.google.common.collect.ImmutableMap;
import java.util.Map;
import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; 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 * @author Tom Page
* @since 2.4.a * @since 2.4.a
*/ */
public class CaveatDAOCache<SMAP extends Map<String, CaveatGroup> & Serializable> implements CaveatDAOInterface<SMAP> public class CaveatDAOCache implements CaveatDAOInterface
{ {
/** The wrapped caveat DAO. */ /** The wrapped caveat DAO. */
private CaveatDAOInterface<SMAP> caveatDAO; private CaveatDAOInterface caveatDAO;
/** A cache of the system caveat groups. */ /** A cache of the system caveat groups. */
private SMAP caveatGroups; private ImmutableMap<String, CaveatGroup> caveatGroups;
/** /**
* {@inheritDoc} The first call to this method will be cached and returned for every successive call. * {@inheritDoc} The first call to this method will be cached and returned for every successive call.
*/ */
@Override @Override
public SMAP getCaveatGroups() public ImmutableMap<String, CaveatGroup> getCaveatGroups()
{ {
if (caveatGroups == null) if (caveatGroups == null)
{ {
@@ -54,7 +52,7 @@ public class CaveatDAOCache<SMAP extends Map<String, CaveatGroup> & Serializable
* *
* @param caveatDAO The caveat DAO to be wrapped. * @param caveatDAO The caveat DAO to be wrapped.
*/ */
public void setCaveatDAO(CaveatDAOInterface<SMAP> caveatDAO) public void setCaveatDAO(CaveatDAOInterface caveatDAO)
{ {
this.caveatDAO = caveatDAO; this.caveatDAO = caveatDAO;
} }

View File

@@ -21,14 +21,13 @@ package org.alfresco.module.org_alfresco_module_rm.caveat.dao;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; 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.CaveatException.MalformedConfiguration;
import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup;
import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroupType; import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroupType;
@@ -47,7 +46,7 @@ import org.slf4j.LoggerFactory;
* @author Tom Page * @author Tom Page
* @since 2.4.a * @since 2.4.a
*/ */
public class CaveatDAOFromJSON<SMAP extends Map<String, CaveatGroup> & Serializable> implements CaveatDAOInterface<SMAP> public class CaveatDAOFromJSON implements CaveatDAOInterface
{ {
/** JSON key for the group id. */ /** JSON key for the group id. */
private static final String GROUP_ID_JSON_KEY = "id"; private static final String GROUP_ID_JSON_KEY = "id";
@@ -81,10 +80,9 @@ public class CaveatDAOFromJSON<SMAP extends Map<String, CaveatGroup> & Serializa
* @throws MalformedConfiguration If the configuration file cannot be interpreted. * @throws MalformedConfiguration If the configuration file cannot be interpreted.
*/ */
@Override @Override
public SMAP getCaveatGroups() public ImmutableMap<String, CaveatGroup> getCaveatGroups()
{ {
@SuppressWarnings("unchecked") Builder<String, CaveatGroup> builder = ImmutableMap.builder();
SMAP result = (SMAP) new HashMap<String, CaveatGroup>();
try (final InputStream in = this.getClass().getResourceAsStream(configLocation)) try (final InputStream in = this.getClass().getResourceAsStream(configLocation))
{ {
if (in != null) if (in != null)
@@ -97,9 +95,7 @@ public class CaveatDAOFromJSON<SMAP extends Map<String, CaveatGroup> & Serializa
final JSONObject nextObj = jsonArray.getJSONObject(i); final JSONObject nextObj = jsonArray.getJSONObject(i);
CaveatGroup caveatGroup = createGroup(nextObj); CaveatGroup caveatGroup = createGroup(nextObj);
String caveatGroupId = caveatGroup.getId(); String caveatGroupId = caveatGroup.getId();
if (result.containsKey(caveatGroupId)) { throw new MalformedConfiguration( builder.put(caveatGroupId, caveatGroup);
"Configuration contains two caveat groups with id " + caveatGroupId); }
result.put(caveatGroupId, caveatGroup);
} }
} }
else else
@@ -111,7 +107,17 @@ public class CaveatDAOFromJSON<SMAP extends Map<String, CaveatGroup> & Serializa
{ {
throw new MalformedConfiguration("Could not read caveat configuration: " + configLocation, e); throw new MalformedConfiguration("Could not read caveat configuration: " + configLocation, e);
} }
return result;
ImmutableMap<String, CaveatGroup> map;
try
{
map = builder.build();
}
catch (IllegalArgumentException e)
{
throw new MalformedConfiguration("Configuration contains two caveat groups with the same id.", e);
}
return map;
} }
/** /**

View File

@@ -23,6 +23,7 @@ import static org.alfresco.module.org_alfresco_module_rm.caveat.CaveatConstants.
import java.io.Serializable; import java.io.Serializable;
import java.util.Map; 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.CaveatException.MalformedConfiguration;
import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup;
import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil; import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil;
@@ -41,7 +42,7 @@ import org.springframework.extensions.surf.util.AbstractLifecycleBean;
* @author Tom Page * @author Tom Page
* @since 2.4.a * @since 2.4.a
*/ */
public class CaveatDAOFromJSONBootstrap<SMAP extends Map<String, CaveatGroup> & Serializable> extends AbstractLifecycleBean public class CaveatDAOFromJSONBootstrap extends AbstractLifecycleBean
{ {
/** Logging utility for the class. */ /** Logging utility for the class. */
private static final Logger LOGGER = LoggerFactory.getLogger(CaveatDAOFromJSONBootstrap.class); private static final Logger LOGGER = LoggerFactory.getLogger(CaveatDAOFromJSONBootstrap.class);
@@ -49,14 +50,14 @@ public class CaveatDAOFromJSONBootstrap<SMAP extends Map<String, CaveatGroup> &
private final AuthenticationUtil authenticationUtil; private final AuthenticationUtil authenticationUtil;
private final TransactionService transactionService; private final TransactionService transactionService;
private AttributeService attributeService; private AttributeService attributeService;
private CaveatDAOInterface<SMAP> caveatDAO; private CaveatDAOInterface caveatDAO;
private boolean isInitialised = false; private boolean isInitialised = false;
public CaveatDAOFromJSONBootstrap(AuthenticationUtil authUtil, public CaveatDAOFromJSONBootstrap(AuthenticationUtil authUtil,
TransactionService txService, TransactionService txService,
AttributeService attributeService, AttributeService attributeService,
CaveatDAOInterface<SMAP> caveatDAO) CaveatDAOInterface caveatDAO)
{ {
this.authenticationUtil = authUtil; this.authenticationUtil = authUtil;
this.transactionService = txService; this.transactionService = txService;
@@ -65,7 +66,7 @@ public class CaveatDAOFromJSONBootstrap<SMAP extends Map<String, CaveatGroup> &
} }
/** Set the object from which configuration options will be read. */ /** Set the object from which configuration options will be read. */
public void setClassificationServiceDAO(CaveatDAOInterface<SMAP> caveatDAO) { this.caveatDAO = caveatDAO; } public void setClassificationServiceDAO(CaveatDAOInterface caveatDAO) { this.caveatDAO = caveatDAO; }
public void setAttributeService(AttributeService attributeService) { this.attributeService = attributeService; } public void setAttributeService(AttributeService attributeService) { this.attributeService = attributeService; }
public boolean isInitialised() public boolean isInitialised()
@@ -99,15 +100,15 @@ public class CaveatDAOFromJSONBootstrap<SMAP extends Map<String, CaveatGroup> &
* *
* @return the persisted caveat groups if they have been persisted, else {@code null}. * @return the persisted caveat groups if they have been persisted, else {@code null}.
*/ */
private SMAP getPersistedCaveatGroups(final Serializable[] key) private ImmutableMap<String, CaveatGroup> getPersistedCaveatGroups(final Serializable[] key)
{ {
return authenticationUtil.runAsSystem(new RunAsWork<SMAP>() return authenticationUtil.runAsSystem(new RunAsWork<ImmutableMap<String, CaveatGroup>>()
{ {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public SMAP doWork() throws Exception public ImmutableMap<String, CaveatGroup> doWork() throws Exception
{ {
return (SMAP) attributeService.getAttribute(key); return (ImmutableMap<String, CaveatGroup>) attributeService.getAttribute(key);
} }
}); });
} }
@@ -133,8 +134,8 @@ public class CaveatDAOFromJSONBootstrap<SMAP extends Map<String, CaveatGroup> &
*/ */
protected void initialiseConfiguredCaveatGroups(Serializable[] key) protected void initialiseConfiguredCaveatGroups(Serializable[] key)
{ {
final SMAP persistedGroups = getPersistedCaveatGroups(key); final ImmutableMap<String, CaveatGroup> persistedGroups = getPersistedCaveatGroups(key);
final SMAP classpathGroups = caveatDAO.getCaveatGroups(); final ImmutableMap<String, CaveatGroup> classpathGroups = caveatDAO.getCaveatGroups();
// Note! We cannot log the entities or even the size of these lists for security reasons. // Note! We cannot log the entities or even the size of these lists for security reasons.
LOGGER.debug("Persisted CaveatGroup: {}", loggableStatusOf(persistedGroups)); LOGGER.debug("Persisted CaveatGroup: {}", loggableStatusOf(persistedGroups));

View File

@@ -19,21 +19,20 @@
package org.alfresco.module.org_alfresco_module_rm.caveat.dao; package org.alfresco.module.org_alfresco_module_rm.caveat.dao;
import java.io.Serializable; import com.google.common.collect.ImmutableMap;
import java.util.Map;
import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup;
/** /**
* An object responsible for providing access to the configured caveat groups and marks. * An object responsible for providing access to the configured caveat groups and marks.
* *
* @author Tom Page * @author Tom Page
* @since 2.4.a * @since 2.4.a
*/ */
public interface CaveatDAOInterface<SMAP extends Map<String, CaveatGroup> & Serializable> public interface CaveatDAOInterface
{ {
/** /**
* Gets a map of all the available caveat groups keyed by id. * Gets a map of all the available caveat groups keyed by id.
*/ */
SMAP getCaveatGroups(); ImmutableMap<String, CaveatGroup> getCaveatGroups();
} }

View File

@@ -37,8 +37,7 @@ import org.junit.Test;
public class CaveatDAOFromJSONUnitTest public class CaveatDAOFromJSONUnitTest
{ {
/** The class under test. */ /** 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 that loading the default caveat configuration file doesn't throw any exceptions. */
@Test @Test