@SuppressWarnings({ "rawtypes", "unchecked" }) protected CommandWrapper<Command, Answer, ServerResource> retrieveCommands(final Class<? extends Command> commandClass, final Hashtable<Class<? extends Command>, CommandWrapper> resourceCommands) { Class<? extends Command> keepCommandClass = commandClass; CommandWrapper<Command, Answer, ServerResource> commandWrapper = resourceCommands.get(keepCommandClass); while (commandWrapper == null) { try { final Class<? extends Command> commandClass2 = (Class<? extends Command>) keepCommandClass.getSuperclass(); if (commandClass2 == null) { throw new CommandNotSupported("All the COMMAND hierarchy tree has been visited but no compliant key has been found for '" + commandClass + "'."); } commandWrapper = resourceCommands.get(commandClass2); keepCommandClass = commandClass2; } catch (final ClassCastException e) { throw new CommandNotSupported("No key found for '" + keepCommandClass.getClass() + "' in the Map!"); } catch (final NullPointerException e) { // Will now traverse all the resource hierarchy. Returning null // is not a problem. // It is all being nicely checked and in case we do not have a // resource, an Unsupported answer will be thrown by the base // class. return null; } } return commandWrapper; }
@SuppressWarnings({ "rawtypes", "unchecked" }) protected CommandWrapper<Command, Answer, ServerResource> retryWhenAllFails(final Command command, final Class<? extends ServerResource> resourceClass, final Hashtable<Class<? extends Command>, CommandWrapper> resourceCommands) { Class<? extends ServerResource> keepResourceClass = resourceClass; CommandWrapper<Command, Answer, ServerResource> commandWrapper = resourceCommands.get(command.getClass()); while (commandWrapper == null) { // Could not find the command in the given resource, will traverse // the family tree. try { final Class<? extends ServerResource> resourceClass2 = (Class<? extends ServerResource>) keepResourceClass.getSuperclass(); if (resourceClass2 == null) { throw new CommandNotSupported("All the SERVER-RESOURCE hierarchy tree has been visited but no compliant key has been found for '" + command.getClass() + "'."); } final Hashtable<Class<? extends Command>, CommandWrapper> resourceCommands2 = retrieveResource(command, (Class<? extends ServerResource>) keepResourceClass.getSuperclass()); keepResourceClass = resourceClass2; commandWrapper = retrieveCommands(command.getClass(), resourceCommands2); } catch (final ClassCastException | NullPointerException e) { throw new CommandNotSupported("No key found for '" + command.getClass() + "' in the Map!", e); } } return commandWrapper; }
@SuppressWarnings({"rawtypes" }) @Override public Answer execute(final Command command, final ServerResource serverResource) { final Class<? extends ServerResource> resourceClass = serverResource.getClass(); final Hashtable<Class<? extends Command>, CommandWrapper> resourceCommands = retrieveResource(command, resourceClass); CommandWrapper<Command, Answer, ServerResource> commandWrapper = retrieveCommands(command.getClass(), resourceCommands); while (commandWrapper == null) { //Could not find the command in the given resource, will traverse the family tree. commandWrapper = retryWhenAllFails(command, resourceClass, resourceCommands); } if (commandWrapper == null) { throw new CommandNotSupported("No way to handle " + command.getClass()); } return commandWrapper.execute(command, serverResource); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) protected Hashtable<Class<? extends Command>, CommandWrapper> retrieveResource(final Command command, final Class<? extends ServerResource> resourceClass) { Class<? extends ServerResource> keepResourceClass = resourceClass; Hashtable<Class<? extends Command>, CommandWrapper> resource = resources.get(keepResourceClass); while (resource == null) { try { final Class<? extends ServerResource> keepResourceClass2 = (Class<? extends ServerResource>) keepResourceClass.getSuperclass(); resource = resources.get(keepResourceClass2); keepResourceClass = keepResourceClass2; } catch (final ClassCastException e) { throw new CommandNotSupported("No key found for '" + command.getClass() + "' in the Map!"); } } return resource; }
public CommandNotSupported(String msg, Throwable cause) { super(msg); initCause(cause); } }