KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > update > Backup


1 /*
2  * Created on May 17, 2006
3  */

4 package com.openedit.modules.update;
5
6 import java.io.File JavaDoc;
7 import java.io.FileOutputStream JavaDoc;
8 import java.io.FilenameFilter JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.text.SimpleDateFormat JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.Date JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19
20 import com.openedit.OpenEditException;
21 import com.openedit.page.manage.PageManager;
22 import com.openedit.util.FileUtils;
23 import com.openedit.util.PageZipUtil;
24 import com.openedit.util.ZipUtil;
25
26 public class Backup
27 {
28     protected PageManager fieldPageManager;
29     protected SimpleDateFormat JavaDoc fieldFormat = new SimpleDateFormat JavaDoc("yyyy_MM_dd_HH_mm_ss");
30     protected File JavaDoc fieldRoot;
31     protected String JavaDoc fieldIncludePath = "/";
32     protected FileUtils fieldUtils = new FileUtils();
33     private static final Log log = LogFactory.getLog(Backup.class);
34     
35     public PageManager getPageManager()
36     {
37         return fieldPageManager;
38     }
39     public void setPageManager(PageManager inPageManager)
40     {
41         fieldPageManager = inPageManager;
42     }
43     
44     protected File JavaDoc backupCurrentSite(String JavaDoc inName) throws OpenEditException
45     {
46         PageZipUtil zip = new PageZipUtil(getPageManager());
47         zip.setRoot(getRoot());
48         zip.addExclude("*/WEB-INF/*");
49         zip.addExclude("*/.versions/*");
50 // zip.addExclude("*/WEB-INF/trash/*");
51
// zip.addExclude("*/WEB-INF/tmp/*");
52
// zip.addExclude("*/WEB-INF/log*");
53

54         inName = inName.replace(" ", "_");
55         inName = inName.replace("/", "_");
56         inName = inName.replace("\\", "_");
57
58         String JavaDoc id = fieldFormat.format(new Date JavaDoc() ) + "_" + inName;
59         File JavaDoc out = new File JavaDoc( getRoot() , "WEB-INF/versions/" + id + ".zip" );
60         log.info("Backing up " + out);
61         try
62         {
63             out.getParentFile().mkdirs();
64             FileOutputStream JavaDoc stream = new FileOutputStream JavaDoc(out);
65             try
66             {
67                 zip.zipFile(getIncludePath(), stream );
68             }
69             finally
70             {
71                 FileUtils.safeClose(stream);
72             }
73             return out;
74         }
75         catch ( IOException JavaDoc ex)
76         {
77             throw new OpenEditException(ex);
78         }
79     }
80     public File JavaDoc getRoot()
81     {
82         return fieldRoot;
83     }
84     public void setRoot(File JavaDoc inRoot)
85     {
86         fieldRoot = inRoot;
87     }
88     public List JavaDoc listSiteVersions()
89     {
90         File JavaDoc verdir = new File JavaDoc( getRoot() , "WEB-INF/versions");
91         verdir.mkdirs();
92         
93         File JavaDoc[] children = verdir.listFiles(new FilenameFilter JavaDoc() {
94             public boolean accept(File JavaDoc inDir, String JavaDoc inName)
95             {
96                 return inName.endsWith(".zip");
97             }
98         });
99         List JavaDoc list = new ArrayList JavaDoc();
100         if ( children != null)
101         {
102             for (int i = 0; i < children.length; i++)
103             {
104                 File JavaDoc child = children[i];
105                 list.add(child);
106             }
107         }
108         Collections.sort(list);
109         return list;
110     }
111     public File JavaDoc loadVersion(String JavaDoc inName)
112     {
113         List JavaDoc list = listSiteVersions();
114         for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();)
115         {
116             File JavaDoc version= (File JavaDoc) iter.next();
117             if ( version.getName().equals( inName ))
118             {
119                 return version;
120             }
121         }
122         return null;
123     }
124     protected void restoreBackup(File JavaDoc inVersion) throws OpenEditException
125     {
126         log.info("Restoring " + inVersion.getName() );
127         File JavaDoc tmp = null;
128         try
129         {
130             tmp = File.createTempFile("upgrade", "");
131             tmp.delete();
132             tmp.mkdirs();
133             
134             ZipUtil utils = new ZipUtil();
135             //unzip the zip file in a tmp directory
136
utils.unzip(inVersion,tmp);
137         }
138         catch (IOException JavaDoc ex )
139         {
140             throw new OpenEditException("No harm done", ex);
141         }
142         File JavaDoc old = new File JavaDoc( getRoot(),"WEB-INF/trash/" + fieldFormat.format(new Date JavaDoc() ));
143
144         try
145         {
146             replaceDirectories(tmp,getRoot(),old);
147         }
148         catch (IOException JavaDoc ex )
149         {
150             throw new OpenEditException("Some files could not be moved. Backup located in " + old.getPath() );
151         }
152
153
154         
155         // try
156
// {
157
// File tmpold = null;
158
// }
159
// finally
160
// {
161
// if( !tmp.renameTo(getRoot()) )
162
// {
163
// //copy it
164
// try
165
// {
166
// log.error("Could not rename");
167
// //org.apache.commons.io.FileUtils.copyFile(tmp, getRoot(), true);
168
// new FileUtils().copyFiles(tmp, getRoot() );
169
// }
170
// catch ( IOException ex)
171
// {
172
// throw new OpenEditException(ex);
173
// }
174
// }
175
// //bring back the versions
176
// File versions = new File( tmpold , "WEB-INF/versions");
177
// File newversions = new File( getRoot(), "WEB-INF/versions");
178
// versions.renameTo(newversions);
179
// }
180
}
181     /**
182      * This method does a replacement of top level directories.
183      * One exception is the WEB-INF directory that it will go into
184      * @param inNewDirs
185      * @param inRoot
186      * @param inSubPath
187      * @param inOldDirectory
188      * @throws IOException
189      */

