270 Problem Determination Using Self-Managing Autonomic Technology
Figure 8-10 FreeDiskStorageILT class
8.5 Implementing the utility class
The implementation of the utility class requires the development of a Java class
including the following three utility methods.
򐂰 getFreeDiskSpace()
򐂰 zipOldFiles()
򐂰 deleteOldZipFiles()
The utility class described in this section is available for download. Refer to
Appendix A, “Additional material” on page 379 for details.
getFreeDiskSpace()
This method finds available space left under a specified path. Example 8-2 on
page 271 shows the implementation.
Chapter 8. Problem Determination scenario one 271
Example 8-2 getFreeDiskSpace implementation
public static long getFreeDiskSpace(String path) throws Exception {
long bytesFree = -1;
try {
Process p = Runtime.getRuntime().exec(
new String[] { "cmd", "/C", "dir", path }); // for Windows
BufferedReader reader = new BufferedReader(new InputStreamReader(
new BufferedInputStream(p.getInputStream())));
String line;
while ((line = reader.readLine()) != null) {
if (line.endsWith("bytes free")) { // for dir on Windows
StringTokenizer tokenizer = new StringTokenizer(line, " ");
tokenizer.nextToken();
tokenizer.nextToken();
bytesFree = Long.parseLong(tokenizer.nextToken().replaceAll(",",
""));
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
throw e;
} catch (IOException e) {
e.printStackTrace();
throw e;
}
return bytesFree;
}
zipOldFiles()
This method zips files older than a specified number of days into a backup file.
Example 8-3 shows the implementation.
Example 8-3 zipOldFiles implementation
public static void zipOldFiles(String path, int daysOld) throws Exception {
// First find all files under the path that is too old.
File filePath = new File(path);
// Create the file filter
FileFilter filter = new OldFilesFilter(daysOld);
// find files to zip
File[] filesToZip = filePath.listFiles(filter);
if (filesToZip != null && filesToZip.length > 0) {
// Some files need to be zipped
272 Problem Determination Using Self-Managing Autonomic Technology
File zipFile = new File(filePath, "backup_"
+ System.currentTimeMillis() + ".zip");
System.out.println("Creating zip backup " + zipFile.getName());
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(
zipFile));
zipOut.setMethod(ZipOutputStream.DEFLATED);
for (int i = 0; i < filesToZip.length; i++) {
System.out.println("Adding file " + filesToZip[i].getName()
+ " to " + zipFile.getName());
ZipEntry zipEntry = new ZipEntry(filesToZip[i].getName());
zipEntry.setSize(filesToZip[i].length());
zipEntry.setTime(filesToZip[i].lastModified());
zipOut.putNextEntry(zipEntry);
byte[] fileContent = null;
FileInputStream sourceFile = null;
try {
fileContent = new byte[1024];
sourceFile = new FileInputStream(filesToZip[i]);
int size = 0;
while ((size = sourceFile.read(fileContent)) != -1) {
zipOut.write(fileContent, 0, size);
}
} finally {
// make sure the file is always closed of open.
if (sourceFile != null) {
sourceFile.close();
}
}
}
zipOut.close();
// Now delete the old files
for (int i = 0; i < filesToZip.length; i++) {
System.out.println("Deleting file " + filesToZip[i].getName());
filesToZip[i].delete();
}
System.out.println("Backup done (" + zipFile.getName() + ")");
}
}
deleteOldZipFiles()
This method deletes old backup files from a specified path. Example 8-4 on
page 273 shows the implementation.

Get Problem Determination Using Self-Managing Autonomic Technology now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.