Audit DAO

- Audit config (alf_audit_cfg) now uses CRC check and persists via ContentService and alf_content_data
 - Reverted 3.3 references back to 3.2


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@15805 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2009-08-19 02:30:42 +00:00
parent cd850a3d28
commit 528df9ef5f
28 changed files with 639 additions and 29 deletions

View File

@@ -32,7 +32,7 @@ import org.alfresco.util.Pair;
* Helper class to calculate CRC values for string persistence.
*
* @author Derek Hulley
* @since 3.3
* @since 3.2
*/
public class CrcHelper
{

View File

@@ -0,0 +1,150 @@
/*
* 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.IOException;
import java.io.InputStream;
import java.net.URL;
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.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.util.Pair;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Abstract helper DAO for <b>alf_audit_XXX</b> tables.
*
* @author Derek Hulley
* @since 3.2
*/
public abstract class AbstractAuditDAOImpl implements AuditDAO
{
private static final Log logger = LogFactory.getLog(AbstractAuditDAOImpl.class);
private ContentService contentService;
private ContentDataDAO contentDataDAO;
public void setContentService(ContentService contentService)
{
this.contentService = contentService;
}
public void setContentDataDAO(ContentDataDAO contentDataDAO)
{
this.contentDataDAO = contentDataDAO;
}
/**
* {@inheritDoc}
*/
public Pair<Long, ContentData> getOrCreateAuditConfig(URL url)
{
InputStream is = null;
try
{
is = url.openStream();
// Calculate the CRC and find an entry that matches
CRC32 crcCalc = new CRC32();
byte[] buffer = new byte[1024];
int read = -1;
do
{
read = is.read(buffer);
if (read < 0)
{
break;
}
crcCalc.update(buffer, 0, read);
}
while (true);
long crc = crcCalc.getValue();
// Find an existing entry
AuditConfigEntity existingEntity = getAuditConfigByCrc(crc);
if (existingEntity != null)
{
Long existingEntityId = existingEntity.getId();
// Locate the content
ContentData existingContentData = contentDataDAO.getContentData(
existingEntity.getContentDataId()
).getSecond();
Pair<Long, ContentData> result = new Pair<Long, ContentData>(existingEntityId, existingContentData);
// Done
if (logger.isDebugEnabled())
{
logger.debug(
"Found existing configuration with same CRC: \n" +
" URL: " + url + "\n" +
" CRC: " + crc + "\n" +
" Result: " + result);
}
return result;
}
else
{
// Upload the content afresh
is.close();
is = url.openStream();
ContentWriter writer = contentService.getWriter(null, null, false);
writer.setEncoding("UTF-8");
writer.setMimetype(MimetypeMap.MIMETYPE_XML);
writer.putContent(is);
ContentData newContentData = writer.getContentData();
Long newContentDataId = contentDataDAO.createContentData(newContentData).getFirst();
AuditConfigEntity newEntity = createAuditConfig(newContentDataId, crc);
Pair<Long, ContentData> result = new Pair<Long, ContentData>(newEntity.getId(), newContentData);
// Done
if (logger.isDebugEnabled())
{
logger.debug(
"Created new audit config: \n" +
" URL: " + url + "\n" +
" CRC: " + crc + "\n" +
" Result: " + result);
}
return result;
}
}
catch (IOException e)
{
throw new AlfrescoRuntimeException("Failed to read Audit configuration: " + url);
}
finally
{
if (is != null)
{
try { is.close(); } catch (Throwable e) {}
}
}
}
protected abstract AuditConfigEntity getAuditConfigByCrc(long crc);
protected abstract AuditConfigEntity createAuditConfig(Long contentDataId, long crc);
}

View File

@@ -0,0 +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;
}
}

View File

@@ -0,0 +1,48 @@
/*
* 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.net.URL;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.util.Pair;
/**
* DAO services for <b>alf_audit_XXX</b> tables.
*
* @author Derek Hulley
* @since 3.2
*/
public interface AuditDAO
{
/**
* Creates a new audit config 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);
}

View File

@@ -0,0 +1,87 @@
/*
* 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);
}
}

View File

@@ -0,0 +1,71 @@
/*
* 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.ibatis;
import org.alfresco.repo.domain.audit.AbstractAuditDAOImpl;
import org.alfresco.repo.domain.audit.AuditConfigEntity;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
/**
* iBatis-specific implementation of the DAO for <b>alf_audit_XXX</b> tables.
*
* @author Derek Hulley
* @since 3.2
*/
public class AuditDAOImpl extends AbstractAuditDAOImpl
{
private static final String SELECT_CONFIG_BY_CRC = "select.AuditConfigByCrc";
private static final String INSERT_CONFIG = "insert.AuditConfig";
private SqlMapClientTemplate template;
public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate)
{
this.template = sqlMapClientTemplate;
}
@Override
protected AuditConfigEntity getAuditConfigByCrc(long crc)
{
AuditConfigEntity entity = new AuditConfigEntity();
entity.setContentCrc(crc);
entity = (AuditConfigEntity) template.queryForObject(
SELECT_CONFIG_BY_CRC,
entity);
// Done
return entity;
}
@Override
protected AuditConfigEntity createAuditConfig(Long contentDataId, long crc)
{
AuditConfigEntity entity = new AuditConfigEntity();
entity.setContentDataId(contentDataId);
entity.setContentCrc(crc);
Long id = (Long) template.insert(INSERT_CONFIG, entity);
entity.setId(id);
return entity;
}
}

