mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
#390 - ENH: Add Change log mechanism for easy fine grained logging of insert, update, delete activity
This commit is contained in:
1 parent
46b158a0a0
commit
84190f3c2a
34 files changed
+1642
-84
No files matched your search
@@ -3,6 +3,8 @@ package com.avaje.ebeaninternal.api;
|
||||
import com.avaje.ebean.TransactionCallback;
|
||||
import com.avaje.ebean.bean.PersistenceContext;
|
||||
import com.avaje.ebean.config.PersistBatch;
|
||||
import com.avaje.ebean.event.changelog.BeanChange;
|
||||
import com.avaje.ebean.event.changelog.ChangeSet;
|
||||
import com.avaje.ebeaninternal.server.core.PersistRequest;
|
||||
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
|
||||
import com.avaje.ebeaninternal.server.persist.BatchControl;
|
||||
@@ -87,6 +89,16 @@ public class ScopedTransaction implements SpiTransaction {
|
||||
transaction.logSummary(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBeanChange(BeanChange beanChange) {
|
||||
transaction.addBeanChange(beanChange);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendChangeLog(ChangeSet changes) {
|
||||
transaction.sendChangeLog(changes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerDerivedRelationship(DerivedRelationshipData assocBean) {
|
||||
transaction.registerDerivedRelationship(assocBean);
|
||||
|
||||
@@ -25,7 +25,7 @@ import java.util.List;
|
||||
* Service Provider extension to EbeanServer.
|
||||
*/
|
||||
public interface SpiEbeanServer extends EbeanServer, BeanLoader, BeanCollectionLoader {
|
||||
|
||||
|
||||
/**
|
||||
* For internal use, shutdown of the server invoked by JVM Shutdown.
|
||||
*/
|
||||
|
||||
@@ -5,6 +5,8 @@ import java.util.List;
|
||||
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.ebean.bean.PersistenceContext;
|
||||
import com.avaje.ebean.event.changelog.BeanChange;
|
||||
import com.avaje.ebean.event.changelog.ChangeSet;
|
||||
import com.avaje.ebeaninternal.server.core.PersistRequest;
|
||||
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
|
||||
import com.avaje.ebeaninternal.server.persist.BatchControl;
|
||||
@@ -233,4 +235,13 @@ public interface SpiTransaction extends Transaction {
|
||||
*/
|
||||
void flushBatchOnCollection();
|
||||
|
||||
/**
|
||||
* Add a bean change to the change log.
|
||||
*/
|
||||
void addBeanChange(BeanChange beanChange);
|
||||
|
||||
/**
|
||||
* Send the change set to be prepared and then logged.
|
||||
*/
|
||||
void sendChangeLog(ChangeSet changeSet);
|
||||
}
|
||||
@@ -1,23 +1,31 @@
|
||||
package com.avaje.ebeaninternal.server.core;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import com.avaje.ebean.event.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.avaje.ebean.config.CompoundType;
|
||||
import com.avaje.ebean.config.ScalarTypeConverter;
|
||||
import com.avaje.ebean.config.ServerConfig;
|
||||
import com.avaje.ebean.event.BeanFindController;
|
||||
import com.avaje.ebean.event.BeanPersistController;
|
||||
import com.avaje.ebean.event.BeanPersistListener;
|
||||
import com.avaje.ebean.event.BeanPostLoad;
|
||||
import com.avaje.ebean.event.BeanQueryAdapter;
|
||||
import com.avaje.ebean.event.ServerConfigStartup;
|
||||
import com.avaje.ebean.event.TransactionEventListener;
|
||||
import com.avaje.ebean.event.changelog.ChangeLogListener;
|
||||
import com.avaje.ebean.event.changelog.ChangeLogPrepare;
|
||||
import com.avaje.ebean.event.changelog.ChangeLogRegister;
|
||||
import com.avaje.ebeaninternal.server.type.ScalarType;
|
||||
import com.avaje.ebeaninternal.server.util.ClassPathSearchMatcher;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Interesting classes for a EbeanServer such as Embeddable, Entity,
|
||||
@@ -58,6 +66,14 @@ public class BootupClasses implements ClassPathSearchMatcher {
|
||||
private final List<BeanQueryAdapter> queryAdapterInstances = new ArrayList<BeanQueryAdapter>();
|
||||
private final List<TransactionEventListener> transactionEventListenerInstances = new ArrayList<TransactionEventListener>();
|
||||
|
||||
private Class<?> changeLogPrepareClass;
|
||||
private Class<?> changeLogListenerClass;
|
||||
private Class<?> changeLogRegisterClass;
|
||||
|
||||
private ChangeLogPrepare changeLogPrepare;
|
||||
private ChangeLogListener changeLogListener;
|
||||
private ChangeLogRegister changeLogRegister;
|
||||
|
||||
public BootupClasses() {
|
||||
}
|
||||
|
||||
@@ -175,98 +191,122 @@ public class BootupClasses implements ClassPathSearchMatcher {
|
||||
}
|
||||
}
|
||||
|
||||
public List<BeanQueryAdapter> getBeanQueryAdapters() {
|
||||
// add class registered BeanQueryAdapter to the
|
||||
// already created instances
|
||||
for (Class<?> cls : beanQueryAdapterList) {
|
||||
try {
|
||||
BeanQueryAdapter newInstance = (BeanQueryAdapter) cls.newInstance();
|
||||
queryAdapterInstances.add(newInstance);
|
||||
} catch (Exception e) {
|
||||
String msg = "Error creating BeanQueryAdapter " + cls;
|
||||
logger.error(msg, e);
|
||||
public void addChangeLogInstances(ServerConfig serverConfig) {
|
||||
changeLogListener = serverConfig.getChangeLogListener();
|
||||
changeLogRegister = serverConfig.getChangeLogRegister();
|
||||
changeLogPrepare = serverConfig.getChangeLogPrepare();
|
||||
|
||||
// if not already set create the implementations found
|
||||
// via classpath scanning
|
||||
if (changeLogPrepare == null && changeLogPrepareClass != null) {
|
||||
changeLogPrepare = (ChangeLogPrepare)create(changeLogPrepareClass, false);
|
||||
}
|
||||
if (changeLogListener == null && changeLogListenerClass != null) {
|
||||
changeLogListener = (ChangeLogListener)create(changeLogListenerClass, false);
|
||||
}
|
||||
if (changeLogRegister == null && changeLogRegisterClass != null) {
|
||||
changeLogRegister = (ChangeLogRegister)create(changeLogRegisterClass, false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance using the default constructor returning null if there
|
||||
* is no default constructor (implying the class was not intended to be instantiated
|
||||
* automatically via classpath scanning.
|
||||
* <p>
|
||||
* Use logOnException = true to log the error and carry on.
|
||||
*/
|
||||
private Object create(Class<?> cls, boolean logOnException) {
|
||||
try {
|
||||
// instantiate via found class
|
||||
Constructor constructor = cls.getConstructor();
|
||||
return constructor.newInstance();
|
||||
|
||||
} catch (NoSuchMethodException e) {
|
||||
logger.debug("Ignore/expected - no default constructor", e);
|
||||
return null;
|
||||
|
||||
} catch (Exception e) {
|
||||
if (logOnException) {
|
||||
// not expected but we log and carry on
|
||||
logger.error("Error creating " + cls, e);
|
||||
return null;
|
||||
|
||||
} else {
|
||||
// ok, stop the bus
|
||||
throw new IllegalStateException("Error creating " + cls, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the instance if it has a default constructor and add it to the list of instances.
|
||||
*/
|
||||
@SuppressWarnings(value = "unchecked")
|
||||
private <T> void createAdd(Class<?> cls, List<T> instances) {
|
||||
Object newInstance = create(cls, true);
|
||||
if (newInstance != null) {
|
||||
instances.add((T)newInstance);
|
||||
}
|
||||
}
|
||||
|
||||
public ChangeLogPrepare getChangeLogPrepare() {
|
||||
return changeLogPrepare;
|
||||
}
|
||||
|
||||
public ChangeLogListener getChangeLogListener() {
|
||||
return changeLogListener;
|
||||
}
|
||||
|
||||
public ChangeLogRegister getChangeLogRegister() {
|
||||
return changeLogRegister;
|
||||
}
|
||||
|
||||
public List<BeanQueryAdapter> getBeanQueryAdapters() {
|
||||
// add class registered BeanQueryAdapter to the already created instances
|
||||
for (Class<?> cls : beanQueryAdapterList) {
|
||||
createAdd(cls, queryAdapterInstances);
|
||||
}
|
||||
return queryAdapterInstances;
|
||||
}
|
||||
|
||||
public List<BeanFindController> getBeanFindControllers() {
|
||||
// add class registered BeanFindController to the
|
||||
// list of created instances
|
||||
// add class registered BeanFindController to the list of created instances
|
||||
for (Class<?> cls : beanFindControllerList) {
|
||||
try {
|
||||
BeanFindController newInstance = (BeanFindController) cls.newInstance();
|
||||
findControllerInstances.add(newInstance);
|
||||
} catch (Exception e) {
|
||||
String msg = "Error creating BeanPersistController " + cls;
|
||||
logger.error(msg, e);
|
||||
}
|
||||
createAdd(cls, findControllerInstances);
|
||||
}
|
||||
|
||||
return findControllerInstances;
|
||||
}
|
||||
|
||||
public List<BeanPersistListener> getBeanPersistListeners() {
|
||||
// add class registered BeanPersistController to the
|
||||
// already created instances
|
||||
// add class registered BeanPersistController to the already created instances
|
||||
for (Class<?> cls : beanListenerList) {
|
||||
try {
|
||||
BeanPersistListener newInstance = (BeanPersistListener) cls.newInstance();
|
||||
persistListenerInstances.add(newInstance);
|
||||
} catch (Exception e) {
|
||||
String msg = "Error creating BeanPersistController " + cls;
|
||||
logger.error(msg, e);
|
||||
}
|
||||
createAdd(cls, persistListenerInstances);
|
||||
}
|
||||
|
||||
return persistListenerInstances;
|
||||
}
|
||||
|
||||
public List<BeanPersistController> getBeanPersistControllers() {
|
||||
// add class registered BeanPersistController to the
|
||||
// already created instances
|
||||
// add class registered BeanPersistController to the already created instances
|
||||
for (Class<?> cls : beanControllerList) {
|
||||
try {
|
||||
BeanPersistController newInstance = (BeanPersistController) cls.newInstance();
|
||||
persistControllerInstances.add(newInstance);
|
||||
} catch (Exception e) {
|
||||
String msg = "Error creating BeanPersistController " + cls;
|
||||
logger.error(msg, e);
|
||||
}
|
||||
createAdd(cls, persistControllerInstances);
|
||||
}
|
||||
|
||||
return persistControllerInstances;
|
||||
}
|
||||
|
||||
public List<BeanPostLoad> getBeanPostLoaders() {
|
||||
// add class registered BeanPostLoad to the already created instances
|
||||
for (Class<?> cls : beanPostLoadList) {
|
||||
try {
|
||||
BeanPostLoad newInstance = (BeanPostLoad) cls.newInstance();
|
||||
beanPostLoadInstances.add(newInstance);
|
||||
} catch (Exception e) {
|
||||
String msg = "Error creating BeanPersistController " + cls;
|
||||
logger.error(msg, e);
|
||||
}
|
||||
createAdd(cls, beanPostLoadInstances);
|
||||
}
|
||||
|
||||
return beanPostLoadInstances;
|
||||
}
|
||||
|
||||
public List<TransactionEventListener> getTransactionEventListeners() {
|
||||
// add class registered TransactionEventListener to the
|
||||
// already created instances
|
||||
// add class registered TransactionEventListener to the already created instances
|
||||
for (Class<?> cls : transactionEventListenerList) {
|
||||
try {
|
||||
TransactionEventListener newInstance = (TransactionEventListener) cls.newInstance();
|
||||
transactionEventListenerInstances.add(newInstance);
|
||||
} catch (Exception e) {
|
||||
String msg = "Error creating TransactionEventListener " + cls;
|
||||
logger.error(msg, e);
|
||||
}
|
||||
createAdd(cls, transactionEventListenerInstances);
|
||||
}
|
||||
|
||||
return transactionEventListenerInstances;
|
||||
}
|
||||
|
||||
@@ -385,6 +425,21 @@ public class BootupClasses implements ClassPathSearchMatcher {
|
||||
interesting = true;
|
||||
}
|
||||
|
||||
if (ChangeLogListener.class.isAssignableFrom(cls)) {
|
||||
changeLogListenerClass = cls;
|
||||
interesting = true;
|
||||
}
|
||||
|
||||
if (ChangeLogRegister.class.isAssignableFrom(cls)) {
|
||||
changeLogRegisterClass = cls;
|
||||
interesting = true;
|
||||
}
|
||||
|
||||
if (ChangeLogPrepare.class.isAssignableFrom(cls)) {
|
||||
changeLogPrepareClass = cls;
|
||||
interesting = true;
|
||||
}
|
||||
|
||||
return interesting;
|
||||
}
|
||||
|
||||
|
||||
@@ -227,6 +227,7 @@ public class DefaultContainer implements SpiContainer {
|
||||
bootupClasses.addPersistListeners(serverConfig.getPersistListeners());
|
||||
bootupClasses.addQueryAdapters(serverConfig.getQueryAdapters());
|
||||
bootupClasses.addServerConfigStartup(serverConfig.getServerConfigStartupListeners());
|
||||
bootupClasses.addChangeLogInstances(serverConfig);
|
||||
|
||||
// run any ServerConfigStartup instances
|
||||
bootupClasses.runServerConfigStartup(serverConfig);
|
||||
|
||||
@@ -204,7 +204,7 @@ public final class DefaultServer implements SpiEbeanServer {
|
||||
this.serverCacheManager = cache;
|
||||
this.databasePlatform = config.getDatabasePlatform();
|
||||
this.backgroundExecutor = config.getBackgroundExecutor();
|
||||
|
||||
|
||||
this.serverName = serverConfig.getName();
|
||||
this.diffHelp = new DiffHelp(serverConfig.isDiffFlatMode());
|
||||
this.lazyLoadBatchSize = serverConfig.getLazyLoadBatchSize();
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.avaje.ebeaninternal.server.core;
|
||||
|
||||
import com.avaje.ebean.ValuePair;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Helper to perform a 'diff' for an insert.
|
||||
* <p>
|
||||
* This intentionally does not include any OneToMany or ManyToMany properties.
|
||||
* </p>
|
||||
*/
|
||||
public class DiffHelpInsert {
|
||||
|
||||
private DiffHelpInsert() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 static Map<String, ValuePair> diff(EntityBean newBean, BeanDescriptor<?> desc) {
|
||||
|
||||
Map<String, ValuePair> map = new LinkedHashMap<String, ValuePair>();
|
||||
diff(null, map, newBean, desc);
|
||||
return map;
|
||||
}
|
||||
|
||||
private static void diff(String prefix, Map<String, ValuePair> map, EntityBean newBean, BeanDescriptor<?> desc) {
|
||||
|
||||
// check the simple properties
|
||||
BeanProperty[] base = desc.propertiesBaseScalar();
|
||||
for (int i = 0; i < base.length; i++) {
|
||||
Object newVal = (newBean == null) ? null : base[i].getValue(newBean);
|
||||
if (newVal != null) {
|
||||
String propName = (prefix == null) ? base[i].getName() : prefix + base[i].getName();
|
||||
map.put(propName, new ValuePair(newVal, null));
|
||||
}
|
||||
}
|
||||
|
||||
diffAssocOne(prefix, newBean, desc, map);
|
||||
diffEmbedded(prefix, newBean, desc, map);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the Embedded bean properties for differences.
|
||||
* <p>
|
||||
* If ANY of the properties are different then the whole Embedded bean is
|
||||
* determined to be different as is added to the map.
|
||||
* </p>
|
||||
*/
|
||||
private static void diffEmbedded(String prefix, EntityBean newBean, BeanDescriptor<?> desc, Map<String, ValuePair> map) {
|
||||
|
||||
BeanPropertyAssocOne<?>[] emb = desc.propertiesEmbedded();
|
||||
|
||||
for (int i = 0; i < emb.length; i++) {
|
||||
EntityBean newVal = (EntityBean) emb[i].getValue(newBean);
|
||||
|
||||
if (newVal != null) {
|
||||
String propName = (prefix == null) ? emb[i].getName() : prefix + emb[i].getName();
|
||||
|
||||
BeanDescriptor<?> embDesc = emb[i].getTargetDescriptor();
|
||||
diff(propName + ".", map, newVal, embDesc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If the properties are different by null OR if the id value is different,
|
||||
* then add the Assoc One bean to the map.
|
||||
*/
|
||||
private static void diffAssocOne(String prefix, EntityBean newBean, BeanDescriptor<?> desc, Map<String, ValuePair> map) {
|
||||
|
||||
BeanPropertyAssocOne<?>[] ones = desc.propertiesOne();
|
||||
|
||||
for (int i = 0; i < ones.length; i++) {
|
||||
Object newVal = ones[i].getValue(newBean);
|
||||
|
||||
if (newVal != null) {
|
||||
BeanDescriptor<?> oneDesc = ones[i].getTargetDescriptor();
|
||||
Object newId = oneDesc.getId((EntityBean)newVal);
|
||||
if (newId != null) {
|
||||
String propName = (prefix == null) ? ones[i].getName() : prefix + ones[i].getName();
|
||||
String idName = oneDesc.getIdProperty().getName();
|
||||
map.put(propName + "." + idName, new ValuePair(newId, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -176,7 +176,7 @@ public class InternalConfiguration {
|
||||
|
||||
public JsonContext createJsonContext(SpiEbeanServer server) {
|
||||
|
||||
return new DJsonContext(server, jsonFactory);
|
||||
return new DJsonContext(server, jsonFactory, typeManager);
|
||||
}
|
||||
|
||||
public XmlConfig getXmlConfig() {
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.avaje.ebean.bean.EntityBeanIntercept;
|
||||
import com.avaje.ebean.event.BeanPersistController;
|
||||
import com.avaje.ebean.event.BeanPersistListener;
|
||||
import com.avaje.ebean.event.BeanPersistRequest;
|
||||
import com.avaje.ebean.event.changelog.BeanChange;
|
||||
import com.avaje.ebeaninternal.api.DerivedRelationshipData;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.api.SpiTransaction;
|
||||
@@ -529,11 +530,20 @@ public final class PersistRequestBean<T> extends PersistRequest implements BeanP
|
||||
beanDescriptor.cacheHandleDelete(idValue, this);
|
||||
}
|
||||
|
||||
private void changeLog() {
|
||||
BeanChange changeLogBean = beanDescriptor.getChangeLogBean(this);
|
||||
if (changeLogBean != null) {
|
||||
transaction.addBeanChange(changeLogBean);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Post processing.
|
||||
*/
|
||||
public void postExecute() {
|
||||
|
||||
changeLog();
|
||||
|
||||
if (controller != null) {
|
||||
controllerPost();
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.avaje.ebeaninternal.server.deploy;
|
||||
|
||||
import com.avaje.ebean.SqlUpdate;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.ebean.ValuePair;
|
||||
import com.avaje.ebean.annotation.ConcurrencyMode;
|
||||
import com.avaje.ebean.bean.BeanCollection;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
@@ -15,6 +16,9 @@ import com.avaje.ebean.event.BeanPersistController;
|
||||
import com.avaje.ebean.event.BeanPersistListener;
|
||||
import com.avaje.ebean.event.BeanPostLoad;
|
||||
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.meta.MetaBeanInfo;
|
||||
import com.avaje.ebean.meta.MetaQueryPlanStatistic;
|
||||
import com.avaje.ebeaninternal.api.HashQueryPlan;
|
||||
@@ -26,7 +30,9 @@ 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.InternString;
|
||||
import com.avaje.ebeaninternal.server.core.PersistRequest;
|
||||
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
|
||||
import com.avaje.ebeaninternal.server.deploy.id.IdBinder;
|
||||
import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanDescriptor;
|
||||
@@ -175,6 +181,11 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
|
||||
*/
|
||||
private final BeanFindController beanFinder;
|
||||
|
||||
/**
|
||||
* Used for fine grain filtering for the change log.
|
||||
*/
|
||||
private final ChangeLogFilter changeLogFilter;
|
||||
|
||||
/**
|
||||
* The table joins for this bean.
|
||||
*/
|
||||
@@ -337,6 +348,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
|
||||
this.persistListener = deploy.getPersistListener();
|
||||
this.beanPostLoad = deploy.getPostLoad();
|
||||
this.queryAdapter = deploy.getQueryAdapter();
|
||||
this.changeLogFilter = deploy.getChangeLogFilter();
|
||||
|
||||
this.defaultSelectClause = deploy.getDefaultSelectClause();
|
||||
this.defaultSelectClauseSet = deploy.parseDefaultSelectClause(defaultSelectClause);
|
||||
@@ -421,7 +433,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
|
||||
this.unloadProperties = derivePropertiesToUnload(prototypeEntityBean);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Derive an array of property positions for properties that are initialised in the constructor.
|
||||
* These properties need to be unloaded when populating beans for queries.
|
||||
@@ -598,6 +610,60 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this request should be included in the change log.
|
||||
*/
|
||||
public BeanChange getChangeLogBean(PersistRequestBean<T> request) {
|
||||
|
||||
if (changeLogFilter == null) {
|
||||
return null;
|
||||
}
|
||||
PersistRequest.Type type = request.getType();
|
||||
switch (type) {
|
||||
case INSERT:
|
||||
return changeLogFilter.includeInsert(request) ? insertBeanChange(request): null;
|
||||
case UPDATE:
|
||||
return changeLogFilter.includeUpdate(request) ? updateBeanChange(request): null;
|
||||
case DELETE:
|
||||
return changeLogFilter.includeDelete(request) ? deleteBeanChange(request) :null;
|
||||
default:
|
||||
throw new IllegalStateException("Unhandled request type " + type);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the bean change for a delete.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private BeanChange deleteBeanChange(PersistRequestBean<T> request) {
|
||||
return newBeanChange(request.getBeanId(), ChangeType.DELETE, Collections.EMPTY_MAP);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the bean change for an update.
|
||||
*/
|
||||
private BeanChange updateBeanChange(PersistRequestBean<T> request) {
|
||||
return newBeanChange(request.getBeanId(), ChangeType.UPDATE, 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()));
|
||||
}
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
@@ -16,6 +16,10 @@ import com.avaje.ebean.config.dbplatform.DbHistorySupport;
|
||||
import com.avaje.ebean.config.dbplatform.DbIdentity;
|
||||
import com.avaje.ebean.config.dbplatform.IdGenerator;
|
||||
import com.avaje.ebean.config.dbplatform.IdType;
|
||||
import com.avaje.ebean.event.changelog.ChangeLogFilter;
|
||||
import com.avaje.ebean.event.changelog.ChangeLogListener;
|
||||
import com.avaje.ebean.event.changelog.ChangeLogPrepare;
|
||||
import com.avaje.ebean.event.changelog.ChangeLogRegister;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.api.TransactionEventTable;
|
||||
import com.avaje.ebeaninternal.server.core.BootupClasses;
|
||||
@@ -105,6 +109,12 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
|
||||
|
||||
private final BeanManagerFactory beanManagerFactory;
|
||||
|
||||
private final ChangeLogListener changeLogListener;
|
||||
|
||||
private final ChangeLogRegister changeLogRegister;
|
||||
|
||||
private final ChangeLogPrepare changeLogPrepare;
|
||||
|
||||
private int enhancedClassCount;
|
||||
|
||||
private final boolean updateChangesOnly;
|
||||
@@ -199,6 +209,9 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
|
||||
|
||||
this.reflectFactory = createReflectionFactory();
|
||||
this.transientProperties = new TransientProperties();
|
||||
this.changeLogPrepare = bootupClasses.getChangeLogPrepare();
|
||||
this.changeLogListener = bootupClasses.getChangeLogListener();
|
||||
this.changeLogRegister = bootupClasses.getChangeLogRegister();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1003,6 +1016,14 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
|
||||
persistListenerManager.addPersistListeners(descriptor);
|
||||
beanQueryAdapterManager.addQueryAdapter(descriptor);
|
||||
beanFinderManager.addFindControllers(descriptor);
|
||||
|
||||
if (changeLogRegister != null) {
|
||||
ChangeLogFilter changeFilter = changeLogRegister.getChangeFilter(descriptor.getBeanType());
|
||||
if (changeFilter != null) {
|
||||
descriptor.setChangeLogFilter(changeFilter);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1451,7 +1472,22 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the changeLogPrepare (for setting user context into the ChangeSet
|
||||
* in the foreground thread).
|
||||
*/
|
||||
public ChangeLogPrepare getChangeLogPrepare() {
|
||||
return changeLogPrepare;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the changeLogListener (that actually does the logging).
|
||||
*/
|
||||
public ChangeLogListener getChangeLogListener() {
|
||||
return changeLogListener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Comparator to sort the BeanDescriptors by name.
|
||||
*/
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.avaje.ebean.event.BeanPersistController;
|
||||
import com.avaje.ebean.event.BeanPersistListener;
|
||||
import com.avaje.ebean.event.BeanPostLoad;
|
||||
import com.avaje.ebean.event.BeanQueryAdapter;
|
||||
import com.avaje.ebean.event.changelog.ChangeLogFilter;
|
||||
import com.avaje.ebeaninternal.server.core.CacheOptions;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor.EntityType;
|
||||
import com.avaje.ebeaninternal.server.deploy.ChainedBeanPersistController;
|
||||
@@ -154,6 +155,8 @@ public class DeployBeanDescriptor<T> {
|
||||
|
||||
private boolean processedRawSqlExtend;
|
||||
|
||||
private ChangeLogFilter changeLogFilter;
|
||||
|
||||
/**
|
||||
* Construct the BeanDescriptor.
|
||||
*/
|
||||
@@ -305,6 +308,14 @@ public class DeployBeanDescriptor<T> {
|
||||
return beanType;
|
||||
}
|
||||
|
||||
public void setChangeLogFilter(ChangeLogFilter changeLogFilter) {
|
||||
this.changeLogFilter = changeLogFilter;
|
||||
}
|
||||
|
||||
public ChangeLogFilter getChangeLogFilter() {
|
||||
return changeLogFilter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Inheritance mapping information. This will be null if this type
|
||||
* of bean is not involved in any ORM inheritance mapping.
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.avaje.ebean.text.json.*;
|
||||
import com.avaje.ebean.text.PathProperties;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.type.TypeManager;
|
||||
import com.avaje.ebeaninternal.util.ParamTypeHelper;
|
||||
import com.avaje.ebeaninternal.util.ParamTypeHelper.ManyType;
|
||||
import com.avaje.ebeaninternal.util.ParamTypeHelper.TypeInfo;
|
||||
@@ -25,17 +26,24 @@ public class DJsonContext implements JsonContext {
|
||||
|
||||
private final JsonFactory jsonFactory;
|
||||
|
||||
private final TypeManager typeManager;
|
||||
|
||||
private final Object defaultObjectMapper;
|
||||
|
||||
private final JsonConfig.Include defaultInclude;
|
||||
|
||||
public DJsonContext(SpiEbeanServer server, JsonFactory jsonFactory) {
|
||||
public DJsonContext(SpiEbeanServer server, JsonFactory jsonFactory, TypeManager typeManager) {
|
||||
this.server = server;
|
||||
this.typeManager = typeManager;
|
||||
this.jsonFactory = (jsonFactory != null) ? jsonFactory : new JsonFactory();
|
||||
this.defaultObjectMapper = this.server.getServerConfig().getObjectMapper();
|
||||
this.defaultInclude = this.server.getServerConfig().getJsonInclude();
|
||||
}
|
||||
|
||||
public JsonScalar getScalar(JsonGenerator generator) {
|
||||
return new DefaultJsonScalar(typeManager, new WriteJson(generator, defaultInclude));
|
||||
}
|
||||
|
||||
public boolean isSupportedType(Type genericType) {
|
||||
return server.isSupportedType(genericType);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import com.avaje.ebean.text.json.JsonScalar;
|
||||
import com.avaje.ebeaninternal.server.type.ScalarType;
|
||||
import com.avaje.ebeaninternal.server.type.TypeManager;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Default implementation of JsonScalar.
|
||||
*/
|
||||
public class DefaultJsonScalar implements JsonScalar {
|
||||
|
||||
|
||||
private final TypeManager typeManager;
|
||||
|
||||
private final WriteJson writeJson;
|
||||
|
||||
public DefaultJsonScalar(TypeManager typeManager, WriteJson writeJson) {
|
||||
this.typeManager = typeManager;
|
||||
this.writeJson = writeJson;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(String name, Object value) throws IOException {
|
||||
|
||||
if (value instanceof String) {
|
||||
writeJson.writeStringField(name, (String)value);
|
||||
|
||||
} else {
|
||||
ScalarType scalarType = (ScalarType)typeManager.getScalarType(value.getClass());
|
||||
if (scalarType == null) {
|
||||
throw new IllegalArgumentException("unhandled type " + value.getClass());
|
||||
}
|
||||
scalarType.jsonWrite(writeJson, name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,27 +26,38 @@ public class WriteJson implements JsonWriter {
|
||||
|
||||
private final PathProperties pathProperties;
|
||||
|
||||
private final PathStack pathStack = new PathStack();
|
||||
private final PathStack pathStack;
|
||||
|
||||
private final ArrayStack<Object> parentBeans = new ArrayStack<Object>();
|
||||
private final ArrayStack<Object> parentBeans;
|
||||
|
||||
private final Object objectMapper;
|
||||
|
||||
private final JsonConfig.Include include;
|
||||
|
||||
/**
|
||||
* Construct for full bean use (normal).
|
||||
*/
|
||||
public WriteJson(SpiEbeanServer server, JsonGenerator generator, PathProperties pathProperties, Object objectMapper, JsonConfig.Include include){
|
||||
this.server = server;
|
||||
this.generator = generator;
|
||||
this.pathProperties = pathProperties;
|
||||
this.objectMapper = objectMapper;
|
||||
this.include = include;
|
||||
this.parentBeans = new ArrayStack<Object>();
|
||||
this.pathStack = new PathStack();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct for testing purposes only.
|
||||
* Construct for Json scalar use.
|
||||
*/
|
||||
public WriteJson(JsonGenerator generator, JsonConfig.Include include) {
|
||||
this(null, generator, null, null, include);
|
||||
public WriteJson(JsonGenerator generator, JsonConfig.Include include){
|
||||
this.generator = generator;
|
||||
this.include = include;
|
||||
this.server = null;
|
||||
this.pathProperties = null;
|
||||
this.objectMapper = null;
|
||||
this.parentBeans = null;
|
||||
this.pathStack = null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,6 +3,9 @@ package com.avaje.ebeaninternal.server.transaction;
|
||||
import com.avaje.ebean.TransactionCallback;
|
||||
import com.avaje.ebean.bean.PersistenceContext;
|
||||
import com.avaje.ebean.config.PersistBatch;
|
||||
import com.avaje.ebean.config.dbplatform.DatabasePlatform.OnQueryOnly;
|
||||
import com.avaje.ebean.event.changelog.BeanChange;
|
||||
import com.avaje.ebean.event.changelog.ChangeSet;
|
||||
import com.avaje.ebeaninternal.api.DerivedRelationshipData;
|
||||
import com.avaje.ebeaninternal.api.SpiTransaction;
|
||||
import com.avaje.ebeaninternal.api.TransactionEvent;
|
||||
@@ -10,7 +13,6 @@ import com.avaje.ebeaninternal.server.core.PersistRequest;
|
||||
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
|
||||
import com.avaje.ebeaninternal.server.lib.util.Str;
|
||||
import com.avaje.ebeaninternal.server.persist.BatchControl;
|
||||
import com.avaje.ebean.config.dbplatform.DatabasePlatform.OnQueryOnly;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -19,7 +21,12 @@ import javax.persistence.RollbackException;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* JDBC Connection based transaction.
|
||||
@@ -133,6 +140,8 @@ public class JdbcTransaction implements SpiTransaction {
|
||||
|
||||
protected boolean batchOnCascadeSet;
|
||||
|
||||
protected TChangeLogHolder changeLogHolder;
|
||||
|
||||
/**
|
||||
* Create a new JdbcTransaction.
|
||||
*/
|
||||
@@ -180,6 +189,19 @@ public class JdbcTransaction implements SpiTransaction {
|
||||
return logPrefix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBeanChange(BeanChange beanChange) {
|
||||
if (changeLogHolder == null) {
|
||||
changeLogHolder = new TChangeLogHolder(this, 100);
|
||||
}
|
||||
changeLogHolder.addBeanChange(beanChange);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendChangeLog(ChangeSet changesRequest) {
|
||||
manager.sendChangeLog(changesRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(TransactionCallback callback) {
|
||||
if (callbackList == null) {
|
||||
@@ -210,6 +232,9 @@ public class JdbcTransaction implements SpiTransaction {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (changeLogHolder != null) {
|
||||
changeLogHolder.postRollback();
|
||||
}
|
||||
}
|
||||
|
||||
protected void firePreCommit() {
|
||||
@@ -234,6 +259,9 @@ public class JdbcTransaction implements SpiTransaction {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (changeLogHolder != null) {
|
||||
changeLogHolder.postCommit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.avaje.ebeaninternal.server.transaction;
|
||||
|
||||
import com.avaje.ebean.event.changelog.BeanChange;
|
||||
import com.avaje.ebean.event.changelog.ChangeSet;
|
||||
import com.avaje.ebean.event.changelog.TxnState;
|
||||
import com.avaje.ebeaninternal.api.SpiTransaction;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Holder of the changes handling the case when we send the changes
|
||||
* prior to commit or rollback as we hit the allowed 'batch size'.
|
||||
*/
|
||||
public class TChangeLogHolder {
|
||||
|
||||
/**
|
||||
* The owning transaction.
|
||||
*/
|
||||
private final SpiTransaction owner;
|
||||
|
||||
/**
|
||||
* A transaction id that can be used to join many changeSets when
|
||||
* we send multiple for a large transaction.
|
||||
*/
|
||||
private final String transactionId;
|
||||
|
||||
/**
|
||||
* When we hit batch size then send the changeSet even when the tranaction
|
||||
* has not yet completed.
|
||||
*/
|
||||
private final int batchSize;
|
||||
|
||||
/**
|
||||
* The changes we collect to send to the listener.
|
||||
*/
|
||||
private ChangeSet changes;
|
||||
|
||||
private long batchId;
|
||||
|
||||
/**
|
||||
* Counter to check when we hit the batch size.
|
||||
*/
|
||||
private int count;
|
||||
|
||||
/**
|
||||
* Construct with the owning transaction and batch size to use.
|
||||
*/
|
||||
public TChangeLogHolder(SpiTransaction owner, int batchSize) {
|
||||
this.owner = owner;
|
||||
this.transactionId = UUID.randomUUID().toString();
|
||||
this.batchSize = batchSize;
|
||||
this.changes = new ChangeSet(transactionId, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a bean change to the change set.
|
||||
*/
|
||||
public void addBeanChange(BeanChange change) {
|
||||
|
||||
changes.addBeanChange(change);
|
||||
if (++count >= batchSize) {
|
||||
// we hit the batch size so send what we have knowing
|
||||
// that the transaction has not completed yet and
|
||||
// reset the changes and count
|
||||
owner.sendChangeLog(changes);
|
||||
changes = new ChangeSet(transactionId, ++batchId);
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* On post commit send the changes we have collected.
|
||||
*/
|
||||
public void postCommit() {
|
||||
changes.setTxnState(TxnState.COMMITTED);
|
||||
owner.sendChangeLog(changes);
|
||||
}
|
||||
|
||||
/**
|
||||
* On post rollback send the changes we have collected and
|
||||
* leave it up to the listener to decide what to do.
|
||||
*/
|
||||
public void postRollback() {
|
||||
changes.setTxnState(TxnState.ROLLBACK);
|
||||
owner.sendChangeLog(changes);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,9 @@ import com.avaje.ebean.config.ServerConfig;
|
||||
import com.avaje.ebean.config.dbplatform.DatabasePlatform.OnQueryOnly;
|
||||
import com.avaje.ebean.dbmigration.DbOffline;
|
||||
import com.avaje.ebean.event.TransactionEventListener;
|
||||
import com.avaje.ebean.event.changelog.ChangeLogListener;
|
||||
import com.avaje.ebean.event.changelog.ChangeLogPrepare;
|
||||
import com.avaje.ebean.event.changelog.ChangeSet;
|
||||
import com.avaje.ebeaninternal.api.SpiTransaction;
|
||||
import com.avaje.ebeaninternal.api.TransactionEvent;
|
||||
import com.avaje.ebeaninternal.api.TransactionEventTable;
|
||||
@@ -80,6 +83,17 @@ public class TransactionManager {
|
||||
|
||||
protected final TransactionEventListener[] transactionEventListeners;
|
||||
|
||||
/**
|
||||
* Used to prepare the change set setting user context information in the
|
||||
* foreground thread before logging.
|
||||
*/
|
||||
private final ChangeLogPrepare changeLogPrepare;
|
||||
|
||||
/**
|
||||
* Performs the actual logging of the change set in background.
|
||||
*/
|
||||
private final ChangeLogListener changeLogListener;
|
||||
|
||||
/**
|
||||
* Create the TransactionManager
|
||||
*/
|
||||
@@ -89,6 +103,8 @@ public class TransactionManager {
|
||||
this.persistBatch = config.getPersistBatch();
|
||||
this.persistBatchOnCascade = config.appliedPersistBatchOnCascade();
|
||||
this.beanDescriptorManager = descMgr;
|
||||
this.changeLogPrepare = descMgr.getChangeLogPrepare();
|
||||
this.changeLogListener = descMgr.getChangeLogListener();
|
||||
this.clusterManager = clusterManager;
|
||||
this.serverName = config.getName();
|
||||
this.backgroundExecutor = backgroundExecutor;
|
||||
@@ -439,4 +455,21 @@ public class TransactionManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare and then send/log the changeSet.
|
||||
*/
|
||||
public void sendChangeLog(final ChangeSet changeSet) {
|
||||
|
||||
// can set userId, userIpAddress & userContext if desired
|
||||
if (changeLogPrepare.prepare(changeSet)) {
|
||||
|
||||
// call the log method in background
|
||||
backgroundExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
changeLogListener.log(changeSet);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user