简体   繁体   中英

Java Interface-Based Polymorphism Not Being Called

I'm working on a program that takes in commands as string data and passes it to the correct handler, as defined by a HashMap .

The code that passes the command to the correct handler is the following:

//This is run in its own thread, hence the run() method
@Override
public void run() {
    try {
        //Socket is a Socket object containing the client's connection
        InputStreamReader is = new InputStreamReader(socket.getInputStream());
        //MAX_MESSAGE_SIZE is an integer, specifically 1024
        char[] data = new char[MAX_MESSAGE_SIZE];
        is.read(data, 0, MAX_MESSAGE_SIZE);
        String strData = new String(data);
        //Split on UNIX or Windows type newline
        String[] lines = strData.split("\\r?\\n");
        //First verb determines the command
        String command = (lines[0].split(": "))[0];
        //Re-add the ": " because the HashMap will look for it
        if (handlerMap.containsKey((command + ": "))) {
            System.err.println("Got command: " + command);
            AbstractHandler handler = handlerMap.get(command);
            System.err.println("Passing connection + data to handler...");
            handler.handleData(socket, lines);
            System.err.println("Handler is done!");
        } else {
            System.err.println("Didn't understand command: " + command);
            DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
            outputStream.writeBytes(Protocol.UNKNOWN_ERROR);
            outputStream.flush();
            socket.close();
        }
    } catch (IOException e) {
        System.err.println("Caught IOException: " + e.getMessage());
    }
}

The value part of the HashMap is an object that implements the interface AbstractHandler . AbstractHandler defines the one method: handleData(Socket s, String[] lines) . For reference, here is where the map is initialized:

     public RequestManager(Socket socket) {
    this.socket = socket;
    handlerMap = new HashMap<>();
    handlerMap.put(Protocol.USERNAME, new AuthenticationHandler());
    //Set arg to true b/c it is a set request, not a change request
    handlerMap.put(Protocol.SET_PASSWORD, new ChangePasswordHandler(true));
    handlerMap.put(Protocol.CHANGE_PASSWORD, new ChangePasswordHandler());
    handlerMap.put(Protocol.FORGOT_PASSWORD, new ForgotPasswordHandler());
}

and all of the handleData methods in the objects just contain the following code:

@Override
public void handleData(Socket s, String[] lines) {
     clientSocket = s; //clientSocket field in class
     System.err.println("Made it into handler");
}

What's strange is after "Passing connection + data to handler" is shown, nothing happens. I don't see anything about getting into the handler, nor do I see exceptions or the message that the handler is done. What could cause this?

You test to see if a handler exists with

if (handlerMap.containsKey((command + ": "))) {

but you try and get a handler with

AbstractHandler handler = handlerMap.get(command);

So if it exists as for the key CommandName: , you presumably won't get it with the key CommandName . Therefore you'll have an unchecked NullPointerException on the call handler.handleData(socket, lines); and your runnable will die a horrid death.

It looks like you need to change either the first, or the second. Given you say you get to see it print "Passing connection + data to handler..." I think you need to change it to:

AbstractHandler handler = handlerMap.get(command + ": ");

A small stylistic change when you deal with maps would prevent this biting you now and in the future. Map.get returns null if the key is not found , so you can just do:

AbstractHandler handler = handlerMap.get(command + ": ");
if (handler != null) {
    /* ... */
    handler.handleData(socket, lines);
    System.err.println("Handler is done!");
} else {
    /* ... */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM