refactoring

pull/2300/head
Nikita Koksharov 6 years ago
parent 90c19483c2
commit d7e186eb12

@ -304,7 +304,7 @@ public class RedissonLiveObjectService implements RLiveObjectService {
continue; continue;
} }
RObject rObject = commandExecutor.getObjectBuilder().createObject(id, detachedObject.getClass(), object.getClass(), field.getName(), redisson); RObject rObject = commandExecutor.getObjectBuilder().createObject(id, detachedObject.getClass(), object.getClass(), field.getName());
if (rObject != null) { if (rObject != null) {
commandExecutor.getObjectBuilder().store(rObject, field.getName(), liveMap); commandExecutor.getObjectBuilder().store(rObject, field.getName(), liveMap);
if (rObject instanceof SortedSet) { if (rObject instanceof SortedSet) {

@ -111,23 +111,17 @@ public class RedissonReference implements Serializable {
/** /**
* @return the type * @return the type
* @throws java.lang.Exception - which could be: * @throws java.lang.ClassNotFoundException - if the class cannot be located
* LinkageError - if the linkage fails
* ExceptionInInitializerError - if the initialization provoked by this method fails
* ClassNotFoundException - if the class cannot be located
*/ */
public Class<?> getType() throws Exception { public Class<?> getType() throws ClassNotFoundException {
return Class.forName(type); return Class.forName(type);
} }
/** /**
* @return the type * @return the type
* @throws java.lang.Exception - which could be: * @throws java.lang.ClassNotFoundException - if the class cannot be located
* LinkageError - if the linkage fails
* ExceptionInInitializerError - if the initialization provoked by this method fails
* ClassNotFoundException - if the class cannot be located
*/ */
public Class<?> getReactiveType() throws Exception { public Class<?> getReactiveType() throws ClassNotFoundException {
if (REACTIVE_MAP.containsValue(type)) { if (REACTIVE_MAP.containsValue(type)) {
return Class.forName(REACTIVE_MAP.reverseGet(type)); //live object is not supported in reactive client return Class.forName(REACTIVE_MAP.reverseGet(type)); //live object is not supported in reactive client
} }
@ -179,12 +173,9 @@ public class RedissonReference implements Serializable {
/** /**
* @return the codec * @return the codec
* @throws java.lang.Exception - which could be: * @throws java.lang.ClassNotFoundException - if the class cannot be located
* LinkageError - if the linkage fails
* ExceptionInInitializerError - if the initialization provoked by this method fails
* ClassNotFoundException - if the class cannot be located
*/ */
public Class<? extends Codec> getCodecType() throws Exception { public Class<? extends Codec> getCodecType() throws ClassNotFoundException {
if (codec != null) { if (codec != null) {
return (Class<? extends Codec>) Class.forName(codec); return (Class<? extends Codec>) Class.forName(codec);
} }

@ -101,9 +101,6 @@ public class CommandAsyncService implements CommandAsyncExecutor {
final ConnectionManager connectionManager; final ConnectionManager connectionManager;
private RedissonObjectBuilder objectBuilder; private RedissonObjectBuilder objectBuilder;
protected RedissonClient redisson;
protected RedissonReactiveClient redissonReactive;
protected RedissonRxClient redissonRx;
public CommandAsyncService(ConnectionManager connectionManager) { public CommandAsyncService(ConnectionManager connectionManager) {
this.connectionManager = connectionManager; this.connectionManager = connectionManager;
@ -116,47 +113,32 @@ public class CommandAsyncService implements CommandAsyncExecutor {
@Override @Override
public CommandAsyncExecutor enableRedissonReferenceSupport(RedissonClient redisson) { public CommandAsyncExecutor enableRedissonReferenceSupport(RedissonClient redisson) {
if (redisson != null) { enableRedissonReferenceSupport(redisson.getConfig(), redisson, null, null);
this.redisson = redisson;
enableRedissonReferenceSupport(redisson.getConfig());
this.redissonReactive = null;
this.redissonRx = null;
}
return this; return this;
} }
@Override @Override
public CommandAsyncExecutor enableRedissonReferenceSupport(RedissonReactiveClient redissonReactive) { public CommandAsyncExecutor enableRedissonReferenceSupport(RedissonReactiveClient redissonReactive) {
if (redissonReactive != null) { enableRedissonReferenceSupport(redissonReactive.getConfig(), null, redissonReactive, null);
this.redissonReactive = redissonReactive;
enableRedissonReferenceSupport(redissonReactive.getConfig());
this.redisson = null;
this.redissonRx = null;
}
return this; return this;
} }
@Override @Override
public CommandAsyncExecutor enableRedissonReferenceSupport(RedissonRxClient redissonRx) { public CommandAsyncExecutor enableRedissonReferenceSupport(RedissonRxClient redissonRx) {
if (redissonRx != null) { enableRedissonReferenceSupport(redissonRx.getConfig(), null, null, redissonRx);
this.redissonReactive = null;
enableRedissonReferenceSupport(redissonRx.getConfig());
this.redisson = null;
this.redissonRx = redissonRx;
}
return this; return this;
} }
private void enableRedissonReferenceSupport(Config config) { private void enableRedissonReferenceSupport(Config config, RedissonClient redisson, RedissonReactiveClient redissonReactive, RedissonRxClient redissonRx) {
Codec codec = config.getCodec(); Codec codec = config.getCodec();
objectBuilder = new RedissonObjectBuilder(config); objectBuilder = new RedissonObjectBuilder(config, redisson, redissonReactive, redissonRx);
ReferenceCodecProvider codecProvider = objectBuilder.getReferenceCodecProvider(); ReferenceCodecProvider codecProvider = objectBuilder.getReferenceCodecProvider();
codecProvider.registerCodec((Class<Codec>) codec.getClass(), codec); codecProvider.registerCodec((Class<Codec>) codec.getClass(), codec);
} }
@Override @Override
public boolean isRedissonReferenceSupportEnabled() { public boolean isRedissonReferenceSupportEnabled() {
return redisson != null || redissonReactive != null || redissonRx != null; return objectBuilder != null;
} }
@Override @Override
@ -1085,7 +1067,6 @@ public class CommandAsyncService implements CommandAsyncExecutor {
AsyncDetails.release(details); AsyncDetails.release(details);
} catch (Exception e) { } catch (Exception e) {
handleError(details, details.getMainPromise(), e); handleError(details, details.getMainPromise(), e);
throw e;
} }
} }
@ -1093,7 +1074,7 @@ public class CommandAsyncService implements CommandAsyncExecutor {
mainPromise.tryFailure(cause); mainPromise.tryFailure(cause);
} }
protected <V, R> void handleSuccess(AsyncDetails<V, R> details, RPromise<R> promise, RedisCommand<?> command, R res) { protected <V, R> void handleSuccess(AsyncDetails<V, R> details, RPromise<R> promise, RedisCommand<?> command, R res) throws ReflectiveOperationException {
if (isRedissonReferenceSupportEnabled()) { if (isRedissonReferenceSupportEnabled()) {
handleReference(promise, res); handleReference(promise, res);
} else { } else {
@ -1101,11 +1082,11 @@ public class CommandAsyncService implements CommandAsyncExecutor {
} }
} }
private <R, V> void handleReference(RPromise<R> mainPromise, R res) { private <R, V> void handleReference(RPromise<R> mainPromise, R res) throws ReflectiveOperationException {
mainPromise.trySuccess((R) tryHandleReference(res)); mainPromise.trySuccess((R) tryHandleReference(res));
} }
protected Object tryHandleReference(Object o) { protected Object tryHandleReference(Object o) throws ReflectiveOperationException {
boolean hasConversion = false; boolean hasConversion = false;
if (o instanceof List) { if (o instanceof List) {
List<Object> r = (List<Object>) o; List<Object> r = (List<Object>) o;
@ -1198,7 +1179,7 @@ public class CommandAsyncService implements CommandAsyncExecutor {
} }
} }
private Object tryHandleReference0(Object o) { private Object tryHandleReference0(Object o) throws ReflectiveOperationException {
if (o instanceof RedissonReference) { if (o instanceof RedissonReference) {
return fromReference(o); return fromReference(o);
} else if (o instanceof ScoredEntry && ((ScoredEntry) o).getValue() instanceof RedissonReference) { } else if (o instanceof ScoredEntry && ((ScoredEntry) o).getValue() instanceof RedissonReference) {
@ -1215,22 +1196,12 @@ public class CommandAsyncService implements CommandAsyncExecutor {
return o; return o;
} }
private Object fromReference(Object res) { private Object fromReference(Object res) throws ReflectiveOperationException {
if (objectBuilder == null) { if (objectBuilder == null) {
return res; return res;
} }
try { return objectBuilder.fromReference((RedissonReference) res);
if (redisson != null) {
return objectBuilder.fromReference(redisson, (RedissonReference) res);
}
if (redissonReactive != null) {
return objectBuilder.fromReference(redissonReactive, (RedissonReference) res);
}
return objectBuilder.fromReference(redissonRx, (RedissonReference) res);
} catch (Exception exception) {
throw new IllegalStateException(exception);
}
} }
protected <R, V> void sendCommand(AsyncDetails<V, R> details, RedisConnection connection) { protected <R, V> void sendCommand(AsyncDetails<V, R> details, RedisConnection connection) {

@ -205,7 +205,7 @@ public class CommandBatchService extends CommandAsyncService {
} }
@Override @Override
protected <V, R> void handleSuccess(AsyncDetails<V, R> details, RPromise<R> promise, RedisCommand<?> command, R res) { protected <V, R> void handleSuccess(AsyncDetails<V, R> details, RPromise<R> promise, RedisCommand<?> command, R res) throws ReflectiveOperationException {
if (RedisCommands.EXEC.getName().equals(command.getName())) { if (RedisCommands.EXEC.getName().equals(command.getName())) {
super.handleSuccess(details, promise, command, res); super.handleSuccess(details, promise, command, res);
return; return;
@ -568,7 +568,11 @@ public class CommandBatchService extends CommandAsyncService {
} else if (!commandEntry.getCommand().getName().equals(RedisCommands.MULTI.getName()) } else if (!commandEntry.getCommand().getName().equals(RedisCommands.MULTI.getName())
&& !commandEntry.getCommand().getName().equals(RedisCommands.EXEC.getName())) { && !commandEntry.getCommand().getName().equals(RedisCommands.EXEC.getName())) {
Object entryResult = commandEntry.getPromise().getNow(); Object entryResult = commandEntry.getPromise().getNow();
entryResult = tryHandleReference(entryResult); try {
entryResult = tryHandleReference(entryResult);
} catch (ReflectiveOperationException exc) {
log.error("Unable to handle reference from " + entryResult, exc);
}
responses.add(entryResult); responses.add(entryResult);
} }
} }

@ -79,7 +79,7 @@ public class AccessorInterceptor {
if (isGetter(method, fieldName)) { if (isGetter(method, fieldName)) {
Object result = liveMap.get(fieldName); Object result = liveMap.get(fieldName);
if (result == null) { if (result == null) {
RObject ar = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), fieldType, fieldName, redisson); RObject ar = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), fieldType, fieldName);
if (ar != null) { if (ar != null) {
objectBuilder.store(ar, fieldName, liveMap); objectBuilder.store(ar, fieldName, liveMap);
return ar; return ar;
@ -93,7 +93,7 @@ public class AccessorInterceptor {
return result; return result;
} }
if (result instanceof RedissonReference) { if (result instanceof RedissonReference) {
return objectBuilder.fromReference(redisson, (RedissonReference) result); return objectBuilder.fromReference((RedissonReference) result);
} }
return result; return result;
} }
@ -121,7 +121,7 @@ public class AccessorInterceptor {
&& TransformationMode.ANNOTATION_BASED && TransformationMode.ANNOTATION_BASED
.equals(ClassUtils.getAnnotation(me.getClass().getSuperclass(), .equals(ClassUtils.getAnnotation(me.getClass().getSuperclass(),
REntity.class).fieldTransformation())) { REntity.class).fieldTransformation())) {
RObject rObject = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), arg.getClass(), fieldName, redisson); RObject rObject = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), arg.getClass(), fieldName);
if (arg != null) { if (arg != null) {
if (rObject instanceof Collection) { if (rObject instanceof Collection) {
Collection<?> c = (Collection<?>) rObject; Collection<?> c = (Collection<?>) rObject;

@ -16,7 +16,6 @@
package org.redisson.liveobject.core; package org.redisson.liveobject.core;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.Deque; import java.util.Deque;
@ -89,6 +88,9 @@ public class RedissonObjectBuilder {
} }
private final Config config; private final Config config;
private final RedissonClient redisson;
private final RedissonReactiveClient redissonReactive;
private final RedissonRxClient redissonRx;
public static class CodecMethodRef { public static class CodecMethodRef {
@ -105,9 +107,13 @@ public class RedissonObjectBuilder {
private final ReferenceCodecProvider codecProvider = new DefaultReferenceCodecProvider(); private final ReferenceCodecProvider codecProvider = new DefaultReferenceCodecProvider();
public RedissonObjectBuilder(Config config) { public RedissonObjectBuilder(Config config,
RedissonClient redisson, RedissonReactiveClient redissonReactive, RedissonRxClient redissonRx) {
super(); super();
this.config = config; this.config = config;
this.redisson = redisson;
this.redissonReactive = redissonReactive;
this.redissonRx = redissonRx;
} }
public ReferenceCodecProvider getReferenceCodecProvider() { public ReferenceCodecProvider getReferenceCodecProvider() {
@ -123,7 +129,7 @@ public class RedissonObjectBuilder {
new RedissonReference(ar.getClass(), ar.getName(), codec)); new RedissonReference(ar.getClass(), ar.getName(), codec));
} }
public RObject createObject(Object id, Class<?> clazz, Class<?> fieldType, String fieldName, RedissonClient redisson) { public RObject createObject(Object id, Class<?> clazz, Class<?> fieldType, String fieldName) {
Class<? extends RObject> mappedClass = getMappedClass(fieldType); Class<? extends RObject> mappedClass = getMappedClass(fieldType);
try { try {
if (mappedClass != null) { if (mappedClass != null) {
@ -142,7 +148,7 @@ public class RedissonObjectBuilder {
/** /**
* WARNING: rEntity has to be the class of @This object. * WARNING: rEntity has to be the class of @This object.
*/ */
private Codec getFieldCodec(Class<?> rEntity, Class<? extends RObject> rObjectClass, String fieldName) throws Exception { private Codec getFieldCodec(Class<?> rEntity, Class<? extends RObject> rObjectClass, String fieldName) throws ReflectiveOperationException {
Field field = ClassUtils.getDeclaredField(rEntity, fieldName); Field field = ClassUtils.getDeclaredField(rEntity, fieldName);
if (field.isAnnotationPresent(RObjectField.class)) { if (field.isAnnotationPresent(RObjectField.class)) {
RObjectField anno = field.getAnnotation(RObjectField.class); RObjectField anno = field.getAnnotation(RObjectField.class);
@ -197,7 +203,18 @@ public class RedissonObjectBuilder {
} }
} }
public Object fromReference(RedissonClient redisson, RedissonReference rr) throws Exception { public Object fromReference(RedissonReference rr) throws ReflectiveOperationException {
if (redisson != null) {
return fromReference(redisson, rr);
} else if (redissonReactive != null) {
return fromReference(redissonReactive, rr);
} else if (redissonRx != null) {
return fromReference(redissonRx, rr);
}
throw new IllegalStateException();
}
private Object fromReference(RedissonClient redisson, RedissonReference rr) throws ReflectiveOperationException {
Class<? extends Object> type = rr.getType(); Class<? extends Object> type = rr.getType();
if (type != null) { if (type != null) {
if (ClassUtils.isAnnotationPresent(type, REntity.class)) { if (ClassUtils.isAnnotationPresent(type, REntity.class)) {
@ -213,8 +230,7 @@ public class RedissonObjectBuilder {
} }
private Object getObject(Object redisson, RedissonReference rr, Class<? extends Object> type, private Object getObject(Object redisson, RedissonReference rr, Class<? extends Object> type,
ReferenceCodecProvider codecProvider) ReferenceCodecProvider codecProvider) throws ReflectiveOperationException {
throws IllegalAccessException, InvocationTargetException, Exception, ClassNotFoundException {
if (type != null) { if (type != null) {
CodecMethodRef b = REFERENCES.get(type); CodecMethodRef b = REFERENCES.get(type);
if (b == null && type.getInterfaces().length > 0) { if (b == null && type.getInterfaces().length > 0) {
@ -236,7 +252,7 @@ public class RedissonObjectBuilder {
return rr.getCodec() == null; return rr.getCodec() == null;
} }
public Object fromReference(RedissonRxClient redisson, RedissonReference rr) throws Exception { private Object fromReference(RedissonRxClient redisson, RedissonReference rr) throws ReflectiveOperationException {
Class<? extends Object> type = rr.getReactiveType(); Class<? extends Object> type = rr.getReactiveType();
/** /**
* Live Object from reference in reactive client is not supported yet. * Live Object from reference in reactive client is not supported yet.
@ -244,7 +260,7 @@ public class RedissonObjectBuilder {
return getObject(redisson, rr, type, codecProvider); return getObject(redisson, rr, type, codecProvider);
} }
public Object fromReference(RedissonReactiveClient redisson, RedissonReference rr) throws Exception { private Object fromReference(RedissonReactiveClient redisson, RedissonReference rr) throws ReflectiveOperationException {
Class<? extends Object> type = rr.getReactiveType(); Class<? extends Object> type = rr.getReactiveType();
/** /**
* Live Object from reference in reactive client is not supported yet. * Live Object from reference in reactive client is not supported yet.
@ -294,7 +310,7 @@ public class RedissonObjectBuilder {
return null; return null;
} }
public <T extends RObject, K extends Codec> T createRObject(RedissonClient redisson, Class<T> expectedType, String name, K codec) throws Exception { private <T extends RObject, K extends Codec> T createRObject(RedissonClient redisson, Class<T> expectedType, String name, K codec) throws ReflectiveOperationException {
List<Class<?>> interfaces = Arrays.asList(expectedType.getInterfaces()); List<Class<?>> interfaces = Arrays.asList(expectedType.getInterfaces());
for (Class<?> iType : interfaces) { for (Class<?> iType : interfaces) {
if (REFERENCES.containsKey(iType)) {// user cache to speed up things a little. if (REFERENCES.containsKey(iType)) {// user cache to speed up things a little.

Loading…
Cancel
Save