colus       2002/10/29 22:15:10

  Modified:    ftpserver/src/java/org/apache/avalon/ftpserver/usermanager
                        DbUserManager.java
  Log:
  connection recovery.
  
  Revision  Changes    Path
  1.12      +116 -54   
jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/usermanager/DbUserManager.java
  
  Index: DbUserManager.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/usermanager/DbUserManager.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- DbUserManager.java        29 Oct 2002 17:08:02 -0000      1.11
  +++ DbUserManager.java        30 Oct 2002 06:15:10 -0000      1.12
  @@ -43,6 +43,9 @@
       private static final String UPDATE_USER_SQL   = "UPDATE FTP_USER SET PASSWORD = 
?, HOME_DIR = ?, ENABLED = ?, WRITE_PERM = ?, IDLE_TIME = ?, UPLOAD_RATE = ?, 
DOWNLOAD_RATE = ? WHERE LOGIN_ID = ?";
       private static final String DELETE_USER_SQL   = "DELETE FROM FTP_USER WHERE 
LOGIN_ID = ?";
   
  +    private String m_dbUrl;
  +    private String m_dbUser;
  +    private String m_dbPassword;
       private Connection mDbConnection = null;
   
       private PreparedStatement mNewUserStmt = null;
  @@ -69,35 +72,113 @@
   
           // open database connection
           String className = conf.getChild("driver").getValue();
  -        String url       = conf.getChild("url").getValue();
  -        String user      = conf.getChild("user").getValue();
  -        String password  = conf.getChild("password").getValue();
  +        m_dbUrl = conf.getChild("url").getValue();
  +        m_dbUser = conf.getChild("user").getValue();
  +        m_dbPassword = conf.getChild("password").getValue();
           
           try {
               Class.forName(className);
  -            mDbConnection = DriverManager.getConnection(url, user, password);
  -            mDbConnection.setAutoCommit(true);
  -                
  -            // prepare statements
  -            mGetAllStmt = mDbConnection.prepareStatement( GET_ALL_USERS_SQL );
  -            mGetUserStmt = mDbConnection.prepareStatement( GET_USER_SQL );
  -            mNewUserStmt = mDbConnection.prepareStatement( NEW_USER_SQL );
  -            mUpdUserStmt = mDbConnection.prepareStatement( UPDATE_USER_SQL );
  -            mDelUserStmt = mDbConnection.prepareStatement( DELETE_USER_SQL );
  +
  +            openDbConnection();
               getLogger().info("Database user manager opened.");
           }
           catch(Exception ex) {
               throw new ConfigurationException("DbUserManager.configure()", ex);
           }
       }
  -       
  +
  +    /**
  +     * Open connection to database.
  +     */
  +    private void openDbConnection()
  +        throws SQLException
  +    {
  +        mDbConnection = DriverManager.getConnection(m_dbUrl, m_dbUser, 
m_dbPassword);
  +        mDbConnection.setAutoCommit(true);
  +            
  +        // prepare statements
  +        mGetAllStmt = mDbConnection.prepareStatement( GET_ALL_USERS_SQL );
  +        mGetUserStmt = mDbConnection.prepareStatement( GET_USER_SQL );
  +        mNewUserStmt = mDbConnection.prepareStatement( NEW_USER_SQL );
  +        mUpdUserStmt = mDbConnection.prepareStatement( UPDATE_USER_SQL );
  +        mDelUserStmt = mDbConnection.prepareStatement( DELETE_USER_SQL );
  +
  +        getLogger().info("Connection opened.");
  +    }
  +
  +    /**
  +     * Close connection to database.
  +     */
  +    private void closeDbConnection()
  +    {
  +        if (mNewUserStmt != null) {
  +            try {mNewUserStmt.close(); } catch(SQLException ex) {}
  +            mNewUserStmt = null;
  +        }
  +        
  +        if (mDelUserStmt != null) {
  +            try {mDelUserStmt.close(); } catch(SQLException ex) {}
  +            mDelUserStmt = null;
  +        }
  +        
  +        if (mGetUserStmt != null) {
  +            try {mGetUserStmt.close(); } catch(SQLException ex) {}
  +            mGetUserStmt = null;
  +        }
  +        
  +        if (mGetAllStmt != null) {        
  +            try {mGetAllStmt.close(); } catch(SQLException ex) {}
  +            mGetAllStmt = null;
  +        }
  +        
  +        if (mUpdUserStmt != null) {
  +            try {mUpdUserStmt.close(); } catch(SQLException ex) {}
  +            mUpdUserStmt = null;
  +        }
  +        
  +        if (mDbConnection != null) {        
  +            try {mDbConnection.close(); } catch(SQLException ex) {}
  +            mDbConnection = null;
  +        }
  +
  +        getLogger().info("Connection closed.");
  +    }
           
       /**
  +     * Prepare connection to database.
  +     */
  +    private void prepareDbConnection()
  +        throws SQLException
  +    {
  +        boolean closed = false;
  +        try
  +        {
  +            //FIXME: better connection check.
  +            if ( null == mDbConnection || mDbConnection.isClosed() )
  +            {
  +                closed = true;
  +            }
  +        }
  +        catch ( final SQLException se )
  +        {
  +            closed = true;
  +        }
  +
  +        if ( closed )
  +        {
  +            closeDbConnection();
  +            openDbConnection();
  +        }
  +    }
  +     
  +    /**
        * Delete user. Delete the row from the table.
        */
       public synchronized void delete(String name) throws SQLException {
  -       mDelUserStmt.setString(1, name);
  -       mDelUserStmt.executeUpdate();
  +        prepareDbConnection();
  +
  +        mDelUserStmt.setString(1, name);
  +        mDelUserStmt.executeUpdate();
       }
   
   
  @@ -105,6 +186,7 @@
        * Save user. If new insert a new row, else update the existing row.
        */
       public synchronized void save(User user) throws SQLException {
  +        prepareDbConnection();
   
           // null value check
           if(user.getName() == null) {
  @@ -141,6 +223,8 @@
        */
       public synchronized User getUserByName(String name) {
           try {
  +            prepareDbConnection();
  +
               User thisUser = null;
               mGetUserStmt.setString(1, name);
               ResultSet rs = mGetUserStmt.executeQuery();
  @@ -158,7 +242,7 @@
               return thisUser;
           }
           catch(SQLException ex) {
  -             getLogger().error("DbUserManager.getUserByName()", ex);
  +            getLogger().error("DbUserManager.getUserByName()", ex);
           }
           return null;
       }
  @@ -170,6 +254,8 @@
       public synchronized boolean doesExist(String name) {
           boolean bValid = false;
           try {
  +            prepareDbConnection();
  +
               mGetUserStmt.setString(1, name);
               ResultSet rs = mGetUserStmt.executeQuery();
               bValid = rs.next();
  @@ -177,7 +263,7 @@
           }
           catch(SQLException ex) {
               bValid = false;
  -             getLogger().error("DbUserManager.doesExist()", ex);
  +            getLogger().error("DbUserManager.doesExist()", ex);
           }
           return bValid;
       }
  @@ -189,6 +275,8 @@
       public synchronized List getAllUserNames() {
           ArrayList names = new ArrayList();
           try {
  +            prepareDbConnection();
  +
               ResultSet rs = mGetAllStmt.executeQuery();
               while(rs.next()) {
                   names.add(rs.getString("LOGIN_ID"));
  @@ -196,7 +284,7 @@
               rs.close();
           }
           catch(SQLException ex) {
  -             getLogger().error("DbUserManager.getAllUserNames()", ex);
  +            getLogger().error("DbUserManager.getAllUserNames()", ex);
           }
           Collections.sort(names);
           return names;
  @@ -216,16 +304,17 @@
        * </pre>
        */
       private synchronized String getPassword(User user) throws SQLException {
  -     
  +        prepareDbConnection();
  +
           if (user.getPassword() != null) {
  -             return user.getPassword();
  +            return user.getPassword();
           }
           
           String password = "";
           mGetUserStmt.setString(1, user.getName());
           ResultSet rs = mGetUserStmt.executeQuery();
  -     if (rs.next()) {
  -             password = rs.getString("PASSWORD");
  +        if (rs.next()) {
  +            password = rs.getString("PASSWORD");
           }
           rs.close();
           if (password == null) {
  @@ -238,15 +327,16 @@
        * User authentication
        */
       public synchronized boolean authenticate(String user, String password) {
  -     
           String existPassword = null;
           
           try {
  +            prepareDbConnection();
  +
               mGetUserStmt.setString(1, user);
               ResultSet rs = mGetUserStmt.executeQuery();
               if (rs.next()) {
                   existPassword = rs.getString("PASSWORD");
  -             }
  +            }
               rs.close();
           }
           catch(SQLException ex) {
  @@ -266,34 +356,6 @@
        * Close this user manager. Close the database statements and connection.
        */
       public synchronized void dispose() {
  -        if (mNewUserStmt != null) {
  -            try {mNewUserStmt.close(); } catch(SQLException ex) {}
  -            mNewUserStmt = null;
  -        }
  -        
  -        if (mDelUserStmt != null) {
  -            try {mDelUserStmt.close(); } catch(SQLException ex) {}
  -            mDelUserStmt = null;
  -        }
  -        
  -        if (mGetUserStmt != null) {
  -            try {mGetUserStmt.close(); } catch(SQLException ex) {}
  -            mGetUserStmt = null;
  -        }
  -        
  -        if (mGetAllStmt != null) {        
  -            try {mGetAllStmt.close(); } catch(SQLException ex) {}
  -            mGetAllStmt = null;
  -        }
  -        
  -        if (mUpdUserStmt != null) {
  -            try {mUpdUserStmt.close(); } catch(SQLException ex) {}
  -            mUpdUserStmt = null;
  -        }
  -        
  -        if (mDbConnection != null) {        
  -            try {mDbConnection.close(); } catch(SQLException ex) {}
  -            mDbConnection = null;
  -        }
  +        closeDbConnection();
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@;jakarta.apache.org>

Reply via email to