Remove ObjectProcessor#getInputClass

Replace an explicit input class declaration with nasty reflection code.
This commit is contained in:
Kai S. K. Engelbart 2020-07-25 17:29:32 +02:00
parent 1cdad2df0b
commit 63f42ab8d9
15 changed files with 13 additions and 52 deletions

View File

@ -3,6 +3,7 @@ package envoy.server.net;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.lang.reflect.ParameterizedType;
import java.util.Set; import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -50,14 +51,19 @@ public class ObjectMessageProcessor implements IMessageProcessor {
logger.fine("Received " + obj); logger.fine("Received " + obj);
// Process object // Get processor and input class and process object
processors.stream().filter(p -> p.getInputClass().equals(obj.getClass())).forEach((@SuppressWarnings("rawtypes") ObjectProcessor p) -> { for (@SuppressWarnings("rawtypes")
try { ObjectProcessor p : processors) {
p.process(p.getInputClass().cast(obj), message.socketId, new ObjectWriteProxy(writeProxy)); Class<?> c = (Class<?>) ((ParameterizedType) p.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0];
} catch (IOException e) { if (c.equals(obj.getClass())) {
logger.log(Level.SEVERE, "Exception during processor execution: ", e); try {
p.process(c.cast(obj), message.socketId, new ObjectWriteProxy(writeProxy));
break;
} catch (IOException e) {
logger.log(Level.SEVERE, "Exception during processor execution: ", e);
}
} }
}); }
} catch (IOException | ClassNotFoundException e) { } catch (IOException | ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -41,7 +41,4 @@ public class ContactOperationProcessor implements ObjectProcessor<ContactOperati
logger.warning(String.format("Received %s with an unsupported operation.", evt)); logger.warning(String.format("Received %s with an unsupported operation.", evt));
} }
} }
@Override
public Class<ContactOperation> getInputClass() { return ContactOperation.class; }
} }

View File

@ -40,7 +40,4 @@ public class GroupCreationProcessor implements ObjectProcessor<GroupCreation> {
.map(connectionManager::getSocketID) .map(connectionManager::getSocketID)
.forEach(memberSocketID -> writeProxy.write(memberSocketID, new ContactOperation(group.toCommon(), ElementOperation.ADD))); .forEach(memberSocketID -> writeProxy.write(memberSocketID, new ContactOperation(group.toCommon(), ElementOperation.ADD)));
} }
@Override
public Class<GroupCreation> getInputClass() { return GroupCreation.class; }
} }

View File

@ -61,7 +61,4 @@ public class GroupMessageProcessor implements ObjectProcessor<GroupMessage> {
logger.warning("Received a groupMessage with an ID that already exists"); logger.warning("Received a groupMessage with an ID that already exists");
} }
} }
@Override
public Class<GroupMessage> getInputClass() { return GroupMessage.class; }
} }

View File

@ -63,7 +63,4 @@ public class GroupMessageStatusChangeProcessor implements ObjectProcessor<GroupM
} }
persistenceManager.updateMessage(gmsg); persistenceManager.updateMessage(gmsg);
} }
@Override
public Class<GroupMessageStatusChange> getInputClass() { return GroupMessageStatusChange.class; }
} }

View File

@ -47,7 +47,4 @@ public class GroupResizeProcessor implements ObjectProcessor<GroupResize> {
.map(connectionManager::getSocketID) .map(connectionManager::getSocketID)
.forEach(memberSocketID -> writeProxy.write(memberSocketID, commonGroup)); .forEach(memberSocketID -> writeProxy.write(memberSocketID, commonGroup));
} }
@Override
public Class<GroupResize> getInputClass() { return GroupResize.class; }
} }

View File

