Changeset 28201 for main/trunk


Ignore:
Timestamp:
2013-09-03T12:29:09+12:00 (11 years ago)
Author:
sjm84
Message:

Some major changes to DerbyWrapper to try and make it more reliable and consistent

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/DerbyWrapper.java

    r27908 r28201  
    2727import java.util.ArrayList;
    2828import java.util.HashMap;
     29import java.util.HashSet;
    2930
    3031import org.greenstone.gsdl3.service.Authentication;
     
    3940    static final String DATA = "data";
    4041    private Connection conn = null;
    41     private Statement state = null;
    4242    private String protocol_str;
    4343
    44     public DerbyWrapper()
    45     {
    46     }
    47 
    4844    public DerbyWrapper(String dbpath)
    4945    {
     
    5551        try
    5652        {
     53            if (conn != null)
     54            {
     55                System.err.println("Connection already established, close the database first");
     56                return;
     57            }
     58
    5759            Class.forName(DRIVER).newInstance();
    58             //System.out.println("Loaded the embedded driver.");
    5960            protocol_str = PROTOCOL + dbpath;
    6061            if (create_database)
     
    6667                conn = DriverManager.getConnection(protocol_str);
    6768            }
    68             state = conn.createStatement();
     69            conn.setAutoCommit(false);
    6970        }
    7071        catch (Throwable e)
     
    8485    public void closeDatabase()
    8586    {
    86         //state = null;
    87         //conn = null;
    88         boolean gotSQLExc = false;
    89         try
    90         {
    91             //  shutdown the database
    92             DriverManager.getConnection(protocol_str + ";shutdown=true");
    93 
    94         }
    95         catch (SQLException se)
    96         {
    97             // this is good (i.e. what Derby is designed to do on a successful shutdown)
    98             gotSQLExc = true;
    99         }
    100         catch (Exception e)
     87        try
     88        {
     89            conn.commit();
     90            conn.close();
     91            conn = null;
     92        }
     93        catch (SQLException e)
    10194        {
    10295            e.printStackTrace();
    103         }
    104 
    105         if (!gotSQLExc)
    106         {
    107             System.err.println("Warning: Derby Database did not shut down normally");
    10896        }
    10997    }
     
    117105            //  shutdown the whole server
    118106            DriverManager.getConnection(PROTOCOL + ";shutdown=true");
    119 
    120107        }
    121108        catch (SQLException se)
     
    127114        catch (Exception e)
    128115        {
    129 
    130116            e.printStackTrace();
    131117        }
     
    140126        try
    141127        {
    142             conn.setAutoCommit(false);
     128            Statement state = conn.createStatement();
    143129            state.execute("drop table data");
    144130            state.execute("create table data (username varchar(40) not null, name varchar(128) not null, value clob, primary key (username, name))");
    145131            conn.commit();
     132            state.close();
    146133        }
    147134        catch (SQLException e)
     
    151138    }
    152139
     140    public void clearTrackerData()
     141    {
     142        try
     143        {
     144            Statement state = conn.createStatement();
     145            state.execute("drop table usertracker");
     146            state.execute("create table usertracker (username varchar(40) not null, collection varchar(128) not null, site varchar(128) not null, oid varchar(128) not null, time varchar(128) not null, action varchar(128) not null, primary key (username, time))");
     147            conn.commit();
     148            state.close();
     149        }
     150        catch (SQLException e)
     151        {
     152            e.printStackTrace();
     153        }
     154    }
     155
    153156    public void createDatabase()
    154157    {
    155158        try
    156159        {
    157             conn.setAutoCommit(false);
     160            Statement state = conn.createStatement();
    158161            state.execute("create table users (username varchar(40) not null, password varchar(40) not null, accountstatus varchar(10), comment varchar(100), email varchar(40), primary key(username))");
    159162            state.execute("create table roles (username varchar(40) not null, role varchar(40) not null, primary key (username, role))");
    160163            state.execute("create table data (username varchar(40) not null, name varchar(128) not null, value clob, primary key (username, name))");
     164            state.execute("create table usertracker (username varchar(40) not null, collection varchar(128) not null, site varchar(128) not null, oid varchar(128) not null, time varchar(128) not null, action varchar(128) not null, primary key (username, time))");
     165
    161166            state.execute("insert into " + USERS + " values ('admin', '" + Authentication.hashPassword("admin") + "', 'true', 'change the password for this account as soon as possible', '')");
    162167            state.execute("insert into " + ROLES + " values ('admin', 'administrator')");
    163168            state.execute("insert into " + ROLES + " values ('admin', 'all-collections-editor')");
    164169            conn.commit();
     170            state.close();
    165171        }
    166172        catch (Exception ex)
     
    170176    }
    171177
    172     public UserQueryResult listAllUser() throws SQLException
     178    public void addUserAction(String username, String site, String collection, String oid, String action)
     179    {
     180        try
     181        {
     182            Statement state = conn.createStatement();
     183            state.execute("INSERT INTO usertracker VALUES ('" + username + "', '" + collection + "', '" + site + "', '" + oid + "', '" + System.currentTimeMillis() + "', '" + action + "')");
     184            conn.commit();
     185            state.close();
     186        }
     187        catch (Exception ex)
     188        {
     189            ex.printStackTrace();
     190        }
     191    }
     192
     193    public ArrayList<HashMap<String, String>> getMostRecentUserActions(String site, String collection, String oid)
     194    {
     195        ArrayList<HashMap<String, String>> actions = new ArrayList<HashMap<String, String>>();
     196
     197        try
     198        {
     199            String query = "SELECT username, action FROM usertracker WHERE site = '" + site + "' and collection = '" + collection + "' and oid = '" + oid + "' ORDER BY time";
     200            Statement state = conn.createStatement();
     201            ResultSet rs = state.executeQuery(query);
     202            conn.commit();
     203
     204            HashSet<String> usernamesSeen = new HashSet<String>();
     205
     206            while (rs.next())
     207            {
     208                HashMap<String, String> action = new HashMap<String, String>();
     209                if (!usernamesSeen.contains(rs.getString("username")))
     210                {
     211                    action.put("username", rs.getString("username"));
     212                    action.put("action", rs.getString("action"));
     213                    actions.add(action);
     214
     215                    usernamesSeen.add(rs.getString("username"));
     216                }
     217            }
     218            state.close();
     219        }
     220        catch (Exception ex)
     221        {
     222            ex.printStackTrace();
     223        }
     224        return actions;
     225    }
     226
     227    public UserQueryResult listAllUsers() throws SQLException
    173228    {
    174229        UserQueryResult userQueryResult = new UserQueryResult();
     
    176231
    177232        ArrayList<HashMap<String, String>> users = new ArrayList<HashMap<String, String>>();
     233        Statement state = conn.createStatement();
    178234        ResultSet rs = state.executeQuery(sql_list_all_user);
     235        conn.commit();
     236        state.close();
     237
    179238        while (rs.next())
    180239        {
     
    221280        try
    222281        {
     282            Statement state = conn.createStatement();
    223283            ResultSet rs = state.executeQuery("SELECT * FROM " + DATA + " WHERE username='" + username + "' AND name='" + name + "'");
     284            conn.commit();
     285            state.close();
    224286            if (rs.next())
    225287            {
     
    242304                ex.printStackTrace();
    243305            }
    244             closeDatabase();
     306
    245307            System.out.println("Error:" + ex.getMessage());
    246308            return false;
     
    249311        try
    250312        {
     313            PreparedStatement stmt = null;
    251314            if (!found)
    252315            {
    253                 PreparedStatement stmt = null;
    254316                stmt = conn.prepareStatement("INSERT INTO " + DATA + " VALUES (?, ?, ?)");
    255317                stmt.setString(1, username);
     
    260322            else
    261323            {
    262                 PreparedStatement stmt = null;
    263324                stmt = conn.prepareStatement("UPDATE " + DATA + " SET value=? WHERE username=? AND name=?");
    264325                stmt.setString(1, value);
     
    267328                stmt.executeUpdate();
    268329            }
     330            conn.commit();
     331            stmt.close();
    269332        }
    270333        catch (Exception ex)
     
    279342                ex.printStackTrace();
    280343            }
    281             closeDatabase();
     344
    282345            System.out.println("Error:" + ex.getMessage());
    283346            return false;
     
    290353        try
    291354        {
     355            Statement state = conn.createStatement();
    292356            ResultSet rs = state.executeQuery("SELECT * FROM " + DATA + " WHERE username='" + username + "' AND name='" + name + "'");
     357            conn.commit();
     358            state.close();
    293359            if (rs.next())
    294360            {
     
    307373                ex.printStackTrace();
    308374            }
    309             closeDatabase();
     375
    310376            System.out.println("Error:" + ex.getMessage());
    311377        }
     
    317383        try
    318384        {
    319             conn.setAutoCommit(false);
     385            Statement state = conn.createStatement();
    320386            String sql_insert_user = "insert into " + USERS + " values ('" + username + "', '" + password + "', '" + accountstatus + "', '" + comment + "', '" + email + "')";
    321387            state.execute(sql_insert_user);
     
    329395
    330396            conn.commit();
     397            state.close();
    331398        }
    332399        catch (Throwable e)
     
    341408                e.printStackTrace();
    342409            }
    343             closeDatabase();
     410
    344411            System.out.println("Error:" + e.getMessage());
    345412            return false;
     
    353420        try
    354421        {
    355             conn.setAutoCommit(false);
    356422            String sql_delete_user = "delete from " + USERS + " where username='" + del_username + "'";
    357423            String sql_delete_groups = "delete from " + ROLES + " where username='" + del_username + "'";
     424            Statement state = conn.createStatement();
    358425            state.execute(sql_delete_user);
    359426            state.execute(sql_delete_groups);
    360427            conn.commit();
     428            state.close();
    361429        }
    362430        catch (Throwable e)
     
    371439                e.printStackTrace();
    372440            }
    373             closeDatabase();
    374441            return false;
    375442        }
     
    379446    public boolean deleteAllUser() throws SQLException
    380447    {
    381         conn.setAutoCommit(false);
    382         try
    383         {
     448        try
     449        {
     450            Statement state = conn.createStatement();
    384451            state.execute("delete from " + USERS);
    385452            state.execute("delete from " + ROLES);
    386453            conn.commit();
     454            state.close();
    387455        }
    388456        catch (Throwable e)
     
    397465                e.printStackTrace();
    398466            }
    399             closeDatabase();
     467
    400468            return false;
    401469        }
     
    406474    {
    407475        UserQueryResult userQueryResult = new UserQueryResult();
    408 
    409         try
    410         {
    411             conn.setAutoCommit(false);
    412         }
    413         catch (Exception ex)
    414         {
    415             ex.printStackTrace();
    416             return null;
    417         }
    418476
    419477        String sql_find_user = "SELECT  username, password, accountstatus, comment, email FROM " + USERS;
     
    443501        {
    444502            ArrayList<HashMap<String, String>> users = new ArrayList<HashMap<String, String>>();
     503            Statement state = conn.createStatement();
    445504            ResultSet rs = state.executeQuery(sql_find_user);
    446505            while (rs.next())
     
    473532                userQueryResult.addUserTerm(user.get("username"), user.get("password"), group, user.get("as"), user.get("comment"), user.get("email"));
    474533            }
     534            state.close();
    475535        }
    476536        catch (Exception ex)
     
    496556        UserQueryResult userQueryResult = new UserQueryResult();
    497557
    498         conn.setAutoCommit(false);
    499558        String sql_find_user = "SELECT  username, password, accountstatus, comment, email FROM " + USERS;
    500559        String append_sql = "";
     
    510569
    511570        ArrayList<HashMap<String, String>> users = new ArrayList<HashMap<String, String>>();
     571
     572        Statement state = conn.createStatement();
    512573        ResultSet rs = state.executeQuery(sql_find_user);
    513574        while (rs.next())
     
    540601            userQueryResult.addUserTerm(user.get("username"), user.get("password"), group, user.get("as"), user.get("comment"), user.get("email"));
    541602        }
     603        state.close();
    542604
    543605        if (userQueryResult.getSize() > 0)
     
    556618        try
    557619        {
    558             conn.setAutoCommit(false);
    559620            String sql_modify_user_info = "update " + USERS + " set ";
    560621
     
    578639
    579640            sql_modify_user_info += " where username='" + username + "'";
     641            Statement state = conn.createStatement();
    580642            state.execute(sql_modify_user_info);
    581643
     
    591653
    592654            conn.commit();
     655            state.close();
    593656        }
    594657        catch (Throwable e)
     
    603666                e.printStackTrace();
    604667            }
    605             closeDatabase();
     668
    606669            return "Error:" + e.getMessage();
    607670        }
     
    620683        try
    621684        {
    622             conn.setAutoCommit(false); // An exception at this line can happen when the GS3 tomcat server is already running
    623                                         // and GS3 is already accessing the usersDB when this function independently tries to
    624                                         // connect to it (via usersDB2txt.java's main(). For an explanation of the possible
    625                                         // reasons, see http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html
    626                                         // section "Embedded Derby supports multiple users in one JVM".
    627685            String sql_list_all_user = "select username, password, accountstatus, comment, email from " + USERS;
     686
     687            Statement state = conn.createStatement();
    628688            ResultSet rs = state.executeQuery(sql_list_all_user);
    629689
     
    668728
    669729            conn.commit();
    670             closeDatabase();
     730            state.close();
    671731        }
    672732        catch (Exception ex)
     
    693753        try
    694754        {
    695             conn.setAutoCommit(false);
     755            Statement state = conn.createStatement();
    696756            state.execute("DELETE FROM data WHERE username = '" + username + "' AND SUBSTR(name, 1, " + prefix.length() + ") = '" + prefix + "'");
    697757            conn.commit();
     758            state.close();
    698759        }
    699760        catch (Exception ex)
Note: See TracChangeset for help on using the changeset viewer.