001    package org.esupportail.cas.server.handlers.database;
002    
003    import java.sql.Connection;
004    
005    import org.dom4j.Element;
006    import org.esupportail.cas.server.util.RedundantHandler;
007    
008    /**
009     * This class implements a database server class, which can
010     * authenticate users by binding to a database. It is used by 
011     * BindDatabaseHandler.
012     *
013     * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr> 
014     * @author Jean-Baptiste Daniel <danielj at users.sourceforge.net>
015     */
016    public final class BindDatabaseServer extends DatabaseServer {
017            
018            /**
019             * Constructor.
020             *
021             * @param handlerDebug debugging mode of the handler
022             * @param handler the handler the server will be used by
023             * @param serverElement the XML element that declares the server 
024             * @throws Exception Exception
025             */
026            public BindDatabaseServer(final Boolean handlerDebug,
027                                                              final RedundantHandler handler,
028                                                              final Element serverElement) throws Exception {
029                    super(handlerDebug, handler, serverElement);
030                    traceBegin();
031                    traceEnd();
032            }
033                    
034            /**
035             * Try to authenticate a user (by binding to the database).
036             *
037             * @param username the user's name
038             * @param password the user's password
039             *
040             * @return Server.AUTHENTICATE_SUCCESS, Server.AUTHENTICATE_NOAUTH
041             * or Server.AUTHENTICATE_FAILURE.
042             */     
043            public int authenticate(
044                            final String username,
045                            final String password) {
046                    traceBegin();
047                    
048                    Connection connection = connect(username, password);
049                    
050                    switch (getConnectError()) {
051                            case CONNECT_SUCCESS:
052                                    trace("Connection succeeded.");
053                                    traceEnd("AUTHENTICATE_SUCCESS");
054                                    return AUTHENTICATE_SUCCESS;
055                            case CONNECT_NOAUTH:
056                                    trace("Connection refused.");
057                                    traceEnd("AUTHENTICATE_NOAUTH");
058                                    return AUTHENTICATE_NOAUTH;
059                            default:
060                                    trace("Connection failure.");
061                            traceEnd("AUTHENTICATE_FAILURE");
062                            return AUTHENTICATE_FAILURE;
063                    }
064            }
065            
066    }
067