KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > backup > pluggable > impl > GenericStorage


1 /*
2  * DirectoryStorage.java
3  *
4  * Created on December 19, 2003, 11:39 PM
5  */

6
7 package com.sun.enterprise.config.backup.pluggable.impl;
8
9 import java.io.*;
10 import java.util.*;
11
12 import com.sun.enterprise.util.io.FileUtils;
13 import com.sun.enterprise.config.backup.pluggable.BackupStorage;
14 import com.sun.enterprise.config.backup.utils.FactoryHelper;
15 import com.sun.enterprise.config.backup.utils.LoggerHelper;
16 import com.sun.enterprise.config.backup.BackupException;
17 import com.sun.enterprise.config.backup.DefaultConstants;
18 import com.sun.enterprise.config.backup.status.Status;
19
20 /**
21  *
22  * @author bnevins
23  */

24 abstract class GenericStorage implements BackupStorage
25 {
26     abstract long copyFiles(File fromDir, File toDir) throws BackupException;
27     
28     ///////////////////////////////////////////////////////////////////////////
29

30     public long backup(String JavaDoc[] srcDirs, String JavaDoc targetString) throws BackupException
31     {
32         File target = new File(targetString);
33         
34         // make sure there ARE source dirs
35
if(srcDirs == null || srcDirs.length == 0)
36             throw new BackupException("No source directories specified", "");
37         
38         // make sure they point to bonafide directories
39
for (int i=0;i<srcDirs.length;i++)
40             if(!FileUtils.safeIsDirectory(srcDirs[i]))
41                 throw new BackupException("A specified source directory is NOT a directory: " + srcDirs[i], "");
42         
43         // create the target directory if it doesn't exist
44
if(!FileUtils.safeIsDirectory(target))
45             target.mkdirs();
46
47         // Now make sure the target directory exists
48
if(!FileUtils.safeIsDirectory(target))
49             throw new BackupException("The target directory is NOT a directory: " + target.getPath(), "");
50
51         if(!target.canWrite())
52             throw new BackupException("The target directory is read-only: " + target.getPath(), "");
53
54         int totalSizeBackedUp = 0;
55         for (int i=0;i<srcDirs.length;i++)
56         {
57             // Note: say srcDirs[i] is C:/domains/domain1 and target is C:/backups/backup12345678
58
// then what we really want to do is this:
59
// mkdir C:/backups/backup12345678/domain1
60
// xcopy C:/domains/domain1/*.* C:/backups/backup12345678/domain1 /s
61

62             try
63             {
64                 File srcFile = new File(srcDirs[i]);
65                 File currentTarget = new File(target, srcFile.getName());
66                 totalSizeBackedUp += copyFiles(srcFile, currentTarget);
67             }
68             catch(Exception JavaDoc ioe)
69             {
70                 throw new BackupException("backup error", "Exception copying "
71                 + srcDirs[i] + " to " + target.getPath(), ioe);
72             }
73         }
74         return totalSizeBackedUp;
75     }
76
77     ///////////////////////////////////////////////////////////////////////////
78

79     public long restore(File file) throws BackupException
80     {
81         if(file == null || !file.exists() || !file.isDirectory())
82             return -1;
83         
84         File[] childrenFiles = getBUSubdirs(file);
85         
86         String JavaDoc[] targetDirs = FactoryHelper.getEnv().getDirectoriesToBackup();
87         
88         if(targetDirs == null || targetDirs.length == 0)
89             return -1;
90         
91         if(targetDirs.length == 1 && childrenFiles.length == 1)
92             return restoreSingle(childrenFiles[0], new File(targetDirs[0]));
93         else if(targetDirs.length == 1)
94             return restoreMultiToSingle(childrenFiles, new File(targetDirs[0]));
95         else
96             return restoreMultiToMulti(childrenFiles, targetDirs);
97     }
98
99     ///////////////////////////////////////////////////////////////////////////
100

101     private String JavaDoc getTargetDirToRestore(String JavaDoc[] targetDirs, File child)
102     {
103         for(int i = 0; i < targetDirs.length;i++)
104         {
105             File f = new File(targetDirs[i]);
106             if(f.getName().equals(child.getName()))
107             {
108                 return f.getParent();
109             }
110         }
111         LoggerHelper.warning("no_target_directory_to_restore " + child);
112         return null;
113     }
114
115     ///////////////////////////////////////////////////////////////////////////
116

117     public void createStatusFile(String JavaDoc target, Status status)
118     {
119         if (status == null) return;
120         
121         File f = new File(target, FactoryHelper.getEnv().getStatusInfoFileName());
122         FileOutputStream fos = null;
123         try
124         {
125             fos = new FileOutputStream(f);
126             fos.write(status.toString().getBytes());
127         } catch(Exception JavaDoc e)
128         {
129             //ignore
130
} finally
131         {
132             try
133             {
134                 fos.close();
135             } catch(Exception JavaDoc ex)
136             {}
137         }
138     }
139
140     ///////////////////////////////////////////////////////////////////////////
141

142     public void deleteTarget()
143     {
144         String JavaDoc[] targetDirs = FactoryHelper.getEnv().getDirectoriesToBackup();
145         
146         for(int i = 0; i < targetDirs.length; i++)
147         {
148             FileUtils.whack(new File(targetDirs[i]));
149         }
150     }
151
152     ///////////////////////////////////////////////////////////////////////////
153

154     private File[] pruneNonDirs(File[] files)
155     {
156         // bnevins -- prune out regular files (like the status.txt file)
157
if(files == null)
158             return new File[0];
159         
160         ArrayList list = new ArrayList(files.length);
161         
162         for(int i = 0; i < files.length; i++)
163             if(files[i].isDirectory())
164                 list.add(files[i]);
165         
166         File[] ret = new File[list.size()];
167         
168         if(list.size() == 0)
169             return ret;
170         
171         return (File[])(list.toArray(ret));
172     }
173
174     ///////////////////////////////////////////////////////////////////////////
175

176     private long restoreSingle(File from, File to) throws BackupException
177     {
178         if(from.getName().equals(to.getName()))
179         {
180             to = to.getParentFile();
181         }
182         
183         try
184         {
185             FileUtils.copyTree(from, to);
186         }
187         catch(IOException ioe)
188         {
189             throw new BackupException("xx", "IOException copying tree -- from: " + from + ", To: " + to, ioe);
190         }
191         
192         return 100;
193     }
194
195     ///////////////////////////////////////////////////////////////////////////
196

197     private long restoreMultiToSingle(File[] from, File to) throws BackupException
198     {
199         // by definition -- put all the bu dirs under one dir
200
try
201         {
202             for(int i = 0; i < from.length; i++)
203             {
204                 FileUtils.copyTree(from[i], to);
205             }
206             
207             return 100;
208         }
209         catch(IOException ioe)
210         {
211             throw new BackupException("xx", "IOException copying tree -- from: " + from + ", To: " + to, ioe);
212         }
213     }
214
215     ///////////////////////////////////////////////////////////////////////////
216

217     private long restoreMultiToMulti(File[] from, String JavaDoc[] toStrings) throws BackupException
218     {
219         // They all have to match their last directory name or
220
// it's an error...bnevins
221
File[] to = new File[toStrings.length];
222         
223         for(int i = 0; i < toStrings.length; i++)
224             to[i] = new File(toStrings[i]);
225         
226         if(to.length != from.length)
227             throw new BackupException("restoreMultiToMulti", "Number of backup-dirs (" + from.length + " doesn't match number of target dirs (" + to.length + ")");
228         
229         int numMatches = 0;
230         
231         // inefficient to do this twice -- but very time-consuming to do neatly...
232
for(int i = 0; i < to.length; i++)
233         {
234             String JavaDoc toName = to[i].getName();
235             
236             for(int j = 0; j < from.length; j++)
237             {
238                 if(toName.equals(from[j].getName()))
239                 {
240                     ++numMatches;
241                     break;
242                 }
243             }
244         }
245         
246         if(numMatches != to.length)
247             throw new BackupException("restoreMultiToMulti", "backup-dirs don't match up with target dirs (" + to.length + ")");
248         
249         try
250         {
251             for(int i = 0; i < to.length; i++)
252             {
253                 String JavaDoc toName = to[i].getName();
254                 
255                 for(int j = 0; j < from.length; j++)
256                 {
257                     if(toName.equals(from[j].getName()))
258                     {
259                         FileUtils.copyTree(from[j], to[i].getParentFile());
260                         break;
261                     }
262                 }
263             }
264             return 100;
265         }
266         catch(IOException ioe)
267         {
268             throw new BackupException("xxx", "IOException copying tree -- from: " + from + ", To: " + to, ioe);
269         }
270     }
271
272     ///////////////////////////////////////////////////////////////////////////
273

274     private File[] getBUSubdirs(File root) throws BackupException
275     {
276         File[] files = root.listFiles();
277         files = pruneNonDirs(files);
278         
279         if(files.length == 0)
280             throw new BackupException("getBUSubDirs", "Nothing in backup dir: " + root);
281         
282         return files;
283     }
284 }
285
Popular Tags