org.springframework.data.jpa.domain.GenericPersistable Maven / Gradle / Ivy
package org.springframework.data.jpa.domain;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import org.hibernate.id.UUIDGeneratorCustom;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Persistable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
@Access(AccessType.FIELD)
@MappedSuperclass
public abstract class GenericPersistable extends AbstractSerializable implements Persistable {
private static final long serialVersionUID = -6238982308882471551L;
protected static final String GENERATOR_NAME = "system-uuid";
protected static final String GENERATOR_STRATEGY_MANUAL = UUIDGeneratorCustom.GENERATOR_STRATEGY;
protected static final String GENERATOR_STRATEGY = "uuid2";
@Transient
private final boolean security = ClassUtils.isPresent("org.springframework.security.core.context.SecurityContextHolder", null);
@Transient
private final boolean web = ClassUtils.isPresent("org.springframework.web.context.request.RequestContextHolder", null);
@Override
public boolean isNew() {
return getId() == null;
}
@SuppressWarnings("unchecked")
public static , ID extends Serializable> T newInstance(Class clazz, ID id) {
Assert.notNull(id, "'id' must not be null");
Constructor> constructor = ClassUtils.getConstructorIfAvailable(clazz, id.getClass());
if (constructor == null) {
throw new IllegalStateException("No constructor accepting " + id + " in class " + clazz);
}
try {
return (T) BeanUtils.instantiateClass(constructor, id);
}
catch (RuntimeException e) {
throw e;
}
}
public abstract void setId(ID id);
@SuppressWarnings("unchecked")
protected > PK getId(T persistable) {
if (persistable instanceof HibernateProxy) {
LazyInitializer lazyInitializer = ((HibernateProxy) persistable).getHibernateLazyInitializer();
if (lazyInitializer.isUninitialized()) {
Serializable identifier = lazyInitializer.getIdentifier();
if (identifier != null) {
return (PK) identifier;
}
}
}
return persistable == null ? null : persistable.getId();
}
/**
* @see org.springframework.util.ClassUtils#isPresent(String, ClassLoader)
* @see org.springframework.beans.BeanUtils#getPropertyDescriptor(Class, String)
* @see org.springframework.security.web.authentication.WebAuthenticationDetails
* @see org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails
* @see org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter
*/
protected String getName(String defaultName) {
if (ClassUtils.isPresent("org.springframework.security.core.context.SecurityContextHolder", null)) {
org.springframework.security.core.Authentication authentication = org.springframework.security.core.context.SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
return authentication.getName();
}
}
return defaultName;
}
protected String getRemoteAddress(String defaultRemoteAddress) {
Object details = getDetails();
Method method;
if (details != null && (method = ClassUtils.getMethodIfAvailable(details.getClass(), "getRemoteAddress")) != null && (details = ReflectionUtils.invokeMethod(method, details)) instanceof String) {
return (String) details;
}
try {
Object request = getRequest();
if (request != null && (method = ClassUtils.getMethodIfAvailable(request.getClass(), "getRemoteAddr")) != null && (details = ReflectionUtils.invokeMethod(method, request)) instanceof String) {
return (String) details;
}
}
catch (Exception e) {
// ignore
}
return defaultRemoteAddress;
}
protected String getSessionId(String defaultSessionId) {
Object details = getDetails();
Method method;
if (details != null && (method = ClassUtils.getMethodIfAvailable(details.getClass(), "getSessionId")) != null && (details = ReflectionUtils.invokeMethod(method, details)) instanceof String) {
return (String) details;
}
try {
Object request = getRequest();
if (request != null && (method = ClassUtils.getMethodIfAvailable(request.getClass(), "getRequestedSessionId")) != null
&& (details = ReflectionUtils.invokeMethod(method, request)) instanceof String) {
return (String) details;
}
}
catch (Exception e) {
// ignore
}
return defaultSessionId;
}
private Object getDetails() {
if (this.security) {
org.springframework.security.core.Authentication authentication = org.springframework.security.core.context.SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
return authentication.getDetails();
}
}
return null;
}
private Object getRequest() {
if (this.web) {
org.springframework.web.context.request.RequestAttributes requestAttributes = org.springframework.web.context.request.RequestContextHolder.getRequestAttributes();
if (requestAttributes instanceof org.springframework.web.context.request.ServletRequestAttributes) {
return ((org.springframework.web.context.request.ServletRequestAttributes) requestAttributes).getRequest();
}
}
return null;
}
}