-1

I have an interface

    import javax.naming.AuthenticationException;

    import com.unboundid.ldap.sdk.LDAPException;


    public interface AuthenticationServiceManager {

/**
 * Creates the Connection for the specific user logging in, and binds the
 * user's credentials to  object.
 * 
 * @param userName
 *            The user name to authenticate .
 * @param password
 *            The user's password to check .
 * @param args
 *            is an object that holds variable arguments which can be used
 *            to authenticate using both LDAP and OpenId
 * @return boolean The connection status showing whether the user has been
 *         successfully authenticated or not.
 * @throws AuthenticationException
 *             If there is an error authenticating with the passed
 *             parameters
 **/

boolean authenticate(String username, String password, Object... args)
        throws AuthenticationException, LDAPException;

/**
 * Disconnects the connection.
 */
void disconnect();

}

A class that implements this interface

    package com.cerner.jira.plugins.esig.servicemanagerimpl;

   import javax.naming.AuthenticationException;

    import com.cerner.jira.plugins.esig.servicemanager.AuthenticationServiceManager;
   import com.unboundid.ldap.sdk.LDAPException;

    public class AuthenticationManagerImpl implements AuthenticationServiceManager {

@Override
public boolean authenticate(String username, String password)
        throws AuthenticationException, LDAPException {

    return false;
}

@Override
public boolean authenticate(String username, String password, String url)
{
    return false;


}

@Override
public void disconnect() {
    // TODO Auto-generated method stub

}

}

I'm trying to create an interface that can be used to implement a connection class. I should be able to use this for different authentication like LDAP, OpenId etc; So I want to pass username,password(if its LDAP) and variable number of arguments(If OpenId). How do I do that? I tried this. It is throwing error. How do I initialize the object to hold variable parameters?

Error :The method authenticate(String, String) of type AuthenticationManagerImpl must override or implement a supertype method

animuson
  • 53,861
  • 28
  • 137
  • 147
user3438489
  • 309
  • 2
  • 5
  • 14
  • 1
    *"It is throwing error."* ***What*** error? Always, always, *always* include basic information like that. Why would you think it was optional? – T.J. Crowder Apr 10 '14 at 21:06
  • I think you are reinventing the wheel a bit here. Have you checked this out yet? http://docs.spring.io/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ns-openid – Jackie Apr 10 '14 at 21:10
  • Im sorry about that.. this is the error "The method authenticate(String, String) of type AuthenticationManagerImpl must override or implement a supertype method" – user3438489 Apr 10 '14 at 21:10

2 Answers2

0

A couple possibilities.

One is to overload the method in the interface, by declaring the method multiple times with different sets of parameters.

Another is to list the arguments as a variable-length data structure such as an array. The command line interface does this with public static void main(String[] args) whereby you can pass multiple command line arguments. You can do this as follows:

boolean authenticate(String username, String password, Object[] args)
    throws AuthenticationException, LDAPException;
La-comadreja
  • 5,627
  • 11
  • 36
  • 64
0
@Override
public boolean authenticate(String username, String password)
        throws AuthenticationException, LDAPException {

    return false;
}

@Override
public boolean authenticate(String username, String password, String url) {
    return false;
}

There's no method in AuthenticationServiceManagerinterface that receives String, String arguments or String, String, String arguments. Instead, you have a single authenticate method that receives a varargs, so, in your class implementing AuthenticationServiceManager interface, you should implement the method as:

@Override
public boolean authenticate(String username, String password, Object ... args) {
    //handle args as you want/need
    //if you don't need it for this specific case, then do nothing with the variable
    //and yes, it is an ugly design =\
    return false;
}

Another design option is having AuthenticationServiceManager interface with boolean authenticate(String username, String password) method and other methods where you add more parameters according to your needs:

public interface AuthenticationServiceManager {
    boolean authenticate(String username, String password)
        throws AuthenticationException, LDAPException;
    boolean authenticate(String username, String password, String url)
        throws AuthenticationException, LDAPException;
    boolean authenticate(String username, String password, String url, String anotherParam)
        throws AuthenticationException, LDAPException;
    //and on and on...
    boolean authenticate(String username, String password, Object... args)
        throws AuthenticationException, LDAPException;

    /**
     * Disconnects the connection.
     */
    void disconnect();
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332