KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.zip.*;
29
30 public abstract class InstallationCommander
31 {
32     protected String JavaDoc appServer = null;
33     protected String JavaDoc cmsTargetFolder = null;
34     protected String JavaDoc deliverWorkingTargetFolder = null;
35     protected String JavaDoc deliverPreviewTargetFolder = null;
36     protected String JavaDoc deliverLiveTargetFolder = null;
37     protected String JavaDoc applicationServerHomePath = null;
38     protected String JavaDoc tomcatContextFolder = null;
39     protected String JavaDoc createCMS = null;
40     protected String JavaDoc createDeliverWorking = null;
41     protected String JavaDoc createDeliverPreview = null;
42     protected String JavaDoc createDeliverLive = null;
43     protected String JavaDoc appNameSuffix = null;
44     
45     public static InstallationCommander getInstallationCommander(String JavaDoc appServer, String JavaDoc targetFolder, String JavaDoc appNameSuffix, String JavaDoc applicationServerHomePath, String JavaDoc createCMS, String JavaDoc createDeliverWorking, String JavaDoc createDeliverPreview, String JavaDoc createDeliverLive, String JavaDoc tomcatContextFolder/*, String tomcatVersion*/)
46     {
47         if(appServer.startsWith("Tomcat"))
48             return new TomcatInstallationCommander(appServer, targetFolder, appNameSuffix, applicationServerHomePath, createCMS, createDeliverWorking, createDeliverPreview, createDeliverLive, tomcatContextFolder);
49         else if(appServer.startsWith("JBoss"))
50             return new JBossInstallationCommander(appServer, targetFolder, appNameSuffix, applicationServerHomePath, createCMS, createDeliverWorking, createDeliverPreview, createDeliverLive, tomcatContextFolder);
51         else
52             return null;
53     }
54     
55     public InstallationCommander(String JavaDoc appServer, String JavaDoc targetFolder, String JavaDoc appNameSuffix, String JavaDoc applicationServerHomePath, String JavaDoc createCMS, String JavaDoc createDeliverWorking, String JavaDoc createDeliverPreview, String JavaDoc createDeliverLive, String JavaDoc tomcatContextFolder)
56     {
57         this.appServer = appServer;
58         this.applicationServerHomePath = applicationServerHomePath;
59         this.createCMS = createCMS;
60         this.createDeliverWorking = createDeliverWorking;
61         this.createDeliverPreview = createDeliverPreview;
62         this.createDeliverLive = createDeliverLive;
63         this.appNameSuffix = appNameSuffix;
64         this.tomcatContextFolder = tomcatContextFolder;
65         //this.tomcatVersion = tomcatVersion;
66
this.cmsTargetFolder = targetFolder + File.separator + "infoglueCMS" + appNameSuffix;
67         this.deliverWorkingTargetFolder = targetFolder + File.separator + "infoglueDeliverWorking" + appNameSuffix;
68         this.deliverPreviewTargetFolder = targetFolder + File.separator + "infoglueDeliverPreview" + appNameSuffix;
69         this.deliverLiveTargetFolder = targetFolder + File.separator + "infoglueDeliverLive" + appNameSuffix;
70     }
71
72     public abstract String JavaDoc getPortletBase();
73     
74     public abstract void installFiles(String JavaDoc databaseTypeName) throws Exception JavaDoc;
75     
76     /**
77      * This method copies the newly created config-files to the installation dir.
78      */

79     
80     protected abstract void moveLocalConfigurationFiles(String JavaDoc databaseTypeName) throws Exception JavaDoc;
81     
82     
83     /**
84      * This method checks if xml-packages are needed.
85      */

86     
87     protected boolean checkIfXMLLibsIsNeeded()
88     {
89         try
90         {
91             Class JavaDoc testClass = Class.forName("org.apache.xml.serialize.XMLSerializer");
92             return false;
93         }
94         catch(ClassNotFoundException JavaDoc e)
95         {
96             Logger.logInfo("The installer will install the xml-packages as they seem to be missing.");
97             return true;
98         }
99     }
100     
101     
102     
103     /**
104      * This method unzips the cms war-file.
105      */

106     
107     protected void unzipCMSWarFile(String JavaDoc cmsTargetFolder) throws Exception JavaDoc
108     {
109         String JavaDoc warFile = "infoglue-cms.war";
110         
111         Enumeration entries;
112         ZipFile zipFile;
113
114         zipFile = new ZipFile(warFile);
115         entries = zipFile.entries();
116
117         while(entries.hasMoreElements())
118         {
119             ZipEntry entry = (ZipEntry)entries.nextElement();
120
121             if(entry.isDirectory())
122             {
123                 (new File(cmsTargetFolder + File.separator + entry.getName())).mkdir();
124                 continue;
125             }
126     
127             //System.err.println("Extracting file: " + this.cmsTargetFolder + File.separator + entry.getName());
128
copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(cmsTargetFolder + File.separator + entry.getName())));
129         }
130     
131         zipFile.close();
132     }
133
134
135     /**
136      * This method unzips the cms war-file.
137      */

