Audit session and bootstrap support

- Sessions are created using an application name (shared prop) and a persisted model ID
 - Added a bootstrap bean for audit that unmarshalls the models
 - Added hook points for repo-loading models, but won't implement yet


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@15863 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2009-08-21 17:50:36 +00:00
parent 99395b7ed2
commit 00dfb8ee66
19 changed files with 877 additions and 280 deletions

View File

@@ -32,6 +32,7 @@ import java.util.zip.CRC32;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.domain.contentdata.ContentDataDAO;
import org.alfresco.repo.domain.propval.PropertyValueDAO;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
@@ -51,6 +52,7 @@ public abstract class AbstractAuditDAOImpl implements AuditDAO
private ContentService contentService;
private ContentDataDAO contentDataDAO;
private PropertyValueDAO propertyValueDAO;
public void setContentService(ContentService contentService)
{
@@ -61,11 +63,21 @@ public abstract class AbstractAuditDAOImpl implements AuditDAO
{
this.contentDataDAO = contentDataDAO;
}
public void setPropertyValueDAO(PropertyValueDAO propertyValueDAO)
{
this.propertyValueDAO = propertyValueDAO;
}
/*
* alf_audit_model
*/
/**
* {@inheritDoc}
*/
public Pair<Long, ContentData> getOrCreateAuditConfig(URL url)
public Pair<Long, ContentData> getOrCreateAuditModel(URL url)
{
InputStream is = null;
try
@@ -87,7 +99,7 @@ public abstract class AbstractAuditDAOImpl implements AuditDAO
while (true);
long crc = crcCalc.getValue();
// Find an existing entry
AuditConfigEntity existingEntity = getAuditConfigByCrc(crc);
AuditModelEntity existingEntity = getAuditModelByCrc(crc);
if (existingEntity != null)
{
Long existingEntityId = existingEntity.getId();
@@ -100,7 +112,7 @@ public abstract class AbstractAuditDAOImpl implements AuditDAO
if (logger.isDebugEnabled())
{
logger.debug(
"Found existing configuration with same CRC: \n" +
"Found existing model with same CRC: \n" +
" URL: " + url + "\n" +
" CRC: " + crc + "\n" +
" Result: " + result);
@@ -118,13 +130,13 @@ public abstract class AbstractAuditDAOImpl implements AuditDAO
writer.putContent(is);
ContentData newContentData = writer.getContentData();
Long newContentDataId = contentDataDAO.createContentData(newContentData).getFirst();
AuditConfigEntity newEntity = createAuditConfig(newContentDataId, crc);
AuditModelEntity newEntity = createAuditModel(newContentDataId, crc);
Pair<Long, ContentData> result = new Pair<Long, ContentData>(newEntity.getId(), newContentData);
// Done
if (logger.isDebugEnabled())
{
logger.debug(
"Created new audit config: \n" +
"Created new audit model: \n" +
" URL: " + url + "\n" +
" CRC: " + crc + "\n" +
" Result: " + result);
@@ -134,7 +146,7 @@ public abstract class AbstractAuditDAOImpl implements AuditDAO
}
catch (IOException e)
{
throw new AlfrescoRuntimeException("Failed to read Audit configuration: " + url);
throw new AlfrescoRuntimeException("Failed to read Audit model: " + url);
}
finally
{
@@ -145,6 +157,22 @@ public abstract class AbstractAuditDAOImpl implements AuditDAO
}
}
protected abstract AuditConfigEntity getAuditConfigByCrc(long crc);
protected abstract AuditConfigEntity createAuditConfig(Long contentDataId, long crc);
protected abstract AuditModelEntity getAuditModelByCrc(long crc);
protected abstract AuditModelEntity createAuditModel(Long contentDataId, long crc);
/*
* alf_audit_model
*/
public Long createAuditSession(Long modelId, String application)
{
// Persist the string
Long appNameId = propertyValueDAO.getOrCreatePropertyValue(application).getFirst();
// Create the audit session
AuditSessionEntity entity = createAuditSession(appNameId, modelId);
// Done
return entity.getId();
}
protected abstract AuditSessionEntity createAuditSession(Long appNameId, Long modelId);
}

View File

@@ -38,11 +38,13 @@ import org.alfresco.util.Pair;
public interface AuditDAO
{
/**
* Creates a new audit config entry or finds an existing one
* Creates a new audit model entry or finds an existing one
*
* @param the URL of the configuration
* @return Returns the ID of the config matching the input stream and the
* content storage details
*/
Pair<Long, ContentData> getOrCreateAuditConfig(URL url);
Pair<Long, ContentData> getOrCreateAuditModel(URL url);
Long createAuditSession(Long modelId, String application);
}

View File

@@ -1,87 +1,122 @@
/*
* 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.domain.audit;
import java.io.File;
import java.net.URL;
import junit.framework.TestCase;
import org.alfresco.repo.content.transform.AbstractContentTransformerTest;
import org.alfresco.repo.domain.contentdata.ContentDataDAO;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.alfresco.util.Pair;
import org.springframework.context.ConfigurableApplicationContext;
/**
* @see ContentDataDAO
*
* @author Derek Hulley
* @since 3.2
*/
public class AuditDAOTest extends TestCase
{
private ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) ApplicationContextHelper.getApplicationContext();
private TransactionService transactionService;
private RetryingTransactionHelper txnHelper;
private AuditDAO auditDAO;
@Override
public void setUp() throws Exception
{
ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
transactionService = serviceRegistry.getTransactionService();
txnHelper = transactionService.getRetryingTransactionHelper();
auditDAO = (AuditDAO) ctx.getBean("auditDAO");
}
public void testAuditConfig() throws Exception
{
final File file = AbstractContentTransformerTest.loadQuickTestFile("pdf");
assertNotNull(file);
final URL url = new URL("file:" + file.getAbsolutePath());
RetryingTransactionCallback<Pair<Long, ContentData>> callback = new RetryingTransactionCallback<Pair<Long, ContentData>>()
{
public Pair<Long, ContentData> execute() throws Throwable
{
Pair<Long, ContentData> contentDataPair = auditDAO.getOrCreateAuditConfig(url);
return contentDataPair;
}
};
Pair<Long, ContentData> configPair = txnHelper.doInTransaction(callback);
assertNotNull(configPair);
// Now repeat. The results should be exactly the same.
Pair<Long, ContentData> configPairCheck = txnHelper.doInTransaction(callback);
assertNotNull(configPairCheck);
assertEquals(configPair, configPairCheck);
}
}
/*
* 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.domain.audit;
import java.io.File;
import java.net.URL;
import junit.framework.TestCase;
import org.alfresco.repo.content.transform.AbstractContentTransformerTest;
import org.alfresco.repo.domain.contentdata.ContentDataDAO;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.alfresco.util.Pair;
import org.springframework.context.ConfigurableApplicationContext;
/**
* @see ContentDataDAO
*
* @author Derek Hulley
* @since 3.2
*/
public class AuditDAOTest extends TestCase
{
private ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) ApplicationContextHelper.getApplicationContext();
private TransactionService transactionService;
private RetryingTransactionHelper txnHelper;
private AuditDAO auditDAO;
@Override
public void setUp() throws Exception
{
ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
transactionService = serviceRegistry.getTransactionService();
txnHelper = transactionService.getRetryingTransactionHelper();
auditDAO = (AuditDAO) ctx.getBean("auditDAO");
}
public void testAuditModel() throws Exception
{
final File file = AbstractContentTransformerTest.loadQuickTestFile("pdf");
assertNotNull(file);
final URL url = new URL("file:" + file.getAbsolutePath());
RetryingTransactionCallback<Pair<Long, ContentData>> callback = new RetryingTransactionCallback<Pair<Long, ContentData>>()
{
public Pair<Long, ContentData> execute() throws Throwable
{
Pair<Long, ContentData> auditModelPair = auditDAO.getOrCreateAuditModel(url);
return auditModelPair;
}
};
Pair<Long, ContentData> configPair = txnHelper.doInTransaction(callback);
assertNotNull(configPair);
// Now repeat. The results should be exactly the same.
Pair<Long, ContentData> configPairCheck = txnHelper.doInTransaction(callback);
assertNotNull(configPairCheck);
assertEquals(configPair, configPairCheck);
}
public void testAuditSession() throws Exception
{
final File file = AbstractContentTransformerTest.loadQuickTestFile("pdf");
assertNotNull(file);
final URL url = new URL("file:" + file.getAbsolutePath());
RetryingTransactionCallback<Long> createModelCallback = new RetryingTransactionCallback<Long>()
{
public Long execute() throws Throwable
{
return auditDAO.getOrCreateAuditModel(url).getFirst();
}
};
final Long modelId = txnHelper.doInTransaction(createModelCallback);
final String appName = getName() + "." + System.currentTimeMillis();
final int count = 1000;
RetryingTransactionCallback<Void> createSessionCallback = new RetryingTransactionCallback<Void>()
{
public Void execute() throws Throwable
{
for (int i = 0; i < count; i++)
{
auditDAO.createAuditSession(modelId, appName);
}
return null;
}
};
long before = System.nanoTime();
txnHelper.doInTransaction(createSessionCallback);
long after = System.nanoTime();
System.out.println(
"Time for " + count + " session creations was " +
((double)(after - before)/(10E6)) + "ms");
}
}

View File

@@ -1,110 +1,110 @@
/*
* 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.domain.audit;
import org.alfresco.util.EqualsHelper;
/**
* Entity bean for <b>alf_audit_cfg</b> table.
*
* @author Derek Hulley
* @since 3.2
*/
public class AuditConfigEntity
{
private Long id;
private Long contentDataId;
private long contentCrc;
public AuditConfigEntity()
{
}
@Override
public int hashCode()
{
return (int) contentCrc;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
else if (obj instanceof AuditConfigEntity)
{
AuditConfigEntity that = (AuditConfigEntity) obj;
return EqualsHelper.nullSafeEquals(this.id, that.id);
}
else
{
return false;
}
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder(512);
sb.append("AuditConfigEntity")
.append("[ ID=").append(id)
.append(", contentDataId=").append(contentDataId)
.append(", contentCrc=").append(contentCrc)
.append("]");
return sb.toString();
}
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public Long getContentDataId()
{
return contentDataId;
}
public void setContentDataId(Long contentDataId)
{
this.contentDataId = contentDataId;
}
public long getContentCrc()
{
return contentCrc;
}
public void setContentCrc(long contentCrc)
{
this.contentCrc = contentCrc;
}
}
/*
* 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.domain.audit;
import org.alfresco.util.EqualsHelper;
/**
* Entity bean for <b>alf_audit_model</b> table.
*
* @author Derek Hulley
* @since 3.2
*/
public class AuditModelEntity
{
private Long id;
private Long contentDataId;
private long contentCrc;
public AuditModelEntity()
{
}
@Override
public int hashCode()
{
return (int) contentCrc;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
else if (obj instanceof AuditModelEntity)
{
AuditModelEntity that = (AuditModelEntity) obj;
return EqualsHelper.nullSafeEquals(this.id, that.id);
}
else
{
return false;
}
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder(512);
sb.append("AuditModelEntity")
.append("[ ID=").append(id)
.append(", contentDataId=").append(contentDataId)
.append(", contentCrc=").append(contentCrc)
.append("]");
return sb.toString();
}
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public Long getContentDataId()
{
return contentDataId;
}
public void setContentDataId(Long contentDataId)
{
this.contentDataId = contentDataId;
}
public long getContentCrc()
{
return contentCrc;
}
public void setContentCrc(long contentCrc)
{
this.contentCrc = contentCrc;
}
}

