KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cmsinstaller > Uninstaller


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cmsinstaller;
25
26 import java.io.*;
27 import java.util.*;
28
29 public class Uninstaller
30 {
31     /**
32      * This is the entry point for the uninstaller.
33      */

34     
35     public static void main(String JavaDoc[] args)
36     {
37         Logger.logInfo("******************************************");
38         Logger.logInfo("* Welcome to the uninstaller of infoglue *");
39         Logger.logInfo("******************************************");
40         
41         Uninstaller uninstaller = new Uninstaller();
42         uninstaller.uninstall();
43     }
44     
45     
46     /**
47      * Unistaller constructor
48      */

49     
50     public Uninstaller()
51     {
52     }
53     
54
55     /**
56      * This is the method that creates the menu option in the start-menu but of
57      * course only if it's a windows-machine. If linux the kiddies know the
58      * way of the beast or soon learn to. Got to check the possibilities later.
59      */

60     
61     public void createStartMenuItems(List removableDirectories, String JavaDoc dbUrl, String JavaDoc databaseServer, String JavaDoc driverName, String JavaDoc tomcatHomePath, String JavaDoc dbName, String JavaDoc hostName) throws Exception JavaDoc
62     {
63         String JavaDoc operativeSystemName = System.getProperty("os.name");
64         Logger.logInfo("operativeSystemName:" + operativeSystemName);
65         
66         if(operativeSystemName.toLowerCase().indexOf("windows") > -1)
67         {
68             String JavaDoc userHomePath = System.getProperty("user.home");
69             Logger.logInfo("userHomePath:" + userHomePath);
70
71             String JavaDoc startMenuFolderPath = userHomePath + File.separator + "Start Menu" + File.separator + "Programs" + File.separator + "InfoGlue";
72             File startMenuFolder = new File(startMenuFolderPath);
73             startMenuFolder.mkdirs();
74             
75             System.err.println("Copying file: " + "org" + File.separator + "infoglue" + File.separator + "cmsinstaller" + File.separator + "Uninstaller.class");
76             File classFile = new File(startMenuFolder + File.separator + "org" + File.separator + "infoglue" + File.separator + "cmsinstaller");
77             classFile.mkdirs();
78             copyInputStream(new FileInputStream("org" + File.separator + "infoglue" + File.separator + "cmsinstaller" + File.separator + "Uninstaller.class"), new BufferedOutputStream(new FileOutputStream(classFile.getPath() + File.separator + "Uninstaller.class")));
79             
80             System.err.println("Copying file: " + "org" + File.separator + "infoglue" + File.separator + "cmsinstaller" + File.separator + "MySQLDatabaseCommander.class");
81             copyInputStream(new FileInputStream("org" + File.separator + "infoglue" + File.separator + "cmsinstaller" + File.separator + "MySQLDatabaseCommander.class"), new BufferedOutputStream(new FileOutputStream(classFile.getPath() + File.separator + "MySQLDatabaseCommander.class")));
82             
83             System.err.println("Copying file: " + "org" + File.separator + "infoglue" + File.separator + "cmsinstaller" + File.separator + "SQLServerDatabaseCommander.class");
84             copyInputStream(new FileInputStream("org" + File.separator + "infoglue" + File.separator + "cmsinstaller" + File.separator + "SQLServerDatabaseCommander.class"), new BufferedOutputStream(new FileOutputStream(classFile.getPath() + File.separator + "SQLServerDatabaseCommander.class")));
85
86             System.err.println("Copying file: " + "org" + File.separator + "infoglue" + File.separator + "cmsinstaller" + File.separator + "DatabaseCommander.class");
87             copyInputStream(new FileInputStream("org" + File.separator + "infoglue" + File.separator + "cmsinstaller" + File.separator + "DatabaseCommander.class"), new BufferedOutputStream(new FileOutputStream(classFile.getPath() + File.separator + "DatabaseCommander.class")));
88
89             System.err.println("Copying file: " + "org" + File.separator + "infoglue" + File.separator + "cmsinstaller" + File.separator + "Logger.class");
90             copyInputStream(new FileInputStream("org" + File.separator + "infoglue" + File.separator + "cmsinstaller" + File.separator + "Logger.class"), new BufferedOutputStream(new FileOutputStream(classFile.getPath() + File.separator + "Logger.class")));
91             
92             System.err.println("Copying file: " + "configTemplates" + File.separator + "Uninstall.bat");
93             copyInputStream(new FileInputStream("configTemplates" + File.separator + "Uninstall.bat"), new BufferedOutputStream(new FileOutputStream(startMenuFolder + File.separator + "Uninstall.bat")));
94             //System.err.println("Copying file: " + "configTemplates" + File.separator + "Uninstall.lnk");
95
//createShortcutFile(startMenuFolderPath);
96
//copyInputStream(new FileInputStream("configTemplates" + File.separator + "Uninstall.lnk"), new BufferedOutputStream(new FileOutputStream(startMenuFolder + File.separator + "Uninstall.lnk")));
97
System.err.println("Copying file: " + "lib" + File.separator + "mysql-connector-java-3.0.6-stable-bin.jar");
98             copyInputStream(new FileInputStream("lib" + File.separator + "mysql-connector-java-3.0.6-stable-bin.jar"), new BufferedOutputStream(new FileOutputStream(startMenuFolder + File.separator + "mysql-connector-java-3.0.6-stable-bin.jar")));
99
100             
101             copyInputStream(new FileInputStream("lib" + File.separator + "jtds-0.7.jar"), new BufferedOutputStream(new FileOutputStream(startMenuFolder + File.separator + "jtds-0.7.jar")));
102             /*
103             copyInputStream(new FileInputStream("lib" + File.separator + "msutil.jar"), new BufferedOutputStream(new FileOutputStream(startMenuFolder + File.separator + "msutil.jar")));
104             copyInputStream(new FileInputStream("lib" + File.separator + "mssqlserver.jar"), new BufferedOutputStream(new FileOutputStream(startMenuFolder + File.separator + "mssqlserver.jar")));
105             copyInputStream(new FileInputStream("lib" + File.separator + "msbase.jar"), new BufferedOutputStream(new FileOutputStream(startMenuFolder + File.separator + "msbase.jar")));
106             */

107             /*
108             System.err.println("Copying file: " + "localConfigs/cms.properties");
109             copyInputStream(new FileInputStream("localConfigs/cms.properties"), new BufferedOutputStream(new FileOutputStream(startMenuFolder + File.separator + "cmsuninstall.properties")));
110             System.err.println("Copying file: " + "localConfigs/deliver.properties");
111             copyInputStream(new FileInputStream("localConfigs/deliver.properties"), new BufferedOutputStream(new FileOutputStream(startMenuFolder + File.separator + "deliveruninstall.properties")));
112             */

113
114             int i = 0;
115             Properties p = new Properties();
116             Iterator iterator = removableDirectories.iterator();
117             while(iterator.hasNext())
118             {
119                 String JavaDoc dir = (String JavaDoc)iterator.next();
120                 p.setProperty(i + ".removableDirectory", dir);
121                 i++;
122             }
123             
124             p.setProperty("dbUrl", dbUrl);
125             p.setProperty("databaseServer", databaseServer);
126             p.setProperty("driverName", driverName);
127             p.setProperty("tomcatHomePath", tomcatHomePath);
128             p.setProperty("dbName", dbName);
129             p.setProperty("hostName", hostName);
130             
131             p.store(new FileOutputStream(startMenuFolder + File.separator + "uninstall.properties"), "Uninstall information");
132         }
133     }
134
135
136
137     /**
138      * This method updates the config-files needed to deploy a new application with a context.
139      */

140     public void updateTomcatConfiguration(String JavaDoc path) throws Exception JavaDoc
141     {
142         new File(path + File.separator + ".." + File.separator + "webapps" + File.separator + "infoglueCMS.xml").delete();
143     }
144     
145
146     public void createShortcutFile(String JavaDoc programHome) throws Exception JavaDoc
147     {
148         FileInputStream fis = new FileInputStream("configTemplates/Uninstall.lnk");
149         String JavaDoc result = "";
150         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
151         
152         int c;
153         while((c = fis.read()) != -1)
154         {
155             sb.append((char)c);
156         }
157         result = sb.toString();
158         Logger.logInfo("result" + result);
159         
160         result = result.replaceAll("\\{path\\}", programHome);
161         
162         PrintWriter pw = new PrintWriter(new FileOutputStream("configTemplates/Uninstall.lnk"));
163         pw.println(result);
164         pw.close();
165     }
166     
167
168     /**
169      * This is the method removes every trace of the application.
170      */

171     
172     public void uninstall()
173     {
174         Logger.logInfo("Starting uninstall...");
175         
176         try
177         {
178             Logger.logInfo("Reading uninstall.properties...");
179             Properties cmsProperties = new Properties();
180             cmsProperties.load(new FileInputStream("uninstall.properties"));
181                     
182             int i = 0;
183             String JavaDoc removableDirectory = cmsProperties.getProperty(i + ".removableDirectory");
184             while(removableDirectory != null)
185             {
186                 File currentRemovableDirectory = new File(removableDirectory);
187                 Logger.logInfo("Removing directory " + currentRemovableDirectory.getName());
188                 currentRemovableDirectory.delete();
189                 i++;
190                 removableDirectory = cmsProperties.getProperty(i + ".removableDirectory");
191             }
192
193             String JavaDoc url = cmsProperties.getProperty("dbUrl");
194             String JavaDoc databaseServer = cmsProperties.getProperty("databaseServer");
195             String JavaDoc databasePort = cmsProperties.getProperty("databasePort");
196             String JavaDoc databaseInstance = cmsProperties.getProperty("databaseInstance");
197             String JavaDoc driverName = cmsProperties.getProperty("driverName");
198             String JavaDoc dbName = cmsProperties.getProperty("dbName");
199             String JavaDoc dbSuffix = cmsProperties.getProperty("dbSuffix");
200             String JavaDoc hostName = cmsProperties.getProperty("hostName");
201             
202             System.out.print("Do you want to delete the database and all associated data as well (This action will be irreversible). [Yes]:");
203             if(getInputWithDefault("Yes").toLowerCase().indexOf("yes") > -1)
204             {
205                 System.out.print("Please state the database administrator username:");
206                 String JavaDoc userName = getInputWithDefault("");
207                 System.out.print("Please state the database administrator password:");
208                 String JavaDoc password = getInputWithDefault("");
209                     
210                 MySQLDatabaseCommander commander = new MySQLDatabaseCommander(driverName, databaseServer, databasePort, databaseInstance, url, userName, password, "", "", dbName, dbSuffix, hostName, false, false, false, false, false);
211                 commander.deleteDatabase();
212             }
213             
214             updateTomcatConfiguration(cmsProperties.getProperty("tomcatHomePath"));
215             
216             String JavaDoc currentDir = System.getProperty("user.dir");
217             new File(currentDir).deleteOnExit();
218             
219             System.exit(0);
220         }
221         catch(Exception JavaDoc e)
222         {
223             Logger.logInfo("Error:" + e.getMessage());
224         }
225     }
226     
227     
228     /**
229      * Just copies the files...
230      */

231     
232     private void copyInputStream(InputStream in, OutputStream out) throws IOException
233     {
234         byte[] buffer = new byte[1024];
235         int len;
236
237         while((len = in.read(buffer)) >= 0)
238             out.write(buffer, 0, len);
239
240         in.close();
241         out.close();
242     }
243     
244     
245     public String JavaDoc getInputWithDefault(String JavaDoc defaultValue) throws Exception JavaDoc
246     {
247         String JavaDoc value = "";
248         
249         // block until enter is pressed
250
while (true)
251         {
252             char c = (char)System.in.read();
253             if (c == '\r')
254             {
255                 c = (char)System.in.read();
256                 if (c == '\n')
257                 {
258                    break;
259                 }
260                 else
261                 {
262                    continue;
263                 }
264             }
265             else if (c == '\n')
266             {
267                 break;
268             }
269             else
270             {
271                 value += c;
272             }
273         }
274         
275         if(value.equals(""))
276             value = defaultValue;
277         
278         return value;
279     }
280 }
Popular Tags