138     
139     protected void unzipDeliverWarFile(String JavaDoc path) throws Exception JavaDoc
140     {
141         String JavaDoc warFile = "infoglue-deliver.war";
142         
143         Enumeration entries;
144         ZipFile zipFile;
145
146         zipFile = new ZipFile(warFile);
147         entries = zipFile.entries();
148
149         while(entries.hasMoreElements())
150         {
151             ZipEntry entry = (ZipEntry)entries.nextElement();
152
153             if(entry.isDirectory())
154             {
155                 (new File(path + File.separator + entry.getName())).mkdir();
156                 continue;
157             }
158     
159             //System.err.println("Extracting file: " + this.deliverTargetFolder + File.separator + entry.getName());
160
copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(path + File.separator + entry.getName())));
161         }
162     
163         zipFile.close();
164     }
165
166     
167     /**
168      * Just copies the files...
169      */

170     
171     protected void copyInputStream(InputStream in, OutputStream out) throws IOException
172     {
173         byte[] buffer = new byte[1024];
174         int len;
175
176         while((len = in.read(buffer)) >= 0)
177             out.write(buffer, 0, len);
178
179         in.close();
180         out.close();
181     }
182
183     /**
184      * This method recursively zips a directory structure.
185      */

186
187     public void zipDir(String JavaDoc dir2zip, int numberOfCharsToRemove, ZipOutputStream zos) throws Exception JavaDoc
188     {
189         // create a new File object based on the directory we have to zip File
190
File zipDir = new File(dir2zip);
191         
192         // get a listing of the directory content
193
String JavaDoc[] dirList = zipDir.list();
194         
195         byte[] readBuffer = new byte[2156];
196         
197         int bytesIn = 0;
198
199         for(int i=0; i<dirList.length; i++)
200         {
201             File f = new File(zipDir, dirList[i]);
202             if(f.isDirectory())
203             {
204                 String JavaDoc filePath = f.getPath();
205                 zipDir(filePath, numberOfCharsToRemove, zos);
206                 continue;
207             }
208             
209             FileInputStream fis = new FileInputStream(f);
210
211             ZipEntry anEntry = new ZipEntry(f.getPath().substring(numberOfCharsToRemove));
212             System.out.println("Adding " + f.getPath().substring(numberOfCharsToRemove));
213
214             zos.putNextEntry(anEntry);
215             
216             while((bytesIn = fis.read(readBuffer)) != -1)
217             {
218                 zos.write(readBuffer, 0, bytesIn);
219             }
220            
221             fis.close();
222         }
223     }
224     
225     public void modifyOSCachePropertyFile(String JavaDoc targetFolder) throws Exception JavaDoc
226     {
227         FileInputStream fis = new FileInputStream(targetFolder + File.separator + "WEB-INF/classes/oscache.properties");
228         String JavaDoc result = "";
229         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
230         
231         int c;
232         while((c = fis.read()) != -1)
233         {
234             sb.append((char)c);
235         }
236         result = sb.toString();
237         fis.close();
238         
239         result = result.replaceAll("@cachePath@", targetFolder + File.separator + "cache");
240         result = result.replaceAll("\\@cachePath\\@", targetFolder + File.separator + "cache");
241         
242         PrintWriter pw = new PrintWriter(new FileOutputStream(targetFolder + File.separator + "WEB-INF/classes/oscache.properties"));
243         pw.println(result);
244         pw.close();
245
246     }
247
248     
249     protected void deleteRecursive(File file) throws IOException
250     {
251         File[] files = file.listFiles();
252
253         for(int i=0; i<files.length; ++i)
254         {
255             if(files[i].isDirectory())
256                 deleteRecursive(files[i]);
257             
258             files[i].delete();
259         }
260     }
261
262
263 }
264
Popular Tags