#405 - ENH: Add @ReadAudit and associated functionality for auditing reads including queries and L2 cache access

This commit is contained in:
Robin Bygrave committed 2015-09-03 13:54:52 +12:00
1 parent c292da736b
commit 54172aec6c
54 files changed
+2020 -200

No files matched your search

@@ -19,6 +19,9 @@ import com.avaje.ebean.event.BeanQueryAdapter;
import com.avaje.ebean.event.changelog.BeanChange;
import com.avaje.ebean.event.changelog.ChangeLogFilter;
import com.avaje.ebean.event.changelog.ChangeType;
import com.avaje.ebean.event.readaudit.ReadAuditLogger;
import com.avaje.ebean.event.readaudit.ReadAuditPrepare;
import com.avaje.ebean.event.readaudit.ReadEvent;
import com.avaje.ebean.meta.MetaBeanInfo;
import com.avaje.ebean.meta.MetaQueryPlanStatistic;
import com.avaje.ebean.plugin.SpiBeanType;
@@ -31,7 +34,7 @@ import com.avaje.ebeaninternal.api.TransactionEventTable.TableIUD;
import com.avaje.ebeaninternal.server.cache.CachedBeanData;
import com.avaje.ebeaninternal.server.core.CacheOptions;
import com.avaje.ebeaninternal.server.core.DefaultSqlUpdate;
import com.avaje.ebeaninternal.server.core.DiffHelpInsert;
import com.avaje.ebeaninternal.server.core.DiffHelpUpdate;
import com.avaje.ebeaninternal.server.core.InternString;
import com.avaje.ebeaninternal.server.core.PersistRequest;
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
@@ -143,6 +146,11 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
private final String baseTableVersionsBetween;
private final boolean historySupport;
/**
* Set to true if read auditing is on for this bean type.
*/
private final boolean readAuditing;
/**
* Map of BeanProperty Linked so as to preserve order.
*/
@@ -362,6 +370,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
this.updateChangesOnly = deploy.isUpdateChangesOnly();
this.compoundUniqueConstraints = deploy.getCompoundUniqueConstraints();
this.readAuditing = deploy.isReadAuditing();
this.historySupport = deploy.isHistorySupport();
this.baseTable = InternString.intern(deploy.getBaseTable());
this.baseTableAsOf = deploy.getBaseTableAsOf();
@@ -608,6 +617,20 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
}
}
/**
* Return the ReadAuditLogger for logging read audit events.
*/
public ReadAuditLogger getReadAuditLogger() {
return ebeanServer.getReadAuditLogger();
}
/**
* Return the ReadAuditPrepare for preparing read audit events prior to logging.
*/
public ReadAuditPrepare getReadAuditPrepare() {
return ebeanServer.getReadAuditPrepare();
}
/**
* Return true if this request should be included in the change log.
*/
@@ -641,27 +664,20 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
* Return the bean change for an update.
*/
private BeanChange updateBeanChange(PersistRequestBean<T> request) {
return newBeanChange(request.getBeanId(), ChangeType.UPDATE, request.getEntityBeanIntercept().getDirtyValues());
return newBeanChange(request.getBeanId(), ChangeType.UPDATE, diffFlatten(request.getEntityBeanIntercept().getDirtyValues()));
}
/**
* Return the bean change for an insert.
*/
private BeanChange insertBeanChange(PersistRequestBean<T> request) {
return newBeanChange(request.getBeanId(), ChangeType.INSERT, insertDiff(request.getEntityBean()));
return newBeanChange(request.getBeanId(), ChangeType.INSERT, diffForInsert(request.getEntityBean()));
}
private BeanChange newBeanChange(Object id, ChangeType changeType, Map<String, ValuePair> values) {
return new BeanChange(getBaseTable(), id, changeType, values);
}
/**
* For insert we create a Map of ValuePair to have the same structure as update.
*/
private Map<String, ValuePair> insertDiff(EntityBean entityBean) {
return DiffHelpInsert.diff(entityBean, this);
}
/**
* Initialise the cache once the server has started.
*/
@@ -947,10 +963,55 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
public void cacheHandleUpdate(Object id, PersistRequestBean<T> updateRequest) {
cacheHelp.handleUpdate(id, updateRequest);
}
/**
* Return the base table alias. This is always the first letter of the bean
* name.
* Prepare the read audit of a findFutureList() query.
*/
public void readAuditFutureList(SpiQuery<T> spiQuery) {
if (isReadAuditing()) {
ReadEvent event = new ReadEvent(fullName);
// prepare in the foreground thread while we have the user context
// information (query is processed/executed later in bg thread)
readAuditPrepare(event);
spiQuery.setFutureFetchAudit(event);
}
}
/**
* Write a bean read to the read audit log.
*/
public void readAuditBean(String queryKey, String bindLog, Object bean) {
ReadEvent event = new ReadEvent(fullName, queryKey, bindLog, getIdForJson(bean));
readAuditPrepare(event);
getReadAuditLogger().auditBean(event);
}
private void readAuditPrepare(ReadEvent event) {
ReadAuditPrepare prepare = getReadAuditPrepare();
if (prepare != null) {
prepare.prepare(event);
}
}
/**
* Write a many bean read to the read audit log.
*/
public void readAuditMany(String queryKey, String bindLog, List<Object> ids) {
ReadEvent event = new ReadEvent(fullName, queryKey, bindLog, ids);
readAuditPrepare(event);
getReadAuditLogger().auditMany(event);
}
/**
* Write a futureList many read to the read audit log.
*/
public void readAuditFutureMany(ReadEvent event) {
// this has already been prepared (in foreground thread)
getReadAuditLogger().auditMany(event);
}
/**
* Return the base table alias. This is always the first letter of the bean name.
*/
public String getBaseTableAlias() {
return baseTableAlias;
@@ -1208,6 +1269,9 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
if (d != null) {
Object shareableBean = d.getSharableBean();
if (shareableBean != null) {
if (isReadAuditing()) {
readAuditBean("ref", "", shareableBean);
}
return (T) shareableBean;
}
}
@@ -1362,6 +1426,26 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
return getId((EntityBean) bean);
}
/**
* Return the Id value for the bean with embeddedId beans converted into maps.
* <p>
* The usage is to provide simple id types for JSON processing (for embeddedId's).
* </p>
*/
public Object getIdForJson(Object bean) {
return idBinder.getIdForJson((EntityBean) bean);
}
/**
* Convert the idValue assuming embeddedId values are Maps.
* <p>
* The usage is to provide simple id types for JSON processing (for embeddedId's).
* </p>
*/
public Object convertIdFromJson(Object idValue) {
return idBinder.convertIdFromJson(idValue);
}
/**
* Return the default order by that may need to be added if a many property is
* included in the query.
@@ -1374,7 +1458,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
* Convert the type of the idValue if required.
*/
public Object convertId(Object idValue) {
return idBinder.convertSetId(idValue, null);
return idBinder.convertId(idValue);
}
/**
@@ -1777,6 +1861,13 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
}
}
/**
* Return true if read auditing is on this entity bean.
*/
public boolean isReadAuditing() {
return readAuditing;
}
/**
* Return true if this entity bean has history support.
*/
@@ -1943,6 +2034,61 @@ public class BeanDescriptor<T> implements MetaBeanInfo, SpiBeanType<T> {
}
}
/**
* Flatten the diff that comes from the entity bean intercept.
*/
Map<String, ValuePair> diffFlatten(Map<String, ValuePair> diff) {
return DiffHelpUpdate.flatten(diff, this);
}
/**
* Return a map of the differences between a and b.
* <p>
* A and B must be of the same type. B can be null, in which case the 'dirty
* diff' of a is returned.
* </p>
* <p>
* This intentionally does not include as OneToMany or ManyToMany properties.
* </p>
*/
public Map<String, ValuePair> diffForInsert(EntityBean newBean) {
Map<String, ValuePair> map = new LinkedHashMap<String, ValuePair>();
diffForInsert(null, map, newBean);
return map;
}
/**
* Populate the diff for inserts with flattened non-null property values.
*/
public void diffForInsert(String prefix, Map<String, ValuePair> map, EntityBean newBean) {
for (int i = 0; i < propertiesBaseScalar.length; i++) {
propertiesBaseScalar[i].diffForInsert(prefix, map, newBean);
}
for (int i = 0; i < propertiesOne.length; i++) {
propertiesOne[i].diffForInsert(prefix, map, newBean);
}
for (int i = 0; i < propertiesEmbedded.length; i++) {
propertiesEmbedded[i].diffForInsert(prefix, map, newBean);
}
}
/**
* Populate the diff for updates with flattened non-null property values.
*/
public void diff(String prefix, Map<String, ValuePair> map, EntityBean newBean, EntityBean oldBean) {
for (int i = 0; i < propertiesBaseScalar.length; i++) {
propertiesBaseScalar[i].diff(prefix, map, newBean, oldBean);
}
for (int i = 0; i < propertiesOne.length; i++) {
propertiesOne[i].diff(prefix, map, newBean, oldBean);
}
for (int i = 0; i < propertiesEmbedded.length; i++) {
propertiesEmbedded[i].diff(prefix, map, newBean, oldBean);
}
}
/**
* All the BeanPropertyAssocOne that are not embedded. These are effectively
* joined beans. For ManyToOne and OneToOne associations.
@@ -432,6 +432,9 @@ public final class BeanDescriptorCacheHelp<T> {
if (beanLog.isTraceEnabled()) {
beanLog.trace(" GET {}({}) - hit shared bean", cacheName, id);
}
if (desc.isReadAuditing()) {
desc.readAuditBean("l2", "", bean);
}
return (T) bean;
}
}
@@ -450,6 +453,9 @@ public final class BeanDescriptorCacheHelp<T> {
if (beanLog.isTraceEnabled()) {
beanLog.trace(" GET {}({}) - hit", cacheName, id);
}
if (desc.isReadAuditing()) {
desc.readAuditBean("l2", "", bean);
}
return (T) bean;
}
@@ -1,5 +1,6 @@
package com.avaje.ebeaninternal.server.deploy;
import com.avaje.ebean.ValuePair;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.config.EncryptKey;
import com.avaje.ebean.config.dbplatform.DbEncryptFunction;
@@ -21,6 +22,7 @@ import com.avaje.ebeaninternal.server.text.json.ReadJson;
import com.avaje.ebeaninternal.server.text.json.WriteJson;
import com.avaje.ebeaninternal.server.type.DataBind;
import com.avaje.ebeaninternal.server.type.ScalarType;
import com.avaje.ebeaninternal.util.ValueUtil;
import com.fasterxml.jackson.core.JsonToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -33,6 +35,7 @@ import java.lang.reflect.Field;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import java.util.Map;
/**
* Description of a property of a bean. Includes its deployment information such
@@ -1098,4 +1101,34 @@ public class BeanProperty implements ElPropertyValue {
}
}
}
/**
* Populate diff map for insert if the property is not null.
*/
public void diffForInsert(String prefix, Map<String, ValuePair> map, EntityBean newBean) {
Object newVal = (newBean == null) ? null : getValue(newBean);
if (newVal != null) {
String propName = (prefix == null) ? name : prefix + "." + name;
map.put(propName, new ValuePair(newVal, null));
}
}
/**
* Populate diff map comparing the property values between the beans.
*/
public void diff(String prefix, Map<String, ValuePair> map, EntityBean newBean, EntityBean oldBean) {
Object newVal = (newBean == null) ? null : getValue(newBean);
Object oldVal = (oldBean == null) ? null : getValue(oldBean);
diffVal(prefix, map, newVal, oldVal);
}
/**
* Populate diff map comparing the property values.
*/
public void diffVal(String prefix, Map<String, ValuePair> map, Object newVal, Object oldVal) {
if (!ValueUtil.areEqual(newVal, oldVal)) {
String propName = (prefix == null) ? name : prefix + "." + name;
map.put(propName, new ValuePair(newVal, oldVal));
}
}
}
@@ -4,6 +4,7 @@ import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.Query;
import com.avaje.ebean.SqlUpdate;
import com.avaje.ebean.Transaction;
import com.avaje.ebean.ValuePair;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.bean.EntityBeanIntercept;
import com.avaje.ebean.bean.PersistenceContext;
@@ -26,6 +27,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Property mapped to a joined bean.
@@ -318,6 +320,53 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> {
return importedPrimaryKey;
}
@Override
public void diffForInsert(String prefix, Map<String, ValuePair> map, EntityBean newBean) {
Object newEmb = (newBean == null) ? null : getValue(newBean);
if (newEmb != null) {
prefix = (prefix == null) ? name : prefix + "." + name;
if (embedded) {
getTargetDescriptor().diffForInsert(prefix, map, (EntityBean) newEmb);
} else {
// we are only interested in the Id value
BeanDescriptor<T> targetDescriptor = getTargetDescriptor();
BeanProperty idProperty = targetDescriptor.getIdProperty();
idProperty.diffForInsert(prefix, map, (EntityBean) newEmb);
}
}
}
@Override
public void diff(String prefix, Map<String, ValuePair> map, EntityBean newBean, EntityBean oldBean) {
Object newEmb = (newBean == null) ? null : getValue(newBean);
Object oldEmb = (oldBean == null) ? null : getValue(oldBean);
if (newEmb == null && oldEmb == null) {
return;
}
if (embedded) {
prefix = (prefix == null) ? name : prefix + "." + name;
BeanDescriptor<T> targetDescriptor = getTargetDescriptor();
targetDescriptor.diff(prefix, map, (EntityBean) newEmb, (EntityBean) oldEmb);
} else {
// we are only interested in the Id value
newBean = (EntityBean)newEmb;
oldBean = (EntityBean)oldEmb;
BeanDescriptor<T> targetDescriptor = getTargetDescriptor();
BeanProperty idProperty = targetDescriptor.getIdProperty();
Object newId = (newBean == null) ? null : idProperty.getValue(newBean);
Object oldId = (oldBean == null) ? null : idProperty.getValue(oldBean);
if (newId != null || oldId != null) {
prefix = (prefix == null) ? name : prefix + "." + name;
idProperty.diffVal(prefix, map, newId, oldId);
}
}
}
/**
* Same as getPropertyType(). Return the type of the bean this property
* represents.
@@ -84,6 +84,28 @@ public interface IdBinder {
*/
Object[] getBindValues(Object idValue);
/**
* For EmbeddedId convert the idValue into a simple map.
* Otherwise the idValue is just returned as is.
* <p>
* This is used to provide a simple JSON serializable version of the id value.
* </p>
*/
Object getIdForJson(EntityBean idValue);
/**
* For EmbeddedId the value is assumed to be a Map and this is
* takes the values from the map and builds an embedded id bean.
* <p>
* For other simple id's this just returns the value (no conversion required).
* </p>
* <p>
* This is used to provide a simple JSON serializable version of the id value.
* </p>
*/
Object convertIdFromJson(Object value);
/**
* Return the id values for a given bean.
*/
@@ -171,4 +193,8 @@ public interface IdBinder {
*/
Object convertSetId(Object idValue, EntityBean bean);
/**
* Cast or convert the Id value if necessary.
*/
Object convertId(Object idValue);
}
@@ -15,7 +15,9 @@ import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Bind an Id that is an Embedded bean.
@@ -225,6 +227,36 @@ public final class IdBinderEmbedded implements IdBinder {
return bindvalues;
}
/**
* Convert from embedded bean to Map.
*/
@Override
public Object getIdForJson(EntityBean bean) {
EntityBean ebValue = (EntityBean)embIdProperty.getValue(bean);
Map<String,Object> map = new LinkedHashMap<String, Object>();
for (int i = 0; i < props.length; i++) {
map.put(props[i].getName(), props[i].getValue(ebValue));
}
return map;
}
/**
* Convert back from a Map to embedded bean.
*/
public Object convertIdFromJson(Object value) {
Map<String,Object> map = (Map<String, Object>)value;
EntityBean idValue = idDesc.createEntityBean();
for (int i = 0; i < props.length; i++) {
Object val = map.get(props[i].getName());
props[i].setValue(idValue, val);
}
return idValue;
}
public void bindId(DefaultSqlUpdate sqlUpdate, Object value) {
for (int i = 0; i < props.length; i++) {
Object embFieldValue = props[i].getValue((EntityBean) value);
@@ -386,6 +418,12 @@ public final class IdBinderEmbedded implements IdBinder {
return sb.toString();
}
@Override
public Object convertId(Object idValue) {
// can not cast/convert if it is embedded
return idValue;
}
public Object convertSetId(Object idValue, EntityBean bean) {
// can not cast/convert if it is embedded
@@ -98,6 +98,16 @@ public final class IdBinderEmpty implements IdBinder {
return new Object[]{idValue};
}
@Override
public Object getIdForJson(EntityBean bean) {
return null;
}
@Override
public Object convertIdFromJson(Object value) {
return value;
}
public void bindId(DefaultSqlUpdate sqlUpdate, Object value) {
}
@@ -124,6 +134,11 @@ public final class IdBinderEmpty implements IdBinder {
return idValue;
}
@Override
public Object convertId(Object idValue) {
return idValue;
}
public Object readData(DataInput dataOutput) throws IOException {
return null;
}
@@ -131,6 +131,17 @@ public final class IdBinderSimple implements IdBinder {
request.addBindValue(value);
}
@Override
public Object getIdForJson(EntityBean bean) {
return idProperty.getValue(bean);
}
@Override
public Object convertIdFromJson(Object value) {
// handle simple type conversion if required
return convertId(value);
}
public void bindId(DefaultSqlUpdate sqlUpdate, Object value) {
sqlUpdate.addParameter(value);
}
@@ -193,6 +204,14 @@ public final class IdBinderSimple implements IdBinder {
return sb.toString();
}
public Object convertId(Object idValue) {
if (!idValue.getClass().equals(expectedType)) {
return scalarType.toBeanType(idValue);
}
return idValue;
}
public Object convertSetId(Object idValue, EntityBean bean) {
if (!idValue.getClass().equals(expectedType)) {
@@ -120,6 +120,8 @@ public class DeployBeanDescriptor<T> {
private boolean historySupport;
private boolean readAuditing;
private TableName baseTableFull;
private String[] properties;
@@ -185,6 +187,20 @@ public class DeployBeanDescriptor<T> {
return historySupport;
}
/**
* Set read auditing on for this entity bean.
*/
public void setReadAuditing() {
readAuditing = true;
}
/**
* Return true if read auditing is on for this entity bean.
*/
public boolean isReadAuditing() {
return readAuditing;
}
public boolean isScalaObject() {
Class<?>[] interfaces = beanType.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
@@ -741,7 +757,7 @@ public class DeployBeanDescriptor<T> {
}
DeployBeanProperty p = ids.get(0);
if (p instanceof DeployBeanPropertyAssocOne<?>) {
return ((DeployBeanPropertyAssocOne<?>)p).isCompound();
return ((DeployBeanPropertyAssocOne<?>) p).isCompound();
} else {
return !p.isDbNumberType();
}
@@ -859,17 +875,17 @@ public class DeployBeanDescriptor<T> {
}
/**
* Check valid mapping annotations on the class hierarchy.
* Check valid mapping annotations on the class hierarchy.
*/
private void checkInheritance(Class<?> beanType) {
Class<?> parent = beanType.getSuperclass();
if (parent == null || Object.class.equals(parent)) {
// all good
return;
}
if (parent.isAnnotationPresent(Entity.class)) {
String msg = "Checking "+getBeanType()+" and found "+parent+" that has @Entity annotation rather than MappedSuperclass?";
String msg = "Checking " + getBeanType() + " and found " + parent + " that has @Entity annotation rather than MappedSuperclass?";
throw new IllegalStateException(msg);
}
if (parent.isAnnotationPresent(MappedSuperclass.class)) {
@@ -14,6 +14,7 @@ import com.avaje.ebean.annotation.History;
import com.avaje.ebean.annotation.Index;
import com.avaje.ebean.annotation.NamedUpdate;
import com.avaje.ebean.annotation.NamedUpdates;
import com.avaje.ebean.annotation.ReadAudit;
import com.avaje.ebean.annotation.UpdateMode;
import com.avaje.ebean.config.TableName;
import com.avaje.ebeaninternal.server.core.CacheOptions;
@@ -97,6 +98,11 @@ public class AnnotationClass extends AnnotationParser {
}
}
ReadAudit readAudit = cls.getAnnotation(ReadAudit.class);
if (readAudit != null) {
descriptor.setReadAuditing();
}
History history = cls.getAnnotation(History.class);
if (history != null) {
descriptor.setHistorySupport();