View File

@@ -52,7 +52,7 @@ import org.apache.commons.logging.LogFactory;
* for CRUD operations.
*
* @author Derek Hulley
* @since 3.3
* @since 3.2
*/
public abstract class AbstractPropertyValueDAOImpl implements PropertyValueDAO
{

View File

@@ -33,7 +33,7 @@ import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
* the {@link DefaultTypeConverter} is used.
*
* @author Derek Hulley
* @since 3.3
* @since 3.2
*/
public class DefaultPropertyTypeConverter implements PropertyTypeConverter
{

View File

@@ -33,7 +33,7 @@ import org.alfresco.util.Pair;
* Entity bean for <b>alf_prop_class</b> table.
*
* @author Derek Hulley
* @since 3.3
* @since 3.2
*/
public class PropertyClassEntity
{

View File

@@ -36,7 +36,7 @@ import org.alfresco.util.Pair;
* Entity bean for <b>alf_prop_date_value</b> table.
*
* @author Derek Hulley
* @since 3.3
* @since 3.2
*/
public class PropertyDateValueEntity
{

View File

@@ -31,7 +31,7 @@ import org.alfresco.util.Pair;
* Entity bean for <b>alf_prop_numeric_value</b> table.
*
* @author Derek Hulley
* @since 3.3
* @since 3.2
*/
public class PropertyDoubleValueEntity
{

View File

@@ -30,7 +30,7 @@ import java.io.Serializable;
* Entity bean search results from <b>alf_prop_collections_link</b> and <b>alf_prop_value</b> tables.
*
* @author Derek Hulley
* @since 3.3
* @since 3.2
*/
public class PropertyIdSearchRow
{

View File

@@ -28,7 +28,7 @@ package org.alfresco.repo.domain.propval;
* Entity bean for <b>alf_prop_link</b> table.
*
* @author Derek Hulley
* @since 3.3
* @since 3.2
*/
public class PropertyLinkEntity
{

View File

@@ -31,7 +31,7 @@ import org.alfresco.util.Pair;
* Entity bean for <b>alf_prop_string_value</b> table.
*
* @author Derek Hulley
* @since 3.3
* @since 3.2
*/
public class PropertyStringValueEntity
{

View File

@@ -31,7 +31,7 @@ package org.alfresco.repo.domain.propval;
* and back again.
*
* @author Derek Hulley
* @since 3.3
* @since 3.2
*/
public interface PropertyTypeConverter
{

View File

@@ -33,7 +33,7 @@ import org.alfresco.util.Pair;
* DAO services for <b>alf_prop_XXX</b> tables.
*
* @author Derek Hulley
* @since 3.3
* @since 3.2
*/
public interface PropertyValueDAO
{

View File

@@ -46,7 +46,7 @@ import org.springframework.context.ApplicationContext;
* @see PropertyValueDAO
*
* @author Derek Hulley
* @since 3.3
* @since 3.2
*/
public class PropertyValueDAOTest extends TestCase
{

View File

@@ -44,7 +44,7 @@ import org.apache.commons.logging.LogFactory;
* or will be references to data in other tables.
*
* @author Derek Hulley
* @since 3.3
* @since 3.2
*/
public class PropertyValueEntity
{
@@ -65,7 +65,7 @@ public class PropertyValueEntity
* and should not be used in public interfaces.
*
* @author Derek Hulley
* @since 3.3
* @since 3.2
*/
public static enum PersistedType
{

View File

@@ -44,7 +44,7 @@ import org.springframework.orm.ibatis.SqlMapClientTemplate;
* iBatis-specific implementation of the PropertyValue DAO.
*
* @author Derek Hulley
* @since 3.3
* @since 3.2
*/
public class PropertyValueDAOImpl extends AbstractPropertyValueDAOImpl
{