@ -21,9 +21,6 @@ public class IDGeneratorRequestProcessor implements ObjectProcessor<IDGeneratorR
private static final long ID_RANGE = 200; private static final long ID_RANGE = 200;
@Override
public Class<IDGeneratorRequest> getInputClass() { return IDGeneratorRequest.class; }
@Override @Override
public void process(IDGeneratorRequest input, long socketID, ObjectWriteProxy writeProxy) throws IOException { public void process(IDGeneratorRequest input, long socketID, ObjectWriteProxy writeProxy) throws IOException {
writeProxy.write(socketID, createIDGenerator()); writeProxy.write(socketID, createIDGenerator());

View File

@ -23,9 +23,6 @@ public class IsTypingProcessor implements ObjectProcessor<IsTyping> {
private static final ConnectionManager connectionManager = ConnectionManager.getInstance(); private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance(); private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
@Override
public Class<IsTyping> getInputClass() { return IsTyping.class; }
@Override @Override
public void process(IsTyping event, long socketID, ObjectWriteProxy writeProxy) throws IOException { public void process(IsTyping event, long socketID, ObjectWriteProxy writeProxy) throws IOException {
final var contact = persistenceManager.getContactByID(event.get()); final var contact = persistenceManager.getContactByID(event.get());

View File

@ -191,7 +191,4 @@ public final class LoginCredentialProcessor implements ObjectProcessor<LoginCred
// Complete the handshake // Complete the handshake
writeProxy.write(socketID, user.toCommon()); writeProxy.write(socketID, user.toCommon());
} }
@Override
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
} }

View File

@ -58,7 +58,4 @@ public class MessageProcessor implements ObjectProcessor<Message> {
logger.log(Level.WARNING, "Received " + message + " with an ID that already exists!"); logger.log(Level.WARNING, "Received " + message + " with an ID that already exists!");
} }
} }
@Override
public Class<Message> getInputClass() { return Message.class; }
} }

View File

@ -42,7 +42,4 @@ public class MessageStatusChangeProcessor implements ObjectProcessor<MessageStat
final long senderID = msg.getSender().getID(); final long senderID = msg.getSender().getID();
if (connectionManager.isOnline(senderID)) writeProxy.write(connectionManager.getSocketID(senderID), statusChange); if (connectionManager.isOnline(senderID)) writeProxy.write(connectionManager.getSocketID(senderID), statusChange);
} }
@Override
public Class<MessageStatusChange> getInputClass() { return MessageStatusChange.class; }
} }

View File

@ -28,7 +28,4 @@ public class NameChangeProcessor implements ObjectProcessor<NameChange> {
// Notify online contacts of the name change // Notify online contacts of the name change
writeProxy.writeToOnlineContacts(toUpdate.getContacts(), nameChange); writeProxy.writeToOnlineContacts(toUpdate.getContacts(), nameChange);
} }
@Override
public Class<NameChange> getInputClass() { return NameChange.class; }
} }

View File

@ -18,12 +18,6 @@ import envoy.server.net.ObjectWriteProxy;
*/ */
public interface ObjectProcessor<T> { public interface ObjectProcessor<T> {
/**
* @return the class of the request object
* @since Envoy Server Standalone v0.1-alpha
*/
Class<T> getInputClass();
/** /**
* @param input the request object * @param input the request object
* @param socketID the ID of the socket from which the object was received * @param socketID the ID of the socket from which the object was received

View File

@ -38,7 +38,4 @@ public class UserSearchProcessor implements ObjectProcessor<UserSearchRequest> {
.map(User::toCommon) .map(User::toCommon)
.collect(Collectors.toList()))); .collect(Collectors.toList())));
} }
@Override
public Class<UserSearchRequest> getInputClass() { return UserSearchRequest.class; }
} }

View File

@ -26,9 +26,6 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance(); private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private static final Logger logger = EnvoyLog.getLogger(UserStatusChangeProcessor.class); private static final Logger logger = EnvoyLog.getLogger(UserStatusChangeProcessor.class);
@Override
public Class<UserStatusChange> getInputClass() { return UserStatusChange.class; }
@Override @Override
public void process(UserStatusChange input, long socketID, ObjectWriteProxy writeProxy) { public void process(UserStatusChange input, long socketID, ObjectWriteProxy writeProxy) {
// new status should not equal old status // new status should not equal old status