AuditModelRegistryImpl refactored as dynamic subsystem

- Modules such as RM can now drop in their own config - should fix some unit tests
- Picks up config from 
   classpath*:alfresco/audit/*.xml
   classpath*:alfresco/enterprise/audit/*.xml
   classpath*:alfresco/module/*/audit/*.xml
   classpath*:alfresco/extension/audit/*.xml
- One audit.{applicationkey}.enabled property for each application in the config, defaulting to true
- One audit.enabled global enablement property defaulting to false
- Still controllable by alfresco-global.properties and JMX

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@18841 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Dave Ward
2010-02-25 12:31:17 +00:00
parent 0ba1e3830c
commit 94f4b34aac
20 changed files with 621 additions and 657 deletions

View File

@@ -117,16 +117,7 @@
<property name="nodeService" ref="nodeService" />
</bean>
<!-- Mount registry from Audit subsystem -->
<bean id="auditModel.modelRegistry" class="org.alfresco.repo.management.subsystems.SubsystemProxyFactory">
<property name="sourceApplicationContextFactory">
<ref bean="Audit" />
</property>
<property name="interfaces">
<list>
<value>org.alfresco.repo.audit.model.AuditModelRegistry</value>
</list>
</property>
</bean>
<!-- Reference in the audit registry managed bean -->
<alias name="Audit" alias="auditModel.modelRegistry"/>
</beans>

View File

@@ -552,7 +552,19 @@
</bean>
<!-- Start Auditing -->
<bean id="Audit" class="org.alfresco.repo.management.subsystems.ChildApplicationContextFactory" parent="abstractPropertyBackedBean">
<bean id="Audit" class="org.alfresco.repo.audit.model.AuditModelRegistryImpl" parent="abstractPropertyBackedBean">
<property name="searchPath">
<list>
<value>classpath*:alfresco/audit/*.xml</value>
<value>classpath*:alfresco/enterprise/audit/*.xml</value>
<value>classpath*:alfresco/module/*/audit/*.xml</value>
<value>classpath*:alfresco/extension/audit/*.xml</value>
</list>
</property>
<property name="transactionService" ref="transactionService" />
<property name="auditDAO" ref="auditDAO" />
<property name="dataExtractors" ref="auditModel.extractorRegistry" />
<property name="dataGenerators" ref="auditModel.generatorRegistry" />
<property name="autoStart">
<value>true</value>
</property>

View File

@@ -251,7 +251,7 @@ db.pool.abandoned.time=300
# Audit configuration
audit.enabled=false
audit.repository.enabled=true
audit.cmis.enabled=true
audit.cmischangelog.enabled=true
audit.useNewConfig=true
# Email configuration

View File

@@ -1,43 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
<!-- Start Auditing -->
<bean id="audit.bootstrap" class="org.alfresco.repo.audit.AuditBootstrap">
<property name="active">
<value>${audit.enabled}</value>
</property>
<property name="transactionService" ref="transactionService"/>
<property name="auditModelRegistry" ref="auditModel.modelRegistry"/>
</bean>
<!-- Models -->
<bean id="auditModel.modelRegistry" class="org.alfresco.repo.audit.model.AuditModelRegistryImpl">
<property name="transactionService" ref="transactionService"/>
<property name="auditDAO" ref="auditDAO"/>
<property name="dataExtractors" ref="auditModel.extractorRegistry"/>
<property name="dataGenerators" ref="auditModel.generatorRegistry"/>
</bean>
<bean id="auditModel.repository" class="org.alfresco.repo.audit.model.AuditModelReader">
<property name="active">
<value>${audit.repository.enabled}</value>
</property>
<property name="auditModelUrl">
<value>classpath:alfresco/audit/alfresco-audit-repository.xml</value>
</property>
<property name="auditModelRegistry" ref="auditModel.modelRegistry"/>
</bean>
<bean id="auditModel.cmis" class="org.alfresco.repo.audit.model.AuditModelReader">
<property name="active">
<value>${audit.cmis.enabled}</value>
</property>
<property name="auditModelUrl">
<value>classpath:alfresco/audit/alfresco-audit-cmis.xml</value>
</property>
<property name="auditModelRegistry" ref="auditModel.modelRegistry"/>
</bean>
</beans>

View File

@@ -1,3 +0,0 @@
audit.enabled=false
audit.repository.enabled=true
audit.cmis.enabled=true

View File

@@ -39,7 +39,7 @@ import org.alfresco.cmis.CMISChangeType;
import org.alfresco.cmis.mapping.BaseCMISTest;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.management.subsystems.ApplicationContextFactory;
import org.alfresco.repo.audit.model.AuditModelRegistryImpl;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.repository.NodeRef;
@@ -76,7 +76,7 @@ public class CMISChangeLogServiceTest extends BaseCMISTest
EXPECTED_AMOUNTS.put(CMISChangeType.UPDATED, 6);
}
private ApplicationContextFactory auditSubsystem;
private AuditModelRegistryImpl auditSubsystem;
private CMISChangeLogService changeLogService;
private int actualCount = 0;
@@ -88,14 +88,14 @@ public class CMISChangeLogServiceTest extends BaseCMISTest
{
auditSubsystem.stop();
auditSubsystem.setProperty("audit.enabled", "true");
auditSubsystem.setProperty("audit.cmis.enabled", "false");
auditSubsystem.setProperty("audit.cmischangelog.enabled", "false");
}
private void enableAudit()
{
auditSubsystem.stop();
auditSubsystem.setProperty("audit.enabled", "true");
auditSubsystem.setProperty("audit.cmis.enabled", "true");
auditSubsystem.setProperty("audit.cmischangelog.enabled", "true");
}
/**
@@ -407,7 +407,7 @@ public class CMISChangeLogServiceTest extends BaseCMISTest
nodeService = (NodeService) applicationContext.getBean("NodeService");
permissionService = (PermissionService) applicationContext.getBean("PermissionService");
fileFolderService = (FileFolderService) applicationContext.getBean("FileFolderService");
auditSubsystem = (ApplicationContextFactory) applicationContext.getBean("Audit");
auditSubsystem = (AuditModelRegistryImpl) applicationContext.getBean("Audit");
}
@Override
@@ -415,5 +415,6 @@ public class CMISChangeLogServiceTest extends BaseCMISTest
{
deleteTestData();
super.tearDown();
auditSubsystem.destroy();
}
}

View File

@@ -1,96 +0,0 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have received a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.audit;
import org.alfresco.repo.audit.model.AuditModelRegistry;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.transaction.TransactionService;
import org.springframework.extensions.surf.util.AbstractLifecycleBean;
import org.springframework.context.ApplicationEvent;
/**
* Starts all the necessary audit functionality once the repository has started.
*
* @author Derek Hulley
* @since 3.2
*/
public class AuditBootstrap extends AbstractLifecycleBean
{
private TransactionService transactionService;
private AuditModelRegistry auditModelRegistry;
private boolean isActive = true;
public void setTransactionService(TransactionService transactionService)
{
this.transactionService = transactionService;
}
public void setAuditModelRegistry(AuditModelRegistry registry)
{
this.auditModelRegistry = registry;
}
public void setActive(boolean isActive)
{
this.isActive = isActive;
}
/**
* @see AuditModelRegistry#loadAuditModels()
*/
@Override
protected void onBootstrap(ApplicationEvent event)
{
// Don't bootstrap if the system is read-only
if (transactionService.isReadOnly())
{
return;
}
// Don't start if we've been configured out
if (!this.isActive)
{
return;
}
RetryingTransactionCallback<Void> callback = new RetryingTransactionCallback<Void>()
{
public Void execute() throws Throwable
{
auditModelRegistry.loadAuditModels();
return null;
}
};
transactionService.getRetryingTransactionHelper().doInTransaction(callback, transactionService.isReadOnly());
}
/**
* No-op
*/
@Override
protected void onShutdown(ApplicationEvent event)
{
}
}