190     protected void replaceDirectories(File JavaDoc inNewDirs, File JavaDoc inRoot, File JavaDoc inOldDirectory ) throws IOException JavaDoc
191     {
192         //move the existing content to tmp2
193
// tmpold = File.createTempFile("upgradeold", "");
194
// tmpold.delete();
195
// tmpold.mkdirs();
196
File JavaDoc[] children = inNewDirs.listFiles();
197         for (int i = 0; i < children.length; i++)
198         {
199             File JavaDoc child = children[i];
200             File JavaDoc existing = new File JavaDoc( inRoot, child.getName() );
201             if( existing.exists() )
202             {
203                 //Then move it into away
204

205                 if( child.getName().equals("WEB-INF"))
206                 {
207                     //replaceDirectories(child, existing, inOldDirectory);
208
continue;
209                 }
210                 else
211                 {
212                     if( existing.isDirectory() )
213                     {
214                         fieldUtils.move(existing, new File JavaDoc( inOldDirectory, existing.getName() ));
215                     }
216                     else
217                     {
218                         //this is an existing file in the inNewDirs directory
219
File JavaDoc backup = new File JavaDoc( inOldDirectory, child.getName() );
220                         existing.renameTo(backup);
221                     }
222                 }
223             }
224             //Now replace it
225
fieldUtils.move(child, new File JavaDoc( getRoot(), child.getName() ) );
226             //child.renameTo(new File( getRoot(), child.getName() ));
227

228         }
229         //TODO: Do this in smaller parts so we can exclude WEB-INF/logs/
230
// if ( !getRoot().renameTo(tmpold) )
231
// {
232
// log.info("Had to copy manually");
233
// FileUtils fu = new FileUtils();
234
// fu.copyFiles(getRoot(), tmpold );
235
// fu.deleteAll(getRoot());
236
// }
237
// File[] all = tmpold.listFiles();
238
// if ( all.length == 0)
239
// {
240
// throw new OpenEditException("Problem: Could not move entire existing site. Some files moved to: " + tmpold.getPath());
241
// }
242

243     }
244     public String JavaDoc getIncludePath()
245     {
246         return fieldIncludePath;
247     }
248     public void setIncludePath(String JavaDoc inIncludePath)
249     {
250         fieldIncludePath = inIncludePath;
251     }
252 }
253
Popular Tags