HHH-13361 Refactor mappers
This commit is contained in:
parent
b5755b6945
commit
aa5bdab6ec
|
@ -0,0 +1,136 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.envers.internal.entities.mapper;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.hibernate.envers.exception.AuditException;
|
||||
import org.hibernate.envers.internal.entities.PropertyData;
|
||||
import org.hibernate.envers.internal.tools.ReflectionTools;
|
||||
import org.hibernate.internal.util.ReflectHelper;
|
||||
import org.hibernate.property.access.spi.Getter;
|
||||
import org.hibernate.property.access.spi.Setter;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
/**
|
||||
* A base class for all entity mapper implementations.
|
||||
*
|
||||
* @author Chris Cranford
|
||||
*/
|
||||
public abstract class AbstractMapper {
|
||||
|
||||
/**
|
||||
* Perform an action in a privileged block.
|
||||
*
|
||||
* @param block the lambda to executed in privileged.
|
||||
* @param <T> the return type
|
||||
* @return the result of the privileged call, may be {@literal null}
|
||||
*/
|
||||
protected <T> T doPrivileged(Supplier<T> block) {
|
||||
if ( System.getSecurityManager() != null ) {
|
||||
return AccessController.doPrivileged( (PrivilegedAction<T>) block::get );
|
||||
}
|
||||
else {
|
||||
return block.get();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a value from the specified object.
|
||||
*
|
||||
* @param propertyData the property data, should not be {@literal null}
|
||||
* @param object the object for which the value should be read, should not be {@literal null}
|
||||
* @param serviceRegistry the service registry, should not be {@literal null}
|
||||
* @param <T> the return type
|
||||
* @return the value read from the object, may be {@literal null}
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <T> T getValueFromObject(PropertyData propertyData, Object object, ServiceRegistry serviceRegistry) {
|
||||
return doPrivileged( () -> {
|
||||
final Getter getter = ReflectionTools.getGetter( object.getClass(), propertyData, serviceRegistry );
|
||||
return (T) getter.get( object );
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a value from the specified object.
|
||||
*
|
||||
* @param propertyName the property name, should not be {@literal null}
|
||||
* @param accessType the property access type, should not be {@literal null}
|
||||
* @param object the object for hwich the value should be read, should not be {@literal null}
|
||||
* @param serviceRegistry the service registry, should not be {@literal null}
|
||||
* @param <T> the return type
|
||||
* @return the value read from the object, may be {@literal null}
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <T> T getValueFromObject(String propertyName, String accessType, Object object, ServiceRegistry serviceRegistry) {
|
||||
return doPrivileged( () -> {
|
||||
final Getter getter = ReflectionTools.getGetter( object.getClass(), propertyName, accessType, serviceRegistry );
|
||||
return (T) getter.get( object );
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the specified value on the object.
|
||||
*
|
||||
* @param propertyData the property data, should not be {@literal null}
|
||||
* @param object the object for which the value should be set, should not be {@literal null}
|
||||
* @param value the value ot be set, may be {@literal null}
|
||||
* @param serviceRegistry the service registry, should not be {@literal null}
|
||||
*/
|
||||
protected void setValueOnObject(PropertyData propertyData, Object object, Object value, ServiceRegistry serviceRegistry) {
|
||||
doPrivileged( () -> {
|
||||
final Setter setter = ReflectionTools.getSetter(object.getClass(), propertyData, serviceRegistry );
|
||||
setter.set( object, value );
|
||||
return null;
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value from the source object and sets the value in the destination object.
|
||||
*
|
||||
* @param propertyData the property data, should not be {@literal null}
|
||||
* @param source the source object, should not be {@literal null}
|
||||
* @param destination the destination object, should not be {@literal null}
|
||||
* @param serviceRegistry the service registry, should not be {@literal null}
|
||||
*/
|
||||
protected void getAndSetValue(PropertyData propertyData, Object source, Object destination, ServiceRegistry serviceRegistry) {
|
||||
doPrivileged( () -> {
|
||||
final Getter getter = ReflectionTools.getGetter( source.getClass(), propertyData, serviceRegistry );
|
||||
final Setter setter = ReflectionTools.getSetter( destination.getClass(), propertyData, serviceRegistry );
|
||||
setter.set( destination, getter.get( source ) );
|
||||
return null;
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new object based on the specified class with the given constructor arguments.
|
||||
*
|
||||
* @param clazz the class, must not be {@literal null}
|
||||
* @param args the variadic constructor arguments, may be omitted.
|
||||
* @param <T> the return class type
|
||||
* @return a new instance of the class
|
||||
*/
|
||||
protected <T> T newObjectInstance(Class<T> clazz, Object... args) {
|
||||
return doPrivileged( () -> {
|
||||
try {
|
||||
final Constructor<T> constructor = ReflectHelper.getDefaultConstructor( clazz );
|
||||
if ( constructor == null ) {
|
||||
throw new AuditException( "Failed to locate default constructor for class: " + clazz.getName() );
|
||||
}
|
||||
return constructor.newInstance( args );
|
||||
}
|
||||
catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
||||
throw new AuditException( e );
|
||||
}
|
||||
} );
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ package org.hibernate.envers.internal.entities.mapper;
|
|||
*
|
||||
* @author Chris Cranford
|
||||
*/
|
||||
public abstract class AbstractPropertyMapper implements PropertyMapper {
|
||||
public abstract class AbstractPropertyMapper extends AbstractMapper implements PropertyMapper {
|
||||
private boolean map;
|
||||
|
||||
@Override
|
||||
|
|
|
@ -7,8 +7,6 @@
|
|||
package org.hibernate.envers.internal.entities.mapper;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -118,10 +116,7 @@ public class ComponentPropertyMapper extends AbstractPropertyMapper implements C
|
|||
return;
|
||||
}
|
||||
|
||||
AccessController.doPrivileged(
|
||||
new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
doPrivileged( () -> {
|
||||
try {
|
||||
final Object subObj = ReflectHelper.getDefaultConstructor( componentClass ).newInstance();
|
||||
|
||||
|
@ -152,9 +147,7 @@ public class ComponentPropertyMapper extends AbstractPropertyMapper implements C
|
|||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
);
|
||||
} );
|
||||
}
|
||||
|
||||
private boolean isAllPropertiesNull(Map data) {
|
||||
|
|
|
@ -7,8 +7,6 @@
|
|||
package org.hibernate.envers.internal.entities.mapper;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -101,10 +99,7 @@ public class MultiPropertyMapper extends AbstractPropertyMapper implements Exten
|
|||
final Map<String, Object> data,
|
||||
final Object newObj,
|
||||
final Object oldObj) {
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<Boolean>() {
|
||||
@Override
|
||||
public Boolean run() {
|
||||
return doPrivileged( () -> {
|
||||
boolean ret = false;
|
||||
for ( Map.Entry<PropertyData, PropertyMapper> entry : properties.entrySet() ) {
|
||||
final PropertyData propertyData = entry.getKey();
|
||||
|
@ -141,9 +136,7 @@ public class MultiPropertyMapper extends AbstractPropertyMapper implements Exten
|
|||
);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
);
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -152,10 +145,7 @@ public class MultiPropertyMapper extends AbstractPropertyMapper implements Exten
|
|||
final Map<String, Object> data,
|
||||
final Object newObj,
|
||||
final Object oldObj) {
|
||||
AccessController.doPrivileged(
|
||||
new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
doPrivileged( () -> {
|
||||
for ( Map.Entry<PropertyData, PropertyMapper> entry : properties.entrySet() ) {
|
||||
final PropertyData propertyData = entry.getKey();
|
||||
final PropertyMapper propertyMapper = entry.getValue();
|
||||
|
@ -192,9 +182,7 @@ public class MultiPropertyMapper extends AbstractPropertyMapper implements Exten
|
|||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
);
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -7,8 +7,6 @@
|
|||
package org.hibernate.envers.internal.entities.mapper;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
@ -105,10 +103,7 @@ public class SinglePropertyMapper extends AbstractPropertyMapper implements Simp
|
|||
map.put( propertyData.getBeanName(), value );
|
||||
}
|
||||
else {
|
||||
AccessController.doPrivileged(
|
||||
new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
doPrivileged( () -> {
|
||||
final Setter setter = ReflectionTools.getSetter(
|
||||
obj.getClass(),
|
||||
propertyData,
|
||||
|
@ -121,9 +116,7 @@ public class SinglePropertyMapper extends AbstractPropertyMapper implements Simp
|
|||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
);
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,8 +6,6 @@
|
|||
*/
|
||||
package org.hibernate.envers.internal.entities.mapper.id;
|
||||
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.envers.exception.AuditException;
|
||||
|
@ -66,18 +64,13 @@ public abstract class AbstractCompositeIdMapper extends AbstractIdMapper impleme
|
|||
}
|
||||
|
||||
protected Object instantiateCompositeId() {
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
return doPrivileged( () -> {
|
||||
try {
|
||||
return ReflectHelper.getDefaultConstructor( compositeIdClass ).newInstance();
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
throw new AuditException( e );
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,8 @@ package org.hibernate.envers.internal.entities.mapper.id;
|
|||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.envers.internal.entities.PropertyData;
|
||||
import org.hibernate.envers.internal.entities.mapper.AbstractMapper;
|
||||
import org.hibernate.envers.internal.tools.query.Parameters;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
|
@ -18,7 +20,7 @@ import org.hibernate.service.ServiceRegistry;
|
|||
* @author Adam Warski (adam at warski dot org)
|
||||
* @author Chris Cranford
|
||||
*/
|
||||
public abstract class AbstractIdMapper implements IdMapper {
|
||||
public abstract class AbstractIdMapper extends AbstractMapper implements IdMapper {
|
||||
private final ServiceRegistry serviceRegistry;
|
||||
|
||||
public AbstractIdMapper(ServiceRegistry serviceRegistry) {
|
||||
|
@ -143,6 +145,18 @@ public abstract class AbstractIdMapper implements IdMapper {
|
|||
|
||||
public abstract void mapToEntityFromEntity(Object objectTo, Object objectFrom);
|
||||
|
||||
protected <T> T getValueFromObject(PropertyData propertyData, Object object) {
|
||||
return getValueFromObject( propertyData, object, getServiceRegistry() );
|
||||
}
|
||||
|
||||
protected void setValueOnObject(PropertyData propertyData, Object object, Object value) {
|
||||
setValueOnObject( propertyData, object, value, getServiceRegistry() );
|
||||
}
|
||||
|
||||
protected void getAndSetValue(PropertyData propertyData, Object source, Object destination) {
|
||||
getAndSetValue( propertyData, source, destination, getServiceRegistry() );
|
||||
}
|
||||
|
||||
private void handleNullValue(Parameters parameters, String alias, String propertyName, boolean equals) {
|
||||
if ( equals ) {
|
||||
parameters.addNullRestriction( alias, propertyName );
|
||||
|
|
|
@ -6,8 +6,6 @@
|
|||
*/
|
||||
package org.hibernate.envers.internal.entities.mapper.id;
|
||||
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
@ -17,7 +15,6 @@ import org.hibernate.envers.exception.AuditException;
|
|||
import org.hibernate.envers.internal.entities.PropertyData;
|
||||
import org.hibernate.envers.internal.tools.ReflectionTools;
|
||||
import org.hibernate.mapping.Component;
|
||||
import org.hibernate.property.access.spi.Getter;
|
||||
import org.hibernate.property.access.spi.Setter;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
|
@ -52,22 +49,7 @@ public class EmbeddedIdMapper extends AbstractCompositeIdMapper implements Simpl
|
|||
if ( obj == null ) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Object value = AccessController.doPrivileged(
|
||||
new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
final Getter getter = ReflectionTools.getGetter(
|
||||
obj.getClass(),
|
||||
idPropertyData,
|
||||
getServiceRegistry()
|
||||
);
|
||||
return getter.get( obj );
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
mapToMapFromId( data, value );
|
||||
mapToMapFromId( data, getValueFromObject( idPropertyData, obj ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -76,12 +58,8 @@ public class EmbeddedIdMapper extends AbstractCompositeIdMapper implements Simpl
|
|||
return false;
|
||||
}
|
||||
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<Boolean>() {
|
||||
@Override
|
||||
public Boolean run() {
|
||||
return doPrivileged( () -> {
|
||||
final Setter setter = ReflectionTools.getSetter( obj.getClass(), idPropertyData, getServiceRegistry() );
|
||||
|
||||
try {
|
||||
final Object subObj = instantiateCompositeId();
|
||||
|
||||
|
@ -99,9 +77,7 @@ public class EmbeddedIdMapper extends AbstractCompositeIdMapper implements Simpl
|
|||
catch (Exception e) {
|
||||
throw new AuditException( e );
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -122,19 +98,7 @@ public class EmbeddedIdMapper extends AbstractCompositeIdMapper implements Simpl
|
|||
return null;
|
||||
}
|
||||
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
final Getter getter = ReflectionTools.getGetter(
|
||||
data.getClass(),
|
||||
idPropertyData,
|
||||
getServiceRegistry()
|
||||
);
|
||||
return getter.get( data );
|
||||
}
|
||||
}
|
||||
);
|
||||
return getValueFromObject( idPropertyData, data );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -6,17 +6,12 @@
|
|||
*/
|
||||
package org.hibernate.envers.internal.entities.mapper.id;
|
||||
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.envers.exception.AuditException;
|
||||
import org.hibernate.envers.internal.entities.PropertyData;
|
||||
import org.hibernate.envers.internal.tools.ReflectionTools;
|
||||
import org.hibernate.property.access.spi.Getter;
|
||||
import org.hibernate.property.access.spi.Setter;
|
||||
import org.hibernate.proxy.HibernateProxy;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
|
@ -63,21 +58,9 @@ public class SingleIdMapper extends AbstractIdMapper implements SimpleIdMapperBu
|
|||
return false;
|
||||
}
|
||||
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<Boolean>() {
|
||||
@Override
|
||||
public Boolean run() {
|
||||
final Setter setter = ReflectionTools.getSetter(
|
||||
obj.getClass(),
|
||||
propertyData,
|
||||
getServiceRegistry()
|
||||
);
|
||||
setter.set( obj, value );
|
||||
setValueOnObject( propertyData, obj, value );
|
||||
return true;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object mapToIdFromMap(Map data) {
|
||||
|
@ -99,19 +82,7 @@ public class SingleIdMapper extends AbstractIdMapper implements SimpleIdMapperBu
|
|||
return hibernateProxy.getHibernateLazyInitializer().getInternalIdentifier();
|
||||
}
|
||||
else {
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
final Getter getter = ReflectionTools.getGetter(
|
||||
data.getClass(),
|
||||
propertyData,
|
||||
getServiceRegistry()
|
||||
);
|
||||
return getter.get( data );
|
||||
}
|
||||
}
|
||||
);
|
||||
return getValueFromObject( propertyData, data );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -133,19 +104,7 @@ public class SingleIdMapper extends AbstractIdMapper implements SimpleIdMapperBu
|
|||
data.put( propertyData.getName(), hibernateProxy.getHibernateLazyInitializer().getInternalIdentifier() );
|
||||
}
|
||||
else {
|
||||
final Object value = AccessController.doPrivileged(
|
||||
new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
final Getter getter = ReflectionTools.getGetter(
|
||||
obj.getClass(),
|
||||
propertyData,
|
||||
getServiceRegistry()
|
||||
);
|
||||
return getter.get( obj );
|
||||
}
|
||||
}
|
||||
);
|
||||
final Object value = getValueFromObject( propertyData, obj );
|
||||
data.put( propertyData.getName(), value );
|
||||
}
|
||||
}
|
||||
|
@ -156,28 +115,7 @@ public class SingleIdMapper extends AbstractIdMapper implements SimpleIdMapperBu
|
|||
if ( objTo == null || objFrom == null ) {
|
||||
return;
|
||||
}
|
||||
|
||||
AccessController.doPrivileged(
|
||||
new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
final Getter getter = ReflectionTools.getGetter(
|
||||
objFrom.getClass(),
|
||||
propertyData,
|
||||
getServiceRegistry()
|
||||
);
|
||||
|
||||
final Setter setter = ReflectionTools.getSetter(
|
||||
objTo.getClass(),
|
||||
propertyData,
|
||||
getServiceRegistry()
|
||||
);
|
||||
|
||||
setter.set( objTo, getter.get( objFrom ) );
|
||||
return null;
|
||||
}
|
||||
}
|
||||
);
|
||||
getAndSetValue(propertyData, objFrom, objTo );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -6,9 +6,6 @@
|
|||
*/
|
||||
package org.hibernate.envers.internal.entities.mapper.id;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.Session;
|
||||
|
@ -48,19 +45,7 @@ public class VirtualEntitySingleIdMapper extends SingleIdMapper {
|
|||
|
||||
@Override
|
||||
public void mapToMapFromId(Session session, Map<String, Object> data, Object obj) {
|
||||
final Serializable value = AccessController.doPrivileged(
|
||||
new PrivilegedAction<Serializable>() {
|
||||
@Override
|
||||
public Serializable run() {
|
||||
final Getter getter = ReflectionTools.getGetter(
|
||||
obj.getClass(),
|
||||
propertyData,
|
||||
getServiceRegistry()
|
||||
);
|
||||
return (Serializable) getter.get( obj );
|
||||
}
|
||||
}
|
||||
);
|
||||
final Object value = getValueFromObject( propertyData, obj );
|
||||
|
||||
// Either loads the entity from the session's 1LC if it already exists or potentially creates a
|
||||
// proxy object to represent the entity by identifier so that we can reference it in the map.
|
||||
|
@ -74,10 +59,7 @@ public class VirtualEntitySingleIdMapper extends SingleIdMapper {
|
|||
return;
|
||||
}
|
||||
|
||||
AccessController.doPrivileged(
|
||||
new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
doPrivileged( () -> {
|
||||
final Getter getter = ReflectionTools.getGetter(
|
||||
objFrom.getClass(),
|
||||
propertyData,
|
||||
|
@ -105,9 +87,7 @@ public class VirtualEntitySingleIdMapper extends SingleIdMapper {
|
|||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
);
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -121,10 +101,7 @@ public class VirtualEntitySingleIdMapper extends SingleIdMapper {
|
|||
return false;
|
||||
}
|
||||
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<Boolean>() {
|
||||
@Override
|
||||
public Boolean run() {
|
||||
return doPrivileged( () -> {
|
||||
final Setter setter = ReflectionTools.getSetter(
|
||||
obj.getClass(),
|
||||
propertyData,
|
||||
|
@ -144,9 +121,7 @@ public class VirtualEntitySingleIdMapper extends SingleIdMapper {
|
|||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
);
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -160,19 +135,7 @@ public class VirtualEntitySingleIdMapper extends SingleIdMapper {
|
|||
data.put( propertyData.getName(), proxy.getHibernateLazyInitializer().getInternalIdentifier() );
|
||||
}
|
||||
else {
|
||||
final Object value = AccessController.doPrivileged(
|
||||
new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
final Getter getter = ReflectionTools.getGetter(
|
||||
obj.getClass(),
|
||||
propertyData,
|
||||
getServiceRegistry()
|
||||
);
|
||||
return getter.get( obj );
|
||||
}
|
||||
}
|
||||
);
|
||||
final Object value = getValueFromObject( propertyData, obj );
|
||||
|
||||
if ( propertyData.getVirtualReturnClass().isInstance( value ) ) {
|
||||
// The value is the primary key, need to map it via IdMapper
|
||||
|
|
|
@ -8,8 +8,6 @@ package org.hibernate.envers.internal.entities.mapper.relation;
|
|||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -30,9 +28,7 @@ import org.hibernate.envers.internal.entities.mapper.AbstractPropertyMapper;
|
|||
import org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData;
|
||||
import org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor.Initializor;
|
||||
import org.hibernate.envers.internal.reader.AuditReaderImplementor;
|
||||
import org.hibernate.envers.internal.tools.ReflectionTools;
|
||||
import org.hibernate.persister.collection.CollectionPersister;
|
||||
import org.hibernate.property.access.spi.Setter;
|
||||
|
||||
/**
|
||||
* @author Adam Warski (adam at warski dot org)
|
||||
|
@ -306,22 +302,7 @@ public abstract class AbstractCollectionMapper<T> extends AbstractPropertyMapper
|
|||
map.put( collectionPropertyData.getBeanName(), collectionProxy );
|
||||
}
|
||||
else {
|
||||
AccessController.doPrivileged(
|
||||
new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
final Setter setter = ReflectionTools.getSetter(
|
||||
obj.getClass(),
|
||||
collectionPropertyData,
|
||||
enversService.getServiceRegistry()
|
||||
);
|
||||
|
||||
setter.set( obj, collectionProxy );
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
);
|
||||
setValueOnObject( collectionPropertyData, obj, collectionProxy, enversService.getServiceRegistry() );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,8 +7,6 @@
|
|||
package org.hibernate.envers.internal.entities.mapper.relation;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -21,7 +19,6 @@ import org.hibernate.envers.internal.entities.mapper.AbstractPropertyMapper;
|
|||
import org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData;
|
||||
import org.hibernate.envers.internal.reader.AuditReaderImplementor;
|
||||
import org.hibernate.envers.internal.tools.ReflectionTools;
|
||||
import org.hibernate.property.access.spi.Setter;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
/**
|
||||
|
@ -96,21 +93,7 @@ public abstract class AbstractToOneMapper extends AbstractPropertyMapper {
|
|||
map.put( propertyData.getBeanName(), value );
|
||||
}
|
||||
else {
|
||||
AccessController.doPrivileged(
|
||||
new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
final Setter setter = ReflectionTools.getSetter(
|
||||
targetObject.getClass(),
|
||||
propertyData,
|
||||
serviceRegistry
|
||||
);
|
||||
setter.set( targetObject, value );
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
);
|
||||
setValueOnObject( propertyData, targetObject, value, serviceRegistry );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.envers.internal.entities.mapper.relation.component;
|
||||
|
||||
import org.hibernate.envers.internal.entities.mapper.AbstractMapper;
|
||||
|
||||
/**
|
||||
* An abstract base class for all middle component mappers.
|
||||
*
|
||||
* @author Chris Cranford
|
||||
*/
|
||||
public abstract class AbstractMiddleComponentMapper extends AbstractMapper implements MiddleComponentMapper {
|
||||
|
||||
}
|
|
@ -15,7 +15,7 @@ import org.hibernate.envers.internal.tools.query.Parameters;
|
|||
/**
|
||||
* @author Adam Warski (adam at warski dot org)
|
||||
*/
|
||||
public final class MiddleDummyComponentMapper implements MiddleComponentMapper {
|
||||
public final class MiddleDummyComponentMapper extends AbstractMiddleComponentMapper {
|
||||
public Object mapToObjectFromFullMap(
|
||||
EntityInstantiator entityInstantiator, Map<String, Object> data,
|
||||
Object dataObject, Number revision) {
|
||||
|
|
|
@ -6,9 +6,6 @@
|
|||
*/
|
||||
package org.hibernate.envers.internal.entities.mapper.relation.component;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
|
@ -20,16 +17,16 @@ import org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper;
|
|||
import org.hibernate.envers.internal.entities.mapper.PropertyMapper;
|
||||
import org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper;
|
||||
import org.hibernate.envers.internal.tools.query.Parameters;
|
||||
import org.hibernate.internal.util.ReflectHelper;
|
||||
|
||||
/**
|
||||
* @author Kristoffer Lundberg (kristoffer at cambio dot se)
|
||||
*/
|
||||
public class MiddleEmbeddableComponentMapper implements MiddleComponentMapper, CompositeMapperBuilder {
|
||||
private final MultiPropertyMapper delegate;
|
||||
private final Class componentClass;
|
||||
public class MiddleEmbeddableComponentMapper extends AbstractMiddleComponentMapper implements CompositeMapperBuilder {
|
||||
|
||||
public MiddleEmbeddableComponentMapper(MultiPropertyMapper delegate, Class componentClass) {
|
||||
private final MultiPropertyMapper delegate;
|
||||
private final Class<?> componentClass;
|
||||
|
||||
public MiddleEmbeddableComponentMapper(MultiPropertyMapper delegate, Class<?> componentClass) {
|
||||
this.delegate = delegate;
|
||||
this.componentClass = componentClass;
|
||||
}
|
||||
|
@ -65,26 +62,7 @@ public class MiddleEmbeddableComponentMapper implements MiddleComponentMapper, C
|
|||
if ( dataObject != null ) {
|
||||
return dataObject;
|
||||
}
|
||||
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
try {
|
||||
return ReflectHelper.getDefaultConstructor( componentClass ).newInstance();
|
||||
}
|
||||
catch ( InstantiationException e ) {
|
||||
throw new AuditException( e );
|
||||
}
|
||||
catch ( IllegalAccessException e ) {
|
||||
throw new AuditException( e );
|
||||
}
|
||||
catch ( InvocationTargetException e ) {
|
||||
throw new AuditException( e );
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
return newObjectInstance( componentClass );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -28,7 +28,7 @@ import org.hibernate.envers.internal.tools.query.Parameters;
|
|||
*
|
||||
* @author Chris Cranford
|
||||
*/
|
||||
public class MiddleMapElementNotKeyComponentMapper implements MiddleComponentMapper {
|
||||
public class MiddleMapElementNotKeyComponentMapper extends AbstractMiddleComponentMapper {
|
||||
private final String propertyName;
|
||||
|
||||
public MiddleMapElementNotKeyComponentMapper(String propertyName) {
|
||||
|
|
|
@ -22,7 +22,7 @@ import org.hibernate.envers.internal.tools.query.Parameters;
|
|||
* @author Adam Warski (adam at warski dot org)
|
||||
* @author Chris Cranford
|
||||
*/
|
||||
public final class MiddleMapKeyIdComponentMapper implements MiddleComponentMapper {
|
||||
public final class MiddleMapKeyIdComponentMapper extends AbstractMiddleComponentMapper {
|
||||
private final Configuration configuration;
|
||||
private final IdMapper relatedIdMapper;
|
||||
|
||||
|
|
|
@ -6,15 +6,11 @@
|
|||
*/
|
||||
package org.hibernate.envers.internal.entities.mapper.relation.component;
|
||||
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
import org.hibernate.envers.internal.entities.EntityInstantiator;
|
||||
import org.hibernate.envers.internal.tools.ReflectionTools;
|
||||
import org.hibernate.envers.internal.tools.query.Parameters;
|
||||
import org.hibernate.property.access.spi.Getter;
|
||||
|
||||
/**
|
||||
* A component mapper for the @MapKey mapping with the name parameter specified: the value of the map's key
|
||||
|
@ -23,7 +19,7 @@ import org.hibernate.property.access.spi.Getter;
|
|||
*
|
||||
* @author Adam Warski (adam at warski dot org)
|
||||
*/
|
||||
public class MiddleMapKeyPropertyComponentMapper implements MiddleComponentMapper {
|
||||
public class MiddleMapKeyPropertyComponentMapper extends AbstractMiddleComponentMapper {
|
||||
private final String propertyName;
|
||||
private final String accessType;
|
||||
|
||||
|
@ -39,20 +35,12 @@ public class MiddleMapKeyPropertyComponentMapper implements MiddleComponentMappe
|
|||
final Object dataObject,
|
||||
Number revision) {
|
||||
// dataObject is not null, as this mapper can only be used in an index.
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
final Getter getter = ReflectionTools.getGetter(
|
||||
dataObject.getClass(),
|
||||
return getValueFromObject(
|
||||
propertyName,
|
||||
accessType,
|
||||
dataObject,
|
||||
entityInstantiator.getEnversService().getServiceRegistry()
|
||||
);
|
||||
return getter.get( dataObject );
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -16,7 +16,7 @@ import org.hibernate.envers.internal.tools.query.Parameters;
|
|||
/**
|
||||
* @author Adam Warski (adam at warski dot org)
|
||||
*/
|
||||
public final class MiddleRelatedComponentMapper implements MiddleComponentMapper {
|
||||
public final class MiddleRelatedComponentMapper extends AbstractMiddleComponentMapper {
|
||||
private final MiddleIdData relatedIdData;
|
||||
|
||||
public MiddleRelatedComponentMapper(MiddleIdData relatedIdData) {
|
||||
|
|
|
@ -17,7 +17,7 @@ import org.hibernate.envers.internal.tools.query.Parameters;
|
|||
* @author Adam Warski (adam at warski dot org)
|
||||
* @author Chris Cranford
|
||||
*/
|
||||
public final class MiddleSimpleComponentMapper implements MiddleComponentMapper {
|
||||
public final class MiddleSimpleComponentMapper extends AbstractMiddleComponentMapper {
|
||||
private final Configuration configuration;
|
||||
private final String propertyName;
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ import org.hibernate.envers.internal.tools.query.Parameters;
|
|||
*
|
||||
* @author Adam Warski (adam at warski dot org)
|
||||
*/
|
||||
public final class MiddleStraightComponentMapper implements MiddleComponentMapper {
|
||||
public final class MiddleStraightComponentMapper extends AbstractMiddleComponentMapper {
|
||||
private final String propertyName;
|
||||
|
||||
public MiddleStraightComponentMapper(String propertyName) {
|
||||
|
|
|
@ -6,13 +6,20 @@
|
|||
*/
|
||||
package org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.envers.boot.internal.EnversService;
|
||||
import org.hibernate.envers.exception.AuditException;
|
||||
import org.hibernate.envers.internal.entities.EntityInstantiator;
|
||||
import org.hibernate.envers.internal.entities.mapper.relation.query.RelationQueryGenerator;
|
||||
import org.hibernate.envers.internal.reader.AuditReaderImplementor;
|
||||
import org.hibernate.internal.util.ReflectHelper;
|
||||
|
||||
/**
|
||||
* Initializes a persistent collection.
|
||||
|
@ -58,4 +65,43 @@ public abstract class AbstractCollectionInitializor<T> implements Initializor<T>
|
|||
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform an action in a privileged block.
|
||||
*
|
||||
* @param block the lambda to executed in privileged.
|
||||
* @param <R> the return type
|
||||
* @return the result of the privileged call, may be {@literal null}
|
||||
*/
|
||||
protected <R> R doPrivileged(Supplier<R> block) {
|
||||
if ( System.getSecurityManager() != null ) {
|
||||
return AccessController.doPrivileged( (PrivilegedAction<R>) block::get );
|
||||
}
|
||||
else {
|
||||
return block.get();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new object based on the specified class with the given constructor arguments.
|
||||
*
|
||||
* @param clazz the class, must not be {@literal null}
|
||||
* @param args the variadic constructor arguments, may be omitted.
|
||||
* @param <R> the return class type
|
||||
* @return a new instance of the class
|
||||
*/
|
||||
protected <R> R newObjectInstance(Class<R> clazz, Object... args) {
|
||||
return doPrivileged( () -> {
|
||||
try {
|
||||
final Constructor<R> constructor = ReflectHelper.getDefaultConstructor( clazz );
|
||||
if ( constructor == null ) {
|
||||
throw new AuditException( "Failed to locate default constructor for class: " + clazz.getName() );
|
||||
}
|
||||
return constructor.newInstance( args );
|
||||
}
|
||||
catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
||||
throw new AuditException( e );
|
||||
}
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,19 +6,14 @@
|
|||
*/
|
||||
package org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.envers.boot.internal.EnversService;
|
||||
import org.hibernate.envers.exception.AuditException;
|
||||
import org.hibernate.envers.internal.entities.mapper.relation.MiddleComponentData;
|
||||
import org.hibernate.envers.internal.entities.mapper.relation.query.RelationQueryGenerator;
|
||||
import org.hibernate.envers.internal.reader.AuditReaderImplementor;
|
||||
import org.hibernate.internal.util.ReflectHelper;
|
||||
|
||||
/**
|
||||
* Initializes a non-indexed java collection (set or list, eventually sorted).
|
||||
|
@ -45,25 +40,7 @@ public class BasicCollectionInitializor<T extends Collection> extends AbstractCo
|
|||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected T initializeCollection(int size) {
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<T>() {
|
||||
@Override
|
||||
public T run() {
|
||||
try {
|
||||
return (T) ReflectHelper.getDefaultConstructor( collectionClass ).newInstance();
|
||||
}
|
||||
catch (InstantiationException e) {
|
||||
throw new AuditException( e );
|
||||
}
|
||||
catch (IllegalAccessException e) {
|
||||
throw new AuditException( e );
|
||||
}
|
||||
catch (InvocationTargetException e) {
|
||||
throw new AuditException( e );
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
return newObjectInstance( collectionClass );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -6,18 +6,13 @@
|
|||
*/
|
||||
package org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.envers.boot.internal.EnversService;
|
||||
import org.hibernate.envers.exception.AuditException;
|
||||
import org.hibernate.envers.internal.entities.mapper.relation.MiddleComponentData;
|
||||
import org.hibernate.envers.internal.entities.mapper.relation.query.RelationQueryGenerator;
|
||||
import org.hibernate.envers.internal.reader.AuditReaderImplementor;
|
||||
import org.hibernate.internal.util.ReflectHelper;
|
||||
|
||||
/**
|
||||
* Initializes a map.
|
||||
|
@ -47,25 +42,7 @@ public class MapCollectionInitializor<T extends Map> extends AbstractCollectionI
|
|||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected T initializeCollection(int size) {
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<T>() {
|
||||
@Override
|
||||
public T run() {
|
||||
try {
|
||||
return (T) ReflectHelper.getDefaultConstructor( collectionClass ).newInstance();
|
||||
}
|
||||
catch (InstantiationException e) {
|
||||
throw new AuditException( e );
|
||||
}
|
||||
catch (IllegalAccessException e) {
|
||||
throw new AuditException( e );
|
||||
}
|
||||
catch (InvocationTargetException e) {
|
||||
throw new AuditException( e );
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
return newObjectInstance( collectionClass );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -51,6 +51,7 @@ public class SortedMapCollectionInitializor extends MapCollectionInitializor<Sor
|
|||
if ( comparator == null ) {
|
||||
return super.initializeCollection( size );
|
||||
}
|
||||
return doPrivileged( () -> {
|
||||
try {
|
||||
return collectionClass.getConstructor( Comparator.class ).newInstance( comparator );
|
||||
}
|
||||
|
@ -66,6 +67,7 @@ public class SortedMapCollectionInitializor extends MapCollectionInitializor<Sor
|
|||
catch (InvocationTargetException e) {
|
||||
throw new AuditException( e );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -52,8 +52,9 @@ public class SortedSetCollectionInitializor extends BasicCollectionInitializor<S
|
|||
if ( comparator == null ) {
|
||||
return super.initializeCollection( size );
|
||||
}
|
||||
return doPrivileged( () -> {
|
||||
try {
|
||||
return collectionClass.getConstructor( Comparator.class ).newInstance( comparator );
|
||||
return collectionClass.getConstructor( Comparator.class ).newInstance(comparator);
|
||||
}
|
||||
catch (InstantiationException e) {
|
||||
throw new AuditException( e );
|
||||
|
@ -67,5 +68,6 @@ public class SortedSetCollectionInitializor extends BasicCollectionInitializor<S
|
|||
catch (InvocationTargetException e) {
|
||||
throw new AuditException( e );
|
||||
}
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue