KickJava   Java API By Example, From Geeks To Geeks.

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


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 AuthSchemeUpgrade extends AbstractDatabaseUpgrade {
29
30     AuthSchemeUpgrade(File JavaDoc oldDbDir, File JavaDoc newDbDir) {
31         super("Authentication Schemes",
32             "Copies authentication schemes and the modules they contain to the . "
33                         + "new installation. Any schemes created will be enabled by default but not policies "
34                         + "will be attached.", true, "explorer_configuration", "explorer_configuration", oldDbDir, newDbDir);
35     }
36
37     public void doUpgrade(Upgrader upgrader, Connection JavaDoc oldConx, Connection JavaDoc newConx) throws Exception JavaDoc {
38         // // Auth Schemes
39
upgrader.info("Migrating all authentication schemes");
40         Statement JavaDoc stmt = oldConx.createStatement();
41         try {
42             ResultSet JavaDoc rs = stmt.executeQuery("SELECT * FROM AUTH_SCHEMES");
43             try {
44                 while (rs.next()) {
45                     String JavaDoc authScheme = rs.getString("ID");
46                     PreparedStatement JavaDoc ps = newConx.prepareStatement("SELECT * FROM AUTH_SCHEMES WHERE RESOURCE_NAME = ?");
47                     boolean found = false;
48                     try {
49                         ps.setString(1, authScheme);
50                         ResultSet JavaDoc rs2 = ps.executeQuery();
51                         try {
52                             if (rs2.next()) {
53                                 found = true;
54                             }
55                         } finally {
56                             rs2.close();
57                         }
58                     } finally {
59                         ps.close();
60                     }
61
62                     if (found) {
63                         upgrader.warn(" Auth scheme '" + authScheme + "' already exists, skipping");
64                     } else {
65                         insertScheme(upgrader, newConx, oldConx, authScheme);
66                     }
67                 }
68             } finally {
69                 rs.close();
70             }
71         } finally {
72             stmt.close();
73         }
74
75     }
76
77     private void insertScheme(Upgrader upgrader, Connection JavaDoc newConx, Connection JavaDoc oldConx, String JavaDoc authScheme) throws Exception JavaDoc {
78
79         PreparedStatement JavaDoc ps = newConx.prepareStatement("INSERT INTO AUTH_SCHEMES (RESOURCE_NAME,"
80                         + "RESOURCE_DESCRIPTION, DATE_CREATED,DATE_AMENDED,ENABLED) VALUES (?,?,NOW(),NOW(), 1)");
81         try {
82             upgrader.info(" " + authScheme);
83             ps.setString(1, authScheme);
84             ps.setString(2, authScheme);
85             try {
86                 ps.execute();
87             } catch (Exception JavaDoc e) {
88                 upgrader.warn("Failed to insert auth scheme " + authScheme + ". Probably already exists.");
89             }
90             PreparedStatement JavaDoc ps2 = newConx.prepareStatement("SELECT RESOURCE_ID FROM AUTH_SCHEMES WHERE RESOURCE_NAME = ?");
91             try {
92                 ps2.setString(1, authScheme);
93                 ResultSet JavaDoc rs2 = ps2.executeQuery();
94                 try {
95                     if (rs2.next()) {
96                         PreparedStatement JavaDoc ps3 = oldConx.prepareStatement("SELECT * FROM AUTH_SEQUENCE WHERE ID = ?");
97                         try {
98                             ps3.setString(1, authScheme);
99                             ResultSet JavaDoc rs3 = ps3.executeQuery();
100                             try {
101                                 while (rs3.next()) {
102                                     String JavaDoc moduleId = rs3.getString("MODULE_ID");
103                                     upgrader.warn(" [" + moduleId + "]");
104                                     PreparedStatement JavaDoc ps4 = newConx
105                                                     .prepareStatement("INSERT INTO AUTH_SEQUENCE (SCHEME_ID,MODULE_ID,SEQUENCE) VALUES(?,?,?)");
106                                     try {
107                                         ps4.setInt(1, rs2.getInt("RESOURCE_ID"));
108                                         ps4.setString(2, moduleId);
109                                         ps4.setInt(3, rs3.getInt("SEQUENCE"));
110                                         try {
111                                             ps4.execute();
112                                         } catch (Exception JavaDoc e) {
113                                             upgrader.warn("Failed to insert authentication sequence. Probably already exists.");
114                                         }
115                                     } finally {
116                                         ps4.close();
117                                     }
118                                 }
119                             } finally {
120                                 rs3.close();
121                             }
122                         } finally {
123                             ps3.close();
124                         }
125                     }
126                 } finally {
127                     rs2.close();
128                 }
129             } finally {
130                 ps2.close();
131             }
132         } finally {
133             ps.close();
134         }
135
136     }
137
138 }
139
Popular Tags