Remove ObjectProcessor#getInputClass
Replace an explicit input class declaration with nasty reflection code.
This commit is contained in:
		@@ -3,6 +3,7 @@ package envoy.server.net;
 | 
			
		||||
import java.io.ByteArrayInputStream;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.ObjectInputStream;
 | 
			
		||||
import java.lang.reflect.ParameterizedType;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
import java.util.logging.Level;
 | 
			
		||||
import java.util.logging.Logger;
 | 
			
		||||
@@ -50,14 +51,19 @@ public class ObjectMessageProcessor implements IMessageProcessor {
 | 
			
		||||
 | 
			
		||||
			logger.fine("Received " + obj);
 | 
			
		||||
 | 
			
		||||
			// Process object
 | 
			
		||||
			processors.stream().filter(p -> p.getInputClass().equals(obj.getClass())).forEach((@SuppressWarnings("rawtypes") ObjectProcessor p) -> {
 | 
			
		||||
				try {
 | 
			
		||||
					p.process(p.getInputClass().cast(obj), message.socketId, new ObjectWriteProxy(writeProxy));
 | 
			
		||||
				} catch (IOException e) {
 | 
			
		||||
					logger.log(Level.SEVERE, "Exception during processor execution: ", e);
 | 
			
		||||
			// Get processor and input class and process object
 | 
			
		||||
			for (@SuppressWarnings("rawtypes")
 | 
			
		||||
			ObjectProcessor p : processors) {
 | 
			
		||||
				Class<?> c = (Class<?>) ((ParameterizedType) p.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0];
 | 
			
		||||
				if (c.equals(obj.getClass())) {
 | 
			
		||||
					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) {
 | 
			
		||||
			e.printStackTrace();
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -41,7 +41,4 @@ public class ContactOperationProcessor implements ObjectProcessor<ContactOperati
 | 
			
		||||
				logger.warning(String.format("Received %s with an unsupported operation.", evt));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Class<ContactOperation> getInputClass() { return ContactOperation.class; }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -40,7 +40,4 @@ public class GroupCreationProcessor implements ObjectProcessor<GroupCreation> {
 | 
			
		||||
			.map(connectionManager::getSocketID)
 | 
			
		||||
			.forEach(memberSocketID -> writeProxy.write(memberSocketID, new ContactOperation(group.toCommon(), ElementOperation.ADD)));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Class<GroupCreation> getInputClass() { return GroupCreation.class; }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -61,7 +61,4 @@ public class GroupMessageProcessor implements ObjectProcessor<GroupMessage> {
 | 
			
		||||
			logger.warning("Received a groupMessage with an ID that already exists");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Class<GroupMessage> getInputClass() { return GroupMessage.class; }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -63,7 +63,4 @@ public class GroupMessageStatusChangeProcessor implements ObjectProcessor<GroupM
 | 
			
		||||
		}
 | 
			
		||||
		persistenceManager.updateMessage(gmsg);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Class<GroupMessageStatusChange> getInputClass() { return GroupMessageStatusChange.class; }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -47,7 +47,4 @@ public class GroupResizeProcessor implements ObjectProcessor<GroupResize> {
 | 
			
		||||
			.map(connectionManager::getSocketID)
 | 
			
		||||
			.forEach(memberSocketID -> writeProxy.write(memberSocketID, commonGroup));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Class<GroupResize> getInputClass() { return GroupResize.class; }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -21,9 +21,6 @@ public class IDGeneratorRequestProcessor implements ObjectProcessor<IDGeneratorR
 | 
			
		||||
 | 
			
		||||
	private static final long ID_RANGE = 200;
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Class<IDGeneratorRequest> getInputClass() { return IDGeneratorRequest.class; }
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void process(IDGeneratorRequest input, long socketID, ObjectWriteProxy writeProxy) throws IOException {
 | 
			
		||||
		writeProxy.write(socketID, createIDGenerator());
 | 
			
		||||
 
 | 
			
		||||
@@ -23,9 +23,6 @@ public class IsTypingProcessor implements ObjectProcessor<IsTyping> {
 | 
			
		||||
	private static final ConnectionManager	connectionManager	= ConnectionManager.getInstance();
 | 
			
		||||
	private static final PersistenceManager	persistenceManager	= PersistenceManager.getInstance();
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Class<IsTyping> getInputClass() { return IsTyping.class; }
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void process(IsTyping event, long socketID, ObjectWriteProxy writeProxy) throws IOException {
 | 
			
		||||
		final var contact = persistenceManager.getContactByID(event.get());
 | 
			
		||||
 
 | 
			
		||||
@@ -191,7 +191,4 @@ public final class LoginCredentialProcessor implements ObjectProcessor<LoginCred
 | 
			
		||||
		// Complete the handshake
 | 
			
		||||
		writeProxy.write(socketID, user.toCommon());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -58,7 +58,4 @@ public class MessageProcessor implements ObjectProcessor<Message> {
 | 
			
		||||
			logger.log(Level.WARNING, "Received " + message + " with an ID that already exists!");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Class<Message> getInputClass() { return Message.class; }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -42,7 +42,4 @@ public class MessageStatusChangeProcessor implements ObjectProcessor<MessageStat
 | 
			
		||||
		final long senderID = msg.getSender().getID();
 | 
			
		||||
		if (connectionManager.isOnline(senderID)) writeProxy.write(connectionManager.getSocketID(senderID), statusChange);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Class<MessageStatusChange> getInputClass() { return MessageStatusChange.class; }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -28,7 +28,4 @@ public class NameChangeProcessor implements ObjectProcessor<NameChange> {
 | 
			
		||||
		// Notify online contacts of the name change
 | 
			
		||||
		writeProxy.writeToOnlineContacts(toUpdate.getContacts(), nameChange);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Class<NameChange> getInputClass() { return NameChange.class; }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -18,12 +18,6 @@ import envoy.server.net.ObjectWriteProxy;
 | 
			
		||||
 */
 | 
			
		||||
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 socketID   the ID of the socket from which the object was received
 | 
			
		||||
 
 | 
			
		||||
@@ -38,7 +38,4 @@ public class UserSearchProcessor implements ObjectProcessor<UserSearchRequest> {
 | 
			
		||||
					.map(User::toCommon)
 | 
			
		||||
					.collect(Collectors.toList())));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Class<UserSearchRequest> getInputClass() { return UserSearchRequest.class; }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -26,9 +26,6 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
 | 
			
		||||
	private static final PersistenceManager	persistenceManager	= PersistenceManager.getInstance();
 | 
			
		||||
	private static final Logger				logger				= EnvoyLog.getLogger(UserStatusChangeProcessor.class);
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public Class<UserStatusChange> getInputClass() { return UserStatusChange.class; }
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void process(UserStatusChange input, long socketID, ObjectWriteProxy writeProxy) {
 | 
			
		||||
		// new status should not equal old status
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user