KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > upgrade > UserUpgrade


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.upgrade;
21
22 import java.io.File JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.Statement JavaDoc;
27
28 public class UserUpgrade extends AbstractDatabaseUpgrade {
29     
30     UserUpgrade(File JavaDoc oldDbDir, File JavaDoc newDbDir) {
31         super("Users and Roles", "Copies users and roles created " +
32                 "when the Built-in User Database is in use.", true,
33                 "explorer_accounts",
34                 "explorer_configuration", oldDbDir, newDbDir);
35     }
36
37     public void doUpgrade(Upgrader upgrader, Connection JavaDoc oldConx, Connection JavaDoc newConx) throws Exception JavaDoc {
38 // Roles
39
upgrader.info("Migrating all roles");
40         Statement JavaDoc stmt = oldConx.createStatement();
41         try {
42             ResultSet JavaDoc rs = stmt.executeQuery("SELECT * FROM ROLES");
43             try {
44                 while (rs.next()) {
45                     PreparedStatement JavaDoc ps = newConx.prepareStatement("INSERT INTO ROLES (ROLENAME) VALUES (?)");
46                     try {
47                         String JavaDoc rolename = rs.getString("ROLENAME");
48                         upgrader.info(" " + rolename);
49                         ps.setString(1, rolename);
50                         try {
51                             ps.execute();
52                         } catch (Exception JavaDoc e) {
53                             upgrader.warn("Failed to insert role " + rolename + ". Probably already exists.");
54                         }
55                     } finally {
56                         ps.close();
57                     }
58                 }
59             } finally {
60                 rs.close();
61             }
62         } finally {
63             stmt.close();
64         }
65         // Users
66
upgrader.info("Migrating all users");
67         stmt = oldConx.createStatement();
68         try {
69             ResultSet JavaDoc rs = stmt.executeQuery("SELECT * FROM USERS");
70             try {
71                 while (rs.next()) {
72                     String JavaDoc username = rs.getString("USERNAME");
73                     upgrader.info(" " + username);
74                     PreparedStatement JavaDoc ps = newConx.prepareStatement("INSERT INTO USERS (USERNAME, EMAIL, FULLNAME"
75                                     + ", LAST_PASSWORD_CHANGE, PASSWORD) VALUES (?,?,?,?,?)");
76                     try {
77                         ps.setString(1, username);
78                         ps.setString(2, rs.getString("EMAIL"));
79                         ps.setString(3, rs.getString("FULLNAME"));
80                         ps.setString(4, rs.getString("LAST_PASSWORD_CHANGE"));
81                         ps.setString(5, rs.getString("PASSWORD"));
82                         try {
83                             ps.execute();
84                         } catch (Exception JavaDoc e) {
85                             upgrader.warn("Failed to insert user " + username + ". Probably already exists.");
86                         }
87                     } finally {
88                         ps.close();
89                     }
90                 }
91             } finally {
92                 rs.close();
93             }
94         } finally {
95             stmt.close();
96         }
97         // User roles
98
upgrader.info("Migrating all user roles");
99         stmt = oldConx.createStatement();
100         try {
101             ResultSet JavaDoc rs = stmt.executeQuery("SELECT * FROM USER_ROLES");
102             try {
103                 while (rs.next()) {
104                     String JavaDoc username = rs.getString("USERNAME");
105                     String JavaDoc rolename = rs.getString("ROLENAME");
106                     upgrader.info(" " + username + "/" + rolename);
107                     PreparedStatement JavaDoc ps = newConx
108                                     .prepareStatement("INSERT INTO USER_ROLES (USERNAME, ROLENAME) VALUES (?," + "?)");
109                     try {
110                         ps.setString(1, username);
111                         ps.setString(2, rolename);
112                         try {
113                             ps.execute();
114                         } catch (Exception JavaDoc e) {
115                             upgrader.warn("Failed to insert user role " + username + "/" + rolename
116                                             + ".Probably already exists.");
117                         }
118                     } finally {
119                         ps.close();
120                     }
121                 }
122             } finally {
123                 rs.close();
124             }
125         } finally {
126             stmt.close();
127         }
128
129     }
130
131 }
132
Popular Tags