KickJava   Java API By Example, From Geeks To Geeks.

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


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 ReplacementsUpgrade extends AbstractDatabaseUpgrade {
29
30     ReplacementsUpgrade(File JavaDoc oldDbDir, File JavaDoc newDbDir) {
31         super(
32                         "Replacements",
33                         "Copies replacement patterns used by the replacement " +
34                         "proxy web forwards (previously known as Secure Proxy). " +
35                         "All new replacements will be deleted and the ones from " +
36                         "your old installation will be copied across. Only " +
37                         "global replacements will be copied. DO NOT SELECT " +
38                         "THIS UPGRADE UNLESS YOU HAVE CREATED " +
39                         "CUSTOM REPLACEMENTS AND WISH TO KEEP THEM. ",
40                         false, "explorer_configuration", "explorer_configuration", oldDbDir, newDbDir);
41     }
42
43     public void doUpgrade(Upgrader upgrader, Connection JavaDoc oldConx, Connection JavaDoc newConx) throws Exception JavaDoc {
44         // // Auth Schemes
45
upgrader.info("Migrating all replacements");
46         Statement JavaDoc stmt = oldConx.createStatement();
47         try {
48             ResultSet JavaDoc rs = stmt.executeQuery("SELECT * FROM REPLACEMENTS");
49             try {
50                 while (rs.next()) {
51                     Statement JavaDoc stmt2 = newConx.createStatement();
52                     try {
53                         stmt2.execute("DELETE FROM REPLACEMENTS");
54                     }
55                     finally {
56                         stmt2.close();
57                     }
58
59                     PreparedStatement JavaDoc ps = newConx.prepareStatement(
60                         "INSERT INTO REPLACEMENTS (USERNAME,"
61                                     + "SITE_PATTERN,MIME_TYPE,SEQUENCE,MATCH_PATTERN,REPLACE_PATTERN,REPLACE_TYPE) VALUES (?,?,?,?,?,?,?)");
62                     try {
63                         String JavaDoc username =rs.getString("USERNAME");
64                         int seq = rs.getInt("SEQUENCE");
65                         if(username.equals("")) {
66                             ps.setString(1, username);
67                             ps.setString(2, rs.getString("SITE_PATTERN"));
68                             ps.setString(3, rs.getString("MIME_TYPE"));
69                             ps.setInt(4, seq);
70                             ps.setString(5, rs.getString("MATCH_PATTERN"));
71                             ps.setString(6, rs.getString("REPLACE_PATTERN"));
72                             ps.setInt(7, rs.getInt("REPLACE_TYPE"));
73                             try {
74                                 ps.execute();
75                             } catch (Exception JavaDoc e) {
76                                 upgrader.warn("Failed to insert IP restriction for sequence " + seq + ". Probably already exists.");
77                             }
78                         }
79                         else {
80                             upgrader.warn("Skipping user replacement for user '" + username + "'.");
81                         }
82                     } finally {
83                         ps.close();
84                     }
85                 }
86             } finally {
87                 rs.close();
88             }
89         } finally {
90             stmt.close();
91         }
92
93     }
94
95 }
96
Popular Tags