View File

@@ -0,0 +1,84 @@
/*
* 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.domain.audit;
/**
* Entity bean for <b>alf_audit_session</b> table.
*
* @author Derek Hulley
* @since 3.2
*/
public class AuditSessionEntity
{
private Long id;
private Long applicationNameId;
private Long auditModelId;
public AuditSessionEntity()
{
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder(512);
sb.append("AuditSessionEntity")
.append("[ ID=").append(id)
.append(", applicationNameId=").append(applicationNameId)
.append(", auditModelId=").append(auditModelId)
.append("]");
return sb.toString();
}
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public Long getAuditModelId()
{
return auditModelId;
}
public void setAuditModelId(Long auditModelId)
{
this.auditModelId = auditModelId;
}
public Long getApplicationNameId()
{
return applicationNameId;
}
public void setApplicationNameId(Long applicationNameId)
{
this.applicationNameId = applicationNameId;
}
}

View File

@@ -25,7 +25,8 @@
package org.alfresco.repo.domain.audit.ibatis;
import org.alfresco.repo.domain.audit.AbstractAuditDAOImpl;
import org.alfresco.repo.domain.audit.AuditConfigEntity;
import org.alfresco.repo.domain.audit.AuditModelEntity;
import org.alfresco.repo.domain.audit.AuditSessionEntity;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
/**
@@ -36,8 +37,10 @@ import org.springframework.orm.ibatis.SqlMapClientTemplate;
*/
public class AuditDAOImpl extends AbstractAuditDAOImpl
{
private static final String SELECT_CONFIG_BY_CRC = "select.AuditConfigByCrc";
private static final String INSERT_CONFIG = "insert.AuditConfig";
private static final String SELECT_MODEL_BY_CRC = "select.AuditModelByCrc";
private static final String INSERT_MODEL = "insert.AuditModel";
private static final String INSERT_SESSION = "insert.AuditSession";
private SqlMapClientTemplate template;
@@ -47,24 +50,35 @@ public class AuditDAOImpl extends AbstractAuditDAOImpl
}
@Override
protected AuditConfigEntity getAuditConfigByCrc(long crc)
protected AuditModelEntity getAuditModelByCrc(long crc)
{
AuditConfigEntity entity = new AuditConfigEntity();
AuditModelEntity entity = new AuditModelEntity();
entity.setContentCrc(crc);
entity = (AuditConfigEntity) template.queryForObject(
SELECT_CONFIG_BY_CRC,
entity = (AuditModelEntity) template.queryForObject(
SELECT_MODEL_BY_CRC,
entity);
// Done
return entity;
}
@Override
protected AuditConfigEntity createAuditConfig(Long contentDataId, long crc)
protected AuditModelEntity createAuditModel(Long contentDataId, long crc)
{
AuditConfigEntity entity = new AuditConfigEntity();
AuditModelEntity entity = new AuditModelEntity();
entity.setContentDataId(contentDataId);
entity.setContentCrc(crc);
Long id = (Long) template.insert(INSERT_CONFIG, entity);
Long id = (Long) template.insert(INSERT_MODEL, entity);
entity.setId(id);
return entity;
}
@Override
protected AuditSessionEntity createAuditSession(Long appNameId, Long modelId)
{
AuditSessionEntity entity = new AuditSessionEntity();
entity.setApplicationNameId(appNameId);
entity.setAuditModelId(modelId);
Long id = (Long) template.insert(INSERT_SESSION, entity);
entity.setId(id);
return entity;
}