KickJava   Java API By Example, From Geeks To Geeks.

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


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 NetworkPlacesUpgrade extends AbstractDatabaseUpgrade {
29
30     NetworkPlacesUpgrade(File JavaDoc oldDbDir, File JavaDoc newDbDir) {
31         super("Network Places", "Copies all network places to the new format. "
32                         + "All are assumed to be SMB mounts. Any user created network "
33                         + "places are also copied (with the owner username appended to "
34                         + "the new resource name). The resource will not be attached to " + "any policies.", true,
35                         "explorer_configuration", "explorer_configuration", oldDbDir, newDbDir);
36     }
37
38     public void doUpgrade(Upgrader upgrader, Connection JavaDoc oldConx, Connection JavaDoc newConx) throws Exception JavaDoc {
39         // // Auth Schemes
40
upgrader.info("Migrating all network places");
41         Statement JavaDoc stmt = oldConx.createStatement();
42         try {
43             ResultSet JavaDoc rs = stmt.executeQuery("SELECT * FROM NETWORK_PLACES");
44             try {
45                 while (rs.next()) {
46                     String JavaDoc shortName = rs.getString("SHORT_NAME");
47                     String JavaDoc username = rs.getString("username");
48                     String JavaDoc uri = rs.getString("uri");
49                     if (uri.equals("/fs/cifs") || uri.equals("/fs/cifs/")) {
50                         upgrader.warn(" Skipping network neighbourhood network place, no longer supported.");
51                     } else {
52                         shortName = shortName + " (" + (username.equals("") ? "System" : username) + ")";
53                         PreparedStatement JavaDoc ps = newConx.prepareStatement("SELECT * FROM NETWORK_PLACES WHERE SHORT_NAME = ?");
54                         boolean found = false;
55                         try {
56                             ps.setString(1, shortName);
57                             ResultSet JavaDoc rs2 = ps.executeQuery();
58                             try {
59                                 if (rs2.next()) {
60                                     found = true;
61                                 }
62                             } finally {
63                                 rs2.close();
64                             }
65                         } finally {
66                             ps.close();
67                         }
68
69                         if (found) {
70                             upgrader.warn(" Network place '" + shortName + "' already exists, skipping");
71                         } else {
72                             insertNetworkPlace(upgrader, newConx, uri, shortName);
73                         }
74                     }
75                 }
76             } finally {
77                 rs.close();
78             }
79         } finally {
80             stmt.close();
81         }
82
83     }
84
85     void insertNetworkPlace(Upgrader upgrader, Connection JavaDoc newConx, String JavaDoc uri, String JavaDoc shortName) throws Exception JavaDoc {
86
87         PreparedStatement JavaDoc ps = newConx.prepareStatement("INSERT INTO NETWORK_PLACES (SHORT_NAME,"
88                         + "DESCRIPTION, PATH, READ_ONLY, ALLOW_RESURSIVE, "
89                         + "NO_DELETE, SHOW_HIDDEN, PARENT_RESOURCE_PERMISSION, " + "DATE_CREATED, DATE_AMENDED,SCHEME) VALUES "
90                         + "(?,?,?,?,?,?,?,?,NOW(),NOW(),?)");
91         try {
92             upgrader.info(" " + shortName);
93             ps.setString(1, shortName);
94             ps.setString(2, shortName);
95             if (uri.equals("/fs/cifs")) {
96                 uri = "smb://";
97             } else if (uri.startsWith("\\\\")) {
98                 uri = "smb:" + uri.replace('\\', '/');
99             } else {
100                 uri = "smb:/" + uri.substring(8);
101             }
102             ps.setString(3, uri);
103             ps.setBoolean(4, false);
104             ps.setBoolean(5, true);
105             ps.setBoolean(6, false);
106             ps.setBoolean(7, true);
107             ps.setInt(8, 0);
108             ps.setString(9, "smb");
109             try {
110                 ps.execute();
111             } catch (Exception JavaDoc e) {
112                 upgrader.warn("Failed to insert network place " + shortName + ". Probably already exists.");
113             }
114         } finally {
115             ps.close();
116         }
117     }
118
119 }
120
Popular Tags