Question

I am creating a Java class for SmartFox Server Extension. It is trying to access MySQL Database.

I am receiving a error called Unreachable Code on the line session.setProperty("DatabaseID", dbId);

package sfs2x.extension.test.dblogin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.smartfoxserver.bitswarm.sessions.ISession;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.db.IDBManager;
import com.smartfoxserver.v2.exceptions.SFSErrorCode;
import com.smartfoxserver.v2.exceptions.SFSErrorData;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.exceptions.SFSLoginException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;

public class LoginEventHandler extends BaseServerEventHandler 
{
    @Override
    public void handleServerEvent(ISFSEvent e) throws SFSException 
    {
        String email = (String)e.getParameter(SFSEventParam.LOGIN_NAME);
        String pass = (String)e.getParameter(SFSEventParam.LOGIN_PASSWORD);
        ISession session = (ISession)e.getParameter(SFSEventParam.SESSION);

        IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
        Connection connection = null;

        try
        {
            connection = dbManager.getConnection();

            PreparedStatement stmt = connection.prepareStatement("SELECT * FROM users WHERE email=?");
            stmt.setString(1, email);

            ResultSet res = stmt.executeQuery();

            if(!res.first())
            {
                SFSErrorData errData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_USERNAME);
                errData.addParameter(email);

                throw new SFSLoginException("Bad user name: "+ email, errData);
            }

            String dbPword = res.getString("password");
            int dbId = res.getInt("id");

            if(!getApi().checkSecurePassword(session, dbPword, pass));
            {
                SFSErrorData errorData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD);
                errorData.addParameter(email);

                throw new SFSLoginException("Bad password for user: "+ email, errorData);
            }

            session.setProperty("DatabaseID", dbId);
           //UNREACHABLE CODE
           //IF I COMMENT THIS OUT, THERE IS NO UNREACHABLE CODE ERROR

        }

        catch(SQLException eve)
        {
            SFSErrorData erroData = new SFSErrorData(SFSErrorCode.GENERIC_ERROR);
            erroData.addParameter("SQL Error: " + eve.getMessage());

            throw new SFSLoginException("A SQL Error occurred: " + eve.getMessage(), erroData);
        }

        finally
        {
            try 
            {
                connection.close();
            }
            catch (SQLException e1) 
            {

            }
        }
    }

}
Was it helpful?

Solution

Your 2nd if statements terminate with ; that is a valid statement. and you are throwing an exception in the next block , that is why the error.

if(!getApi().checkSecurePassword(session, dbPword, pass));

The above if statement terminates with semicolon which is a valid statement, and if statement will act on it, Your other part of the code is executing irrespective of the if statement and that is throwing an exception at the end.

{
    SFSErrorData errorData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD);
    errorData.addParameter(email);

    throw new SFSLoginException("Bad password for user: "+ email, errorData);
}

That is why you are getting the error because your line session.setProperty("DatabaseID", dbId); would never reach.

OTHER TIPS

There is a bogus ; before the previous code block:

if(!getApi().checkSecurePassword(session, dbPword, pass));
                                                      // ^
                                                      // |
                                                      // +---- remove this ';'
{
   ...
   throw new SFSLoginException("Bad password for user: "+ email, errorData);
}

session.setProperty("DatabaseID", dbId);

The throw is therefore always executed, thus the code never reaches session.setProperty().

Because there is an undesired ; just after the second if, the following {} block will always get executed. Which means a SFSLoginException will always be thrown and execution will jump to the catch.

This will result in the setProperty method never getting called.

You need to remove the semicolon from the following statement in your code:

if(!getApi().checkSecurePassword(session, dbPword, pass));

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top