|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.envers.internal.entities.mapper; |
| 8 | + |
| 9 | +import java.lang.reflect.Constructor; |
| 10 | +import java.lang.reflect.InvocationTargetException; |
| 11 | +import java.security.AccessController; |
| 12 | +import java.security.PrivilegedAction; |
| 13 | +import java.util.function.Supplier; |
| 14 | + |
| 15 | +import org.hibernate.envers.exception.AuditException; |
| 16 | +import org.hibernate.envers.internal.entities.PropertyData; |
| 17 | +import org.hibernate.envers.internal.tools.ReflectionTools; |
| 18 | +import org.hibernate.internal.util.ReflectHelper; |
| 19 | +import org.hibernate.property.access.spi.Getter; |
| 20 | +import org.hibernate.property.access.spi.Setter; |
| 21 | +import org.hibernate.service.ServiceRegistry; |
| 22 | + |
| 23 | +/** |
| 24 | + * A base class for all entity mapper implementations. |
| 25 | + * |
| 26 | + * @author Chris Cranford |
| 27 | + */ |
| 28 | +public abstract class AbstractMapper { |
| 29 | + |
| 30 | + /** |
| 31 | + * Perform an action in a privileged block. |
| 32 | + * |
| 33 | + * @param block the lambda to executed in privileged. |
| 34 | + * @param <T> the return type |
| 35 | + * @return the result of the privileged call, may be {@literal null} |
| 36 | + */ |
| 37 | + protected <T> T doPrivileged(Supplier<T> block) { |
| 38 | + if ( System.getSecurityManager() != null ) { |
| 39 | + return AccessController.doPrivileged( (PrivilegedAction<T>) block::get ); |
| 40 | + } |
| 41 | + else { |
| 42 | + return block.get(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Get a value from the specified object. |
| 48 | + * |
| 49 | + * @param propertyData the property data, should not be {@literal null} |
| 50 | + * @param object the object for which the value should be read, should not be {@literal null} |
| 51 | + * @param serviceRegistry the service registry, should not be {@literal null} |
| 52 | + * @param <T> the return type |
| 53 | + * @return the value read from the object, may be {@literal null} |
| 54 | + */ |
| 55 | + @SuppressWarnings("unchecked") |
| 56 | + protected <T> T getValueFromObject(PropertyData propertyData, Object object, ServiceRegistry serviceRegistry) { |
| 57 | + return doPrivileged( () -> { |
| 58 | + final Getter getter = ReflectionTools.getGetter( object.getClass(), propertyData, serviceRegistry ); |
| 59 | + return (T) getter.get( object ); |
| 60 | + } ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get a value from the specified object. |
| 65 | + * |
| 66 | + * @param propertyName the property name, should not be {@literal null} |
| 67 | + * @param accessType the property access type, should not be {@literal null} |
| 68 | + * @param object the object for hwich the value should be read, should not be {@literal null} |
| 69 | + * @param serviceRegistry the service registry, should not be {@literal null} |
| 70 | + * @param <T> the return type |
| 71 | + * @return the value read from the object, may be {@literal null} |
| 72 | + */ |
| 73 | + @SuppressWarnings("unchecked") |
| 74 | + protected <T> T getValueFromObject(String propertyName, String accessType, Object object, ServiceRegistry serviceRegistry) { |
| 75 | + return doPrivileged( () -> { |
| 76 | + final Getter getter = ReflectionTools.getGetter( object.getClass(), propertyName, accessType, serviceRegistry ); |
| 77 | + return (T) getter.get( object ); |
| 78 | + } ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Set the specified value on the object. |
| 83 | + * |
| 84 | + * @param propertyData the property data, should not be {@literal null} |
| 85 | + * @param object the object for which the value should be set, should not be {@literal null} |
| 86 | + * @param value the value ot be set, may be {@literal null} |
| 87 | + * @param serviceRegistry the service registry, should not be {@literal null} |
| 88 | + */ |
| 89 | + protected void setValueOnObject(PropertyData propertyData, Object object, Object value, ServiceRegistry serviceRegistry) { |
| 90 | + doPrivileged( () -> { |
| 91 | + final Setter setter = ReflectionTools.getSetter(object.getClass(), propertyData, serviceRegistry ); |
| 92 | + setter.set( object, value ); |
| 93 | + return null; |
| 94 | + } ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Gets the value from the source object and sets the value in the destination object. |
| 99 | + * |
| 100 | + * @param propertyData the property data, should not be {@literal null} |
| 101 | + * @param source the source object, should not be {@literal null} |
| 102 | + * @param destination the destination object, should not be {@literal null} |
| 103 | + * @param serviceRegistry the service registry, should not be {@literal null} |
| 104 | + */ |
| 105 | + protected void getAndSetValue(PropertyData propertyData, Object source, Object destination, ServiceRegistry serviceRegistry) { |
| 106 | + doPrivileged( () -> { |
| 107 | + final Getter getter = ReflectionTools.getGetter( source.getClass(), propertyData, serviceRegistry ); |
| 108 | + final Setter setter = ReflectionTools.getSetter( destination.getClass(), propertyData, serviceRegistry ); |
| 109 | + setter.set( destination, getter.get( source ) ); |
| 110 | + return null; |
| 111 | + } ); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Creates a new object based on the specified class with the given constructor arguments. |
| 116 | + * |
| 117 | + * @param clazz the class, must not be {@literal null} |
| 118 | + * @param args the variadic constructor arguments, may be omitted. |
| 119 | + * @param <T> the return class type |
| 120 | + * @return a new instance of the class |
| 121 | + */ |
| 122 | + protected <T> T newObjectInstance(Class<T> clazz, Object... args) { |
| 123 | + return doPrivileged( () -> { |
| 124 | + try { |
| 125 | + final Constructor<T> constructor = ReflectHelper.getDefaultConstructor( clazz ); |
| 126 | + if ( constructor == null ) { |
| 127 | + throw new AuditException( "Failed to locate default constructor for class: " + clazz.getName() ); |
| 128 | + } |
| 129 | + return constructor.newInstance( args ); |
| 130 | + } |
| 131 | + catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { |
| 132 | + throw new AuditException( e ); |
| 133 | + } |
| 134 | + } ); |
| 135 | + } |
| 136 | +} |
0 commit comments