KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > backup > StorageMonitor


1 /*
2  * MaxBackupMonitor.java
3  *
4  * Created on December 21, 2003, 8:29 PM
5  */

6
7 package com.sun.enterprise.config.backup;
8
9 import java.io.IOException JavaDoc;
10 import java.io.File JavaDoc;
11 import java.util.Arrays JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15 import java.io.FilenameFilter JavaDoc;
16 import com.sun.enterprise.config.backup.utils.LoggerHelper;
17 import com.sun.enterprise.config.backup.utils.LocalStringsHelper;
18 import com.sun.enterprise.config.backup.pluggable.EnvironmentFactory;
19 import com.sun.enterprise.config.backup.pluggable.BackupEnvironment;
20 import com.sun.enterprise.config.backup.utils.FactoryHelper;
21 import com.sun.enterprise.config.backup.phase.RemovePhases;
22
23
24 /**
25  * Anything extraneous in the monitored directory will
26  * not be touched
27  * @author sridatta
28  */

29 public abstract class StorageMonitor {
30     
31     /** Creates a new instance of MaxBackupMonitor */
32     public StorageMonitor() {
33     }
34     
35     /**
36      * checks if number of backups exceed max.
37      *
38      * Does not throw any exception. Best effort solution.
39      * Not synchronized. Hence, the number of backups
40      * can potentially be greater once in a while
41      * if so, deletes the oldest ones
42      */

43     public void run() {
44         try {
45             File JavaDoc[] f = getFiles();
46             int num = getNumberOfBackups(f);
47             int max = getMaxBackups();
48             
49             if(num <= max) {
50                 LoggerHelper.fine("no_backup_file_to_delete");
51                 return; //nothing to do
52
}
53             
54             deleteBackups(f, num-max);
55         } catch(Exception JavaDoc e) {
56             //log and ignore
57
LoggerHelper.info("error_monitoring_max_backups", e);
58         }
59     }
60     
61     public File JavaDoc getLatestFile() throws BackupException {
62         File JavaDoc[] f = getFiles();
63          if(f == null) {
64             LoggerHelper.fine("no_backup_file_found");
65             return null;
66         }
67         Arrays.sort(f, Collections.reverseOrder());
68         return f[0];
69     }
70     
71     protected File JavaDoc[] getFiles() throws BackupException {
72         String JavaDoc dir = getMonitoredDirectory();
73         File JavaDoc f = new File JavaDoc(dir);
74         FilenameFilter JavaDoc filter = getFileNameFilter();
75         return f.listFiles(filter);
76     }
77     
78     private int getNumberOfBackups(File JavaDoc[] f) {
79         if (f == null) return 0;
80         
81         return f.length;
82     }
83     
84     
85     private void deleteBackups(File JavaDoc[] f, int number)
86                      throws IOException JavaDoc, BackupException {
87         
88         if(f == null || number == 0) {
89             LoggerHelper.fine("no_backup_file_to_delete");
90             return;
91         }
92         
93         /*File[] actualList = getActualList(f);
94         if(actualList == null) {
95             LoggerHelper.fine("no_backup_file_to_delete");
96             return;
97         }
98          */

99         Arrays.sort(f);
100         deleteFiles(f, number);
101     }
102    
103     /**
104      * return -1 if incorrect file
105      */

106     /*
107     private File[] getActualList(File[] f)
108                         throws BackupInitException {
109
110         ArrayList arr = new ArrayList();
111         
112         for(int i = 0; i < f.length; i++) {
113             String name = f[i].getName();
114             String[] parts =
115             name.split(DefaultConstants.INTRA_FILENAME_SEPARATOR);
116             
117             if (!getFileNamePrefix().equals(parts[0]))
118                 continue;
119             
120             try {
121                 new Long(parts[1]).longValue();
122                 arr.add(f[i]);
123             } catch(Exception e) {
124                 continue;
125             }
126         }
127         return convertToFileArr(arr);
128     }
129         
130      */

131     private File JavaDoc[] convertToFileArr(ArrayList JavaDoc arr) {
132         if(arr==null || arr.size() == 0) return null;
133         File JavaDoc[] res = new File JavaDoc[arr.size()];
134         for(int i=0;i<arr.size();i++) {
135             res[i] = (File JavaDoc) arr.get(i);
136         }
137         return res;
138     }
139     
140     private void deleteFiles(File JavaDoc[] delete, int number) {
141         if(delete == null) return;
142         int max;
143         
144         if(number > delete.length)
145             max = delete.length;
146         else
147             max = number;
148         
149         for(int i=0;i<max;i++) {
150             try {
151                 RemovePhases rp = new RemovePhases();
152                 rp.deleteBackup(delete[i], true);
153             } catch(Exception JavaDoc e) {
154                 LoggerHelper.info("error_deleting_file" + delete[i].getName(), e);
155             }
156         }
157     }
158     
159     protected BackupEnvironment getEnv() {
160         return getFactory().getBackupEnvironment();
161     }
162     
163     protected EnvironmentFactory getFactory() {
164         try {
165             return EnvironmentFactory.getEnvironmentFactory();
166         } catch(Exception JavaDoc e) {
167             throw new BackupAbortException(
168                 "error_getting_environment_factory",
169                 LocalStringsHelper.getString("error_getting_environment_factory"),
170                 e);
171         }
172     }
173     
174     protected abstract FilenameFilter JavaDoc getFileNameFilter();
175     protected abstract String JavaDoc getMonitoredDirectory();
176     protected abstract int getMaxBackups();
177     protected abstract String JavaDoc getFileNamePrefix();
178 }
179
Popular Tags