View File

@@ -34,7 +34,6 @@ import org.alfresco.repo.audit.generator.DataGenerator;
import org.alfresco.repo.audit.model.AuditApplication;
import org.alfresco.repo.audit.model.AuditModelException;
import org.alfresco.repo.audit.model.AuditModelRegistryImpl;
import org.alfresco.repo.management.subsystems.ApplicationContextFactory;
import org.alfresco.util.ApplicationContextHelper;
import org.alfresco.util.PathMapper;
import org.apache.commons.logging.Log;
@@ -63,17 +62,21 @@ public class AuditBootstrapTest extends TestCase
@Override
public void setUp() throws Exception
{
// We have to look inside the subsystem for this test
ApplicationContextFactory subsystem = (ApplicationContextFactory) ctx.getBean("Audit");
ApplicationContext subCtx = subsystem.getApplicationContext();
auditModelRegistry = (AuditModelRegistryImpl) subCtx.getBean("auditModel.modelRegistry");
auditModelRegistry = (AuditModelRegistryImpl) ctx.getBean("auditModel.modelRegistry");
// Register a new model
URL testModelUrl = ResourceUtils.getURL("classpath:alfresco/audit/alfresco-audit-test.xml");
URL testModelUrl = ResourceUtils.getURL("classpath:alfresco/testaudit/alfresco-audit-test.xml");
auditModelRegistry.registerModel(testModelUrl);
auditModelRegistry.loadAuditModels();
}
@Override
protected void tearDown() throws Exception
{
// Throw away the reconfigured registry state
auditModelRegistry.destroy();
}
public void testSetUp()
{
// Just here to fail if the basic startup fails
@@ -97,32 +100,32 @@ public class AuditBootstrapTest extends TestCase
public void testModelLoading_NoDataExtractor() throws Exception
{
loadBadModel("classpath:alfresco/audit/alfresco-audit-test-bad-01.xml");
loadBadModel("classpath:alfresco/testaudit/alfresco-audit-test-bad-01.xml");
}
public void testModelLoading_NoDataGenerator() throws Exception
{
loadBadModel("classpath:alfresco/audit/alfresco-audit-test-bad-02.xml");
loadBadModel("classpath:alfresco/testaudit/alfresco-audit-test-bad-02.xml");
}
public void testModelLoading_DuplicatePath() throws Exception
{
loadBadModel("classpath:alfresco/audit/alfresco-audit-test-bad-03.xml");
loadBadModel("classpath:alfresco/testaudit/alfresco-audit-test-bad-03.xml");
}
public void testModelLoading_UppercasePath() throws Exception
{
loadBadModel("classpath:alfresco/audit/alfresco-audit-test-bad-04.xml");
loadBadModel("classpath:alfresco/testaudit/alfresco-audit-test-bad-04.xml");
}
public void testModelLoading_InvalidDataGeneratorName() throws Exception
{
loadBadModel("classpath:alfresco/audit/alfresco-audit-test-bad-05.xml");
loadBadModel("classpath:alfresco/testaudit/alfresco-audit-test-bad-05.xml");
}
public void testModelLoading_BadGeneratorRegisteredName() throws Exception
{
loadBadModel("classpath:alfresco/audit/alfresco-audit-test-bad-06.xml");
loadBadModel("classpath:alfresco/testaudit/alfresco-audit-test-bad-06.xml");
}
public void testGetApplicationId()

View File

@@ -39,7 +39,6 @@ import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.audit.model.AuditApplication;
import org.alfresco.repo.audit.model.AuditModelException;
import org.alfresco.repo.audit.model.AuditModelRegistryImpl;
import org.alfresco.repo.management.subsystems.ApplicationContextFactory;
import org.alfresco.repo.security.authentication.AuthenticationException;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
@@ -80,7 +79,6 @@ public class AuditComponentTest extends TestCase
private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
private ApplicationContextFactory subsystem;
private AuditModelRegistryImpl auditModelRegistry;
private AuditComponent auditComponent;
private AuditService auditService;
@@ -94,10 +92,7 @@ public class AuditComponentTest extends TestCase
@Override
public void setUp() throws Exception
{
// We have to look inside the subsystem for this test
subsystem = (ApplicationContextFactory) ctx.getBean("Audit");
ApplicationContext subCtx = subsystem.getApplicationContext();
auditModelRegistry = (AuditModelRegistryImpl) subCtx.getBean("auditModel.modelRegistry");
auditModelRegistry = (AuditModelRegistryImpl) ctx.getBean("auditModel.modelRegistry");
auditComponent = (AuditComponent) ctx.getBean("auditComponent");
serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
auditService = serviceRegistry.getAuditService();
@@ -105,7 +100,7 @@ public class AuditComponentTest extends TestCase
nodeService = serviceRegistry.getNodeService();
// Register the test model
URL testModelUrl = ResourceUtils.getURL("classpath:alfresco/audit/alfresco-audit-test.xml");
URL testModelUrl = ResourceUtils.getURL("classpath:alfresco/testaudit/alfresco-audit-test.xml");
auditModelRegistry.registerModel(testModelUrl);
auditModelRegistry.loadAuditModels();
@@ -138,8 +133,8 @@ public class AuditComponentTest extends TestCase
public void tearDown() throws Exception
{
AuthenticationUtil.clearCurrentSecurityContext();
// Throw away the reconfigured registry in the subsystem
subsystem.stop();
// Throw away the reconfigured registry state
auditModelRegistry.destroy();
}
public void testSetUp()
@@ -479,7 +474,7 @@ public class AuditComponentTest extends TestCase
params.setApplicationName(APPLICATION_API_TEST);
// Load in the config for this specific test: alfresco-audit-test-authenticationservice.xml
URL testModelUrl = ResourceUtils.getURL("classpath:alfresco/audit/alfresco-audit-test-authenticationservice.xml");
URL testModelUrl = ResourceUtils.getURL("classpath:alfresco/testaudit/alfresco-audit-test-authenticationservice.xml");
auditModelRegistry.registerModel(testModelUrl);
auditModelRegistry.loadAuditModels();

View File

@@ -1,91 +0,0 @@
/*
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing
*/
package org.alfresco.repo.audit.model;
import java.net.URL;
import org.springframework.extensions.surf.util.PropertyCheck;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.ResourceUtils;
/**
* A component used to load Audit model XML documents.
*
* @author Derek Hulley
* @since 3.2
*/
public class AuditModelReader implements InitializingBean
{
private URL auditModelUrl;
private AuditModelRegistryImpl auditModelRegistry;
private boolean isActive = true;
/**
* Set the XML location using <b>file:</b>, <b>classpath:</b> or any of the
* {@link ResourceUtils Spring-supported} formats.
*
* @param auditModelUrl the location of the XML file
*/
public void setAuditModelUrl(URL auditModelUrl)
{
this.auditModelUrl = auditModelUrl;
}
/**
*
* @param auditModelRegistry the registry that combines all loaded models
*/
public void setAuditModelRegistry(AuditModelRegistryImpl auditModelRegistry)
{
this.auditModelRegistry = auditModelRegistry;
}
/**
* Controls whether or not this bean registers its model with the registry on initialization.
*
* @param isActive
* <code>true</code> if this bean should register its model with the registry on initialization.
*/
public void setActive(boolean isActive)
{
this.isActive = isActive;
}
/**
* Pulls in the configuration and registers it
*/
public void afterPropertiesSet() throws Exception
{
if (!this.isActive)
{
return;
}
PropertyCheck.mandatory(this, "configUrl", auditModelUrl);
PropertyCheck.mandatory(this, "auditModelRegistry", auditModelRegistry);
auditModelRegistry.registerModel(auditModelUrl);
}
}

View File

@@ -28,13 +28,11 @@ import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
@@ -58,165 +56,93 @@ import org.alfresco.repo.audit.model._3.PathMap;
import org.alfresco.repo.audit.model._3.PathMappings;
import org.alfresco.repo.domain.audit.AuditDAO;
import org.alfresco.repo.domain.audit.AuditDAO.AuditApplicationInfo;
import org.alfresco.repo.management.subsystems.AbstractPropertyBackedBean;
import org.alfresco.repo.management.subsystems.PropertyBackedBeanState;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.PathMapper;
import org.alfresco.util.ResourceFinder;
import org.alfresco.util.registry.NamedObjectRegistry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.Resource;
import org.springframework.extensions.surf.util.PropertyCheck;
import org.springframework.util.ResourceUtils;
import org.xml.sax.SAXParseException;
/**
* Component used to store audit model definitions. It ensures that duplicate application and converter
* definitions are detected and provides a single lookup for code using the Audit model.
* Component used to store audit model definitions. It ensures that duplicate application and converter definitions are
* detected and provides a single lookup for code using the Audit model. It is factored as a subsystem and exposes a
* global enablement property plus enablement properties for each individual audit application.
*
* @author Derek Hulley
* @author dward
* @since 3.2
*/
public class AuditModelRegistryImpl implements AuditModelRegistry
public class AuditModelRegistryImpl extends AbstractPropertyBackedBean implements AuditModelRegistry
{
/** The name of the global enablement property. */
private static final String PROPERTY_AUDIT_ENABLED = "audit.enabled";
private static final String AUDIT_SCHEMA_LOCATION = "classpath:alfresco/audit/alfresco-audit-3.2.xsd";
private static final Log logger = LogFactory.getLog(AuditModelRegistryImpl.class);
private String[] searchPath;
private TransactionService transactionService;
private AuditDAO auditDAO;
private NamedObjectRegistry<DataExtractor> dataExtractors;
private NamedObjectRegistry<DataGenerator> dataGenerators;
private final ReentrantReadWriteLock.ReadLock readLock;
private final ReentrantReadWriteLock.WriteLock writeLock;
private final ObjectFactory objectFactory;
private final Set<URL> auditModelUrls;
private final List<Audit> auditModels;
/**
* Used to lookup path translations
* @see org.alfresco.repo.management.subsystems.AbstractPropertyBackedBean#afterPropertiesSet()
*/
private PathMapper auditPathMapper;
/**
* Used to lookup the audit application java hierarchy
*/
private final Map<String, AuditApplication> auditApplicationsByKey;
/**
* Used to lookup the audit application java hierarchy
*/
private final Map<String, AuditApplication> auditApplicationsByName;
/**
* Default constructor
*/
public AuditModelRegistryImpl()
{
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
readLock = lock.readLock();
writeLock = lock.writeLock();
objectFactory = new ObjectFactory();
auditModelUrls = new HashSet<URL>(7);
auditModels = new ArrayList<Audit>(7);
auditPathMapper = new PathMapper();
auditApplicationsByKey = new HashMap<String, AuditApplication>(7);
auditApplicationsByName = new HashMap<String, AuditApplication>(7);
}
/**
* Service to ensure DAO calls are transactionally wrapped
*/
public void setTransactionService(TransactionService transactionService)
{
this.transactionService = transactionService;
}
/**
* Set the DAO used to persisted the registered audit models
*/
public void setAuditDAO(AuditDAO auditDAO)
{
this.auditDAO = auditDAO;
}
/**
* Set the registry of {@link DataExtractor data extractors}
*/
public void setDataExtractors(NamedObjectRegistry<DataExtractor> dataExtractors)
{
this.dataExtractors = dataExtractors;
}
/**
* Set the registry of {@link DataGenerator data generators}
*/
public void setDataGenerators(NamedObjectRegistry<DataGenerator> dataGenerators)
{
this.dataGenerators = dataGenerators;
}
/**
* Ensures that all properties have been set for use.
*/
private void checkProperties()
@Override
public void afterPropertiesSet() throws Exception
{
PropertyCheck.mandatory(this, "searchPath", searchPath);
PropertyCheck.mandatory(this, "transactionService", transactionService);
PropertyCheck.mandatory(this, "auditDAO", auditDAO);
PropertyCheck.mandatory(this, "dataExtractors", dataExtractors);
PropertyCheck.mandatory(this, "dataGenerators", dataGenerators);
super.afterPropertiesSet();
}
/**
* Register an audit model at a given URL.
*
* @param auditModelUrl the source of the model
* @see org.alfresco.repo.management.subsystems.AbstractPropertyBackedBean#getState(boolean)
*/
public void registerModel(URL auditModelUrl)
@Override
protected synchronized AuditModelRegistryState getState(boolean start)
{
checkProperties();
writeLock.lock();
try
{
if (auditModelUrls.contains(auditModelUrl))
{
logger.warn("An audit model has already been registered at URL " + auditModelUrl);
}
auditModelUrls.add(auditModelUrl);
}
finally
{
writeLock.unlock();
}
return (AuditModelRegistryState)super.getState(start);
}
/**
* Register an audit model at a given node reference.
*
* @param auditModelNodeRef the source of the audit model
* @see org.alfresco.repo.audit.model.AuditModelRegistry#getAuditApplicationByKey(java.lang.String)
*/
public void registerModel(NodeRef auditModelNodeRef)
public AuditApplication getAuditApplicationByKey(String key)
{
checkProperties();
writeLock.lock();
try
{
throw new UnsupportedOperationException();
}
finally
{
writeLock.unlock();
}
return getState(true).getAuditApplicationByKey(key);
}
/**
* Cleans out all derived data
* @see org.alfresco.repo.audit.model.AuditModelRegistry#getAuditApplicationByName(java.lang.String)
*/
private void clearCaches()
public AuditApplication getAuditApplicationByName(String applicationName)
{
auditModels.clear();
auditApplicationsByKey.clear();
auditApplicationsByName.clear();
return getState(true).getAuditApplicationByName(applicationName);
}
/**
* @see org.alfresco.repo.audit.model.AuditModelRegistry#getAuditPathMapper()
*/
public PathMapper getAuditPathMapper()
{
return getState(true).getAuditPathMapper();
}
/**
@@ -224,33 +150,188 @@ public class AuditModelRegistryImpl implements AuditModelRegistry
*/
public void loadAuditModels()
{
checkProperties();
stop();
start();
}
RetryingTransactionCallback<Void> loadModelsCallback = new RetryingTransactionCallback<Void>()
/**
* Enables audit and registers an audit model at a given URL. Does not register across the cluster and should only
* be used for unit test purposes.
*
* @param auditModelUrl
* the source of the model
*/
public synchronized void registerModel(URL auditModelUrl)
{
stop();
setProperty(PROPERTY_AUDIT_ENABLED, "true");
getState(false).registerModel(auditModelUrl);
}
/**
* A class encapsulating the disposable/resettable state of the audit model registry.
*/
public class AuditModelRegistryState implements PropertyBackedBeanState
{
/** The audit models. */
private final Map<URL, Audit> auditModels;
/** Used to lookup path translations. */
private PathMapper auditPathMapper;
/** Used to lookup the audit application java hierarchy. */
private Map<String, AuditApplication> auditApplicationsByKey;
/** Used to lookup the audit application java hierarchy. */
private Map<String, AuditApplication> auditApplicationsByName;
/** The exposed configuration properties. */
private final Map<String, Boolean> properties;
/**
* Instantiates a new audit model registry state.
*/
public AuditModelRegistryState()
{
auditModels = new LinkedHashMap<URL, Audit>(7);
properties = new HashMap<String, Boolean>(7);
// Default value for global enabled property
properties.put(PROPERTY_AUDIT_ENABLED, false);
// Let's search for config files in the appropriate places. The individual applications they contain can still
// be enabled/disabled by the bean properties
ResourceFinder resourceFinder = new ResourceFinder(getParent());
try
{
for (Resource resource : resourceFinder.getResources(searchPath))
{
registerModel(resource.getURL());
}
}
catch (IOException e)
{
throw new AlfrescoRuntimeException("Failed to find audit resources", e);
}
}
/**
* Register an audit model at a given URL.
*
* @param auditModelUrl
* the source of the model
*/
public void registerModel(URL auditModelUrl)
{
try
{
if (auditModels.containsKey(auditModelUrl))
{
logger.warn("An audit model has already been registered at URL " + auditModelUrl);
}
Audit audit = AuditModelRegistryImpl.unmarshallModel(auditModelUrl);
// Store the model itself
auditModels.put(auditModelUrl, audit);
// Cache property enabling each application by default
List<Application> applications = audit.getApplication();
for (Application application : applications)
{
properties.put(getEnabledProperty(application.getKey()), true);
}
}
catch (Throwable e)
{
throw new AuditModelException("Failed to load audit model: " + auditModelUrl, e);
}
}
/**
* Gets the name of an enablement property.
*
* @param key
* an application key
* @return the property name
*/
private String getEnabledProperty(String key)
{
return "audit." + key.toLowerCase() + ".enabled";
}
/**
* Checks if an application enabled.
*
* @param key
* the application key
* @return true, if the application is enabled
*/
private boolean isApplicationEnabled(String key)
{
Boolean enabled = properties.get(getEnabledProperty(key));
return enabled != null && enabled;
}
/**
* @see org.alfresco.repo.management.subsystems.PropertyBackedBeanState#getProperty(java.lang.String)
*/
public String getProperty(String name)
{
return String.valueOf(properties.get(name));
}
/**
* @see org.alfresco.repo.management.subsystems.PropertyBackedBeanState#getPropertyNames()
*/
public Set<String> getPropertyNames()
{
return properties.keySet();
}
/**
* @see org.alfresco.repo.management.subsystems.PropertyBackedBeanState#setProperty(java.lang.String, java.lang.String)
*/
public void setProperty(String name, String value)
{
properties.put(name, Boolean.parseBoolean(value));
}
/**
* @see org.alfresco.repo.management.subsystems.PropertyBackedBeanState#start()
*/
public void start()
{
auditApplicationsByKey = new HashMap<String, AuditApplication>(7);
auditApplicationsByName = new HashMap<String, AuditApplication>(7);
auditPathMapper = new PathMapper();
// If we are globally disabled, skip processing the models
Boolean enabled = properties.get(PROPERTY_AUDIT_ENABLED);
if (enabled != null && enabled)
{
final RetryingTransactionCallback<Void> loadModelsCallback = new RetryingTransactionCallback<Void>()
{
public Void execute() throws Throwable
{
// Load models from the URLs
Set<URL> auditModelUrlsInner = new HashSet<URL>(auditModelUrls);
for (URL auditModelUrl : auditModelUrlsInner)
for (Map.Entry<URL, Audit> entry : auditModels.entrySet())
{
URL auditModelUrl = entry.getKey();
Audit audit = entry.getValue();
try
{
Audit audit = AuditModelRegistryImpl.unmarshallModel(auditModelUrl);
// That worked, so now get an input stream and write the model
// Get an input stream and write the model
Long auditModelId = auditDAO.getOrCreateAuditModel(auditModelUrl).getFirst();
// Now cache it (eagerly)
cacheAuditElements(auditModelId, audit);
}
catch (Throwable e)
{
// Mainly for test purposes, we clear out the failed URL
auditModelUrls.remove(auditModelUrl);
clearCaches();
throw new AuditModelException(
"Failed to load audit model: " + auditModelUrl,
e);
throw new AuditModelException("Failed to load audit model: " + auditModelUrl, e);
}
}
// NOTE: If we support other types of loading, then that will have to go here, too
@@ -260,55 +341,60 @@ public class AuditModelRegistryImpl implements AuditModelRegistry
}
};
writeLock.lock();
// Drop all cached data
clearCaches();
try
// Run as system user to avoid cyclical dependency in bootstrap read-only checking (and we are anyway!)
AuthenticationUtil.runAs(new RunAsWork<Void>()
{
public Void doWork() throws Exception
{
auditPathMapper = new PathMapper();
transactionService.getRetryingTransactionHelper().doInTransaction(loadModelsCallback,
transactionService.isReadOnly(), true);
auditPathMapper.lock();
}
finally
{
writeLock.unlock();
return null;
}
}, AuthenticationUtil.getSystemUserName());
}
auditPathMapper.lock();
}
/* (non-Javadoc)
* @see org.alfresco.repo.management.subsystems.PropertyBackedBeanState#stop()
*/
public void stop()
{
auditPathMapper = null;
}
/**
* Gets an audit application by key.
*
* @param key
* the application key
* @return the audit application
* @see org.alfresco.repo.audit.model.AuditModelRegistry#getAuditApplicationByKey(java.lang.String)
*/
public AuditApplication getAuditApplicationByKey(String key)
{
readLock.lock();
try
{
return auditApplicationsByKey.get(key);
}
finally
{
readLock.unlock();
}
}
/**
* Gets an audit application by name.
*
* @param applicationName
* the application name
* @return the audit application
* @see org.alfresco.repo.audit.model.AuditModelRegistry#getAuditApplicationByName(java.lang.String)
*/
public AuditApplication getAuditApplicationByName(String applicationName)
{
readLock.lock();
try
{
return auditApplicationsByName.get(applicationName);
}
finally
{
readLock.unlock();
}
}
/**
* Gets the audit path mapper.
*
* @return the audit path mapper
* @see org.alfresco.repo.audit.model.AuditModelRegistry#getAuditPathMapper()
*/
public PathMapper getAuditPathMapper()
@@ -317,89 +403,13 @@ public class AuditModelRegistryImpl implements AuditModelRegistry
}
/**
* Unmarshalls the Audit model from the URL.
* Caches audit elements from a model.
*
* @throws AlfrescoRuntimeException if an IOException occurs
* @param auditModelId
* the audit model id
* @param audit
* the audit model
*/
public static Audit unmarshallModel(URL configUrl)
{
try
{
// Load it
InputStream is = new BufferedInputStream(configUrl.openStream());
return unmarshallModel(is, configUrl.toString());
}
catch (IOException e)
{
throw new AlfrescoRuntimeException("The Audit model XML failed to load: " + configUrl, e);
}
}
/**
* Unmarshalls the Audit model from a stream
*/
private static Audit unmarshallModel(InputStream is, final String source)
{
final Schema schema;
final JAXBContext jaxbCtx;
final Unmarshaller jaxbUnmarshaller;
try
{
SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
schema = sf.newSchema(ResourceUtils.getURL(AUDIT_SCHEMA_LOCATION));
jaxbCtx = JAXBContext.newInstance("org.alfresco.repo.audit.model._3");
jaxbUnmarshaller = jaxbCtx.createUnmarshaller();
jaxbUnmarshaller.setSchema(schema);
jaxbUnmarshaller.setEventHandler(new ValidationEventHandler()
{
public boolean handleEvent(ValidationEvent ve)
{
if (ve.getSeverity() == ValidationEvent.FATAL_ERROR || ve.getSeverity() == ValidationEvent.ERROR)
{
ValidationEventLocator locator = ve.getLocator();
logger.error("Invalid Audit XML: \n" +
" Source: " + source + "\n" +
" Location: Line " + locator.getLineNumber() + " column " + locator.getColumnNumber() + "\n" +
" Error: " + ve.getMessage());
}
return false;
}
});
}
catch (Throwable e)
{
throw new AlfrescoRuntimeException(
"Failed to load Alfresco Audit Schema from " + AUDIT_SCHEMA_LOCATION, e);
}
try
{
// Unmarshall with validation
@SuppressWarnings("unchecked")
JAXBElement<Audit> auditElement = (JAXBElement<Audit>) jaxbUnmarshaller.unmarshal(is);
Audit audit = auditElement.getValue();
// Done
return audit;
}
catch (Throwable e)
{
// Dig out a SAXParseException, if there is one
Throwable saxError = ExceptionStackUtil.getCause(e, SAXParseException.class);
if (saxError != null)
{
e = saxError;
}
throw new AuditModelException(
"Failed to read Audit model XML: \n" +
" Source: " + source + "\n" +
" Error: " + e.getMessage());
}
finally
{
try { is.close(); } catch (IOException e) {}
}
}
private void cacheAuditElements(Long auditModelId, Audit audit)
{
Map<String, DataExtractor> dataExtractorsByName = new HashMap<String, DataExtractor>(13);
@@ -518,6 +528,11 @@ public class AuditModelRegistryImpl implements AuditModelRegistry
for (Application application : applications)
{
String key = application.getKey();
if (!isApplicationEnabled(key))
{
continue;
}
if (auditApplicationsByKey.containsKey(key))
{
throw new AuditModelException(
@@ -552,14 +567,16 @@ public class AuditModelRegistryImpl implements AuditModelRegistry
auditApplicationsByName.put(name, wrapperApp);
auditApplicationsByKey.put(key, wrapperApp);
}
// Pull out all the audit path maps
buildAuditPathMap(audit);
// Store the model itself
auditModels.add(audit);
}
/**
* Construct the reverse lookup maps for quick conversion of data to target maps
* Construct the reverse lookup maps for quick conversion of data to target maps.
*
* @param audit
* the audit model
*/
private void buildAuditPathMap(Audit audit)
{
@@ -572,7 +589,185 @@ public class AuditModelRegistryImpl implements AuditModelRegistry
{
String sourcePath = pathMap.getSource();
String targetPath = pathMap.getTarget();
// Extract the application key from the root of the path
int keyStart = targetPath.charAt(0) == '/' ? 1 : 0;
int keyEnd = targetPath.indexOf('/', keyStart);
String key = keyEnd == -1 ? targetPath.substring(keyStart) : targetPath.substring(keyStart, keyEnd);
// Only add the path if the application is not disabled
if (isApplicationEnabled(key))
{
auditPathMapper.addPathMap(sourcePath, targetPath);
}
}
}
}
/**
* @see org.alfresco.repo.management.subsystems.AbstractPropertyBackedBean#createInitialState()
*/
@Override
protected PropertyBackedBeanState createInitialState() throws IOException
{
return new AuditModelRegistryState();
}
/**
* Default constructor.
*/
public AuditModelRegistryImpl()
{
objectFactory = new ObjectFactory();
}
/**
* Sets the search path for config files.
*
* @param searchPath
* the search path
*/
public void setSearchPath(String[] searchPath)
{
this.searchPath = searchPath;
}
/**
* Service to ensure DAO calls are transactionally wrapped.
*
* @param transactionService
* the transaction service
*/
public void setTransactionService(TransactionService transactionService)
{
this.transactionService = transactionService;
}
/**
* Set the DAO used to persisted the registered audit models.
*
* @param auditDAO
* the audit dao
*/
public void setAuditDAO(AuditDAO auditDAO)
{
this.auditDAO = auditDAO;
}
/**
* Set the registry of {@link DataExtractor data extractors}.
*
* @param dataExtractors
* the data extractors
*/
public void setDataExtractors(NamedObjectRegistry<DataExtractor> dataExtractors)
{
this.dataExtractors = dataExtractors;
}
/**
* Set the registry of {@link DataGenerator data generators}.
*
* @param dataGenerators
* the data generators
*/
public void setDataGenerators(NamedObjectRegistry<DataGenerator> dataGenerators)
{
this.dataGenerators = dataGenerators;
}
/**
* Unmarshalls the Audit model from the URL.
*
* @param configUrl
* the config url
* @return the audit model
* @throws AlfrescoRuntimeException
* if an IOException occurs
*/
public static Audit unmarshallModel(URL configUrl)
{
try
{
// Load it
InputStream is = new BufferedInputStream(configUrl.openStream());
return unmarshallModel(is, configUrl.toString());
}
catch (IOException e)
{
throw new AlfrescoRuntimeException("The Audit model XML failed to load: " + configUrl, e);
}
}
/**
* Unmarshalls the Audit model from a stream.
*
* @param is
* the is
* @param source
* the source
* @return the audit model
*/
private static Audit unmarshallModel(InputStream is, final String source)
{
final Schema schema;
final JAXBContext jaxbCtx;
final Unmarshaller jaxbUnmarshaller;
try
{
SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
schema = sf.newSchema(ResourceUtils.getURL(AUDIT_SCHEMA_LOCATION));
jaxbCtx = JAXBContext.newInstance("org.alfresco.repo.audit.model._3");
jaxbUnmarshaller = jaxbCtx.createUnmarshaller();
jaxbUnmarshaller.setSchema(schema);
jaxbUnmarshaller.setEventHandler(new ValidationEventHandler()
{
public boolean handleEvent(ValidationEvent ve)
{
if (ve.getSeverity() == ValidationEvent.FATAL_ERROR || ve.getSeverity() == ValidationEvent.ERROR)
{
ValidationEventLocator locator = ve.getLocator();
logger.error("Invalid Audit XML: \n" +
" Source: " + source + "\n" +
" Location: Line " + locator.getLineNumber() + " column " + locator.getColumnNumber() + "\n" +
" Error: " + ve.getMessage());
}
return false;
}
});
}
catch (Throwable e)
{
throw new AlfrescoRuntimeException(
"Failed to load Alfresco Audit Schema from " + AUDIT_SCHEMA_LOCATION, e);
}
try
{
// Unmarshall with validation
@SuppressWarnings("unchecked")
JAXBElement<Audit> auditElement = (JAXBElement<Audit>) jaxbUnmarshaller.unmarshal(is);
Audit audit = auditElement.getValue();
// Done
return audit;
}
catch (Throwable e)
{
// Dig out a SAXParseException, if there is one
Throwable saxError = ExceptionStackUtil.getCause(e, SAXParseException.class);
if (saxError != null)
{
e = saxError;
}
throw new AuditModelException(
"Failed to read Audit model XML: \n" +
" Source: " + source + "\n" +
" Error: " + e.getMessage());
}
finally
{
try { is.close(); } catch (IOException e) {}
}
}
}

View File

@@ -66,7 +66,7 @@ public class ResourceFinder extends ServletContextResourcePatternResolver
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public Resource[] getResources(String[] locationPatterns) throws IOException
public Resource[] getResources(String... locationPatterns) throws IOException
{
List<Resource> resources = new LinkedList<Resource>();
for (String locationPattern : locationPatterns)